实现验证码生成、存储和验证逻辑 - 新增验证码生成工具函数 - 在登录接口中添加验证码校验 - 添加获取验证码图片的API端点 - 更新用户登录schema包含验证码字段 - 添加captcha依赖到requirements.txt
109 lines
3.7 KiB
Python
109 lines
3.7 KiB
Python
from pydantic import BaseModel, EmailStr, Field
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
|
|
|
|
class UserBase(BaseModel):
|
|
"""用户基础模型"""
|
|
|
|
username: str = Field(..., min_length=3, max_length=50, description="登录名")
|
|
nickname: str = Field(..., max_length=50, description="昵称")
|
|
email: Optional[EmailStr] = Field(None, max_length=50, description="电子邮箱")
|
|
phone: Optional[str] = Field(None, max_length=11, description="手机号码")
|
|
wx_openid: Optional[str] = Field(None, max_length=100, description="微信OpenID")
|
|
avatar: Optional[str] = Field(None, max_length=255, description="头像路径")
|
|
role_id: int = Field(2, description="角色ID")
|
|
|
|
|
|
class UserCreateRequest(UserBase):
|
|
"""用户创建请求模型"""
|
|
|
|
password: str = Field(..., min_length=6, max_length=50, description="密码")
|
|
|
|
|
|
class UserCreate(UserBase):
|
|
"""用户创建模型"""
|
|
|
|
password_hash: str = Field(
|
|
..., min_length=6, max_length=128, description="密码哈希值"
|
|
)
|
|
|
|
|
|
class UserUpdate(BaseModel):
|
|
"""用户更新模型"""
|
|
|
|
username: str = Field(..., min_length=3, max_length=50, description="登录名")
|
|
nickname: str = Field(..., max_length=50, description="昵称")
|
|
email: Optional[EmailStr] = Field(None, max_length=50, description="电子邮箱")
|
|
phone: Optional[str] = Field(None, max_length=11, description="手机号码")
|
|
wx_openid: Optional[str] = Field(None, max_length=100, description="微信OpenID")
|
|
avatar: Optional[str] = Field(None, max_length=255, description="头像路径")
|
|
role_id: Optional[int] = Field(2, description="角色ID")
|
|
password: Optional[str] = Field(None, max_length=128, description="密码")
|
|
|
|
|
|
class UserLogin(BaseModel):
|
|
"""用户登录模型"""
|
|
|
|
username: str = Field(..., description="登录名")
|
|
password: str = Field(..., description="密码")
|
|
captcha_id: str = Field(..., description="验证码ID")
|
|
captcha_code: str = Field(..., description="验证码")
|
|
|
|
|
|
class UserResponse(BaseModel):
|
|
"""用户响应模型"""
|
|
|
|
id: int = Field(..., description="用户ID")
|
|
username: str = Field(..., description="登录名")
|
|
nickname: str = Field(..., description="昵称")
|
|
email: Optional[EmailStr] = Field(None, description="电子邮箱")
|
|
phone: Optional[str] = Field(None, description="手机号码")
|
|
wx_openid: Optional[str] = Field(None, description="微信OpenID")
|
|
avatar: Optional[str] = Field(None, description="头像路径")
|
|
role_id: int = Field(..., description="角色ID")
|
|
createtime: datetime = Field(..., description="创建时间")
|
|
updatetime: datetime = Field(..., description="更新时间")
|
|
lastlogintime: Optional[datetime] = Field(None, description="最后登录时间")
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class Token(BaseModel):
|
|
"""令牌模型"""
|
|
|
|
access_token: str = Field(..., description="访问令牌")
|
|
token_type: str = Field(..., description="令牌类型")
|
|
|
|
|
|
class TokenData(BaseModel):
|
|
"""令牌数据模型"""
|
|
|
|
user_id: Optional[int] = None
|
|
email: Optional[EmailStr] = None
|
|
|
|
|
|
class RoleBase(BaseModel):
|
|
"""角色基础模型"""
|
|
|
|
name: str = Field(..., max_length=50, description="角色名称")
|
|
|
|
|
|
class RoleCreate(RoleBase):
|
|
"""角色创建模型"""
|
|
|
|
creator: Optional[str] = Field(None, max_length=50, description="创建人")
|
|
|
|
|
|
class RoleResponse(RoleBase):
|
|
"""角色响应模型"""
|
|
|
|
id: int = Field(..., description="角色ID")
|
|
createtime: datetime = Field(..., description="创建时间")
|
|
updatetime: datetime = Field(..., description="更新时间")
|
|
creator: Optional[str] = Field(None, description="创建人")
|
|
|
|
class Config:
|
|
from_attributes = True
|