You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.4 KiB
39 lines
1.4 KiB
2 months ago
|
package models
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"ims/util/db/dts"
|
||
|
"time"
|
||
|
|
||
|
"gorm.io/gorm"
|
||
|
"zestack.dev/misc"
|
||
|
)
|
||
|
|
||
|
// Account 平台账户
|
||
|
type Account struct {
|
||
|
ID uint `json:"id" gorm:"primaryKey;comment:账户ID"`
|
||
|
Nickname string `json:"nickname" gorm:"comment:昵称"`
|
||
|
AvatarUrl string `json:"avatar_url" gorm:"comment:头像地址"`
|
||
|
Username string `json:"username" gorm:"unique;comment:用户名"`
|
||
|
PhoneNumber string `json:"phone_number" gorm:"uniqueIndex;comment:手机号码"`
|
||
|
WechatOpenid dts.NullString `json:"wechat_openid" gorm:"uniqueIndex;comment:微信openid"`
|
||
|
Email dts.NullString `json:"email" gorm:"uniqueIndex;comment:电子邮箱"`
|
||
|
RawPassword string `json:"-" gorm:"-"` // 原始密码
|
||
|
Password string `json:"-" gorm:"comment:登录密码"`
|
||
|
CreatedAt time.Time `json:"created_at" gorm:"comment:创建时间"`
|
||
|
UpdatedAt time.Time `json:"updated_at" gorm:"comment:更新时间"`
|
||
|
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"comment:删除时间"`
|
||
|
}
|
||
|
|
||
|
func (u *Account) BeforeCreate(_ *gorm.DB) error {
|
||
|
if u.RawPassword != "" {
|
||
|
hash, err := misc.PasswordHash(u.RawPassword)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
u.Password = hash
|
||
|
return nil
|
||
|
}
|
||
|
return errors.New("缺少密码")
|
||
|
}
|