upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Path
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.common.response import SuccessResponse
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.logger import log
|
||||
from app.core.router_class import OperationLogRoute
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .service import MenuService
|
||||
from .schema import (
|
||||
MenuCreateSchema,
|
||||
MenuUpdateSchema,
|
||||
MenuQueryParam
|
||||
)
|
||||
|
||||
MenuRouter = APIRouter(route_class=OperationLogRoute, prefix="/menu", tags=["菜单管理"])
|
||||
|
||||
|
||||
@MenuRouter.get("/tree", summary="查询菜单树", description="查询菜单树")
|
||||
async def get_menu_tree_controller(
|
||||
search: MenuQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询菜单树。
|
||||
|
||||
参数:
|
||||
- search (MenuQueryParam): 查询参数模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含菜单树的 JSON 响应。
|
||||
"""
|
||||
order_by = [{"order": "asc"}]
|
||||
result_dict_list = await MenuService.get_menu_tree_service(search=search, auth=auth, order_by=order_by)
|
||||
log.info(f"查询菜单树成功")
|
||||
return SuccessResponse(data=result_dict_list, msg="查询菜单树成功")
|
||||
|
||||
|
||||
@MenuRouter.get("/detail/{id}", summary="查询菜单详情", description="查询菜单详情")
|
||||
async def get_obj_detail_controller(
|
||||
id: int = Path(..., description="菜单ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询菜单详情。
|
||||
|
||||
参数:
|
||||
- id (int): 菜单ID。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含菜单详情的 JSON 响应。
|
||||
"""
|
||||
result_dict = await MenuService.get_menu_detail_service(id=id, auth=auth)
|
||||
log.info(f"查询菜单情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取菜单成功")
|
||||
|
||||
|
||||
@MenuRouter.post("/create", summary="创建菜单", description="创建菜单")
|
||||
async def create_obj_controller(
|
||||
data: MenuCreateSchema,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:create"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建菜单。
|
||||
|
||||
参数:
|
||||
- data (MenuCreateSchema): 菜单创建模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含创建菜单的 JSON 响应。
|
||||
"""
|
||||
result_dict = await MenuService.create_menu_service(data=data, auth=auth)
|
||||
log.info(f"创建菜单成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建菜单成功")
|
||||
|
||||
|
||||
@MenuRouter.put("/update/{id}", summary="修改菜单", description="修改菜单")
|
||||
async def update_obj_controller(
|
||||
data: MenuUpdateSchema,
|
||||
id: int = Path(..., description="菜单ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:update"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改菜单。
|
||||
|
||||
参数:
|
||||
- id (int): 菜单ID。
|
||||
- data (MenuUpdateSchema): 菜单更新模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含修改菜单的 JSON 响应。
|
||||
"""
|
||||
result_dict = await MenuService.update_menu_service(id=id, data=data, auth=auth)
|
||||
log.info(f"修改菜单成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改菜单成功")
|
||||
|
||||
|
||||
@MenuRouter.delete("/delete", summary="删除菜单", description="删除菜单")
|
||||
async def delete_obj_controller(
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:delete"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除菜单。
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 菜单ID列表。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含删除菜单的 JSON 响应。
|
||||
"""
|
||||
await MenuService.delete_menu_service(ids=ids, auth=auth)
|
||||
log.info(f"删除菜单成功: {ids}")
|
||||
return SuccessResponse(msg="删除菜单成功")
|
||||
|
||||
|
||||
@MenuRouter.patch("/available/setting", summary="批量修改菜单状态", description="批量修改菜单状态")
|
||||
async def batch_set_available_obj_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:patch"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改菜单状态。
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改菜单状态模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 批量修改菜单状态的 JSON 响应。
|
||||
"""
|
||||
await MenuService.set_menu_available_service(data=data, auth=auth)
|
||||
log.info(f"批量修改菜单状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改菜单状态成功")
|
||||
@@ -0,0 +1,75 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Sequence
|
||||
|
||||
from app.core.base_crud import CRUDBase
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .model import MenuModel
|
||||
from .schema import MenuCreateSchema, MenuUpdateSchema
|
||||
|
||||
|
||||
class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
|
||||
"""菜单模块数据层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""初始化菜单CRUD"""
|
||||
self.auth = auth
|
||||
super().__init__(model=MenuModel, auth=auth)
|
||||
|
||||
async def get_by_id_crud(self, id: int, preload: list[str] | None = None) -> MenuModel | None:
|
||||
"""
|
||||
根据 id 获取菜单信息。
|
||||
|
||||
参数:
|
||||
- id (int): 菜单 ID。
|
||||
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- MenuModel | None: 菜单信息,未找到返回 None。
|
||||
"""
|
||||
obj = await self.get(id=id, preload=preload)
|
||||
if not obj:
|
||||
return None
|
||||
return obj
|
||||
|
||||
async def get_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list[str] | None = None) -> Sequence[MenuModel]:
|
||||
"""
|
||||
获取菜单列表。
|
||||
|
||||
参数:
|
||||
- search (dict | None): 搜索条件。
|
||||
- order_by (list[dict] | None): 排序字段列表。
|
||||
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[MenuModel]: 菜单列表。
|
||||
"""
|
||||
return await self.list(search=search, order_by=order_by, preload=preload)
|
||||
|
||||
async def get_tree_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list[str] | None = None) -> Sequence[MenuModel]:
|
||||
"""
|
||||
获取菜单树形列表。
|
||||
|
||||
参数:
|
||||
- search (dict | None): 搜索条件。
|
||||
- order_by (list[dict] | None): 排序字段列表。
|
||||
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[MenuModel]: 菜单树形列表。
|
||||
"""
|
||||
return await self.tree_list(search=search, order_by=order_by, children_attr='children', preload=preload)
|
||||
|
||||
async def set_available_crud(self, ids: list[int], status: str) -> None:
|
||||
"""
|
||||
批量设置菜单可用状态。
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 菜单 ID 列表。
|
||||
- status (str): 可用状态。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
await self.set(ids=ids, status=status)
|
||||
@@ -0,0 +1,68 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from sqlalchemy import Boolean, String, Integer, JSON, ForeignKey
|
||||
from sqlalchemy.orm import relationship, Mapped, mapped_column
|
||||
|
||||
from app.core.base_model import ModelMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.api.v1.module_system.role.model import RoleModel
|
||||
|
||||
|
||||
class MenuModel(ModelMixin):
|
||||
"""
|
||||
菜单表 - 用于存储系统菜单信息
|
||||
|
||||
菜单类型说明:
|
||||
- 1: 目录(一级菜单)
|
||||
- 2: 菜单(二级菜单)
|
||||
- 3: 按钮/权限(页面内按钮权限)
|
||||
- 4: 外部链接
|
||||
"""
|
||||
__tablename__: str = "sys_menu"
|
||||
__table_args__: dict[str, str] = ({'comment': '菜单表'})
|
||||
__loader_options__: list[str] = ["roles"]
|
||||
|
||||
name: Mapped[str] = mapped_column(String(50), nullable=False, comment='菜单名称')
|
||||
type: Mapped[int] = mapped_column(Integer, nullable=False, default=2, comment='菜单类型(1:目录 2:菜单 3:按钮/权限 4:链接)')
|
||||
order: Mapped[int] = mapped_column(Integer, nullable=False, default=999, comment='显示排序')
|
||||
permission: Mapped[str | None] = mapped_column(String(100), comment='权限标识(如:module_system:user:list)')
|
||||
icon: Mapped[str | None] = mapped_column(String(50), comment='菜单图标')
|
||||
route_name: Mapped[str | None] = mapped_column(String(100), comment='路由名称')
|
||||
route_path: Mapped[str | None] = mapped_column(String(200), comment='路由路径')
|
||||
component_path: Mapped[str | None] = mapped_column(String(200), comment='组件路径')
|
||||
redirect: Mapped[str | None] = mapped_column(String(200), comment='重定向地址')
|
||||
hidden: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, comment='是否隐藏(True:隐藏 False:显示)')
|
||||
keep_alive: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False, comment='是否缓存(True:是 False:否)')
|
||||
always_show: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, comment='是否始终显示(True:是 False:否)')
|
||||
title: Mapped[str | None] = mapped_column(String(50), comment='菜单标题')
|
||||
params: Mapped[list[dict[str, str]] | None] = mapped_column(JSON, comment='路由参数(JSON对象)')
|
||||
affix: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, comment='是否固定标签页(True:是 False:否)')
|
||||
|
||||
# 树形结构
|
||||
parent_id: Mapped[int | None] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey('sys_menu.id', ondelete='SET NULL'),
|
||||
default=None,
|
||||
index=True,
|
||||
comment='父菜单ID'
|
||||
)
|
||||
|
||||
# 关联关系
|
||||
parent: Mapped["MenuModel | None"] = relationship(
|
||||
back_populates='children',
|
||||
remote_side="MenuModel.id",
|
||||
foreign_keys="MenuModel.parent_id",
|
||||
uselist=False
|
||||
)
|
||||
children: Mapped[list["MenuModel"] | None] = relationship(
|
||||
back_populates='parent',
|
||||
foreign_keys="MenuModel.parent_id",
|
||||
order_by="MenuModel.order"
|
||||
)
|
||||
roles: Mapped[list["RoleModel"]] = relationship(
|
||||
secondary="sys_role_menus",
|
||||
back_populates="menus",
|
||||
lazy="selectin"
|
||||
)
|
||||
@@ -0,0 +1,102 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Literal
|
||||
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
||||
from fastapi import Query
|
||||
|
||||
from app.core.validator import DateTimeStr
|
||||
from app.core.validator import menu_request_validator
|
||||
from app.core.base_schema import BaseSchema
|
||||
|
||||
|
||||
class MenuCreateSchema(BaseModel):
|
||||
"""菜单创建模型"""
|
||||
name: str = Field(..., max_length=50, description="菜单名称")
|
||||
type: int = Field(..., ge=1, le=4, description="菜单类型(1:目录 2:菜单 3:按钮 4:外链)")
|
||||
order: int = Field(..., ge=1, description="显示顺序")
|
||||
permission: str | None = Field(default=None, max_length=100, description="权限标识")
|
||||
icon: str | None = Field(default=None, max_length=100, description="菜单图标")
|
||||
route_name: str | None = Field(default=None, max_length=100, description="路由名称")
|
||||
route_path: str | None = Field(default=None, max_length=200, description="路由地址")
|
||||
component_path: str | None = Field(default=None, max_length=255, description="组件路径")
|
||||
redirect: str | None = Field(default=None, max_length=200, description="重定向地址")
|
||||
hidden: bool = Field(default=False, description="是否隐藏(True:是 False:否)")
|
||||
keep_alive: bool = Field(default=True, description="是否缓存(True:是 False:否)")
|
||||
always_show: bool = Field(default=False, description="是否始终显示(True:是 False:否)")
|
||||
title: str | None = Field(default=None, max_length=50, description="菜单标题")
|
||||
params: list[dict[str, str]] | None = Field(default=None, description="路由参数,格式为[{key: string, value: string}]")
|
||||
affix: bool = Field(default=False, description="是否固定标签页(True:是 False:否)")
|
||||
parent_id: int | None = Field(default=None, ge=1, description="父菜单ID")
|
||||
status: str = Field(default="0", description="是否启用(0:启用 1:禁用)")
|
||||
description: str | None = Field(default=None, max_length=255, description="描述")
|
||||
|
||||
@model_validator(mode='before')
|
||||
@classmethod
|
||||
def _normalize(cls, values):
|
||||
if isinstance(values, dict):
|
||||
# 字符串去空格
|
||||
for k in ["name", "icon", "permission", "route_name", "route_path", "component_path", "redirect", "title", "description"]:
|
||||
if k in values and isinstance(values[k], str):
|
||||
values[k] = values[k].strip() or None if values[k].strip() == "" else values[k].strip()
|
||||
# 父ID转整型
|
||||
if "parent_id" in values and isinstance(values["parent_id"], str):
|
||||
try:
|
||||
values["parent_id"] = int(values["parent_id"].strip())
|
||||
except Exception:
|
||||
pass
|
||||
# 路由名/路径规范
|
||||
import re
|
||||
if "route_name" in values and isinstance(values["route_name"], str):
|
||||
rn = values["route_name"]
|
||||
if rn and not re.match(r"^[A-Za-z][A-Za-z0-9_.-]{1,99}$", rn):
|
||||
raise ValueError("路由名称需字母开头,仅含字母/数字/_ . -")
|
||||
if "route_path" in values and isinstance(values["route_path"], str):
|
||||
rp = values["route_path"]
|
||||
if rp and not rp.startswith("/"):
|
||||
raise ValueError("路由路径需以 / 开头")
|
||||
return values
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_fields(self):
|
||||
return menu_request_validator(self)
|
||||
|
||||
|
||||
class MenuUpdateSchema(MenuCreateSchema):
|
||||
"""菜单更新模型"""
|
||||
parent_name: str | None = Field(default=None, max_length=50, description="父菜单名称")
|
||||
|
||||
|
||||
class MenuOutSchema(MenuCreateSchema, BaseSchema):
|
||||
"""菜单响应模型"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
parent_name: str | None = Field(default=None, max_length=50, description="父菜单名称")
|
||||
|
||||
|
||||
class MenuQueryParam:
|
||||
"""菜单管理查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str | None = Query(None, description="菜单名称"),
|
||||
route_path: str | None = Query(None, description="路由地址"),
|
||||
component_path: str | None = Query(None, description="组件路径"),
|
||||
type: Literal[1,2,3,4] | None = Query(None, description="菜单类型(1:目录 2:菜单 3:按钮 4:外链)"),
|
||||
permission: str | None = Query(None, description="权限标识"),
|
||||
status: str | None = Query(None, description="菜单状态(0:启用 1:禁用)"),
|
||||
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
) -> None:
|
||||
|
||||
# 模糊查询字段
|
||||
self.name = ("like", name)
|
||||
self.route_path = ("like", route_path)
|
||||
self.component_path = ("like", component_path)
|
||||
self.permission = ("like", permission)
|
||||
|
||||
# 精确查询字段
|
||||
self.type = type
|
||||
self.status = status
|
||||
|
||||
# 时间范围查询
|
||||
if created_time and len(created_time) == 2:
|
||||
self.created_time = ("between", (created_time[0], created_time[1]))
|
||||
@@ -0,0 +1,177 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.exceptions import CustomException
|
||||
from app.utils.common_util import (
|
||||
get_parent_id_map,
|
||||
get_parent_recursion,
|
||||
get_child_id_map,
|
||||
get_child_recursion,
|
||||
traversal_to_tree
|
||||
)
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .crud import MenuCRUD
|
||||
from .schema import (
|
||||
MenuCreateSchema,
|
||||
MenuUpdateSchema,
|
||||
MenuOutSchema,
|
||||
MenuQueryParam
|
||||
)
|
||||
|
||||
|
||||
class MenuService:
|
||||
"""
|
||||
菜单模块服务层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def get_menu_detail_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
获取菜单详情。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- id (int): 菜单ID。
|
||||
|
||||
返回:
|
||||
- dict: 菜单详情对象。
|
||||
"""
|
||||
menu = await MenuCRUD(auth).get_by_id_crud(id=id)
|
||||
# 创建实例后再设置parent_name属性
|
||||
menu_out = MenuOutSchema.model_validate(menu)
|
||||
if menu and menu.parent_id:
|
||||
parent = await MenuCRUD(auth).get_by_id_crud(id=menu.parent_id)
|
||||
if parent:
|
||||
menu_out.parent_name = parent.name
|
||||
|
||||
return menu_out.model_dump()
|
||||
|
||||
@classmethod
|
||||
async def get_menu_tree_service(cls, auth: AuthSchema, search: MenuQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
|
||||
"""
|
||||
获取菜单树形列表。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- search (MenuQueryParam | None): 查询参数对象。
|
||||
- order_by (list[dict] | None): 排序参数列表。
|
||||
|
||||
返回:
|
||||
- list[dict]: 菜单树形列表对象。
|
||||
"""
|
||||
# 使用树形结构查询,预加载children关系
|
||||
menu_list = await MenuCRUD(auth).get_tree_list_crud(search=search.__dict__, order_by=order_by)
|
||||
# 转换为字典列表
|
||||
menu_dict_list = [MenuOutSchema.model_validate(menu).model_dump() for menu in menu_list]
|
||||
# 使用traversal_to_tree构建树形结构
|
||||
return traversal_to_tree(menu_dict_list)
|
||||
|
||||
@classmethod
|
||||
async def create_menu_service(cls, auth: AuthSchema, data: MenuCreateSchema) -> dict:
|
||||
"""
|
||||
创建菜单。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- data (MenuCreateSchema): 创建参数对象。
|
||||
|
||||
返回:
|
||||
- dict: 创建的菜单对象。
|
||||
"""
|
||||
menu = await MenuCRUD(auth).get(name=data.name)
|
||||
if menu:
|
||||
raise CustomException(msg='创建失败,该菜单已存在')
|
||||
|
||||
new_menu = await MenuCRUD(auth).create(data=data)
|
||||
new_menu_dict = MenuOutSchema.model_validate(new_menu).model_dump()
|
||||
return new_menu_dict
|
||||
|
||||
@classmethod
|
||||
async def update_menu_service(cls, auth: AuthSchema,id:int, data: MenuUpdateSchema) -> dict:
|
||||
"""
|
||||
更新菜单。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- id (int): 菜单ID。
|
||||
- data (MenuUpdateSchema): 更新参数对象。
|
||||
|
||||
返回:
|
||||
- dict: 更新的菜单对象。
|
||||
"""
|
||||
menu = await MenuCRUD(auth).get_by_id_crud(id=id)
|
||||
if not menu:
|
||||
raise CustomException(msg='更新失败,该菜单不存在')
|
||||
exist_menu = await MenuCRUD(auth).get(name=data.name)
|
||||
if exist_menu and exist_menu.id != id:
|
||||
raise CustomException(msg='更新失败,菜单名称重复')
|
||||
|
||||
if data.parent_id:
|
||||
parent_menu = await MenuCRUD(auth).get_by_id_crud(id=data.parent_id)
|
||||
if not parent_menu:
|
||||
raise CustomException(msg='更新失败,父级菜单不存在')
|
||||
data.parent_name = parent_menu.name
|
||||
new_menu = await MenuCRUD(auth).update(id=id, data=data)
|
||||
|
||||
await cls.set_menu_available_service(auth=auth, data=BatchSetAvailable(ids=[id], status=data.status))
|
||||
|
||||
new_menu_dict = MenuOutSchema.model_validate(new_menu).model_dump()
|
||||
return new_menu_dict
|
||||
|
||||
@classmethod
|
||||
async def delete_menu_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
||||
"""
|
||||
删除菜单。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- ids (list[int]): 菜单ID列表。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
for id in ids:
|
||||
menu = await MenuCRUD(auth).get_by_id_crud(id=id)
|
||||
if not menu:
|
||||
raise CustomException(msg='删除失败,该菜单不存在')
|
||||
# 校验是否存在子级菜单,存在则禁止删除
|
||||
menu_list = await MenuCRUD(auth).get_list_crud()
|
||||
id_map = get_child_id_map(model_list=menu_list)
|
||||
for id in ids:
|
||||
descendants = get_child_recursion(id=id, id_map=id_map)
|
||||
if len(descendants) > 1:
|
||||
raise CustomException(msg='删除失败,存在子级菜单,请先删除子级菜单')
|
||||
await MenuCRUD(auth).delete(ids=ids)
|
||||
|
||||
@classmethod
|
||||
async def set_menu_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
递归获取所有父、子级菜单,然后批量修改菜单可用状态。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- data (BatchSetAvailable): 批量设置可用参数对象。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
menu_list = await MenuCRUD(auth).get_list_crud()
|
||||
total_ids = []
|
||||
|
||||
if data.status:
|
||||
# 激活,则需要把所有父级菜单都激活
|
||||
id_map = get_parent_id_map(model_list=menu_list)
|
||||
for menu_id in data.ids:
|
||||
enable_ids = get_parent_recursion(id=menu_id, id_map=id_map)
|
||||
total_ids.extend(enable_ids)
|
||||
else:
|
||||
# 禁止,则需要把所有子级菜单都禁止
|
||||
id_map = get_child_id_map(model_list=menu_list)
|
||||
for menu_id in data.ids:
|
||||
disable_ids = get_child_recursion(id=menu_id, id_map=id_map)
|
||||
total_ids.extend(disable_ids)
|
||||
|
||||
await MenuCRUD(auth).set_available_crud(ids=total_ids, status=data.status)
|
||||
Reference in New Issue
Block a user