File size: 2,291 Bytes
bf34e9d 9d66536 bf34e9d 9d66536 32ddb29 9d66536 bf34e9d 9d66536 32ddb29 9d66536 32ddb29 9d66536 32ddb29 9d66536 32ddb29 9d66536 32ddb29 9d66536 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | from fastapi import APIRouter, Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.dependencies import get_current_admin
from app.limiter import limiter
from app.schemas.admin.auth import LoginRequest, LoginResponse, UserInfoResponse
from app.schemas.common import ApiResponse
from app.services import auth_admin
from app.utils.auth import create_access_token, verify_password
router = APIRouter()
@router.post("/login", response_model=ApiResponse)
@limiter.limit("10/minute")
async def login(request: Request, req: LoginRequest, db: AsyncSession = Depends(get_db)):
admin = await auth_admin.authenticate_admin(db, req.username, req.password)
if not admin:
return ApiResponse.fail(40001, "用户名或密码错误")
token = create_access_token(str(admin.id), "admin")
return ApiResponse.ok(LoginResponse(accessToken=token, expires_in=1440).model_dump())
@router.post("/logout", response_model=ApiResponse)
async def logout(admin=Depends(get_current_admin)):
return ApiResponse.ok()
@router.get("/user-info", response_model=ApiResponse)
async def get_user_info(
admin=Depends(get_current_admin),
db: AsyncSession = Depends(get_db),
):
roles = await auth_admin.get_admin_roles(db, admin.id)
return ApiResponse.ok(
UserInfoResponse(
userId=str(admin.id),
username=admin.username,
realName=admin.nickname,
roles=roles,
avatar=admin.avatar,
).model_dump()
)
@router.get("/codes", response_model=ApiResponse)
async def get_access_codes(
admin=Depends(get_current_admin),
db: AsyncSession = Depends(get_db),
):
codes = await auth_admin.get_admin_permissions(db, admin.id)
return ApiResponse.ok(codes)
@router.get("/menus", response_model=ApiResponse)
async def get_menus(
admin=Depends(get_current_admin),
db: AsyncSession = Depends(get_db),
):
menus = await auth_admin.get_menu_tree(db, admin.id)
return ApiResponse.ok(menus)
@router.get("/permissions", response_model=ApiResponse)
async def get_permissions(
admin=Depends(get_current_admin),
db: AsyncSession = Depends(get_db),
):
tree = await auth_admin.get_permission_tree(db)
return ApiResponse.ok(tree) |