Menu.php 4.1 KB

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