user.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # 系统用户管理
  4. from fastapi import APIRouter, Depends, Path, Query
  5. from sqlalchemy import text
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from sqlalchemy.sql import or_, and_
  8. from core.security import hashed_password
  9. from crud.user.user import crud_admin
  10. from models.user import SysUser
  11. from schemas.base import ReturnField, OrderByField
  12. from schemas.user import CreateAdminUser, AdminUserDetail, UpdateAdminUser, AdminUserList
  13. from utils.depends import get_async_db, get_current_user
  14. router = APIRouter()
  15. @router.post("/sysuser", summary="新增管理员", response_model=AdminUserDetail)
  16. async def create_sysuser(user: CreateAdminUser,
  17. db: AsyncSession = Depends(get_async_db),
  18. current_user: SysUser = Depends(get_current_user)):
  19. user_in = user.dict(exclude_none=True)
  20. if not user:
  21. return {"errcode": 400, "mess": "请求参数为空!"}
  22. # 判断是否存在同名
  23. filters = [
  24. or_(SysUser.username == user_in["username"],
  25. SysUser.phone == user_in["username"])
  26. ]
  27. db_user = await crud_admin.find_one(db, filters)
  28. if db_user:
  29. return {"errcode": 400, "mess": "账号或手机号已存在!"}
  30. user_in["creator_id"] = current_user.id
  31. user_in["creator_name"] = current_user.username
  32. user_in["password"] = hashed_password(user.password)
  33. db_user = await crud_admin.insert_one(db, user_in)
  34. return {"data": db_user}
  35. @router.put("/sysuser/{uid}", summary="修改管理员", response_model=AdminUserDetail)
  36. async def update_sysuser(user: UpdateAdminUser,
  37. uid: int = Path(..., description="管理员ID"),
  38. db: AsyncSession = Depends(get_async_db),
  39. current_user: SysUser = Depends(get_current_user)):
  40. user_in = user.dict(exclude_none=True)
  41. if not user:
  42. return {"errcode": 400, "mess": "请求参数为空!"}
  43. # 判断是否存在同名
  44. filters = [and_(SysUser.id != uid, SysUser.phone == user_in["phone"])]
  45. db_user = await crud_admin.find_one(db, filters)
  46. if db_user:
  47. return {"errcode": 400, "mess": "账号或手机号已存在!"}
  48. db_obj = await crud_admin.find_one(db, {"id": uid})
  49. user_in["editor_id"] = current_user.id
  50. user_in["editor_name"] = current_user.username
  51. db_user = await crud_admin.update(db, db_obj, user_in)
  52. return {"data": db_user}
  53. @router.delete("/sysuser/{uid}",
  54. summary="删除管理员",
  55. response_model=AdminUserDetail)
  56. async def delete_sysuser(uid: int = Path(..., description="管理员ID"),
  57. db: AsyncSession = Depends(get_async_db),
  58. current_user: SysUser = Depends(get_current_user)):
  59. existed = await crud_admin.count(db, {"id": uid})
  60. if not existed:
  61. return {"errcode": 404, "mess": "管理员不存在!"}
  62. else:
  63. await crud_admin.delete(db, obj_id=uid)
  64. return {"data": None}
  65. @router.get("/sysuser/{uid}", summary="获取管理员详情", response_model=AdminUserDetail)
  66. async def get_sysuser(uid: int = Path(..., description="管理员ID"),
  67. db: AsyncSession = Depends(get_async_db),
  68. current_user: SysUser = Depends(get_current_user)):
  69. existed = await crud_admin.count(db, {"id": uid})
  70. if not existed:
  71. return {"errcode": 404, "mess": "管理员不存在!"}
  72. else:
  73. db_obj = await crud_admin.find_one(db, {"id": uid})
  74. return {"data": db_obj}
  75. @router.get("/sysusers",
  76. summary="获取管理员列表",
  77. response_model=AdminUserList,
  78. response_model_exclude_none=True)
  79. async def get_sysuser_list(
  80. page: int = 1,
  81. size: int = 10,
  82. username: str = "",
  83. name: str = "",
  84. phone: str = "",
  85. order: OrderByField = Query("-created_at",
  86. description="排序字段,用逗号分隔,升降序以-判断,默认-created_at"),
  87. res: ReturnField = Query("",
  88. description="返回字段,取值范围:id,username,name,phone"),
  89. db: AsyncSession = Depends(get_async_db),
  90. current_user: SysUser = Depends(get_current_user)):
  91. filters = []
  92. if username:
  93. filters.append(text(f"username Like '%{username}%'"))
  94. if name:
  95. filters.append(text(f"name LIKE '%{name}%'"))
  96. if phone:
  97. filters.append(text(f"phone LIKE '%{phone}%'"))
  98. offset = (page - 1) * size
  99. if isinstance(order, str):
  100. order = [text(order)]
  101. total, items = await crud_admin.find_all(db,
  102. filters=filters,
  103. offset=offset,
  104. limit=size,
  105. order_by=order,
  106. return_fields=res)
  107. return {"total": total, "data": items}