Menu.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php namespace Manage\Api;
  2. use Dever;
  3. use Manage\Lib\Auth;
  4. class Menu extends Auth
  5. {
  6. protected $top;
  7. protected $opened;
  8. public function info()
  9. {
  10. # 如果后续增加Root,就要这里置为false
  11. $this->top = true;
  12. $top = Dever::db('menu', 'manage')->select(['parent_id' => '0', 'module_id' => $this->user['select']['module_id']]);
  13. $result = $menu = [];
  14. $result[] = array
  15. (
  16. 'path' => '/',
  17. 'name' => 'Root',
  18. 'component' => 'Layout',
  19. 'meta' => [
  20. 'title' => '首页',
  21. 'icon' => 'home-2-line',
  22. 'breadcrumbHidden' => true,
  23. ],
  24. 'children' => array
  25. (
  26. array
  27. (
  28. 'path' => 'index',
  29. 'name' => 'Index',
  30. 'component' => '@/dever/index/index',
  31. 'meta' => [
  32. 'title' => '控制台',
  33. 'icon' => 'home-2-line',
  34. 'noClosable' => true,
  35. ]
  36. ),
  37. )
  38. );
  39. $this->opened = [];
  40. foreach ($top as $v) {
  41. $v = $this->getMenu($v, '');
  42. if ($v) {
  43. $result[] = $v;
  44. }
  45. }
  46. return ['list' => $result, 'opened' => $this->opened];
  47. }
  48. private function getMenu($v, $parent = '')
  49. {
  50. $info = array
  51. (
  52. 'path' => $parent ? '/' . $parent . '/' . $v['key'] : $v['key'],
  53. 'name' => $parent ? $parent . '_' . $v['key'] : $v['key'],
  54. 'meta' => [
  55. 'title' => $v['name'],
  56. 'icon' => $v['icon'],
  57. //'noClosable' => true,
  58. 'breadcrumbHidden' => false,
  59. 'dynamicNewTab' => true,
  60. ]
  61. );
  62. if ($v['show'] > 1) {
  63. $info['meta']['hidden'] = true;
  64. }
  65. if (isset($v['active']) && $v['active']) {
  66. $info['meta']['activeMenu'] = $v['active'];
  67. }
  68. if ($v['parent_id'] <= 0) {
  69. if ($this->top) {
  70. $info['path'] = '/' . $v['key'];
  71. } else {
  72. $this->top = true;
  73. $info['path'] = '/';
  74. }
  75. $info['component'] = 'Layout';
  76. }
  77. $where = ['parent_id' => $v['id'], 'module_id' => $this->user['select']['module_id']];
  78. $child = Dever::db('menu', 'manage')->select($where);
  79. if ($child) {
  80. foreach ($child as $v1) {
  81. if ($v1['level'] == 3 && $v1['show'] <= 2 && $this->checkMenu($v1['id'])) {
  82. continue;
  83. }
  84. if (!$parent) {
  85. $this->opened[] = '/' . $v['key'] . '/' . $v1['key'];
  86. }
  87. $children = $this->getMenu($v1, $v['key']);
  88. if ($children) {
  89. $info['children'][] = $children;
  90. }
  91. }
  92. if (empty($info['children'])) {
  93. return [];
  94. }
  95. } elseif ($v['path']) {
  96. $info['component'] = '@/dever/page/' . $v['path'];
  97. }
  98. if (!$child) {
  99. if ($v['level'] == 3 && $v['show'] <= 2 && $this->checkMenu($v['id'])) {
  100. return false;
  101. }
  102. }
  103. return $info;
  104. }
  105. }