style: 移除Python文件中的编码声明并优化代码格式

refactor: 重构前端组件和样式,添加AI助手功能

docs: 更新README文档,添加ruff代码检查说明

feat: 新增AI助手相关API和前端组件

chore: 更新.gitignore文件,添加ruff缓存配置

fix: 修复前端布局和设置相关的问题

perf: 优化代码结构和性能,移除冗余代码

test: 更新测试文件,移除编码声明

build: 更新依赖版本,调整requirements.txt
This commit is contained in:
zhangtao
2026-01-16 01:13:59 +08:00
parent 59c1ce1104
commit d50dd9dd1e
210 changed files with 5236 additions and 4037 deletions
@@ -1,2 +1 @@
# -*- coding: utf-8 -*-
@@ -1,56 +1,52 @@
# -*- coding: utf-8 -*-
from typing import Annotated
from fastapi import APIRouter, Body, Depends, Path
from fastapi.responses import JSONResponse
from app.api.v1.module_system.auth.schema import AuthSchema
from app.common.response import SuccessResponse
from app.core.dependencies import AuthPermission
from app.core.base_schema import BatchSetAvailable
from app.core.dependencies import AuthPermission
from app.core.logger import log
from app.core.router_class import OperationLogRoute
from ..auth.schema import AuthSchema
from .schema import MenuCreateSchema, MenuQueryParam, MenuUpdateSchema
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"]))
search: Annotated[MenuQueryParam, Depends()],
auth: Annotated[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"查询菜单树成功")
log.info("查询菜单树成功")
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:detail"]))
id: Annotated[int, Path(description="菜单ID")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:menu:detail"]))]
) -> JSONResponse:
"""
查询菜单详情。
参数:
- id (int): 菜单ID。
返回:
- JSONResponse: 包含菜单详情的 JSON 响应。
"""
@@ -62,14 +58,14 @@ async def get_obj_detail_controller(
@MenuRouter.post("/create", summary="创建菜单", description="创建菜单")
async def create_obj_controller(
data: MenuCreateSchema,
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:create"]))
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:menu:create"]))]
) -> JSONResponse:
"""
创建菜单。
参数:
- data (MenuCreateSchema): 菜单创建模型。
返回:
- JSONResponse: 包含创建菜单的 JSON 响应。
"""
@@ -81,16 +77,16 @@ async def create_obj_controller(
@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"]))
id: Annotated[int, Path(description="菜单ID")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:menu:update"]))]
) -> JSONResponse:
"""
修改菜单。
参数:
- id (int): 菜单ID。
- data (MenuUpdateSchema): 菜单更新模型。
返回:
- JSONResponse: 包含修改菜单的 JSON 响应。
"""
@@ -101,15 +97,15 @@ async def update_obj_controller(
@MenuRouter.delete("/delete", summary="删除菜单", description="删除菜单")
async def delete_obj_controller(
ids: list[int] = Body(..., description="ID列表"),
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:delete"]))
ids: Annotated[list[int], Body(description="ID列表")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:menu:delete"]))]
) -> JSONResponse:
"""
删除菜单。
参数:
- ids (list[int]): 菜单ID列表。
返回:
- JSONResponse: 包含删除菜单的 JSON 响应。
"""
@@ -121,17 +117,17 @@ async def delete_obj_controller(
@MenuRouter.patch("/available/setting", summary="批量修改菜单状态", description="批量修改菜单状态")
async def batch_set_available_obj_controller(
data: BatchSetAvailable,
auth: AuthSchema = Depends(AuthPermission(["module_system:menu:patch"]))
auth: Annotated[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="批量修改菜单状态成功")
return SuccessResponse(msg="批量修改菜单状态成功")
+11 -13
View File
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-
from typing import Sequence
from collections.abc import Sequence
from app.api.v1.module_system.auth.schema import AuthSchema
from app.core.base_crud import CRUDBase
from ..auth.schema import AuthSchema
from .model import MenuModel
from .schema import MenuCreateSchema, MenuUpdateSchema
@@ -20,11 +18,11 @@ class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
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。
"""
@@ -36,12 +34,12 @@ class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
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]: 菜单列表。
"""
@@ -50,12 +48,12 @@ class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
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]: 菜单树形列表。
"""
@@ -64,12 +62,12 @@ class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
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)
await self.set(ids=ids, status=status)
+16 -17
View File
@@ -1,8 +1,7 @@
# -*- 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 sqlalchemy import JSON, Boolean, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.base_model import ModelMixin
@@ -13,10 +12,10 @@ if TYPE_CHECKING:
class MenuModel(ModelMixin):
"""
菜单表 - 用于存储系统菜单信息
菜单类型说明:
- 1: 目录(一级菜单)
- 2: 菜单(二级菜单)
- 2: 菜单(二级菜单)
- 3: 按钮/权限(页面内按钮权限)
- 4: 外部链接
"""
@@ -39,30 +38,30 @@ class MenuModel(ModelMixin):
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,
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",
back_populates='children',
remote_side="MenuModel.id",
foreign_keys="MenuModel.parent_id",
uselist=False
)
children: Mapped[list["MenuModel"] | None] = relationship(
back_populates='parent',
back_populates='parent',
foreign_keys="MenuModel.parent_id",
order_by="MenuModel.order"
)
roles: Mapped[list["RoleModel"]] = relationship(
secondary="sys_role_menus",
back_populates="menus",
secondary="sys_role_menus",
back_populates="menus",
lazy="selectin"
)
@@ -1,11 +1,10 @@
# -*- 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, menu_request_validator
from fastapi import Query
from pydantic import BaseModel, ConfigDict, Field, model_validator
from app.core.base_schema import BaseSchema
from app.core.validator import DateTimeStr, menu_request_validator
class MenuCreateSchema(BaseModel):
@@ -79,7 +78,7 @@ class MenuQueryParam:
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:外链)"),
type: Literal[1, 2, 3, 4] | None = Query(None, description="菜单类型(1:目录 2:菜单 3:按钮 4:外链)"),
permission: str | None = Query(None, description="权限标识"),
description: str | None = Query(None, description="描述"),
status: str | None = Query(None, description="是否启用"),
@@ -1,23 +1,16 @@
# -*- coding: utf-8 -*-
from app.api.v1.module_system.auth.schema import AuthSchema
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
get_parent_id_map,
get_parent_recursion,
traversal_to_tree,
)
from ..auth.schema import AuthSchema
from .crud import MenuCRUD
from .schema import (
MenuCreateSchema,
MenuUpdateSchema,
MenuOutSchema,
MenuQueryParam
)
from .schema import MenuCreateSchema, MenuOutSchema, MenuQueryParam, MenuUpdateSchema
class MenuService:
@@ -29,11 +22,11 @@ class MenuService:
async def get_menu_detail_service(cls, auth: AuthSchema, id: int) -> dict:
"""
获取菜单详情。
参数:
- auth (AuthSchema): 认证对象。
- id (int): 菜单ID。
返回:
- dict: 菜单详情对象。
"""
@@ -44,19 +37,19 @@ class MenuService:
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]: 菜单树形列表对象。
"""
@@ -71,11 +64,11 @@ class MenuService:
async def create_menu_service(cls, auth: AuthSchema, data: MenuCreateSchema) -> dict:
"""
创建菜单。
参数:
- auth (AuthSchema): 认证对象。
- data (MenuCreateSchema): 创建参数对象。
返回:
- dict: 创建的菜单对象。
"""
@@ -88,15 +81,15 @@ class MenuService:
return new_menu_dict
@classmethod
async def update_menu_service(cls, auth: AuthSchema,id:int, data: MenuUpdateSchema) -> dict:
async def update_menu_service(cls, auth: AuthSchema, id: int, data: MenuUpdateSchema) -> dict:
"""
更新菜单。
参数:
- auth (AuthSchema): 认证对象。
- id (int): 菜单ID。
- data (MenuUpdateSchema): 更新参数对象。
返回:
- dict: 更新的菜单对象。
"""
@@ -106,51 +99,51 @@ class MenuService:
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='删除失败,删除对象不能为空')
# 获取所有菜单列表,用于构建树形关系
all_menus = await MenuCRUD(auth).get_list_crud()
# 构建子菜单ID映射
child_id_map = get_child_id_map(model_list=all_menus)
# 收集所有需要删除的菜单ID,包括直接指定的ID和它们的所有子菜单ID
delete_ids_set = set()
for id in ids:
# 递归获取该ID的所有子菜单ID
all_descendants = get_child_recursion(id=id, id_map=child_id_map)
delete_ids_set.update(all_descendants)
# 将集合转换为列表
delete_ids = list(delete_ids_set)
# 执行批量删除操作
await MenuCRUD(auth).delete(ids=delete_ids)
@@ -158,17 +151,17 @@ class MenuService:
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 == "0":
# 激活,则需要把所有父级菜单都激活
id_map = get_parent_id_map(model_list=menu_list)
@@ -182,4 +175,4 @@ class MenuService:
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)
await MenuCRUD(auth).set_available_crud(ids=total_ids, status=data.status)