MenuService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/8/15 14:30
  7. */
  8. namespace App\Http\Services\Backend\Server\System;
  9. use App\Enums\System\MenuType;
  10. use App\Exceptions\ApiException;
  11. use App\Http\Services\Service;
  12. use App\Models\System\Menu;
  13. use Illuminate\Support\Arr;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class MenuService extends Service
  16. {
  17. /**
  18. * @throws ApiException
  19. */
  20. public function createMenu($data)
  21. {
  22. // 校验父菜单存在
  23. self::validateParentMenu($data['parentId'], null);
  24. // 校验菜单(自己)
  25. self::validateMenu($data['parentId'], $data['name'], null);
  26. // 插入数据库
  27. $menu = self::toModel($data, Menu::class);
  28. self::initMenuProperty($menu);
  29. return Menu::create($menu->getAttributes())->id;
  30. }
  31. /**
  32. * @throws ApiException
  33. */
  34. public function updateMenu($params)
  35. {
  36. // 校验更新的菜单是否存在
  37. !self::isExistMenu($params['id']) && self::error('MENU_NOT_EXISTS', Response::HTTP_UNPROCESSABLE_ENTITY);
  38. // 校验父菜单存在
  39. self::validateParentMenu($params['parentId'], $params['id']);
  40. // 校验菜单(自己)
  41. self::validateMenu($params['parentId'], $params['name'], $params['id']);
  42. // 更新到数据库
  43. $menu = self::toModel($params, Menu::class);
  44. self::initMenuProperty($menu);
  45. $menu->update($menu->getAttributes());
  46. }
  47. /**
  48. * @throws ApiException
  49. */
  50. public function deleteMenu($id)
  51. {
  52. // 校验是否还有子菜单
  53. self::validateChildrenMenu($id);
  54. // 校验删除的菜单是否存在
  55. !self::isExistMenu($id) && self::error('MENU_NOT_EXISTS',Response::HTTP_UNPROCESSABLE_ENTITY);
  56. $menu = self::toModel(['id' => $id], Menu::class);
  57. // 标记删除
  58. return $menu->delete();
  59. }
  60. public function getMenu($id)
  61. {
  62. $menu = Menu::query();
  63. $select = ['id','name','permission','type','sort','parent_id as parentId','path','icon','component','component_name as componentName','status','visible','keep_alive as keepAlive','always_show as alwaysShow'];
  64. return $menu->select($select)->find($id)->toArray();
  65. }
  66. public function getMenuList($params = []): array
  67. {
  68. $menu = Menu::query();
  69. $select = ['id','name','permission','type','sort','parent_id as parentId','path','icon','component','component_name as componentName','status','visible','keep_alive as keepAlive','always_show as alwaysShow'];
  70. !empty($params['name']) && $menu->whereLike('name', "%{$params['name']}%");
  71. !empty($params['status']) && $menu->where('status', $params['status']);
  72. return $menu->orderBy('sort')->select($select)->get()->toArray();
  73. }
  74. public function getSimpleMenuList(): array
  75. {
  76. $menus = $this->getMenuList(['status' => 0]);
  77. $list = self::filterDisableMenus($menus);
  78. // list.sort(Comparator . comparing(MenuDO::getSort));
  79. return $list;
  80. }
  81. protected static function isExistMenu(int|array $condition): bool
  82. {
  83. $menu = Menu::query();
  84. is_array($condition) && $menu->where($condition);
  85. is_numeric($condition) && $menu->where('id', $condition);
  86. return $menu->exists();
  87. }
  88. /**
  89. * @throws ApiException
  90. */
  91. protected static function validateChildrenMenu($parentId): void
  92. {
  93. Menu::query()->where('parent_id', $parentId)->count() && self::error('MENU_EXISTS_CHILDREN', Response::HTTP_UNPROCESSABLE_ENTITY);
  94. }
  95. /**
  96. * @throws ApiException
  97. */
  98. protected static function validateParentMenu($parentId, $childId): void
  99. {
  100. if (!$parentId) return;
  101. // 不能设置自己为父菜单
  102. $parentId === $childId && self::error('MENU_PARENT_ERROR',Response::HTTP_UNPROCESSABLE_ENTITY);
  103. $menu = Menu::query()->select('type')->find($parentId);
  104. // 父菜单不存在
  105. !$menu && self::error('MENU_PARENT_NOT_EXISTS', Response::HTTP_UNPROCESSABLE_ENTITY);
  106. // 父菜单必须是目录或者菜单类型
  107. $menu->type != MenuType::DIR && $menu->type != MenuType::MENU && self::error('MENU_PARENT_NOT_DIR_OR_MENU', Response::HTTP_UNPROCESSABLE_ENTITY);
  108. }
  109. /**
  110. * @throws ApiException
  111. */
  112. protected static function validateMenu($parentId, $name, $id): void
  113. {
  114. $where = ['parent_id' => $parentId, 'name' => $name];
  115. $menu = Menu::query()->where($where)->first();
  116. if (is_null($menu)) return;
  117. // 如果 id 为空,说明不用比较是否为相同 id 的菜单
  118. !$id && self::error('MENU_NAME_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
  119. $menu->id !== $id && self::error('MENU_NAME_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
  120. }
  121. /**
  122. * 初始化菜单的通用属性。
  123. * <p>
  124. * 例如说,只有目录或者菜单类型的菜单,才设置 icon
  125. *
  126. */
  127. private static function initMenuProperty(&$menu): void
  128. {
  129. // 菜单为按钮类型时,无需 component、icon、path 属性,进行置空
  130. if ($menu->type === MenuType::BUTTON) {
  131. $menu->component = null;
  132. $menu->componentName = null;
  133. $menu->icon = null;
  134. $menu->path = null;
  135. }
  136. (isset($menu->permission) && empty($menu->permission) || !isset($menu->permission)) && ($menu->permission = null);
  137. }
  138. private static function filterDisableMenus($menus): array
  139. {
  140. if (empty($menus)) return [];
  141. // $menuMap = convertMap($menus,'id');
  142. // $menuMap = collect($menus)->mapWithKeys(function ($menu) {
  143. // return [$menu['id'] => $menu];
  144. // })->all();
  145. // 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果
  146. $enabledMenus = [];
  147. $disabledMenuCache = []; // 存下递归搜索过被禁用的菜单,防止重复的搜索
  148. foreach ($menus as $menu) {
  149. if($menu['status'] !== 0) continue;
  150. // if($menu['type'] === 3) continue;
  151. // if (isMenuDisabled($menu, $menuMap, $disabledMenuCache)) {
  152. // continue;
  153. // }
  154. $enabledMenus[] = ['id' => $menu['id'],'status' => $menu['status'], 'name' => $menu['name'], 'parentId' => $menu['parentId'], 'type' => $menu['type']];
  155. }
  156. return $enabledMenus;
  157. }
  158. }