"""Auth 相关请求 / 响应 schemas。 跟客户端约定: - 字段统一用 snake_case - 时间字段统一 ISO 8601 (UTC) - 登录类接口成功时统一返回 TokenWithUser """ from __future__ import annotations from datetime import datetime from pydantic import BaseModel, ConfigDict, Field # ===== 用户对外信息 ===== class UserOut(BaseModel): model_config = ConfigDict(from_attributes=True) # 允许直接 UserOut.model_validate(orm_user) id: int phone: str nickname: str | None = None avatar_url: str | None = None register_channel: str status: str created_at: datetime last_login_at: datetime # ===== Token 通用结构 ===== class TokenPair(BaseModel): access_token: str refresh_token: str token_type: str = "Bearer" expires_in: int = Field(..., description="access_token 剩余秒数") refresh_expires_in: int = Field(..., description="refresh_token 剩余秒数") class TokenWithUser(TokenPair): user: UserOut # ===== 极光一键登录 ===== class JverifyLoginRequest(BaseModel): login_token: str = Field(..., description="客户端 loginAuth 拿到的 loginToken", min_length=1) operator: str = Field("", description="CM/CU/CT,用于日志,可选") # ===== 短信验证码 ===== class SmsSendRequest(BaseModel): phone: str = Field(..., min_length=11, max_length=11, pattern=r"^1\d{10}$") class SmsSendResponse(BaseModel): sent: bool mock: bool = Field(..., description="是否 mock 发送(true 时验证码不会真发,任意 6 位通过)") cooldown_sec: int = Field(..., description="多少秒后才能再发一次") class SmsLoginRequest(BaseModel): phone: str = Field(..., min_length=11, max_length=11, pattern=r"^1\d{10}$") code: str = Field(..., min_length=4, max_length=8) # ===== Refresh ===== class RefreshRequest(BaseModel): refresh_token: str = Field(..., min_length=1) # ===== Logout ===== class LogoutResponse(BaseModel): ok: bool = True