github.com/Go-To-Byte/DouSheng/user_center@v0.0.0-20230524130918-ad531c1a3f6a/apps/user/app.go (about)

     1  // @Author: Ciusyan 2023/1/25
     2  package user
     3  
     4  import (
     5  	"github.com/go-playground/validator/v10"
     6  
     7  	kitUtil "github.com/Go-To-Byte/DouSheng/dou_kit/utils"
     8  
     9  	"github.com/Go-To-Byte/DouSheng/user_center/common/utils"
    10  )
    11  
    12  const (
    13  	AppName = "user"
    14  )
    15  
    16  var (
    17  	validate = validator.New()
    18  )
    19  
    20  func NewLoginAndRegisterRequest() *LoginAndRegisterRequest {
    21  	return &LoginAndRegisterRequest{}
    22  }
    23  
    24  // Validate 参数校验
    25  func (r *LoginAndRegisterRequest) Validate() error {
    26  	return validate.Struct(r)
    27  }
    28  
    29  func (r *UserInfoRequest) Validate() error {
    30  	return validate.Struct(r)
    31  }
    32  func (r *UserMapRequest) Validate() error {
    33  	return validate.Struct(r)
    34  }
    35  
    36  func NewTokenResponse(id int64, token string) *TokenResponse {
    37  	return &TokenResponse{
    38  		UserId: id,
    39  		Token:  token,
    40  	}
    41  }
    42  
    43  func NewDefaultUser() *User {
    44  	return &User{}
    45  }
    46  
    47  func NewDefaultUserPo() *UserPo {
    48  	return &UserPo{}
    49  }
    50  
    51  func NewUserPo(req *LoginAndRegisterRequest) *UserPo {
    52  	return &UserPo{
    53  		Username: req.Username,
    54  		Password: req.Password,
    55  	}
    56  }
    57  
    58  // Hash 将敏感信息做Hash
    59  func (r *LoginAndRegisterRequest) Hash() *LoginAndRegisterRequest {
    60  	r.Password = utils.BcryptHash(r.Password)
    61  	return r
    62  }
    63  
    64  // CheckHash 比对Hash
    65  func (u *UserPo) CheckHash(data any) bool {
    66  	return utils.VerifyBcryptHash(data, u.Password)
    67  }
    68  
    69  // TableName 指明表名 -> gorm 参数映射
    70  func (*UserPo) TableName() string {
    71  	return AppName
    72  }
    73  
    74  func (po *UserPo) Po2vo() *User {
    75  	return &User{
    76  		Id:   po.Id,
    77  		Name: po.Username,
    78  		// TODO: database
    79  		Avatar:          kitUtil.V2P("https://p3-passport.byteimg.com/img/user-avatar/de432cd6200bc3d3f7d633a3ccd528d8~180x180.awebp"),
    80  		Signature:       kitUtil.V2P("人生没有白走的路,每一步都算数"),
    81  		BackgroundImage: kitUtil.V2P("https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/de73a8b9317a481ea4d488d91c16fe61~tplv-k3u1fbpfcp-zoom-crop-mark:3024:3024:3024:1702.awebp?"),
    82  	}
    83  }
    84  
    85  func NewUserInfoResponse() *UserInfoResponse {
    86  	return &UserInfoResponse{}
    87  }
    88  
    89  func NewUserInfoRequest() *UserInfoRequest {
    90  	return &UserInfoRequest{}
    91  }
    92  
    93  func NewUserMapRequest() *UserMapRequest {
    94  	return &UserMapRequest{}
    95  }