103 lines
2.6 KiB
Python
103 lines
2.6 KiB
Python
import os
|
||
import time
|
||
from typing import Optional
|
||
from datetime import datetime
|
||
from uuid import uuid4
|
||
|
||
from sqlmodel import Field, SQLModel, Column
|
||
from sqlalchemy import func, DateTime
|
||
|
||
|
||
def primary_key_create():
|
||
return f"{uuid4()}_{os.getpid()}_{int(time.time())}"
|
||
|
||
|
||
class Users(SQLModel, table=True):
|
||
__tablename__ = "users" # type: ignore
|
||
|
||
# ---------- 主键 ----------
|
||
id: str = Field(
|
||
default_factory=primary_key_create,
|
||
primary_key=True,
|
||
index=True,
|
||
description="用户唯一ID(UUID格式)"
|
||
)
|
||
|
||
# ---------- 登录凭证(唯一索引) ----------
|
||
username: str = Field(
|
||
max_length=50,
|
||
unique=True,
|
||
index=True,
|
||
nullable=False,
|
||
description="登录用户名"
|
||
)
|
||
email: str = Field(
|
||
max_length=255,
|
||
unique=True,
|
||
index=True,
|
||
nullable=False,
|
||
description="电子邮箱"
|
||
)
|
||
hashed_password: str = Field(
|
||
max_length=255,
|
||
nullable=False,
|
||
description="加密后的密码(不存明文)"
|
||
)
|
||
|
||
# ---------- 个人信息(可选) ----------
|
||
full_name: Optional[str] = Field(
|
||
default=None,
|
||
max_length=100,
|
||
description="用户全名"
|
||
)
|
||
avatar_url: Optional[str] = Field(
|
||
default=None,
|
||
max_length=500,
|
||
description="头像图片链接"
|
||
)
|
||
phone: Optional[str] = Field(
|
||
default=None,
|
||
max_length=20,
|
||
description="手机号码"
|
||
)
|
||
|
||
# ---------- 安全与状态标识 ----------
|
||
is_active: bool = Field(
|
||
default=True,
|
||
description="账户是否启用(用于软禁用)"
|
||
)
|
||
is_verified: bool = Field(
|
||
default=False,
|
||
description="邮箱/手机是否已验证"
|
||
)
|
||
is_superuser: bool = Field(
|
||
default=False,
|
||
description="是否为超级管理员"
|
||
)
|
||
|
||
# ---------- 审计时间戳(数据库自动维护) ----------
|
||
created_at: datetime = Field(
|
||
sa_column=Column(
|
||
DateTime(timezone=True),
|
||
server_default=func.now(),
|
||
nullable=False,
|
||
comment="创建时间"
|
||
)
|
||
)
|
||
updated_at: datetime = Field(
|
||
sa_column=Column(
|
||
DateTime(timezone=True),
|
||
server_default=func.now(),
|
||
onupdate=func.now(),
|
||
nullable=False,
|
||
comment="最后更新时间"
|
||
)
|
||
)
|
||
|
||
# ---------- 最后登录记录(可选) ----------
|
||
last_login_at: Optional[datetime] = Field(
|
||
default=None,
|
||
sa_column=Column(DateTime(timezone=True), nullable=True),
|
||
description="最后一次登录时间"
|
||
)
|