Files
user-system/internal/domain/user.go

71 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package domain
import "time"
// StrPtr 将 string 转为 *string空字符串返回 nil用于可选的 unique 字段)
func StrPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
// DerefStr 安全解引用 *stringnil 返回空字符串
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" 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" 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 编码的恢复码列表
}
// TableName 指定表名
func (User) TableName() string {
return "users"
}