MenuService.php 6.3 KB

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