Menu.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. use Dever\Project;
  4. class Menu
  5. {
  6. protected $icon;
  7. # 初始化菜单
  8. public function init($name = '')
  9. {
  10. echo 11;die;
  11. $config = Dever::config('manage');
  12. if ($config) {
  13. $this->add('manage', $config);
  14. }
  15. if ($name) {
  16. $app = array($name => Dever::get(Project::class)->load($name));
  17. } else {
  18. $app = Dever::get(Project::class)->read();
  19. }
  20. print_r($app);die;
  21. foreach ($app as $k => $v) {
  22. $base = $v['path'] . 'manage/core.php';
  23. if (is_file($base)) {
  24. $core = include $base;
  25. if ($core) {
  26. $this->add($k, $core);
  27. }
  28. }
  29. }
  30. return 'ok';
  31. }
  32. private function add($app, $core)
  33. {
  34. if (isset($core['system'])) {
  35. foreach ($core['system'] as $k => $v) {
  36. $where = [];
  37. $where['key'] = $k;
  38. $data = $where;
  39. $data['name'] = $v['name'];
  40. $data['sort'] = $v['sort'];
  41. $data['partition'] = $v['partition'] ?? 'no';
  42. $data['info_table'] = $v['info_table'];
  43. $data['user_table'] = $v['user_table'];
  44. $data['role_table'] = $v['role_table'];
  45. Dever::db('manage/system')->up($where, $data);
  46. }
  47. }
  48. if (isset($core['module'])) {
  49. foreach ($core['module'] as $k => $v) {
  50. $where = [];
  51. $where['key'] = $k;
  52. $data = $where;
  53. $data['name'] = $v['name'];
  54. $data['sort'] = $v['sort'];
  55. $data['system'] = $v['system'];
  56. $data['data_table'] = $v['data_table'];
  57. if (isset($v['data_where']) && $v['data_where']) {
  58. $data['data_where'] = Dever::json_encode($v['data_where']);
  59. }
  60. Dever::db('manage/system_module')->up($where, $data);
  61. }
  62. }
  63. if (isset($core['menu'])) {
  64. foreach ($core['menu'] as $k => $v) {
  65. $where = [];
  66. if (isset($v['app'])) {
  67. $app = $v['app'];
  68. }
  69. $where['app'] = $app;
  70. $where['key'] = $k;
  71. if (isset($v['parent'])) {
  72. $parent = Dever::db('manage/menu')->find(['key' => $v['parent']]);
  73. if ($parent) {
  74. $where['parent_id'] = $parent['id'];
  75. $where['module_id'] = $parent['module_id'];
  76. $where['level'] = 2;
  77. if ($parent['parent_id']) {
  78. $where['level'] = 3;
  79. }
  80. }
  81. } else {
  82. $where['level'] = 1;
  83. }
  84. if (isset($v['module'])) {
  85. $module = Dever::db('manage/system_module')->find(['key' => $v['module']]);
  86. if ($module) {
  87. $where['module_id'] = $module['id'];
  88. }
  89. }
  90. $data = $where;
  91. $data['name'] = $v['name'];
  92. if (isset($v['icon']) && $v['icon']) {
  93. $data['icon'] = $v['icon'];
  94. } else {
  95. # 随机抽取
  96. $data['icon'] = $this->getIcon();
  97. }
  98. $data['sort'] = $v['sort'];
  99. if (isset($v['show'])) {
  100. $data['show'] = $v['show'];
  101. }
  102. if (isset($v['badge'])) {
  103. $data['badge'] = $v['badge'];
  104. }
  105. if (isset($v['path'])) {
  106. $data['path'] = $v['path'];
  107. } else {
  108. $data['path'] = 'main';
  109. }
  110. if (isset($v['link'])) {
  111. $data['link'] = $v['link'];
  112. }
  113. Dever::db('manage/menu')->up($where, $data);
  114. }
  115. }
  116. }
  117. public function getAll()
  118. {
  119. $data = Dever::db('manage/menu')->select(['parent_id' => '0']);
  120. return $data;
  121. }
  122. public function getIcon()
  123. {
  124. if (empty($this->icon)) {
  125. $this->icon = Dever::db('manage/icon')->select([]);
  126. }
  127. $key = array_rand($this->icon, 1);
  128. $icon = $this->icon[$key]['key'];
  129. $info = Dever::db('manage/menu')->find(['icon' => $icon]);
  130. if ($info) {
  131. return $this->getIcon();
  132. }
  133. return $icon;
  134. }
  135. }