upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,369 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import urllib.parse
|
||||
from fastapi import APIRouter, Depends, Body, Path, UploadFile, Request
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.common.response import StreamResponse, SuccessResponse
|
||||
from app.common.request import PaginationService
|
||||
from app.core.router_class import OperationLogRoute
|
||||
from app.utils.common_util import bytes2file_response
|
||||
from app.core.dependencies import db_getter, get_current_user, AuthPermission
|
||||
from app.core.base_params import PaginationQueryParam
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.logger import log
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .service import UserService
|
||||
from .schema import (
|
||||
CurrentUserUpdateSchema,
|
||||
ResetPasswordSchema,
|
||||
UserCreateSchema,
|
||||
UserForgetPasswordSchema,
|
||||
UserRegisterSchema,
|
||||
UserUpdateSchema,
|
||||
UserChangePasswordSchema,
|
||||
UserQueryParam
|
||||
)
|
||||
|
||||
|
||||
UserRouter = APIRouter(route_class=OperationLogRoute, prefix="/user", tags=["用户管理"])
|
||||
|
||||
|
||||
@UserRouter.get("/current/info", summary="查询当前用户信息", description="查询当前用户信息")
|
||||
async def get_current_user_info_controller(
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询当前用户信息
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 当前用户信息JSON响应
|
||||
"""
|
||||
result_dict = await UserService.get_current_user_info_service(auth=auth)
|
||||
log.info(f"获取当前用户信息成功")
|
||||
return SuccessResponse(data=result_dict, msg='获取当前用户信息成功')
|
||||
|
||||
|
||||
@UserRouter.post("/current/avatar/upload", summary="上传当前用户头像", dependencies=[Depends(get_current_user)])
|
||||
async def user_avatar_upload_controller(
|
||||
file: UploadFile,
|
||||
request: Request
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
上传当前用户头像
|
||||
|
||||
参数:
|
||||
- file (UploadFile): 上传的文件
|
||||
- request (Request): 请求对象
|
||||
|
||||
返回:
|
||||
- JSONResponse: 上传头像JSON响应
|
||||
"""
|
||||
result_str = await UserService.upload_avatar_service(base_url=str(request.base_url), file=file)
|
||||
log.info(f"上传头像成功: {result_str}")
|
||||
return SuccessResponse(data=result_str, msg='上传头像成功')
|
||||
|
||||
|
||||
@UserRouter.put("/current/info/update", summary="更新当前用户基本信息", description="更新当前用户基本信息")
|
||||
async def update_current_user_info_controller(
|
||||
data: CurrentUserUpdateSchema,
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
更新当前用户基本信息
|
||||
|
||||
参数:
|
||||
- data (CurrentUserUpdateSchema): 当前用户更新模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 更新当前用户基本信息JSON响应
|
||||
"""
|
||||
result_dict = await UserService.update_current_user_info_service(data=data, auth=auth)
|
||||
log.info(f"更新当前用户基本信息成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg='更新当前用户基本信息成功')
|
||||
|
||||
|
||||
@UserRouter.put("/current/password/change", summary="修改当前用户密码", description="修改当前用户密码")
|
||||
async def change_current_user_password_controller(
|
||||
data: UserChangePasswordSchema,
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改当前用户密码
|
||||
|
||||
参数:
|
||||
- data (UserChangePasswordSchema): 用户密码修改模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 修改密码JSON响应
|
||||
"""
|
||||
result_dict = await UserService.change_user_password_service(data=data, auth=auth)
|
||||
log.info(f"修改密码成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg='修改密码成功, 请重新登录')
|
||||
|
||||
@UserRouter.put("/reset/password", summary="重置密码", description="重置密码")
|
||||
async def reset_password_controller(
|
||||
data: ResetPasswordSchema,
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
重置密码
|
||||
|
||||
参数:
|
||||
- data (ResetPasswordSchema): 重置密码模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 重置密码JSON响应
|
||||
"""
|
||||
result_dict = await UserService.reset_user_password_service(data=data, auth=auth)
|
||||
log.info(f"重置密码成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg='重置密码成功')
|
||||
|
||||
@UserRouter.post('/register', summary="注册用户", description="注册用户")
|
||||
async def register_user_controller(
|
||||
data: UserRegisterSchema,
|
||||
db: AsyncSession = Depends(db_getter),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
注册用户
|
||||
|
||||
参数:
|
||||
- data (UserRegisterSchema): 用户注册模型
|
||||
- db (AsyncSession): 异步数据库会话
|
||||
|
||||
返回:
|
||||
- JSONResponse: 注册用户JSON响应
|
||||
"""
|
||||
auth = AuthSchema(db=db)
|
||||
user_register_result = await UserService.register_user_service(data=data, auth=auth)
|
||||
log.info(f"{data.username} 注册用户成功: {user_register_result}")
|
||||
return SuccessResponse(data=user_register_result, msg='注册用户成功')
|
||||
|
||||
|
||||
@UserRouter.post('/forget/password', summary="忘记密码", description="忘记密码")
|
||||
async def forget_password_controller(
|
||||
data: UserForgetPasswordSchema,
|
||||
db: AsyncSession = Depends(db_getter),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
忘记密码
|
||||
|
||||
参数:
|
||||
- data (UserForgetPasswordSchema): 用户忘记密码模型
|
||||
- db (AsyncSession): 异步数据库会话
|
||||
|
||||
返回:
|
||||
- JSONResponse: 忘记密码JSON响应
|
||||
"""
|
||||
auth = AuthSchema(db=db)
|
||||
user_forget_password_result = await UserService.forget_password_service(data=data, auth=auth)
|
||||
log.info(f"{data.username} 重置密码成功: {user_forget_password_result}")
|
||||
return SuccessResponse(data=user_forget_password_result, msg='重置密码成功')
|
||||
|
||||
|
||||
@UserRouter.get("/list", summary="查询用户", description="查询用户")
|
||||
async def get_obj_list_controller(
|
||||
page: PaginationQueryParam = Depends(),
|
||||
search: UserQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:query"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询用户
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型
|
||||
- search (UserQueryParam): 查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 分页查询结果JSON响应
|
||||
"""
|
||||
result_dict_list = await UserService.get_user_list_service(search=search, auth=auth, order_by=page.order_by)
|
||||
result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
|
||||
log.info(f"查询用户成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询用户成功")
|
||||
|
||||
|
||||
@UserRouter.get("/detail/{id}", summary="查询用户详情", description="查询用户详情")
|
||||
async def get_obj_detail_controller(
|
||||
id: int = Path(..., description="用户ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:query"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询用户详情
|
||||
|
||||
参数:
|
||||
- id (int): 用户ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 用户详情JSON响应
|
||||
"""
|
||||
result_dict = await UserService.get_detail_by_id_service(id=id, auth=auth)
|
||||
log.info(f"获取用户详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg='获取用户详情成功')
|
||||
|
||||
|
||||
@UserRouter.post("/create", summary="创建用户", description="创建用户")
|
||||
async def create_obj_controller(
|
||||
data: UserCreateSchema,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:create"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建用户
|
||||
|
||||
**注意**:
|
||||
- 创建用户时, 默认密码为: <PASSWORD>
|
||||
- 创建用户时, 默认用户状态为: 启用
|
||||
|
||||
参数:
|
||||
- data (UserCreateSchema): 用户创建模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 创建用户JSON响应
|
||||
"""
|
||||
result_dict = await UserService.create_user_service(data=data, auth=auth)
|
||||
log.info(f"创建用户成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建用户成功")
|
||||
|
||||
|
||||
@UserRouter.put("/update/{id}", summary="修改用户", description="修改用户")
|
||||
async def update_obj_controller(
|
||||
data: UserUpdateSchema,
|
||||
id: int = Path(..., description="用户ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:update"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改用户
|
||||
|
||||
参数:
|
||||
- data (UserUpdateSchema): 用户修改模型
|
||||
- id (int): 用户ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 修改用户JSON响应
|
||||
"""
|
||||
result_dict = await UserService.update_user_service(id=id, data=data, auth=auth)
|
||||
log.info(f"修改用户成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改用户成功")
|
||||
|
||||
|
||||
@UserRouter.delete("/delete", summary="删除用户", description="删除用户")
|
||||
async def delete_obj_controller(
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:delete"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除用户
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 用户ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 删除用户JSON响应
|
||||
"""
|
||||
await UserService.delete_user_service(ids=ids, auth=auth)
|
||||
log.info(f"删除用户成功: {ids}")
|
||||
return SuccessResponse(msg="删除用户成功")
|
||||
|
||||
|
||||
@UserRouter.patch("/available/setting", summary="批量修改用户状态", description="批量修改用户状态")
|
||||
async def batch_set_available_obj_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:patch"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改用户状态
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改用户状态模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 批量修改用户状态JSON响应
|
||||
"""
|
||||
await UserService.set_user_available_service(data=data, auth=auth)
|
||||
log.info(f"批量修改用户状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改用户状态成功")
|
||||
|
||||
|
||||
@UserRouter.post('/import/template', summary="获取用户导入模板", description="获取用户导入模板", dependencies=[Depends(AuthPermission(["module_system:user:import"]))])
|
||||
async def export_obj_template_controller()-> StreamingResponse:
|
||||
"""
|
||||
获取用户导入模板
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 用户导入模板流响应
|
||||
"""
|
||||
user_import_template_result = await UserService.get_import_template_user_service()
|
||||
log.info('获取用户导入模板成功')
|
||||
|
||||
return StreamResponse(
|
||||
data=bytes2file_response(user_import_template_result),
|
||||
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
headers = {
|
||||
'Content-Disposition': f'attachment; filename={urllib.parse.quote("用户导入模板.xlsx")}',
|
||||
'Access-Control-Expose-Headers': 'Content-Disposition'
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@UserRouter.post('/export', summary="导出用户", description="导出用户")
|
||||
async def export_obj_list_controller(
|
||||
page: PaginationQueryParam = Depends(),
|
||||
search: UserQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:export"])),
|
||||
) -> StreamingResponse:
|
||||
"""
|
||||
导出用户
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型
|
||||
- search (UserQueryParam): 查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 用户导出模板流响应
|
||||
"""
|
||||
user_list = await UserService.get_user_list_service(auth=auth, search=search, order_by=page.order_by)
|
||||
user_export_result = await UserService.export_user_list_service(user_list)
|
||||
log.info('导出用户成功')
|
||||
|
||||
return StreamResponse(
|
||||
data=bytes2file_response(user_export_result),
|
||||
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
headers = {
|
||||
'Content-Disposition': 'attachment; filename=user.xlsx'
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@UserRouter.post('/import/data', summary="导入用户", description="导入用户")
|
||||
async def import_obj_list_controller(
|
||||
file: UploadFile,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:user:import"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
导入用户
|
||||
|
||||
参数:
|
||||
- file (UploadFile): 用户导入文件
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 导入用户JSON响应
|
||||
"""
|
||||
batch_import_result = await UserService.batch_import_user_service(file=file, auth=auth, update_support=True)
|
||||
log.info(f"导入用户成功: {batch_import_result}")
|
||||
return SuccessResponse(data=batch_import_result, msg="导入用户成功")
|
||||
@@ -0,0 +1,233 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Sequence, Any
|
||||
from datetime import datetime
|
||||
|
||||
from app.core.base_crud import CRUDBase
|
||||
from app.api.v1.module_system.auth.schema import AuthSchema
|
||||
from .model import UserModel
|
||||
from .schema import UserCreateSchema, UserForgetPasswordSchema, UserUpdateSchema
|
||||
from ..role.crud import RoleCRUD
|
||||
from ..position.crud import PositionCRUD
|
||||
|
||||
|
||||
class UserCRUD(CRUDBase[UserModel, UserCreateSchema, UserUpdateSchema]):
|
||||
"""用户模块数据层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""
|
||||
初始化用户CRUD
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
"""
|
||||
self.auth = auth
|
||||
super().__init__(model=UserModel, auth=auth)
|
||||
|
||||
async def get_by_id_crud(self, id: int, preload: list[str | Any] | None = None) -> UserModel | None:
|
||||
"""
|
||||
根据id获取用户信息
|
||||
|
||||
参数:
|
||||
- id (int): 用户ID
|
||||
- preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- UserModel | None: 用户信息,如果不存在则为None
|
||||
"""
|
||||
return await self.get(
|
||||
preload=preload,
|
||||
id=id,
|
||||
)
|
||||
|
||||
async def get_by_username_crud(self, username: str, preload: list[str | Any] | None = None) -> UserModel | None:
|
||||
"""
|
||||
根据用户名获取用户信息
|
||||
|
||||
参数:
|
||||
- username (str): 用户名
|
||||
- preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- UserModel | None: 用户信息,如果不存在则为None
|
||||
"""
|
||||
return await self.get(
|
||||
preload=preload,
|
||||
username=username,
|
||||
)
|
||||
|
||||
async def get_by_mobile_crud(self, mobile: str, preload: list[str | Any] | None = None) -> UserModel | None:
|
||||
"""
|
||||
根据手机号获取用户信息
|
||||
|
||||
参数:
|
||||
- mobile (str): 手机号
|
||||
- preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- UserModel | None: 用户信息,如果不存在则为None
|
||||
"""
|
||||
return await self.get(
|
||||
preload=preload,
|
||||
mobile=mobile,
|
||||
)
|
||||
|
||||
async def get_list_crud(self, search: dict | None = None, order_by: list[dict[str, str]] | None = None, preload: list[str | Any] | None = None) -> Sequence[UserModel]:
|
||||
"""
|
||||
获取用户列表
|
||||
|
||||
参数:
|
||||
- search (dict | None): 查询参数对象。
|
||||
- order_by (list[dict[str, str]] | None): 排序参数列表。
|
||||
- preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[UserModel]: 用户列表
|
||||
"""
|
||||
return await self.list(
|
||||
search=search,
|
||||
order_by=order_by,
|
||||
preload=preload,
|
||||
)
|
||||
|
||||
async def update_last_login_crud(self, id: int) -> UserModel | None:
|
||||
"""
|
||||
更新用户最后登录时间
|
||||
|
||||
参数:
|
||||
- id (int): 用户ID
|
||||
|
||||
返回:
|
||||
- UserModel | None: 更新后的用户信息
|
||||
"""
|
||||
return await self.update(id=id, data={"last_login": datetime.now()})
|
||||
|
||||
async def set_available_crud(self, ids: list[int], status: str) -> None:
|
||||
"""
|
||||
批量设置用户可用状态
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 用户ID列表
|
||||
- status (bool): 可用状态
|
||||
|
||||
返回:
|
||||
- None:
|
||||
"""
|
||||
await self.set(ids=ids, status=status)
|
||||
|
||||
async def set_user_roles_crud(self, user_ids: list[int], role_ids: list[int]) -> None:
|
||||
"""
|
||||
批量设置用户角色
|
||||
|
||||
参数:
|
||||
- user_ids (list[int]): 用户ID列表
|
||||
- role_ids (list[int]): 角色ID列表
|
||||
|
||||
返回:
|
||||
- None:
|
||||
"""
|
||||
user_objs = await self.list(search={"id": ("in", user_ids)})
|
||||
if role_ids:
|
||||
role_objs = await RoleCRUD(self.auth).get_list_crud(search={"id": ("in", role_ids)})
|
||||
else:
|
||||
role_objs = []
|
||||
|
||||
for obj in user_objs:
|
||||
relationship = obj.roles
|
||||
relationship.clear()
|
||||
relationship.extend(role_objs)
|
||||
await self.auth.db.flush()
|
||||
|
||||
async def set_user_positions_crud(self, user_ids: list[int], position_ids: list[int]) -> None:
|
||||
"""
|
||||
批量设置用户岗位
|
||||
|
||||
参数:
|
||||
- user_ids (list[int]): 用户ID列表
|
||||
- position_ids (list[int]): 岗位ID列表
|
||||
|
||||
返回:
|
||||
- None:
|
||||
"""
|
||||
user_objs = await self.list(search={"id": ("in", user_ids)})
|
||||
if position_ids:
|
||||
position_objs = await PositionCRUD(self.auth).get_list_crud(search={"id": ("in", position_ids)})
|
||||
else:
|
||||
position_objs = []
|
||||
|
||||
for obj in user_objs:
|
||||
relationship = obj.positions
|
||||
relationship.clear()
|
||||
relationship.extend(position_objs)
|
||||
await self.auth.db.flush()
|
||||
|
||||
async def change_password_crud(self, id: int, password_hash: str) -> UserModel:
|
||||
"""
|
||||
修改用户密码
|
||||
|
||||
参数:
|
||||
- id (int): 用户ID
|
||||
- password_hash (str): 密码哈希值
|
||||
|
||||
返回:
|
||||
- UserModel: 更新后的用户信息
|
||||
"""
|
||||
return await self.update(id=id, data=UserUpdateSchema(password=password_hash))
|
||||
|
||||
async def forget_password_crud(self, id: int, password_hash: str) -> UserModel:
|
||||
"""
|
||||
重置密码
|
||||
|
||||
参数:
|
||||
- id (int): 用户ID
|
||||
- password_hash (str): 密码哈希值
|
||||
|
||||
返回:
|
||||
- UserModel: 更新后的用户信息
|
||||
"""
|
||||
return await self.update(id=id, data=UserUpdateSchema(password=password_hash))
|
||||
|
||||
async def register_user_crud(self, data: UserForgetPasswordSchema) -> UserModel:
|
||||
"""
|
||||
用户注册
|
||||
|
||||
参数:
|
||||
- data (UserForgetPasswordSchema): 用户注册信息
|
||||
|
||||
返回:
|
||||
- UserModel: 注册成功的用户信息
|
||||
"""
|
||||
return await self.create(data=UserCreateSchema(**data.model_dump()))
|
||||
|
||||
async def get_by_wx_openid_crud(self, wx_openid: str, preload: list[str | Any] | None = None) -> UserModel | None:
|
||||
"""
|
||||
根据微信小程序OpenID获取用户信息
|
||||
|
||||
参数:
|
||||
- wx_openid (str): 微信小程序OpenID
|
||||
- preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- UserModel | None: 用户信息,如果不存在则为None
|
||||
"""
|
||||
return await self.get(
|
||||
preload=preload,
|
||||
wx_openid=wx_openid,
|
||||
)
|
||||
|
||||
async def bind_wx_openid_crud(self, id: int, wx_openid: str, wx_unionid: str | None = None) -> UserModel | None:
|
||||
"""
|
||||
绑定微信小程序OpenID
|
||||
|
||||
参数:
|
||||
- id (int): 用户ID
|
||||
- wx_openid (str): 微信小程序OpenID
|
||||
- wx_unionid (str | None): 微信UnionID
|
||||
|
||||
返回:
|
||||
- UserModel | None: 更新后的用户信息
|
||||
"""
|
||||
data = {"wx_openid": wx_openid}
|
||||
if wx_unionid:
|
||||
data["wx_unionid"] = wx_unionid
|
||||
return await self.update(id=id, data=data)
|
||||
@@ -0,0 +1,131 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Boolean, String, Integer, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import relationship, Mapped, mapped_column
|
||||
|
||||
from app.core.base_model import MappedBase, ModelMixin, UserMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.api.v1.module_system.dept.model import DeptModel
|
||||
from app.api.v1.module_system.position.model import PositionModel
|
||||
from app.api.v1.module_system.role.model import RoleModel
|
||||
|
||||
|
||||
class UserRolesModel(MappedBase):
|
||||
"""
|
||||
用户角色关联表
|
||||
|
||||
定义用户与角色的多对多关系
|
||||
"""
|
||||
__tablename__: str = "sys_user_roles"
|
||||
__table_args__: dict[str, str] = ({'comment': '用户角色关联表'})
|
||||
|
||||
user_id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("sys_user.id", ondelete="CASCADE", onupdate="CASCADE"),
|
||||
primary_key=True,
|
||||
comment="用户ID"
|
||||
)
|
||||
role_id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("sys_role.id", ondelete="CASCADE", onupdate="CASCADE"),
|
||||
primary_key=True,
|
||||
comment="角色ID"
|
||||
)
|
||||
|
||||
|
||||
class UserPositionsModel(MappedBase):
|
||||
"""
|
||||
用户岗位关联表
|
||||
|
||||
定义用户与岗位的多对多关系
|
||||
"""
|
||||
__tablename__: str = "sys_user_positions"
|
||||
__table_args__: dict[str, str] = ({'comment': '用户岗位关联表'})
|
||||
|
||||
user_id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("sys_user.id", ondelete="CASCADE", onupdate="CASCADE"),
|
||||
primary_key=True,
|
||||
comment="用户ID"
|
||||
)
|
||||
position_id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("sys_position.id", ondelete="CASCADE", onupdate="CASCADE"),
|
||||
primary_key=True,
|
||||
comment="岗位ID"
|
||||
)
|
||||
|
||||
|
||||
class UserModel(ModelMixin, UserMixin):
|
||||
"""
|
||||
用户模型
|
||||
"""
|
||||
__tablename__: str = "sys_user"
|
||||
__table_args__: dict[str, str] = ({'comment': '用户表'})
|
||||
__loader_options__: list[str] = ["dept", "roles", "positions", "created_by", "updated_by"]
|
||||
|
||||
username: Mapped[str] = mapped_column(String(32), nullable=False, unique=True, comment="用户名/登录账号")
|
||||
password: Mapped[str] = mapped_column(String(255), nullable=False, comment="密码哈希")
|
||||
name: Mapped[str] = mapped_column(String(32), nullable=False, comment="昵称")
|
||||
mobile: Mapped[str | None] = mapped_column(String(11), nullable=True, unique=True, comment="手机号")
|
||||
email: Mapped[str | None] = mapped_column(String(64), nullable=True, unique=True, comment="邮箱")
|
||||
gender: Mapped[str | None] = mapped_column(String(1), default='2', nullable=True, comment="性别(0:男 1:女 2:未知)")
|
||||
avatar: Mapped[str | None] = mapped_column(String(255), nullable=True, comment="头像URL地址")
|
||||
is_superuser: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, comment="是否超管")
|
||||
last_login: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, comment="最后登录时间")
|
||||
|
||||
gitee_login: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="Gitee登录")
|
||||
github_login: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="Github登录")
|
||||
wx_login: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="微信登录")
|
||||
qq_login: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="QQ登录")
|
||||
wx_openid: Mapped[str | None] = mapped_column(String(64), nullable=True, unique=True, comment="微信小程序OpenID")
|
||||
wx_unionid: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True, comment="微信UnionID")
|
||||
|
||||
inviter_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True, comment="邀请人ID")
|
||||
|
||||
partner_role: Mapped[str | None] = mapped_column(String(255), nullable=True, comment="身份")
|
||||
|
||||
dept_id: Mapped[int | None] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey('sys_dept.id', ondelete="SET NULL", onupdate="CASCADE"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
comment="部门ID"
|
||||
)
|
||||
dept: Mapped["DeptModel | None"] = relationship(
|
||||
back_populates="users",
|
||||
foreign_keys=[dept_id],
|
||||
lazy="selectin"
|
||||
)
|
||||
roles: Mapped[list["RoleModel"]] = relationship(
|
||||
secondary="sys_user_roles",
|
||||
back_populates="users",
|
||||
lazy="selectin"
|
||||
)
|
||||
positions: Mapped[list["PositionModel"]] = relationship(
|
||||
secondary="sys_user_positions",
|
||||
back_populates="users",
|
||||
lazy="selectin"
|
||||
)
|
||||
|
||||
# 覆盖 UserMixin 的关系定义,显式指定 foreign_keys 避免自引用混淆
|
||||
created_by: Mapped["UserModel | None"] = relationship(
|
||||
"UserModel",
|
||||
foreign_keys="UserModel.created_id",
|
||||
remote_side="UserModel.id",
|
||||
lazy="selectin",
|
||||
uselist=False,
|
||||
viewonly=True # 防止级联操作
|
||||
)
|
||||
|
||||
updated_by: Mapped["UserModel | None"] = relationship(
|
||||
"UserModel",
|
||||
foreign_keys="UserModel.updated_id",
|
||||
remote_side="UserModel.id",
|
||||
lazy="selectin",
|
||||
uselist=False,
|
||||
viewonly=True # 防止级联操作
|
||||
)
|
||||
@@ -0,0 +1,157 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from fastapi import Query
|
||||
from pydantic import BaseModel, ConfigDict, Field, EmailStr, field_validator
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from app.core.validator import DateTimeStr, mobile_validator
|
||||
from app.core.base_schema import BaseSchema, CommonSchema, UserBySchema
|
||||
from app.core.validator import DateTimeStr
|
||||
from app.api.v1.module_system.menu.schema import MenuOutSchema
|
||||
from app.api.v1.module_system.role.schema import RoleOutSchema
|
||||
|
||||
|
||||
class CurrentUserUpdateSchema(BaseModel):
|
||||
"""基础用户信息"""
|
||||
name: str | None = Field(default=None, max_length=32, description="名称")
|
||||
mobile: str | None = Field(default=None, description="手机号")
|
||||
email: EmailStr | None = Field(default=None, description="邮箱")
|
||||
gender: str | None = Field(default=None, description="性别")
|
||||
avatar: str | None = Field(default=None, description="头像")
|
||||
|
||||
@field_validator("mobile")
|
||||
@classmethod
|
||||
def validate_mobile(cls, value: str | None):
|
||||
return mobile_validator(value)
|
||||
|
||||
@field_validator("avatar")
|
||||
@classmethod
|
||||
def validate_avatar(cls, value: str | None):
|
||||
if not value:
|
||||
return value
|
||||
parsed = urlparse(value)
|
||||
if parsed.scheme in ("http", "https") and parsed.netloc:
|
||||
return value
|
||||
raise ValueError("头像地址需为有效的HTTP/HTTPS URL")
|
||||
|
||||
|
||||
class UserRegisterSchema(BaseModel):
|
||||
"""注册"""
|
||||
name: str | None = Field(default=None, max_length=32, description="名称")
|
||||
mobile: str | None = Field(default=None, description="手机号")
|
||||
username: str = Field(..., max_length=32, description="账号")
|
||||
password: str = Field(..., max_length=128, description="密码哈希值")
|
||||
role_ids: list[int] | None = Field(default=[1], description='角色ID')
|
||||
created_id: int | None = Field(default=1, description='创建人ID')
|
||||
description: str | None = Field(default=None, max_length=255, description="备注")
|
||||
|
||||
@field_validator("mobile")
|
||||
@classmethod
|
||||
def validate_mobile(cls, value: str | None):
|
||||
return mobile_validator(value)
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def validate_username(cls, value: str):
|
||||
v = value.strip()
|
||||
if not v:
|
||||
raise ValueError("账号不能为空")
|
||||
# 字母开头,允许字母数字_.-
|
||||
import re
|
||||
if not re.match(r"^[A-Za-z][A-Za-z0-9_.-]{2,31}$", v):
|
||||
raise ValueError("账号需字母开头,3-32位,仅含字母/数字/_ . -")
|
||||
return v
|
||||
|
||||
|
||||
class UserForgetPasswordSchema(BaseModel):
|
||||
"""忘记密码"""
|
||||
username: str = Field(..., max_length=32, description="用户名")
|
||||
new_password: str = Field(..., max_length=128, description="新密码")
|
||||
mobile: str | None = Field(default=None, description="手机号")
|
||||
|
||||
@field_validator("mobile")
|
||||
@classmethod
|
||||
def validate_mobile(cls, value: str | None):
|
||||
return mobile_validator(value)
|
||||
|
||||
|
||||
class UserChangePasswordSchema(BaseModel):
|
||||
"""修改密码"""
|
||||
old_password: str = Field(..., max_length=128, description="旧密码")
|
||||
new_password: str = Field(..., max_length=128, description="新密码")
|
||||
|
||||
|
||||
class ResetPasswordSchema(BaseModel):
|
||||
"""重置密码"""
|
||||
id: int = Field(..., description="主键ID")
|
||||
password: str = Field(..., min_length=6, max_length=128, description="新密码")
|
||||
|
||||
|
||||
class UserCreateSchema(CurrentUserUpdateSchema):
|
||||
"""新增"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
username: str | None = Field(default=None, max_length=32, description="用户名")
|
||||
password: str | None = Field(default=None, max_length=128, description="密码哈希值")
|
||||
status: str = Field(default="0", description="是否可用")
|
||||
description: str | None = Field(default=None, max_length=255, description="备注")
|
||||
is_superuser: bool | None = Field(default=False, description="是否超管")
|
||||
dept_id: int | None = Field(default=None, description='部门ID')
|
||||
role_ids: list[int] | None = Field(default=[], description='角色ID')
|
||||
position_ids: list[int] | None = Field(default=[], description='岗位ID')
|
||||
|
||||
class UserUpdateSchema(UserCreateSchema):
|
||||
"""更新"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
last_login: DateTimeStr | None = Field(default=None, description="最后登录时间")
|
||||
|
||||
|
||||
class UserOutSchema(UserUpdateSchema, BaseSchema, UserBySchema):
|
||||
"""响应"""
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True, from_attributes=True)
|
||||
gitee_login: str | None = Field(default=None, max_length=32, description="Gitee登录")
|
||||
github_login: str | None = Field(default=None, max_length=32, description="Github登录")
|
||||
wx_login: str | None = Field(default=None, max_length=32, description="微信登录")
|
||||
qq_login: str | None = Field(default=None, max_length=32, description="QQ登录")
|
||||
dept_name: str | None = Field(default=None, description='部门名称')
|
||||
dept: CommonSchema | None = Field(default=None, description='部门')
|
||||
positions: list[CommonSchema] | None = Field(default=[], description='岗位')
|
||||
roles: list[RoleOutSchema] | None = Field(default=[], description='角色')
|
||||
menus: list[MenuOutSchema] | None = Field(default=[], description='菜单')
|
||||
|
||||
class UserQueryParam:
|
||||
"""用户管理查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
username: str | None = Query(None, description="用户名"),
|
||||
name: str | None = Query(None, description="名称"),
|
||||
mobile: str | None = Query(None, description="手机号", pattern=r'^1[3-9]\d{9}$'),
|
||||
email: str | None = Query(None, description="邮箱", pattern=r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'),
|
||||
dept_id: int | None = Query(None, description="部门ID"),
|
||||
status: str | None = Query(None, description="是否可用"),
|
||||
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
created_id: int | None = Query(None, description="创建人"),
|
||||
updated_id: int | None = Query(None, description="更新人"),
|
||||
) -> None:
|
||||
|
||||
# 模糊查询字段
|
||||
self.username = ("like", username)
|
||||
self.name = ("like", name)
|
||||
self.mobile = ("like", mobile)
|
||||
self.email = ("like", email)
|
||||
|
||||
# 精确查询字段
|
||||
self.dept_id = dept_id
|
||||
self.created_id = created_id
|
||||
self.updated_id = updated_id
|
||||
self.status = status
|
||||
|
||||
# 时间范围查询
|
||||
if created_time and len(created_time) == 2:
|
||||
self.created_time = ("between", (created_time[0], created_time[1]))
|
||||
if updated_time and len(updated_time) == 2:
|
||||
self.updated_time = ("between", (updated_time[0], updated_time[1]))
|
||||
|
||||
@@ -0,0 +1,631 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import io
|
||||
from typing import Any
|
||||
from fastapi import UploadFile
|
||||
import pandas as pd
|
||||
|
||||
from app.core.exceptions import CustomException
|
||||
from app.utils.hash_bcrpy_util import PwdUtil
|
||||
from app.core.base_schema import BatchSetAvailable, UploadResponseSchema
|
||||
from app.core.logger import log
|
||||
from app.utils.common_util import traversal_to_tree
|
||||
from app.utils.excel_util import ExcelUtil
|
||||
from app.utils.upload_util import UploadUtil
|
||||
|
||||
from ..position.crud import PositionCRUD
|
||||
from ..role.crud import RoleCRUD
|
||||
from ..menu.crud import MenuCRUD
|
||||
from ..dept.crud import DeptCRUD
|
||||
from ..auth.schema import AuthSchema
|
||||
from ..menu.schema import MenuOutSchema
|
||||
from .crud import UserCRUD
|
||||
from .schema import (
|
||||
CurrentUserUpdateSchema,
|
||||
ResetPasswordSchema,
|
||||
UserOutSchema,
|
||||
UserCreateSchema,
|
||||
UserUpdateSchema,
|
||||
UserChangePasswordSchema,
|
||||
UserRegisterSchema,
|
||||
UserForgetPasswordSchema,
|
||||
UserQueryParam
|
||||
)
|
||||
|
||||
|
||||
class UserService:
|
||||
"""用户模块服务层"""
|
||||
|
||||
@classmethod
|
||||
async def get_detail_by_id_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
根据ID获取用户详情
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- id (int): 用户ID
|
||||
|
||||
返回:
|
||||
- dict: 用户详情字典
|
||||
"""
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=id)
|
||||
if not user:
|
||||
raise CustomException(msg="用户不存在")
|
||||
|
||||
# 如果用户绑定了部门,则获取部门名称
|
||||
if user.dept_id:
|
||||
dept = await DeptCRUD(auth).get_by_id_crud(id=user.dept_id)
|
||||
UserOutSchema.dept_name = dept.name if dept else None
|
||||
else:
|
||||
UserOutSchema.dept_name = None
|
||||
|
||||
return UserOutSchema.model_validate(user).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def get_user_list_service(cls, auth: AuthSchema, search: UserQueryParam | None = None, order_by: list[dict[str, str]] | None = None) -> list[dict]:
|
||||
"""
|
||||
获取用户列表
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- search (UserQueryParam | None): 查询参数对象。
|
||||
- order_by (list[dict[str, str]] | None): 排序参数列表。
|
||||
|
||||
返回:
|
||||
- list[dict]: 用户详情字典列表
|
||||
"""
|
||||
user_list = await UserCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
|
||||
user_dict_list = []
|
||||
for user in user_list:
|
||||
user_dict = UserOutSchema.model_validate(user).model_dump()
|
||||
user_dict_list.append(user_dict)
|
||||
|
||||
return user_dict_list
|
||||
|
||||
@classmethod
|
||||
async def create_user_service(cls, data: UserCreateSchema, auth: AuthSchema) -> dict:
|
||||
"""
|
||||
创建用户
|
||||
|
||||
参数:
|
||||
- data (UserCreateSchema): 用户创建信息
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- dict: 创建后的用户详情字典
|
||||
"""
|
||||
if not data.username:
|
||||
raise CustomException(msg="用户名不能为空")
|
||||
# 检查是否试图创建超级管理员
|
||||
if data.is_superuser:
|
||||
raise CustomException(msg='不允许创建超级管理员')
|
||||
# 检查用户名是否存在
|
||||
user = await UserCRUD(auth).get_by_username_crud(username=data.username)
|
||||
if user:
|
||||
raise CustomException(msg='已存在相同用户名称的账号')
|
||||
|
||||
# 检查部门是否存在
|
||||
if data.dept_id:
|
||||
dept = await DeptCRUD(auth).get_by_id_crud(id=data.dept_id)
|
||||
if not dept:
|
||||
raise CustomException(msg='部门不存在')
|
||||
# 创建用户
|
||||
if data.password:
|
||||
data.password = PwdUtil.set_password_hash(password=data.password)
|
||||
user_dict = data.model_dump(exclude_unset=True, exclude={"role_ids", "position_ids"})
|
||||
# 创建用户
|
||||
new_user = await UserCRUD(auth).create(data=user_dict)
|
||||
# 设置角色
|
||||
if data.role_ids and len(data.role_ids) > 0:
|
||||
await UserCRUD(auth).set_user_roles_crud(user_ids=[new_user.id], role_ids=data.role_ids)
|
||||
# 设置岗位
|
||||
if data.position_ids and len(data.position_ids) > 0:
|
||||
await UserCRUD(auth).set_user_positions_crud(user_ids=[new_user.id], position_ids=data.position_ids)
|
||||
|
||||
new_user_dict = UserOutSchema.model_validate(new_user).model_dump()
|
||||
return new_user_dict
|
||||
|
||||
@classmethod
|
||||
async def update_user_service(cls, id: int, data: UserUpdateSchema, auth: AuthSchema) -> dict:
|
||||
"""
|
||||
更新用户
|
||||
|
||||
参数:
|
||||
- id (int): 用户ID
|
||||
- data (UserUpdateSchema): 用户更新信息
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- Dict: 更新后的用户详情字典
|
||||
"""
|
||||
if not data.username:
|
||||
raise CustomException(msg="账号不能为空")
|
||||
|
||||
# 检查用户是否存在
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=id)
|
||||
if not user:
|
||||
raise CustomException(msg='用户不存在')
|
||||
|
||||
# 检查是否尝试修改超级管理员
|
||||
if user.is_superuser:
|
||||
raise CustomException(msg='超级管理员不允许修改')
|
||||
|
||||
# 检查用户名是否重复
|
||||
exist_user = await UserCRUD(auth).get_by_username_crud(username=data.username)
|
||||
if exist_user and exist_user.id != id:
|
||||
raise CustomException(msg='已存在相同的账号')
|
||||
# 新增:检查手机号是否重复
|
||||
if data.mobile:
|
||||
exist_mobile_user = await UserCRUD(auth).get_by_mobile_crud(mobile=data.mobile)
|
||||
if exist_mobile_user and exist_mobile_user.id != id:
|
||||
raise CustomException(msg='更新失败,手机号已存在')
|
||||
# 新增:检查邮箱是否重复
|
||||
if data.email:
|
||||
exist_email_user = await UserCRUD(auth).get(email=data.email)
|
||||
if exist_email_user and exist_email_user.id != id:
|
||||
raise CustomException(msg='更新失败,邮箱已存在')
|
||||
# 检查部门是否存在且可用
|
||||
if data.dept_id:
|
||||
dept = await DeptCRUD(auth).get_by_id_crud(id=data.dept_id)
|
||||
if not dept:
|
||||
raise CustomException(msg='部门不存在')
|
||||
if not dept.status:
|
||||
raise CustomException(msg='部门已被禁用')
|
||||
|
||||
# 更新用户 - 排除不应被修改的字段, 更新不更新密码
|
||||
user_dict = data.model_dump(exclude_unset=True, exclude={"role_ids", "position_ids", "last_login", "password"})
|
||||
new_user = await UserCRUD(auth).update(id=id, data=user_dict)
|
||||
|
||||
# 更新角色和岗位
|
||||
if data.role_ids and len(data.role_ids) > 0:
|
||||
# 检查角色是否都存在且可用
|
||||
roles = await RoleCRUD(auth).get_list_crud(search={"id": ("in", data.role_ids)})
|
||||
if len(roles) != len(data.role_ids):
|
||||
raise CustomException(msg='部分角色不存在')
|
||||
if not all(role.status for role in roles):
|
||||
raise CustomException(msg='部分角色已被禁用')
|
||||
await UserCRUD(auth).set_user_roles_crud(user_ids=[id], role_ids=data.role_ids)
|
||||
|
||||
if data.position_ids and len(data.position_ids) > 0:
|
||||
# 检查岗位是否都存在且可用
|
||||
positions = await PositionCRUD(auth).get_list_crud(search={"id": ("in", data.position_ids)})
|
||||
if len(positions) != len(data.position_ids):
|
||||
raise CustomException(msg='部分岗位不存在')
|
||||
if not all(position.status for position in positions):
|
||||
raise CustomException(msg='部分岗位已被禁用')
|
||||
await UserCRUD(auth).set_user_positions_crud(user_ids=[id], position_ids=data.position_ids)
|
||||
|
||||
user_dict = UserOutSchema.model_validate(new_user).model_dump()
|
||||
return user_dict
|
||||
|
||||
@classmethod
|
||||
async def delete_user_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
||||
"""
|
||||
删除用户
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- ids (list[int]): 用户ID列表
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
for id in ids:
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=id)
|
||||
if not user:
|
||||
raise CustomException(msg="用户不存在")
|
||||
if user.is_superuser:
|
||||
raise CustomException(msg="超级管理员不能删除")
|
||||
if user.status:
|
||||
raise CustomException(msg="用户已启用,不能删除")
|
||||
if auth.user and auth.user.id == id:
|
||||
raise CustomException(msg="不能删除当前登陆用户")
|
||||
# 删除用户角色关联数据
|
||||
await UserCRUD(auth).set_user_roles_crud(user_ids=ids, role_ids=[])
|
||||
|
||||
# 删除用户岗位关联数据
|
||||
await UserCRUD(auth).set_user_positions_crud(user_ids=ids, position_ids=[])
|
||||
|
||||
# 删除用户
|
||||
await UserCRUD(auth).delete(ids=ids)
|
||||
|
||||
@classmethod
|
||||
async def get_current_user_info_service(cls, auth: AuthSchema) -> dict:
|
||||
"""
|
||||
获取当前用户信息
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- Dict: 当前用户详情字典
|
||||
"""
|
||||
# 获取用户基本信息
|
||||
if not auth.user or not auth.user.id:
|
||||
raise CustomException(msg="用户不存在")
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=auth.user.id)
|
||||
# 获取部门名称
|
||||
if user and user.dept:
|
||||
UserOutSchema.dept_name = user.dept.name
|
||||
user_dict = UserOutSchema.model_validate(user).model_dump()
|
||||
|
||||
# 获取菜单权限
|
||||
if auth.user and auth.user.is_superuser:
|
||||
# 使用树形结构查询,预加载children关系
|
||||
menu_all = await MenuCRUD(auth).get_tree_list_crud(search={'type': ('in', [1, 2, 4]), 'status': '0'}, order_by=[{"order": "asc"}])
|
||||
menus = [MenuOutSchema.model_validate(menu).model_dump() for menu in menu_all]
|
||||
|
||||
else:
|
||||
# 收集用户所有角色的菜单ID,使用列表推导式优化代码
|
||||
menu_ids = {
|
||||
menu.id
|
||||
for role in auth.user.roles or []
|
||||
for menu in role.menus
|
||||
if menu.status and menu.type in [1, 2, 4]
|
||||
}
|
||||
|
||||
# 使用树形结构查询,预加载children关系
|
||||
menus = [
|
||||
MenuOutSchema.model_validate(menu).model_dump()
|
||||
for menu in await MenuCRUD(auth).get_tree_list_crud(search={'id': ('in', list(menu_ids))}, order_by=[{"order": "asc"}])
|
||||
] if menu_ids else []
|
||||
user_dict["menus"] = traversal_to_tree(menus)
|
||||
return user_dict
|
||||
|
||||
@classmethod
|
||||
async def update_current_user_info_service(cls, auth: AuthSchema, data: CurrentUserUpdateSchema) -> dict:
|
||||
"""
|
||||
更新当前用户信息
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (CurrentUserUpdateSchema): 当前用户更新信息
|
||||
|
||||
返回:
|
||||
- Dict: 更新后的当前用户详情字典
|
||||
"""
|
||||
if not auth.user or not auth.user.id:
|
||||
raise CustomException(msg="用户不存在")
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=auth.user.id)
|
||||
if not user:
|
||||
raise CustomException(msg="用户不存在")
|
||||
if user.is_superuser:
|
||||
raise CustomException(msg="超级管理员不能修改个人信息")
|
||||
# 新增:检查手机号是否重复
|
||||
if data.mobile:
|
||||
exist_mobile_user = await UserCRUD(auth).get_by_mobile_crud(mobile=data.mobile)
|
||||
if exist_mobile_user and exist_mobile_user.id != auth.user.id:
|
||||
raise CustomException(msg='更新失败,手机号已存在')
|
||||
# 新增:检查邮箱是否重复
|
||||
if data.email:
|
||||
exist_email_user = await UserCRUD(auth).get(email=data.email)
|
||||
if exist_email_user and exist_email_user.id != auth.user.id:
|
||||
raise CustomException(msg='更新失败,邮箱已存在')
|
||||
user_update_data = UserUpdateSchema(**data.model_dump())
|
||||
new_user = await UserCRUD(auth).update(id=auth.user.id, data=user_update_data)
|
||||
return UserOutSchema.model_validate(new_user).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def set_user_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
设置用户状态
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (BatchSetAvailable): 批量设置用户状态数据
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
for id in data.ids:
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=id)
|
||||
if not user:
|
||||
raise CustomException(msg=f"用户ID {id} 不存在")
|
||||
if user.is_superuser:
|
||||
raise CustomException(msg="超级管理员状态不能修改")
|
||||
await UserCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
|
||||
|
||||
@classmethod
|
||||
async def upload_avatar_service(cls, base_url: str, file: UploadFile) -> dict:
|
||||
"""
|
||||
上传用户头像
|
||||
|
||||
参数:
|
||||
- base_url (str): 基础URL
|
||||
- file (UploadFile): 上传的文件
|
||||
|
||||
返回:
|
||||
- Dict: 上传头像响应字典
|
||||
"""
|
||||
filename, filepath, file_url = await UploadUtil.upload_file(file=file, base_url=base_url)
|
||||
|
||||
return UploadResponseSchema(
|
||||
file_path=f'{filepath}',
|
||||
file_name=filename,
|
||||
origin_name=file.filename,
|
||||
file_url=f'{file_url}',
|
||||
).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def change_user_password_service(cls, auth: AuthSchema, data: UserChangePasswordSchema) -> dict:
|
||||
"""
|
||||
修改用户密码
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (UserChangePasswordSchema): 用户密码修改数据
|
||||
|
||||
返回:
|
||||
- Dict: 更新后的当前用户详情字典
|
||||
"""
|
||||
if not auth.user or not auth.user.id:
|
||||
raise CustomException(msg="用户不存在")
|
||||
if not data.old_password or not data.new_password:
|
||||
raise CustomException(msg='密码不能为空')
|
||||
|
||||
# 验证原密码
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=auth.user.id)
|
||||
if not user:
|
||||
raise CustomException(msg="用户不存在")
|
||||
if not PwdUtil.verify_password(plain_password=data.old_password, password_hash=user.password):
|
||||
raise CustomException(msg='原密码输入错误')
|
||||
|
||||
# 更新密码
|
||||
new_password_hash = PwdUtil.set_password_hash(password=data.new_password)
|
||||
new_user = await UserCRUD(auth).change_password_crud(id=user.id, password_hash=new_password_hash)
|
||||
return UserOutSchema.model_validate(new_user).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def reset_user_password_service(cls, auth: AuthSchema, data: ResetPasswordSchema) -> dict:
|
||||
"""
|
||||
重置用户密码
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (ResetPasswordSchema): 用户密码重置数据
|
||||
|
||||
返回:
|
||||
- Dict: 更新后的当前用户详情字典
|
||||
"""
|
||||
if not data.password:
|
||||
raise CustomException(msg='密码不能为空')
|
||||
|
||||
# 验证用户
|
||||
user = await UserCRUD(auth).get_by_id_crud(id=data.id)
|
||||
if not user:
|
||||
raise CustomException(msg="用户不存在")
|
||||
|
||||
# 检查是否是超级管理员
|
||||
if user.is_superuser:
|
||||
raise CustomException(msg="超级管理员密码不能重置")
|
||||
|
||||
# 更新密码
|
||||
new_password_hash = PwdUtil.set_password_hash(password=data.password)
|
||||
new_user = await UserCRUD(auth).change_password_crud(id=data.id, password_hash=new_password_hash)
|
||||
return UserOutSchema.model_validate(new_user).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def register_user_service(cls, auth: AuthSchema, data: UserRegisterSchema) -> dict:
|
||||
"""
|
||||
用户注册
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (UserRegisterSchema): 用户注册数据
|
||||
|
||||
返回:
|
||||
- Dict: 注册后的用户详情字典
|
||||
"""
|
||||
# 检查用户名是否存在
|
||||
username_ok = await UserCRUD(auth).get_by_username_crud(username=data.username)
|
||||
if username_ok:
|
||||
raise CustomException(msg='账号已存在')
|
||||
|
||||
data.password = PwdUtil.set_password_hash(password=data.password)
|
||||
data.name = data.username
|
||||
create_dict = data.model_dump(exclude_unset=True, exclude={"role_ids", "position_ids"})
|
||||
|
||||
# 设置默认用户类型为普通用户
|
||||
create_dict.setdefault("user_type", "0")
|
||||
|
||||
# 设置创建人ID
|
||||
if auth.user and auth.user.id:
|
||||
create_dict["created_id"] = auth.user.id
|
||||
|
||||
result = await UserCRUD(auth).create(data=create_dict)
|
||||
if data.role_ids:
|
||||
await UserCRUD(auth).set_user_roles_crud(user_ids=[result.id], role_ids=data.role_ids)
|
||||
return UserOutSchema.model_validate(result).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def forget_password_service(cls, auth: AuthSchema, data: UserForgetPasswordSchema) -> dict:
|
||||
"""
|
||||
用户忘记密码
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (UserForgetPasswordSchema): 用户忘记密码数据
|
||||
|
||||
返回:
|
||||
- Dict: 更新后的当前用户详情字典
|
||||
"""
|
||||
user = await UserCRUD(auth).get_by_username_crud(username=data.username)
|
||||
if not user:
|
||||
raise CustomException(msg="用户不存在")
|
||||
if not user.status:
|
||||
raise CustomException(msg="用户已停用")
|
||||
|
||||
# 检查是否是超级管理员
|
||||
if user.is_superuser:
|
||||
raise CustomException(msg="超级管理员密码不能重置")
|
||||
|
||||
new_password_hash = PwdUtil.set_password_hash(password=data.new_password)
|
||||
new_user = await UserCRUD(auth).forget_password_crud(id=user.id, password_hash=new_password_hash)
|
||||
return UserOutSchema.model_validate(new_user).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def batch_import_user_service(cls, auth: AuthSchema, file: UploadFile, update_support: bool = False) -> str:
|
||||
"""
|
||||
批量导入用户
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- file (UploadFile): 上传的Excel文件
|
||||
- update_support (bool, optional): 是否支持更新已存在用户. 默认值为False.
|
||||
|
||||
返回:
|
||||
- str: 导入结果消息
|
||||
"""
|
||||
|
||||
header_dict = {
|
||||
'部门编号': 'dept_id',
|
||||
'用户名': 'username',
|
||||
'名称': 'name',
|
||||
'邮箱': 'email',
|
||||
'手机号': 'mobile',
|
||||
'性别': 'gender',
|
||||
'状态': 'status'
|
||||
}
|
||||
|
||||
try:
|
||||
# 读取Excel文件
|
||||
contents = await file.read()
|
||||
df = pd.read_excel(io.BytesIO(contents))
|
||||
await file.close()
|
||||
|
||||
if df.empty:
|
||||
raise CustomException(msg="导入文件为空")
|
||||
|
||||
# 检查表头是否完整
|
||||
missing_headers = [header for header in header_dict.keys() if header not in df.columns]
|
||||
if missing_headers:
|
||||
raise CustomException(msg=f"导入文件缺少必要的列: {', '.join(missing_headers)}")
|
||||
|
||||
# 重命名列名
|
||||
df.rename(columns=header_dict, inplace=True)
|
||||
|
||||
# 验证必填字段
|
||||
required_fields = ['username', 'name', 'dept_id']
|
||||
for field in required_fields:
|
||||
missing_rows = df[df[field].isnull()].index.tolist()
|
||||
raise CustomException(msg=f"{[k for k,v in header_dict.items() if v == field][0]}不能为空,第{[i+1 for i in missing_rows]}行")
|
||||
|
||||
error_msgs = []
|
||||
success_count = 0
|
||||
count = 0
|
||||
|
||||
# 处理每一行数据
|
||||
for index, row in df.iterrows():
|
||||
try:
|
||||
count = count + 1
|
||||
# 数据转换
|
||||
gender = 1 if row['gender'] == '男' else (2 if row['gender'] == '女' else 1)
|
||||
status = True if row['status'] == '正常' else False
|
||||
|
||||
# 构建用户数据
|
||||
user_data = {
|
||||
"username": str(row['username']).strip(),
|
||||
"name": str(row['name']).strip(),
|
||||
"email": str(row['email']).strip(),
|
||||
"mobile": str(row['mobile']).strip(),
|
||||
"gender": gender,
|
||||
"status": status,
|
||||
"dept_id": int(row['dept_id']),
|
||||
"password": PwdUtil.set_password_hash(password="123456") # 设置默认密码
|
||||
}
|
||||
|
||||
# 处理用户导入
|
||||
exists_user = await UserCRUD(auth).get_by_username_crud(username=user_data["username"])
|
||||
if exists_user:
|
||||
# 检查是否是超级管理员
|
||||
if exists_user.is_superuser:
|
||||
error_msgs.append(f"第{count}行: 超级管理员不允许修改")
|
||||
continue
|
||||
if update_support:
|
||||
user_update_data = UserUpdateSchema(**user_data)
|
||||
await UserCRUD(auth).update(id=exists_user.id, data=user_update_data)
|
||||
success_count += 1
|
||||
else:
|
||||
error_msgs.append(f"第{count}行: 用户 {user_data['username']} 已存在")
|
||||
else:
|
||||
user_create_data = UserCreateSchema(**user_data)
|
||||
await UserCRUD(auth).create(data=user_create_data)
|
||||
success_count += 1
|
||||
|
||||
except Exception as e:
|
||||
error_msgs.append(f"第{count}行: 异常{str(e)}")
|
||||
continue
|
||||
|
||||
# 返回详细的导入结果
|
||||
result = f"成功导入 {success_count} 条数据"
|
||||
if error_msgs:
|
||||
result += "\n错误信息:\n" + "\n".join(error_msgs)
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
log.error(f"批量导入用户失败: {str(e)}")
|
||||
raise CustomException(msg=f"导入失败: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
async def get_import_template_user_service(cls) -> bytes:
|
||||
"""
|
||||
获取用户导入模板
|
||||
|
||||
返回:
|
||||
- bytes: Excel文件字节流
|
||||
"""
|
||||
header_list = ['部门编号', '用户名', '名称', '邮箱', '手机号', '性别', '状态']
|
||||
selector_header_list = ['性别', '状态']
|
||||
option_list = [{'性别': ['男', '女', '未知']}, {'状态': ['正常', '停用']}]
|
||||
return ExcelUtil.get_excel_template(
|
||||
header_list=header_list,
|
||||
selector_header_list=selector_header_list,
|
||||
option_list=option_list
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def export_user_list_service(cls, user_list: list[dict[str, Any]]) -> bytes:
|
||||
"""
|
||||
导出用户列表为Excel文件
|
||||
|
||||
参数:
|
||||
- user_list (List[Dict[str, Any]]): 用户列表
|
||||
|
||||
返回:
|
||||
- bytes: Excel文件字节流
|
||||
"""
|
||||
if not user_list:
|
||||
raise CustomException(msg="没有数据可导出")
|
||||
|
||||
# 定义字段映射
|
||||
mapping_dict = {
|
||||
'id': '用户编号',
|
||||
'avatar': '头像',
|
||||
'username': '用户名称',
|
||||
'name': '用户昵称',
|
||||
'dept_name': '部门',
|
||||
'email': '邮箱',
|
||||
'mobile': '手机号',
|
||||
'gender': '性别',
|
||||
'status': '状态',
|
||||
'is_superuser': '是否超级管理员',
|
||||
'last_login': '最后登录时间',
|
||||
'description': '备注',
|
||||
'created_time': '创建时间',
|
||||
'updated_time': '更新时间',
|
||||
'updated_id': '更新者ID',
|
||||
}
|
||||
|
||||
# 复制数据并转换
|
||||
# creator = {'id': 1, 'name': '管理员', 'username': 'admin'}
|
||||
data = user_list.copy()
|
||||
for item in data:
|
||||
item['status'] = '启用' if item.get('status') == "0" else '停用'
|
||||
gender = item.get('gender')
|
||||
item['gender'] = '男' if gender == '1' else ('女' if gender == '2' else '未知')
|
||||
item['is_superuser'] = '是' if item.get('is_superuser') else '否'
|
||||
item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
|
||||
|
||||
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
|
||||
Reference in New Issue
Block a user