Menu.php 3.5 KB

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