#!/usr/bin/env python # -*- coding: utf-8 -*- from typing import Optional from fastapi import APIRouter, Depends, Query from sqlalchemy import text from sqlalchemy.ext.asyncio import AsyncSession from crud.sysdata.role import crud_role, crud_permission from models.sysdata.role import Role, Permission from models.user import Admin from schemas.base import ReturnField, OrderByField from schemas.sysdata.role import RoleList, RoleDetail, NewRoleInfo, UpdateRoleInfo from utils.depends import get_async_db, get_current_user router = APIRouter() @router.post("/role", summary="新增角色", response_model=RoleDetail) async def create_role(info: NewRoleInfo, db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): info = info.dict(exclude_none=True) if not info: return {"errcode": 400, "mess": "请求参数为空!"} db_role = await crud_role.find_one(db, filters={"name": info["name"]}) if db_role: return {"errcode": 400, "mess": "角色名称重复!"} info["creator_id"] = current_user.id info["creator_name"] = current_user.username db_role = await crud_role.insert_one(db, info) return {"data": db_role} @router.put("/role/{rid}", summary="修改角色", response_model=RoleDetail) async def update_role(rid: int, info: UpdateRoleInfo, db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): info_dict = info.dict(exclude_none=True) if not info_dict: return {"errcode": 400, "mess": "请求参数为空!"} db_role = await crud_role.find_one(db, filters={"id": rid}) if not db_role: return {"errcode": 400, "mess": "角色不存在!"} info_dict["editor_id"] = current_user.id info_dict["editor_name"] = current_user.username db_role = await crud_role.update(db, db_role, info_dict) return {"data": db_role} @router.delete("/role/{rid}", summary="删除角色", response_model=RoleDetail) async def delete_role(rid: int, db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): # 不允许删除初始管理员角色,ID=1 if rid == 1: return {"errcode": 400, "mess": "该角色不允许删除!"} db_role = await crud_role.count(db, filters={"id": rid}) if not db_role: return {"errcode": 404, "mess": "角色不存在!"} await crud_role.delete(db, obj_id=rid) return {"data": None} @router.get("/role/{rid}", summary="角色详情", response_model=RoleDetail) async def get_role(rid: int, db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): db_role = await crud_role.find_one(db, filters={"id": rid}) if not db_role: return {"errcode": 404, "mess": "角色不存在!"} return {"data": db_role} @router.get("/roles", summary="角色列表", response_model=RoleList) async def get_role_list(page: Optional[int] = None, size: Optional[int] = None, platform: str = "", name: str = "", res: ReturnField = Query("", description="控制返回字段,字段逗号分隔"), order: OrderByField = Query("-created_at", description="排序字段,用逗号分隔,升降序以-判断,默认-created_at"), db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _q = [] if name: _q.append(Role.name.like(f"%{name}%")) if platform: _q.append(Role.platform == platform) if ((page is not None) and page >= 1) and ((size is not None) and size >= 1): offset = (page - 1) * size else: size = offset = None if isinstance(order, str): order = [text(order)] total, db_roles = await crud_role.find_all(db, filters=_q, offset=offset, limit=size, order_by=order, return_fields=res) return {"data": db_roles, "total": total} @router.get("/permissions", summary="权限类别") async def get_permission_all(name: str = "", res: ReturnField = Query("", description="控制返回字段,字段逗号分隔"), db: AsyncSession = Depends(get_async_db), current_user: Admin = Depends(get_current_user)): _q = [] if name: _q.append(Permission.name.like(f"%{name}%")) total, db_perms = await crud_permission.find_all(db, filters=_q, return_fields=res) db_perms = [{ "name": "首页", "value": "menu.*", "children": [{ "name": "首页", "value": "menu.menu", "children": [] }] }, { "name": "学校管理", "value": "menu2.*", "children": [{ "name": "学校管理", "value": "menu2.menu2-1", "children": [] }, { "name": "班级管理", "value": "menu2.menu2-2", "children": [] }] }, { "name": "师生信息", "value": "menu3.*", "children": [{ "name": "教师管理", "value": "menu3.menu3-1", "children": [] }, { "name": "学生管理", "value": "menu3.menu3-2", "children": [] }] }, { "name": "分类管理", "value": "menu8.*", "children": [{ "name": "作业分类", "value": "menu8.menu8-1", "children": [] }, { "name": "试卷分类", "value": "menu8.menu8-2", "children": [] }] }, { "name": "资源管理", "value": "menu4.*", "children": [{ "name": "作业资源", "value": "menu4.menu4-1", "children": [] }, { "name": "考试资源", "value": "menu4.menu4-2", "children": [] }] }, { "name": "阅卷管理", "value": "menu5.*", "children": [{ "name": "作业管理", "value": "menu5.menu5-1", "children": [] }, { "name": "作业批阅", "value": "menu5.menu5-2", "children": [] }, { "name": "考试批阅", "value": "menu5.menu5-3", "children": [] }, { "name": "考试管理", "value": "menu5.menu5-4", "children": [] }] }, { "name": "错题管理", "value": "menu6.*", "children": [{ "name": "班级错题", "value": "menu6.menu6-1", "children": [] }, { "name": "学生错题", "value": "menu6.menu6-2", "children": [] }, { "name": "错题打印", "value": "menu6.menu6-3", "children": [] }] }, { "name": "系统管理", "value": "menu7.*", "children": [{ "name": "账号管理", "value": "menu7.menu7-1", "children": [] }, { "name": "角色管理", "value": "menu7.menu7-2", "children": [] }] }] return {"data": db_perms, "total": total}