go17/vblog/apps/user/model.go

80 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-12-01 12:00:49 +08:00
package user
2024-12-01 14:54:54 +08:00
import (
"time"
2024-12-08 10:10:37 +08:00
"github.com/infraboard/mcube/v2/exception"
"github.com/infraboard/mcube/v2/ioc/config/validator"
2024-12-08 11:04:25 +08:00
"github.com/infraboard/mcube/v2/tools/pretty"
2024-12-01 14:54:54 +08:00
"gitlab.com/go-course-project/go17/vblog/utils"
2024-12-08 11:04:25 +08:00
"golang.org/x/crypto/bcrypt"
2024-12-01 14:54:54 +08:00
)
2024-12-01 12:00:49 +08:00
2024-12-08 10:10:37 +08:00
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
}
2024-12-01 12:00:49 +08:00
type User struct {
// 存放到数据里的对象的远数据信息
utils.ResourceMeta
// 具体参数
RegistryRequest
}
2024-12-08 11:04:25 +08:00
func (r *User) String() string {
return pretty.ToJSON(r)
}
func NewRegistryRequest() *RegistryRequest {
return &RegistryRequest{}
}
2024-12-01 12:00:49 +08:00
type RegistryRequest struct {
// 用户名
2024-12-08 10:10:37 +08:00
Username string `json:"username" gorm:"column:username;unique;index" validate:"required"`
2024-12-01 12:00:49 +08:00
// 密码
2024-12-08 11:04:25 +08:00
Password string `json:"password" gorm:"column:password;type:varchar(255)" validate:"required"`
2024-12-01 12:00:49 +08:00
// Profile 信息
Profile
2024-12-01 14:54:54 +08:00
// 用户状态
Status
2024-12-01 12:00:49 +08:00
}
2024-12-08 11:04:25 +08:00
func (r *RegistryRequest) CheckPassword(password string) error {
return bcrypt.CompareHashAndPassword([]byte(r.Password), []byte(password))
}
2024-12-08 10:10:37 +08:00
func (r *RegistryRequest) Validate() error {
return validator.Validate(r)
}
2024-12-01 12:00:49 +08:00
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)"`
}
2024-12-01 14:54:54 +08:00
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
}
2024-12-01 12:00:49 +08:00
func (u *User) TableName() string {
return "users"
}