Menu.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. use Dever\Project;
  4. class Menu
  5. {
  6. # 初始化菜单
  7. public function init()
  8. {
  9. $app = Project::read();
  10. foreach ($app as $k => $v) {
  11. $base = $v['path'] . 'table/manage/core.php';
  12. if (is_file($base)) {
  13. $core = include $base;
  14. if ($core) {
  15. $this->add($k, $core);
  16. }
  17. }
  18. }
  19. return 'ok';
  20. }
  21. private function add($app, $core)
  22. {
  23. if (isset($core['system'])) {
  24. foreach ($core['system'] as $k => $v) {
  25. $where = array();
  26. $where['key'] = $k;
  27. $data = $where;
  28. $data['name'] = $v['name'];
  29. $data['sort'] = $v['sort'];
  30. $data['partition'] = $v['partition'] ?? 'database';
  31. $data['relation_table'] = $v['relation_table'];
  32. $data['relation_field'] = $v['relation_field'];
  33. $data['relation_user_table'] = $v['relation_user_table'];
  34. Dever::db('system')->up($where, $data);
  35. }
  36. }
  37. if (isset($core['menu'])) {
  38. foreach ($core['menu'] as $k => $v) {
  39. $where = array();
  40. if (isset($v['app'])) {
  41. $app = $v['app'];
  42. }
  43. $where['app'] = $app;
  44. $where['key'] = $k;
  45. if (isset($v['parent'])) {
  46. $parent = Dever::db('menu')->find(array('key' => $v['parent']));
  47. if ($parent) {
  48. $where['parent_id'] = $parent['id'];
  49. $where['system_id'] = $parent['system_id'];
  50. }
  51. }
  52. if (isset($v['system'])) {
  53. $system = Dever::db('system')->find(array('key' => $v['system']));
  54. if ($system) {
  55. $where['system_id'] = $system['id'];
  56. }
  57. }
  58. $data = $where;
  59. $data['name'] = $v['name'];
  60. $data['icon'] = $v['icon'];
  61. $data['sort'] = $v['sort'];
  62. if (isset($v['show'])) {
  63. $data['show'] = $v['show'];
  64. }
  65. if (isset($v['badge'])) {
  66. $data['badge'] = $v['badge'];
  67. }
  68. if (isset($v['path'])) {
  69. $data['path'] = $v['path'];
  70. } else {
  71. $data['path'] = 'main';
  72. }
  73. if (isset($v['link'])) {
  74. $data['link'] = $v['link'];
  75. }
  76. Dever::db('menu')->up($where, $data);
  77. }
  78. }
  79. }
  80. public function getAll()
  81. {
  82. $data = Dever::db('menu')->select(array('parent_id' => '0'));
  83. return $data;
  84. }
  85. }