Menu.php 4.7 KB

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