package user import ( "time" "github.com/infraboard/mcube/v2/exception" "github.com/infraboard/mcube/v2/ioc/config/validator" "github.com/infraboard/mcube/v2/tools/pretty" "gitlab.com/go-course-project/go17/vblog/utils" "golang.org/x/crypto/bcrypt" ) func New(in *RegistryRequest) (*User, error) { if err := in.Validate(); err != nil { return nil, exception.NewBadRequest("参数校验失败: %s", err) } return &User{ ResourceMeta: *utils.NewResourceMeta(), RegistryRequest: *in, }, nil } type User struct { // 存放到数据里的对象的远数据信息 utils.ResourceMeta // 具体参数 RegistryRequest } func (r *User) String() string { return pretty.ToJSON(r) } func NewRegistryRequest() *RegistryRequest { return &RegistryRequest{} } type RegistryRequest struct { // 用户名 Username string `json:"username" gorm:"column:username;unique;index" validate:"required"` // 密码 Password string `json:"password" gorm:"column:password;type:varchar(255)" validate:"required"` // Profile 信息 Profile // 用户状态 Status } func (r *RegistryRequest) CheckPassword(password string) error { return bcrypt.CompareHashAndPassword([]byte(r.Password), []byte(password)) } func (r *RegistryRequest) Validate() error { return validator.Validate(r) } type Profile struct { // 头像 Avatar string `json:"avatar" gorm:"column:avatar;type:varchar(255)"` // 用户昵称 NickName string `json:"nic_name" gorm:"column:nic_name;type:varchar(100)"` // 用户邮箱, 验证用户有效性 Email string `json:"email" gorm:"column:email;type:varchar(100)"` } type Status struct { // 冻结时间 BlockAt *time.Time `json:"block_at" gorm:"column:block_at"` // 冻结原因 BlockReason string `json:"block_reason" gorm:"column:block_reason;type:text"` } func (s *Status) IsBlocked() bool { return s.BlockAt != nil } func (u *User) TableName() string { return "users" }