mirror of
https://github.com/Cccc-owo/CheckInApp.git
synced 2026-06-17 05:56:29 +00:00
5cdc8b2144
后端改进: - 添加统一异常处理系统 (exceptions.py, response.py) - 实现自定义异常类 (ValidationError, AuthorizationError, ResourceNotFoundError, BusinessLogicError) - 配置全局异常处理器,统一 API 错误响应格式 - 迁移业务逻辑错误到自定义异常 (users.py, auth.py) - 添加 SQL LIKE 通配符转义,防止通配符滥用 - 使用 EmailStr 进行邮箱格式验证 - 移除敏感字段暴露 (jwt_sub) 前端改进: - 配置 ESLint 9 (flat config) 和 Prettier - 修复所有 ESLint 错误和警告 - 移除未使用的变量和导入 - 为组件添加 PropTypes 默认值 - 统一代码格式和风格
64 lines
2.3 KiB
Python
64 lines
2.3 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
from pydantic import BaseModel, Field, EmailStr
|
|
|
|
|
|
class UserBase(BaseModel):
|
|
"""用户基础 Schema"""
|
|
alias: str = Field(..., min_length=2, max_length=50, description="用户别名(用于登录)")
|
|
|
|
|
|
class UserCreate(UserBase):
|
|
"""创建用户 Schema(管理员手动创建,只需要别名)"""
|
|
role: Optional[str] = Field("user", description="角色: user/admin")
|
|
email: Optional[EmailStr] = Field(None, description="邮箱地址")
|
|
is_approved: Optional[bool] = Field(True, description="是否已审批(默认已审批)")
|
|
|
|
|
|
class UserUpdate(BaseModel):
|
|
"""更新用户 Schema(管理员编辑用户)"""
|
|
alias: Optional[str] = Field(None, min_length=2, max_length=50, description="用户别名")
|
|
role: Optional[str] = None
|
|
is_approved: Optional[bool] = None
|
|
email: Optional[EmailStr] = None
|
|
password: Optional[str] = Field(None, min_length=6, description="新密码(可选,留空表示不修改)")
|
|
reset_password: Optional[bool] = Field(False, description="是否清空密码")
|
|
|
|
|
|
class UserUpdateProfile(BaseModel):
|
|
"""用户更新个人信息 Schema"""
|
|
alias: Optional[str] = Field(None, min_length=2, max_length=50, description="新别名")
|
|
email: Optional[EmailStr] = Field(None, description="邮箱地址")
|
|
current_password: Optional[str] = Field(None, min_length=6, description="当前密码(修改密码时必填)")
|
|
new_password: Optional[str] = Field(None, min_length=6, description="新密码")
|
|
|
|
|
|
class UserResponse(BaseModel):
|
|
"""用户响应 Schema"""
|
|
id: int
|
|
alias: str
|
|
role: str
|
|
is_approved: bool
|
|
jwt_exp: str
|
|
email: Optional[EmailStr] = None
|
|
has_password: bool = False # 是否已设置密码
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class UserWithToken(UserResponse):
|
|
"""带 Token 的用户响应 Schema"""
|
|
authorization: Optional[str] = None
|
|
|
|
|
|
class TokenStatus(BaseModel):
|
|
"""Token 状态 Schema"""
|
|
is_valid: bool
|
|
jwt_exp: str
|
|
expires_at: Optional[int] = None # Unix 时间戳(秒)
|
|
days_until_expiry: Optional[int] = None
|
|
expiring_soon: bool = False # 是否即将过期(30分钟内)
|