fix: 时间精度精确到秒 + 同步全部文档到最新状态

时间字段修正:
- init.sql: 所有 TIMESTAMP 改为 TIMESTAMP(0),精确到秒
- GORM 模型: 所有时间字段添加 type:timestamp(0) tag
- 已有数据库表通过 ALTER TABLE 修正列类型

文档同步(架构设计 + 实施步骤):
- system-architecture.md:
  - auth 模块描述更新为"有状态 JWT Token 管理(Redis 存储)"
  - 开发环境端口 8080 → 8085
- phase1-foundation-and-auth.md:
  - Go 版本 1.22+ → 1.23+
  - 常量命名更新为驼峰风格(UserStatusActive / RoleUser 等)
  - Task 4 完整重写:增加 token_store.go、TokenValidator 接口、有状态 JWT
  - Task 5 端口号 8080 → 8085,测试命令增加登出失效验证
  - 依赖版本选择原则说明

Made-with: Cursor
This commit is contained in:
bujinyuan
2026-02-28 17:04:47 +08:00
parent b607434c57
commit e9bd4dd204
5 changed files with 91 additions and 82 deletions

View File

@@ -5,11 +5,11 @@ import "time"
// Role 角色表模型,对应 auth_roles 表
// 系统预置三种角色user普通用户、admin管理员、super_admin超级管理员
type Role struct {
ID int `json:"id" gorm:"primaryKey;autoIncrement"` // 角色唯一标识,自增主键
Code string `json:"code" gorm:"uniqueIndex;size:50;not null"` // 角色代码,全局唯一,如 user/admin/super_admin对应 constants.RoleCode*
Name string `json:"name" gorm:"size:50;not null"` // 角色中文显示名称,如「普通用户」「管理员」
Description string `json:"description" gorm:"size:200;default:''"` // 角色描述说明,用于后台管理界面展示
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime"` // 创建时间,由 GORM 自动填充
ID int `json:"id" gorm:"primaryKey;autoIncrement"` // 角色唯一标识,自增主键
Code string `json:"code" gorm:"uniqueIndex;size:50;not null"` // 角色代码,全局唯一,如 user/admin/super_admin对应 constants.RoleCode*
Name string `json:"name" gorm:"size:50;not null"` // 角色中文显示名称,如「普通用户」「管理员」
Description string `json:"description" gorm:"size:200;default:''"` // 角色描述说明,用于后台管理界面展示
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 创建时间,由 GORM 自动填充,精确到秒
}
// TableName 指定数据库表名
@@ -20,9 +20,9 @@ func (Role) TableName() string {
// UserRole 用户角色关联表模型,对应 auth_user_roles 表
// 采用联合主键user_id + role_id一个用户可拥有多个角色
type UserRole struct {
UserID int64 `json:"user_id" gorm:"primaryKey"` // 关联的用户 ID外键指向 auth_users.id
RoleID int `json:"role_id" gorm:"primaryKey"` // 关联的角色 ID外键指向 auth_roles.id
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime"` // 角色分配时间,由 GORM 自动填充
UserID int64 `json:"user_id" gorm:"primaryKey"` // 关联的用户 ID外键指向 auth_users.id
RoleID int `json:"role_id" gorm:"primaryKey"` // 关联的角色 ID外键指向 auth_roles.id
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 角色分配时间,由 GORM 自动填充,精确到秒
}
// TableName 指定数据库表名

View File

@@ -5,19 +5,19 @@ import "time"
// User 用户主表模型,对应 auth_users 表
type User struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 用户唯一标识,自增主键
Username string `json:"username" gorm:"uniqueIndex;size:50;not null"` // 登录用户名,全局唯一
Email string `json:"email" gorm:"uniqueIndex;size:100;not null"` // 邮箱地址,全局唯一,用于登录和找回密码
PasswordHash string `json:"-" gorm:"column:password_hash;size:255;not null"` // bcrypt 加密后的密码哈希JSON 序列化时隐藏
Nickname string `json:"nickname" gorm:"size:50;not null;default:''"` // 用户昵称,用于页面展示,允许重复
Avatar string `json:"avatar" gorm:"size:500;not null;default:''"` // 头像 URL 地址,为空则使用默认头像
Gender int `json:"gender" gorm:"not null;default:0"` // 性别0=未知, 1=男, 2=女(对应 constants.Gender*
Phone *string `json:"phone" gorm:"size:20"` // 手机号,可选字段,指针类型允许 NULL
Status int `json:"status" gorm:"not null;default:1"` // 账号状态1=正常, 2=禁用, 3=注销(对应 constants.UserStatus*
LastLoginAt *time.Time `json:"last_login_at"` // 最后登录时间,首次注册时为 NULL
LastLoginIP *string `json:"last_login_ip" gorm:"column:last_login_ip;size:50"` // 最后登录 IP 地址,用于安全审计
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime"` // 账号创建时间,由 GORM 自动填充
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime"` // 最后更新时间,由 GORM 自动更新
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 用户唯一标识,自增主键
Username string `json:"username" gorm:"uniqueIndex;size:50;not null"` // 登录用户名,全局唯一
Email string `json:"email" gorm:"uniqueIndex;size:100;not null"` // 邮箱地址,全局唯一,用于登录和找回密码
PasswordHash string `json:"-" gorm:"column:password_hash;size:255;not null"` // bcrypt 加密后的密码哈希JSON 序列化时隐藏
Nickname string `json:"nickname" gorm:"size:50;not null;default:''"` // 用户昵称,用于页面展示,允许重复
Avatar string `json:"avatar" gorm:"size:500;not null;default:''"` // 头像 URL 地址,为空则使用默认头像
Gender int `json:"gender" gorm:"not null;default:0"` // 性别0=未知, 1=男, 2=女(对应 constants.Gender*
Phone *string `json:"phone" gorm:"size:20"` // 手机号,可选字段,指针类型允许 NULL
Status int `json:"status" gorm:"not null;default:1"` // 账号状态1=正常, 2=禁用, 3=注销(对应 constants.UserStatus*
LastLoginAt *time.Time `json:"last_login_at" gorm:"type:timestamp(0)"` // 最后登录时间,首次注册时为 NULL
LastLoginIP *string `json:"last_login_ip" gorm:"column:last_login_ip;size:50"` // 最后登录 IP 地址,用于安全审计
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 账号创建时间,由 GORM 自动填充,精确到秒
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"` // 最后更新时间,由 GORM 自动更新,精确到秒
}
// TableName 指定数据库表名