# #!/usr/bin/env python # # -*- coding: utf-8 -*- # # from typing import Optional # # from fastapi import APIRouter, Path, Depends # from sqlalchemy import delete # from sqlalchemy.ext.asyncio import AsyncSession # # from admin.schemas.sysdata.sysmodule import (SysApiItemList, SysApiItemDetail, # CreateSysApi, UpdateSysApi, # DeleteSysApi, SysModuleList, # SysModuleDetail, CreateSysModule) # from core.security import enforcer # from crud.sysdata.role import crud_role_api # from crud.sysdata.sysmodule import crud_sys_api, crud_sys_module # from models.sysdata.role import RoleApi # from models.sysdata.sysmodule import SysModule # from models.user import SysUser # from utils.depends import get_async_db, get_current_user # # router = APIRouter() # # # @router.get("/modules", response_model=SysModuleList, summary="模块列表") # async def get_modules(page: Optional[int] = 1, # size: Optional[int] = 10, # id: Optional[int] = 0, # name: Optional[str] = "", # level: Optional[int] = 0, # db: AsyncSession = Depends(get_async_db)): # # filter = {} # if name: # 按name查询 # filter["name"] = name # elif id and (not level): # 按ID查询 # filter["id"] = id # elif (not id) and level: # 按 level 查询 # filter["level"] = level # elif id and level: # 按 pid 和 level 查询 # filter.update({"pid": id, "level": level}) # else: # filter = [SysModule.level.in_([0, 1])] # offset = (page - 1) * size # # total, items = await crud_sys_module.findall(db, filter, offset, size) # return {"total": total, "data": items} # # # @router.post("/modules", response_model=SysModuleDetail, summary="新建模块") # async def create_module(module: CreateSysModule, # current_user: SysUser = Depends(get_current_user), # db: AsyncSession = Depends(get_async_db)): # # 参数校验 # obj_in = module.dict(exclude_none=True) # if not obj_in: # return {"errcode": 400, "mess": "请求参数为空!"} # # # 判断是否存在同名模块 # db_obj = await crud_sys_module.find_one(db, { # "name": obj_in["name"], # "pid": obj_in["pid"], # "level": obj_in["level"] # }) # if db_obj: # return {"errcode": 400, "mess": "模块已存在!"} # # # 新增 # obj_in["creator"] = current_user.id # item = await crud_sys_module.create(db, obj_in) # return {"data": item} # # # @router.get("/apis", response_model=SysApiItemList, summary="接口列表") # async def get_apis(page: Optional[int] = 1, # size: Optional[int] = 10, # module: Optional[int] = 0, # name: Optional[str] = "", # path: Optional[str] = "", # db: AsyncSession = Depends(get_async_db)): # # filter = {} # if name: # filter["name"] = name # if path: # filter["path"] = path # if module: # filter["module_id"] = module # offset = (page - 1) * size # # total, items = await crud_sys_api.findall(db, filter, offset, size) # return {"total": total, "data": items} # # # @router.post("/apis", response_model=SysApiItemDetail, summary="新增接口") # async def create_api(api: CreateSysApi, # db: AsyncSession = Depends(get_async_db), # current_user: SysUser = Depends(get_current_user)): # # 参数校验 # obj_in = api.dict(exclude_none=True) # if not obj_in: # return {"errcode": 400, "mess": "请求参数为空!"} # # # 判断path是否重复 # db_api = await crud_sys_api.find_one(db, {"path": obj_in["path"]}) # if db_api: # return {"errcode": 400, "mess": "接口已存在!"} # # # 判断 module 是否存在 # db_module = await crud_sys_module.find_one(db, {"id": api.module_id}) # if db_module: # return {"errcode": 404, "mess": "模块不存在!"} # # # 新增 # obj_in["creator"] = current_user.id # item = await crud_sys_api.create(db, obj_in) # return {"data": item} # # # @router.put("/apis/{id}", response_model=SysApiItemDetail, summary="更新接口") # async def update_api(api: UpdateSysApi, # id: int = Path(..., description="接口ID"), # db: AsyncSession = Depends(get_async_db), # current_user: SysUser = Depends(get_current_user)): # # 参数校验 # obj_in = api.dict(exclude_none=True) # if not obj_in: # return {"errcode": 400, "mess": "请求参数为空!"} # # db_api = await crud_sys_api.find_one(db, {"id": id}) # if not db_api: # return {"errcode": 404, "mess": "接口不存在!"} # # # 判断 module 是否存在 # db_module = await crud_sys_module.find_one(db, {"id": api.module_id}) # if db_module: # return {"errcode": 404, "mess": "模块不存在!"} # # # 更新 # obj_in["editor"] = current_user.id # item = await crud_sys_api.update(db, db_api, obj_in) # return {"data": item} # # # @router.delete("/apis/{id}", response_model=SysApiItemDetail, summary="删除接口") # async def delete_api(id: int = Path(..., description="接口ID"), # db: AsyncSession = Depends(get_async_db), # current_user: SysUser = Depends(get_current_user)): # # db_obj = await crud_sys_api.find_one(db, {"id": id}) # if not db_obj: # return {"errcode": 404, "mess": "接口不存在!"} # # # 删除 # obj_in = DeleteSysApi(id=id, is_active=False, editor=current_user) # item = await crud_sys_api.update(db, db_obj, obj_in) # # # 1、从casbin_rule表中删除与本API相关的权限 # enforcer.remove_filtered_policy(1, db_obj.path) # # 2、从角色的权限表里删除本API # stmt = delete(RoleApi).where(RoleApi.api_id == db_obj.id) # await crud_role_api.execute(db, stmt) # # return {"data": item}