| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- """
- 用户数据ORM定义
- 定义用户的数据库表结构,包含用户基本信息、认证信息和API密钥
- """
- from datetime import datetime, date
- from sqlalchemy import Column, String, Text, DateTime, Date, Index
- from sqlalchemy.sql import func
- from app.database import Base
- class User(Base):
- """
- 用户ORM类
- 存储用户账户信息和认证数据
- """
- __tablename__ = "users"
- # 主键ID
- id = Column(String(50), primary_key=True, comment="账号ID")
- # 用户名(唯一,用于登录)
- username = Column(String(50), unique=True, nullable=True, index=True, comment="用户名")
- # 密码哈希
- password_hash = Column(String(255), nullable=True, comment="密码哈希")
- # 昵称
- nickname = Column(String(100), nullable=False, comment="昵称")
- # 手机号
- phone = Column(String(20), nullable=True, comment="手机号")
- # 邮箱
- email = Column(String(255), nullable=True, comment="邮箱")
- # 头像URL
- avatar = Column(Text, nullable=True, comment="头像URL")
- # 实名认证信息
- real_name = Column(String(100), nullable=True, comment="真实姓名")
- id_card = Column(String(18), nullable=True, comment="身份证号")
- is_verified = Column(String(20), nullable=False, default="unverified", comment="实名认证状态:unverified/pending/verified/rejected")
- verified_at = Column(DateTime, nullable=True, comment="认证通过时间")
- # API密钥
- apikey = Column(String(255), nullable=True, comment="API密钥")
- # 注册日期
- registration_date = Column(Date, nullable=True, comment="注册时间")
- # 账户状态(active/disabled)
- status = Column(String(20), nullable=False, default="active", comment="账户状态:active/disabled")
- # 创建时间(自动设置)
- created_at = Column(
- DateTime,
- nullable=False,
- server_default=func.now(),
- comment="创建时间"
- )
- # 更新时间(自动更新)
- updated_at = Column(
- DateTime,
- nullable=False,
- server_default=func.now(),
- onupdate=func.now(),
- comment="更新时间"
- )
- # 表级配置
- __table_args__ = (
- Index('idx_users_phone', 'phone'),
- Index('idx_users_email', 'email'),
- Index('idx_users_apikey', 'apikey'),
- Index('idx_users_status', 'status'),
- Index('idx_users_is_verified', 'is_verified'),
- Index('idx_users_id_card', 'id_card'),
- {'schema': 'aigcspace', 'comment': '用户信息表'}
- )
- def __repr__(self):
- return f"<User(id='{self.id}', username='{self.username}', nickname='{self.nickname}')>"
|