service.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. from typing import Any
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.core.base_schema import BatchSetAvailable
  4. from app.core.exceptions import CustomException
  5. from app.utils.common_util import (
  6. get_child_id_map,
  7. get_child_recursion,
  8. get_parent_id_map,
  9. get_parent_recursion,
  10. traversal_to_tree,
  11. )
  12. from .crud import MenuCRUD
  13. from .schema import (
  14. MenuCreateSchema,
  15. MenuOutSchema,
  16. MenuQueryParam,
  17. MenuUpdateSchema,
  18. )
  19. def _transform_external_menus(items: list[dict]) -> None:
  20. """递归转换外链菜单的路由和组件路径"""
  21. for item in items:
  22. if item.get("type") == 4:
  23. original_url = item.get("route_path", "")
  24. route_name = item.get("route_name") or "external"
  25. item["route_path"] = f"/{route_name}"
  26. item["component_path"] = "module_system/menu/ExternalLink"
  27. # 把原始 URL 存入 params,供 ExternalLink 组件读取
  28. item["params"] = [{"key": "url", "value": original_url}]
  29. if item.get("children"):
  30. _transform_external_menus(item["children"])
  31. class MenuService:
  32. """
  33. 菜单模块服务层
  34. """
  35. @classmethod
  36. async def _validate_parent_child_type(
  37. cls, auth: AuthSchema, parent_id: int | None, child_type: int
  38. ) -> None:
  39. """
  40. 父子类型约束:目录下仅允许目录/菜单/外链;菜单下仅允许按钮;按钮与外链下不可挂子级。
  41. 无父级时仅允许目录、菜单、外链(与前端一致)。
  42. """
  43. if parent_id is None:
  44. if child_type not in (1, 2, 4):
  45. raise CustomException(msg="顶级菜单仅允许目录、菜单或外链类型")
  46. return
  47. parent = await MenuCRUD(auth).get_by_id_crud(id=parent_id)
  48. if not parent:
  49. raise CustomException(msg="父级菜单不存在")
  50. pt = parent.type
  51. if pt == 1:
  52. if child_type not in (1, 2, 4):
  53. raise CustomException(msg="目录下仅允许新增目录、菜单或外链")
  54. elif pt == 2:
  55. if child_type != 3:
  56. raise CustomException(msg="菜单下仅允许新增按钮")
  57. else:
  58. raise CustomException(msg="菜单或链接类型下不允许新增子菜单")
  59. @classmethod
  60. async def get_menu_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  61. """
  62. 获取菜单详情。
  63. 参数:
  64. - auth (AuthSchema): 认证对象。
  65. - id (int): 菜单ID。
  66. 返回:
  67. - dict: 菜单详情对象。
  68. """
  69. menu = await MenuCRUD(auth).get_by_id_crud(id=id)
  70. # 创建实例后再设置parent_name属性
  71. menu_out = MenuOutSchema.model_validate(menu)
  72. if menu and menu.parent_id:
  73. parent = await MenuCRUD(auth).get_by_id_crud(id=menu.parent_id)
  74. if parent:
  75. menu_out.parent_name = parent.name
  76. return menu_out.model_dump()
  77. @classmethod
  78. async def get_menu_tree_service(
  79. cls,
  80. auth: AuthSchema,
  81. search: MenuQueryParam | None = None,
  82. order_by: list[dict] | None = None,
  83. ) -> list[dict]:
  84. """
  85. 获取菜单树形列表。
  86. 参数:
  87. - auth (AuthSchema): 认证对象。
  88. - search (MenuQueryParam | None): 查询参数对象。
  89. - order_by (list[dict] | None): 排序参数列表。
  90. 返回:
  91. - list[dict]: 菜单树形列表对象。
  92. """
  93. # 使用树形结构查询,预加载children关系
  94. menu_list = await MenuCRUD(auth).get_tree_list_crud(
  95. search=search.__dict__, order_by=order_by
  96. )
  97. # 转换为字典列表
  98. menu_dict_list = [MenuOutSchema.model_validate(menu).model_dump() for menu in menu_list]
  99. # 使用traversal_to_tree构建树形结构
  100. tree = traversal_to_tree(menu_dict_list)
  101. # 对外链菜单(type=4)转换路由数据
  102. from app.core.logger import log
  103. _transform_external_menus(tree)
  104. for item in (tree or []):
  105. log.info(f"外部链接菜单: path={item.get('route_path')}, name={item.get('route_name')}, component={item.get('component_path')}, params={item.get('params')}")
  106. for child in (item.get('children') or []):
  107. log.info(f" 子项: path={child.get('route_path')}, component={child.get('component_path')}, params={child.get('params')}")
  108. return tree
  109. @classmethod
  110. async def create_menu_service(cls, auth: AuthSchema, data: MenuCreateSchema) -> dict:
  111. """
  112. 创建菜单。
  113. 参数:
  114. - auth (AuthSchema): 认证对象。
  115. - data (MenuCreateSchema): 创建参数对象。
  116. 返回:
  117. - dict: 创建的菜单对象。
  118. """
  119. search: dict[str, Any] = {"title": data.title}
  120. if data.parent_id is not None:
  121. search["parent_id"] = data.parent_id
  122. menu = await MenuCRUD(auth).get(**search)
  123. if menu:
  124. raise CustomException(msg="创建失败,该菜单已存在")
  125. await cls._validate_parent_child_type(auth, data.parent_id, data.type)
  126. new_menu = await MenuCRUD(auth).create(data=data)
  127. new_menu_dict = MenuOutSchema.model_validate(new_menu).model_dump()
  128. return new_menu_dict
  129. @classmethod
  130. async def update_menu_service(cls, auth: AuthSchema, id: int, data: MenuUpdateSchema) -> dict:
  131. """
  132. 更新菜单。
  133. 参数:
  134. - auth (AuthSchema): 认证对象。
  135. - id (int): 菜单ID。
  136. - data (MenuUpdateSchema): 更新参数对象。
  137. 返回:
  138. - dict: 更新的菜单对象。
  139. """
  140. menu = await MenuCRUD(auth).get_by_id_crud(id=id)
  141. if not menu:
  142. raise CustomException(msg="更新失败,该菜单不存在")
  143. await cls._validate_parent_child_type(auth, data.parent_id, data.type)
  144. search: dict[str, Any] = {"title": data.title}
  145. if data.parent_id is not None:
  146. search["parent_id"] = data.parent_id
  147. exist_menu = await MenuCRUD(auth).get(**search)
  148. if exist_menu and exist_menu.id != id:
  149. raise CustomException(msg="更新失败,菜单标题重复")
  150. if data.parent_id:
  151. parent_menu = await MenuCRUD(auth).get_by_id_crud(id=data.parent_id)
  152. if not parent_menu:
  153. raise CustomException(msg="更新失败,父级菜单不存在")
  154. data.parent_name = parent_menu.title
  155. new_menu = await MenuCRUD(auth).update(id=id, data=data)
  156. await cls.set_menu_available_service(
  157. auth=auth, data=BatchSetAvailable(ids=[id], status=data.status)
  158. )
  159. new_menu_dict = MenuOutSchema.model_validate(new_menu).model_dump()
  160. return new_menu_dict
  161. @classmethod
  162. async def delete_menu_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  163. """
  164. 删除菜单。
  165. 参数:
  166. - auth (AuthSchema): 认证对象。
  167. - ids (list[int]): 菜单ID列表。
  168. 返回:
  169. - None
  170. """
  171. if len(ids) < 1:
  172. raise CustomException(msg="删除失败,删除对象不能为空")
  173. # 获取所有菜单列表,用于构建树形关系
  174. all_menus = await MenuCRUD(auth).get_list_crud()
  175. # 构建子菜单ID映射
  176. child_id_map = get_child_id_map(model_list=all_menus)
  177. # 收集所有需要删除的菜单ID,包括直接指定的ID和它们的所有子菜单ID
  178. delete_ids_set = set()
  179. for id in ids:
  180. # 递归获取该ID的所有子菜单ID
  181. all_descendants = get_child_recursion(id=id, id_map=child_id_map)
  182. delete_ids_set.update(all_descendants)
  183. # 将集合转换为列表
  184. delete_ids = list(delete_ids_set)
  185. # 执行批量删除操作
  186. await MenuCRUD(auth).delete(ids=delete_ids)
  187. @classmethod
  188. async def set_menu_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
  189. """
  190. 递归获取所有父、子级菜单,然后批量修改菜单可用状态。
  191. 参数:
  192. - auth (AuthSchema): 认证对象。
  193. - data (BatchSetAvailable): 批量设置可用参数对象。
  194. 返回:
  195. - None
  196. """
  197. menu_list = await MenuCRUD(auth).get_list_crud()
  198. total_ids = []
  199. if data.status == "0":
  200. # 激活,则需要把所有父级菜单都激活
  201. id_map = get_parent_id_map(model_list=menu_list)
  202. for menu_id in data.ids:
  203. enable_ids = get_parent_recursion(id=menu_id, id_map=id_map)
  204. total_ids.extend(enable_ids)
  205. else:
  206. # 禁止,则需要把所有子级菜单都禁止
  207. id_map = get_child_id_map(model_list=menu_list)
  208. for menu_id in data.ids:
  209. disable_ids = get_child_recursion(id=menu_id, id_map=id_map)
  210. total_ids.extend(disable_ids)
  211. await MenuCRUD(auth).set_available_crud(ids=total_ids, status=data.status)