"""用户表。 登录方式有两种,但都映射到同一行 user: - 极光一键登录 → phone 唯一索引 - 短信验证码登录(mock 阶段) → 也是 phone 唯一索引 注册即登录:phone 不存在则 insert,存在则更新 last_login_at。 后续如果加微信/Apple 登录,新增 oauth_account 表,这张表不动。 """ from __future__ import annotations from datetime import datetime, timezone from sqlalchemy import DateTime, Integer, String, func from sqlalchemy.orm import Mapped, mapped_column from app.db.base import Base def _utcnow() -> datetime: return datetime.now(timezone.utc) class User(Base): __tablename__ = "user" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) phone: Mapped[str] = mapped_column(String(20), unique=True, index=True, nullable=False) # 注册渠道:jverify / sms。后续加 wechat / apple 时扩 register_channel: Mapped[str] = mapped_column(String(20), nullable=False, default="jverify") nickname: Mapped[str | None] = mapped_column(String(64), nullable=True) avatar_url: Mapped[str | None] = mapped_column(String(512), nullable=True) # 微信开放平台 openid(微信登录绑定后存,提现转账到零钱用)。同一 appid 下唯一; # unique 索引保证一个微信只能绑一个账号(nullable,允许多个 NULL=未绑定)。 wechat_openid: Mapped[str | None] = mapped_column( String(64), unique=True, index=True, nullable=True ) # 微信昵称/头像(绑定时从 sns/userinfo 拉,展示在提现绑定卡)。与上面通用 nickname/avatar_url 分开,不互相覆盖。 wechat_nickname: Mapped[str | None] = mapped_column(String(64), nullable=True) wechat_avatar_url: Mapped[str | None] = mapped_column(String(512), nullable=True) # 账号状态:active / disabled / deleted status: Mapped[str] = mapped_column(String(20), nullable=False, default="active") created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) last_login_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_utcnow, nullable=False ) def __repr__(self) -> str: # pragma: no cover return f""