Files
2026-03-08 19:28:32 +08:00

63 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pydantic import BaseModel, validator
from typing import Optional
from datetime import datetime
import re
# 用户注册
class UserRegister(BaseModel):
phone: str # 手机号必填,作为唯一标识
password: str
nickname: Optional[str] = None
@validator('phone')
def validate_phone(cls, v):
"""验证手机号格式11位数字"""
if not v:
raise ValueError('手机号不能为空')
if not re.match(r'^1[3-9]\d{9}$', v):
raise ValueError('手机号格式错误请输入11位有效手机号')
return v
@validator('password')
def validate_password(cls, v):
"""验证密码"""
if not v:
raise ValueError('密码不能为空')
if len(v) < 6:
raise ValueError('密码长度不能少于6位')
if len(v) > 72:
raise ValueError('密码长度不能超过72位')
return v
# 用户登录
class UserLogin(BaseModel):
phone: str # 手机号登录
password: str
@validator('phone')
def validate_phone(cls, v):
"""验证手机号格式11位数字"""
if not v:
raise ValueError('手机号不能为空')
if not re.match(r'^1[3-9]\d{9}$', v):
raise ValueError('手机号格式错误请输入11位有效手机号')
return v
# 用户响应
class UserResponse(BaseModel):
id: int
phone: str
nickname: Optional[str]
avatar: Optional[str]
balance: float
created_at: datetime
class Config:
from_attributes = True
# Token 响应
class Token(BaseModel):
access_token: str
token_type: str = "bearer"
user: UserResponse