package models import ( // "crypto/rand" "crypto/sha256" "fmt" "io" "time" ) type User struct { ID uint `gorm:"primaryKey" json:"id"` Username string `gorm:"uniqueIndex;size:50" json:"username"` Email string `gorm:"uniqueIndex;size:100" json:"email"` Password string `gorm:"size:255" json:"-"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // HashPassword 迁移到 utils 包更合适(可选) func HashPassword(password string) ([]byte, error) { h := sha256.New() if _, err := io.WriteString(h, password); err != nil { // 修正[]byte转换 return nil, err } return h.Sum(nil), nil } // Validate 建议移动到 service 层(可选) func (u *User) Validate() error { if u.Password == "" { return fmt.Errorf("password cannot be empty") } return nil }