user.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. 用户数据ORM定义
  3. 定义用户的数据库表结构,包含用户基本信息、认证信息和API密钥
  4. """
  5. from datetime import datetime, date
  6. from sqlalchemy import Column, String, Text, DateTime, Date, Index
  7. from sqlalchemy.sql import func
  8. from app.database import Base
  9. class User(Base):
  10. """
  11. 用户ORM类
  12. 存储用户账户信息和认证数据
  13. """
  14. __tablename__ = "users"
  15. # 主键ID
  16. id = Column(String(50), primary_key=True, comment="账号ID")
  17. # 用户名(唯一,用于登录)
  18. username = Column(String(50), unique=True, nullable=True, index=True, comment="用户名")
  19. # 密码哈希
  20. password_hash = Column(String(255), nullable=True, comment="密码哈希")
  21. # 昵称
  22. nickname = Column(String(100), nullable=False, comment="昵称")
  23. # 手机号
  24. phone = Column(String(20), nullable=True, comment="手机号")
  25. # 邮箱
  26. email = Column(String(255), nullable=True, comment="邮箱")
  27. # 头像URL
  28. avatar = Column(Text, nullable=True, comment="头像URL")
  29. # 实名认证信息
  30. real_name = Column(String(100), nullable=True, comment="真实姓名")
  31. id_card = Column(String(18), nullable=True, comment="身份证号")
  32. is_verified = Column(String(20), nullable=False, default="unverified", comment="实名认证状态:unverified/pending/verified/rejected")
  33. verified_at = Column(DateTime, nullable=True, comment="认证通过时间")
  34. # API密钥
  35. apikey = Column(String(255), nullable=True, comment="API密钥")
  36. # 注册日期
  37. registration_date = Column(Date, nullable=True, comment="注册时间")
  38. # 账户状态(active/disabled)
  39. status = Column(String(20), nullable=False, default="active", comment="账户状态:active/disabled")
  40. # 创建时间(自动设置)
  41. created_at = Column(
  42. DateTime,
  43. nullable=False,
  44. server_default=func.now(),
  45. comment="创建时间"
  46. )
  47. # 更新时间(自动更新)
  48. updated_at = Column(
  49. DateTime,
  50. nullable=False,
  51. server_default=func.now(),
  52. onupdate=func.now(),
  53. comment="更新时间"
  54. )
  55. # 表级配置
  56. __table_args__ = (
  57. Index('idx_users_phone', 'phone'),
  58. Index('idx_users_email', 'email'),
  59. Index('idx_users_apikey', 'apikey'),
  60. Index('idx_users_status', 'status'),
  61. Index('idx_users_is_verified', 'is_verified'),
  62. Index('idx_users_id_card', 'id_card'),
  63. {'schema': 'aigcspace', 'comment': '用户信息表'}
  64. )
  65. def __repr__(self):
  66. return f"<User(id='{self.id}', username='{self.username}', nickname='{self.nickname}')>"