- 添加用户登录、登出及令牌管理功能 - 实现基于JWT和Redis的认证系统 - 完善用户和角色管理API - 添加密码加密与验证功能 - 配置数据库连接和Redis客户端 - 实现中间件进行权限验证 - 初始化管理员和测试用户数据 - 更新模型和接口文档 - 添加测试API和配置文件
76 lines
2.7 KiB
Python
76 lines
2.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 UserCreate(UserBase):
|
|
"""用户创建模型"""
|
|
password: str = Field(..., min_length=6, max_length=50, description="密码")
|
|
|
|
|
|
class UserLogin(BaseModel):
|
|
"""用户登录模型"""
|
|
username: str = Field(..., description="登录名")
|
|
password: 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: datetime = Field(..., 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 |