BookSystem/backend/auth/models.py
jayhgq f1da855793 feat(auth): 实现用户认证与授权功能
- 添加用户登录、登出及令牌管理功能
- 实现基于JWT和Redis的认证系统
- 完善用户和角色管理API
- 添加密码加密与验证功能
- 配置数据库连接和Redis客户端
- 实现中间件进行权限验证
- 初始化管理员和测试用户数据
- 更新模型和接口文档
- 添加测试API和配置文件
2026-02-28 18:08:53 +08:00

39 lines
1.9 KiB
Python

from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, func
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
Base = declarative_base()
class Role(Base):
"""角色数据库模型"""
__tablename__ = "role"
id = Column(Integer, primary_key=True, index=True, comment="角色ID")
name = Column(String(50), nullable=False, unique=True, comment="角色名称")
createtime = Column(DateTime, default=datetime.utcnow, comment="创建时间")
updatetime = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")
creator = Column(String(50), nullable=True, comment="创建人")
# 关联到User
users = relationship("User", back_populates="role_rel")
class User(Base):
"""用户数据库模型"""
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String(50), unique=True, nullable=False, comment="登录名")
nickname = Column(String(50), nullable=False, comment="昵称")
email = Column(String(50), nullable=True, comment="电子邮箱")
phone = Column(String(11), nullable=True, comment="手机号码")
wx_openid = Column(String(100), nullable=True, comment="微信OpenID")
password_hash = Column(String(128), nullable=False, comment="密码哈希值")
avatar = Column(String(255), nullable=True, comment="头像路径")
role_id = Column(Integer, ForeignKey("role.id"), default=2, comment="角色ID")
createtime = Column(DateTime, default=datetime.utcnow, comment="创建时间")
updatetime = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")
lastlogintime = Column(DateTime, nullable=True, comment="最后登录时间")
# 关联到Role
role_rel = relationship("Role", back_populates="users")