P0-01: Add ESCAPE clause to LIKE queries in operation_log.go and device.go P0-02: Add atomic Increment to L1Cache and L2Cache interfaces P0-07: Add TOTP verification step after password login P1-01: Sanitize error messages in error.go middleware P1-03: Remove err.Error() from export error messages P1-04: Add error return to CountByResultSince in login_log.go P1-05: Add transactional DeleteCascade to RoleRepository P1-06: Add PasswordChangedAt tracking for JWT token invalidation P1-07: Wrap theme SetDefault in database transaction P1-08: Use config values for database pool parameters P1-09: Add rows.Err() checks in social_account_repo.go P1-10: Validate sortOrder with map in user.go ORDER BY P1-11: Add GORM tags to Announcement struct P1-15: Add pageSize upper limit (100) to device and log handlers
74 lines
2.6 KiB
Go
74 lines
2.6 KiB
Go
package domain
|
||
|
||
import "time"
|
||
|
||
// StrPtr 将 string 转为 *string(空字符串返回 nil,用于可选的 unique 字段)
|
||
func StrPtr(s string) *string {
|
||
if s == "" {
|
||
return nil
|
||
}
|
||
return &s
|
||
}
|
||
|
||
// DerefStr 安全解引用 *string,nil 返回空字符串
|
||
func DerefStr(s *string) string {
|
||
if s == nil {
|
||
return ""
|
||
}
|
||
return *s
|
||
}
|
||
|
||
// Gender 性别
|
||
type Gender int
|
||
|
||
const (
|
||
GenderUnknown Gender = iota // 未知
|
||
GenderMale // 男
|
||
GenderFemale // 女
|
||
)
|
||
|
||
// UserStatus 用户状态
|
||
type UserStatus int
|
||
|
||
const (
|
||
UserStatusInactive UserStatus = 0 // 未激活
|
||
UserStatusActive UserStatus = 1 // 已激活
|
||
UserStatusLocked UserStatus = 2 // 已锁定
|
||
UserStatusDisabled UserStatus = 3 // 已禁用
|
||
)
|
||
|
||
// User 用户模型
|
||
type User struct {
|
||
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||
Username string `gorm:"type:varchar(50);uniqueIndex;not null" json:"username"`
|
||
// Email/Phone 使用指针类型:nil 存储为 NULL,允许多个用户没有邮箱/手机(唯一约束对 NULL 不生效)
|
||
Email *string `gorm:"type:varchar(100);uniqueIndex" json:"email"`
|
||
Phone *string `gorm:"type:varchar(20);uniqueIndex" json:"phone"`
|
||
Nickname string `gorm:"type:varchar(50)" json:"nickname"`
|
||
Avatar string `gorm:"type:varchar(255)" json:"avatar"`
|
||
Password string `gorm:"type:varchar(255)" json:"-"`
|
||
Gender Gender `gorm:"type:int;default:0" json:"gender"`
|
||
Birthday *time.Time `gorm:"type:date" json:"birthday,omitempty"`
|
||
Region string `gorm:"type:varchar(50)" json:"region"`
|
||
Bio string `gorm:"type:varchar(500)" json:"bio"`
|
||
Status UserStatus `gorm:"type:int;default:0;index;index:idx_users_status_created_at" json:"status"`
|
||
LastLoginTime *time.Time `json:"last_login_time,omitempty"`
|
||
LastLoginIP string `gorm:"type:varchar(50)" json:"last_login_ip"`
|
||
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_users_status_created_at" json:"created_at"`
|
||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||
DeletedAt *time.Time `gorm:"index" json:"deleted_at,omitempty"`
|
||
|
||
// 2FA / TOTP 字段
|
||
TOTPEnabled bool `gorm:"default:false" json:"totp_enabled"`
|
||
TOTPSecret string `gorm:"type:varchar(64)" json:"-"` // Base32 密钥,不返回给前端
|
||
TOTPRecoveryCodes string `gorm:"type:text" json:"-"` // JSON 编码的恢复码列表
|
||
|
||
// PasswordChangedAt 密码更新时间,用于 token 失效机制
|
||
PasswordChangedAt time.Time `gorm:"type:timestamp;index" json:"password_changed_at,omitempty"`
|
||
}
|
||
|
||
// TableName 指定表名
|
||
func (User) TableName() string {
|
||
return "users"
|
||
}
|