Menu.php 4.6 KB

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