Menu.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace plugin\admin\api;
  3. use plugin\admin\app\model\Role;
  4. use plugin\admin\app\model\Rule;
  5. use support\exception\BusinessException;
  6. use function admin;
  7. /**
  8. * 对外提供的菜单接口
  9. */
  10. class Menu
  11. {
  12. /**
  13. * 根据key获取菜单
  14. * @param $key
  15. * @return array
  16. */
  17. public static function get($key)
  18. {
  19. $menu = Rule::where('key', $key)->first();
  20. return $menu ? $menu->toArray() : null;
  21. }
  22. /**
  23. * 根据id获得菜单
  24. * @param $id
  25. * @return array
  26. */
  27. public static function find($id): array
  28. {
  29. return Rule::find($id)->toArray();
  30. }
  31. /**
  32. * 添加菜单
  33. * @param array $menu
  34. * @return int
  35. */
  36. public static function add(array $menu)
  37. {
  38. $item = new Rule;
  39. foreach ($menu as $key => $value) {
  40. $item->$key = $value;
  41. }
  42. $item->save();
  43. return $item->id;
  44. }
  45. /**
  46. * 导入菜单
  47. * @param array $menu_tree
  48. * @return void
  49. */
  50. public static function import(array $menu_tree)
  51. {
  52. if (is_numeric(key($menu_tree)) && !isset($menu_tree['key'])) {
  53. foreach ($menu_tree as $item) {
  54. static::import($item);
  55. }
  56. return;
  57. }
  58. $children = $menu_tree['children'] ?? [];
  59. unset($menu_tree['children']);
  60. if ($old_menu = Menu::get($menu_tree['key'])) {
  61. $pid = $old_menu['id'];
  62. Rule::where('key', $menu_tree['key'])->update($menu_tree);
  63. } else {
  64. $pid = static::add($menu_tree);
  65. }
  66. foreach ($children as $menu) {
  67. $menu['pid'] = $pid;
  68. static::import($menu);
  69. }
  70. }
  71. /**
  72. * 删除菜单
  73. * @param $key
  74. * @return void
  75. */
  76. public static function delete($key)
  77. {
  78. $item = Rule::where('key', $key)->first();
  79. if (!$item) {
  80. return;
  81. }
  82. // 子规则一起删除
  83. $delete_ids = $children_ids = [$item['id']];
  84. while($children_ids) {
  85. $children_ids = Rule::whereIn('pid', $children_ids)->pluck('id')->toArray();
  86. $delete_ids = array_merge($delete_ids, $children_ids);
  87. }
  88. Rule::whereIn('id', $delete_ids)->delete();
  89. }
  90. /**
  91. * 获取菜单中某个(些)字段的值
  92. * @param $menu
  93. * @param null $column
  94. * @param null $index
  95. * @return array|mixed
  96. */
  97. public static function column($menu, $column = null, $index = null)
  98. {
  99. $values = [];
  100. if (is_numeric(key($menu)) && !isset($menu['key'])) {
  101. foreach ($menu as $item) {
  102. $values = array_merge($values, static::column($item, $column, $index));
  103. }
  104. return $values;
  105. }
  106. $children = $menu['children'] ?? [];
  107. unset($menu['children']);
  108. if ($column === null) {
  109. if ($index) {
  110. $values[$menu[$index]] = $menu;
  111. } else {
  112. $values[] = $menu;
  113. }
  114. } else {
  115. if (is_array($column)) {
  116. $item = [];
  117. foreach ($column as $f) {
  118. $item[$f] = $menu[$f] ?? null;
  119. }
  120. if ($index) {
  121. $values[$menu[$index]] = $item;
  122. } else {
  123. $values[] = $item;
  124. }
  125. } else {
  126. $value = $menu[$column] ?? null;
  127. if ($index) {
  128. $values[$menu[$index]] = $value;
  129. } else {
  130. $values[] = $value;
  131. }
  132. }
  133. }
  134. foreach ($children as $child) {
  135. $values = array_merge($values, static::column($child, $column, $index));
  136. }
  137. return $values;
  138. }
  139. }