package models import ( "time" "gorm.io/gorm" ) // User 用户模型 type User struct { ID uint `json:"id" gorm:"primaryKey;column:id"` Username string `json:"username" gorm:"column:username;uniqueIndex;not null"` Email string `json:"email" gorm:"column:email"` Password string `json:"password,omitempty" gorm:"-"` // Virtual field for input PasswordHash string `json:"-" gorm:"column:password_hash"` RoleID uint `json:"roleId" gorm:"column:role_id"` Role string `json:"role" gorm:"column:role"` // 保持兼容,或者作为Role Name IsActive int `json:"isActive" gorm:"column:is_active;default:1"` CreatedAt int64 `json:"createdAt" gorm:"column:created_at"` UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"` DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"` } // TableName 指定表名 func (User) TableName() string { return "users" } // BeforeCreate 创建前钩子 func (u *User) BeforeCreate(tx *gorm.DB) error { now := time.Now().Unix() if u.CreatedAt == 0 { u.CreatedAt = now } if u.UpdatedAt == 0 { u.UpdatedAt = now } if u.DeletedAt == 0 { u.DeletedAt = 0 } return nil } // BeforeUpdate 更新前钩子 func (u *User) BeforeUpdate(tx *gorm.DB) error { u.UpdatedAt = time.Now().Unix() return nil } // UserResponse 用户响应模型 type UserResponse struct { ID uint `json:"id"` Username string `json:"username"` Email string `json:"email"` RoleID uint `json:"roleId"` Role string `json:"role"` IsActive int `json:"isActive"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` } // LoginRequest 登录请求模型 type LoginRequest struct { Username string `json:"username" binding:"required"` Password string `json:"password" binding:"required"` } // LoginResponse 登录响应模型 type LoginResponse struct { Token string `json:"token"` User UserResponse `json:"user"` Expire int64 `json:"expire"` }