package models import ( "crypto/hmac" "crypto/sha256" "fmt" "gorm.io/gorm" "product/backend/moo/db" ) type User struct { // body Password string `gorm:"type:varchar(255)" json:"-"` PasswordSalt string `gorm:"type:varchar(255)" json:"-"` Name *string `gorm:"type:varchar(255);uniqueIndex;" json:"name"` Telephone *string `gorm:"type:varchar(255);uniqueIndex;" json:"telephone"` Email *string `gorm:"type:varchar(255);uniqueIndex;" json:"email"` RealName string `gorm:"type:varchar(255)" json:"RealName"` Order int `gorm:"" json:"order"` State bool `gorm:"" json:"state"` Remark string `gorm:"type:varchar(1023)" json:"remark"` AuthToken string `gorm:"type:varchar(2047)" json:"-"` // body association field RoleID uint `gorm:"" json:"roleID"` // basic models Model // associations Role Role `gorm:"" json:"role"` } var UserSigningKey = []byte("AllYourBase") var UserPasswordSalt = "salt" var UserDefaultPassword = "00000000" // methods func UserRow(id uint) (User, error) { // model row := User{} if err := db.DB.First(&row, id).Error; err != nil { return row, err } // with association _ = db.DB.Model(&row).Association("Role").Find(&row.Role) return row, nil } func UserRows(pager Pager, withTotal bool, keyword string) ([]User, int64, error) { // query query := db.DB.Model(&User{}) // filter // keyword if keyword != "" { query.Where("title LIKE ?", keyword+"%") } // fetch total var total int64 if withTotal { query.Count(&total) } // order query.Order(fmt.Sprintf("`%s` %s", pager.SortField, pager.SortOrder)) // # offset limit offset := -1 // 不限制 if pager.PageSize > 0 { offset = (pager.PageIndex - 1) * pager.PageSize } query.Offset(offset).Limit(pager.PageSize) // select query.Select("*") // fetch var rows []User if result := query.Find(&rows); result.Error != nil { return rows, 0, result.Error } // with association return rows, total, nil } func UserRowDel(id uint) error { // db tx return db.DB.Transaction(func(tx *gorm.DB) error { // model delete if id != 0 { if result := tx.Delete(&User{}, id); result.Error != nil { return result.Error } } // association delete return nil }) } func UserRowUpdate(model *User) error { // db tx return db.DB.Transaction(func(tx *gorm.DB) error { // model update if result := tx.Omit("Password", "PasswordSalt", "AuthToken").Updates(&model); result.Error != nil { return result.Error } // association update return nil }) } func UserRowInsert(model *User) error { // default value model.Password = "00000000" // password model.PasswordSalt = "salt" // random model.Password = UserGeneratePassword(model.Password, model.PasswordSalt) // db tx return db.DB.Transaction(func(tx *gorm.DB) error { // model create if result := tx.Create(&model); result.Error != nil { return result.Error } // association create return nil }) } func UserGeneratePassword(plain string, key string) string { mac := hmac.New(sha256.New, []byte(key)) mac.Write([]byte(plain)) return fmt.Sprintf("%X", mac.Sum(nil)) } func UserGetByName(name string) (User, error) { user := User{} if err := db.DB.Where("name=?", name).First(&user).Error; err != nil { return user, err } return user, nil }