Menu.php 3.5 KB

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