This commit is contained in:
shaoyongjun 2024-10-08 00:08:51 +08:00
parent 20faef5bd4
commit 1341dad9b4
2 changed files with 44 additions and 23 deletions

View File

@ -2,11 +2,10 @@ package convert
import (
"mylomen_server/common/dto"
"mylomen_server/common/utils"
"mylomen_server/infrastructure/repository"
)
func UserReq2DO(req dto.RegisterReq) repository.UserDO {
func UserReq2DO(req dto.LoginReq) repository.UserDO {
data := repository.UserDO{}
data.Account = req.Account
data.GoogleId = req.GoogleId
@ -16,16 +15,6 @@ func UserReq2DO(req dto.RegisterReq) repository.UserDO {
data.Phone = req.Phone
data.Email = req.Email
data.NickName = req.NickName
if data.NickName == nil {
data.NickName = utils.ToPtr("default")
}
//todo 默认头像
data.Avatar = req.Avatar
if data.Avatar == nil {
data.Avatar = utils.ToPtr("default")
}
return data
}

View File

@ -37,12 +37,20 @@ func (l user) Register(ctx context.Context, req dto.RegisterReq) error {
}
//fill data
userDO := convert.UserReq2DO(req)
userDO := convert.UserReq2DO(req.LoginReq)
userDO.Sn = utils.GetTrueRandomCode(8)
userDO.Deleted = false
userDO.UpdateTime = time.Now()
userDO.NickName = req.NickName
if userDO.NickName == nil {
userDO.NickName = utils.ToPtr("default")
}
//todo 默认头像
userDO.Avatar = req.Avatar
if userDO.Avatar == nil {
userDO.Avatar = utils.ToPtr("default")
}
//密码加密
h := sha256.Sum256([]byte(req.Password))
@ -58,19 +66,43 @@ func (l user) Login(ctx context.Context, req dto.LoginReq) (*dto.UserVO, error)
//1. 查询用户
acUser := repository.GUser.FindByReq(ctx, req)
if acUser == nil || acUser.Deleted == true {
return nil, errors.New("user not exist")
}
//fill data
userDO := convert.UserReq2DO(req)
userDO.Sn = utils.GetTrueRandomCode(8)
userDO.Deleted = false
userDO.UpdateTime = time.Now()
//2. email 和 手机号登录 和account 需要验证密码。其他情况不需要验证密码
if req.Account != nil || req.Email != nil || req.Phone != nil {
if acUser.Pwd == nil {
return nil, errors.New("password not set")
}
// 验证账号密码
userDO.NickName = utils.ToPtr("default")
//todo 默认头像
userDO.Avatar = utils.ToPtr("default")
//密码加密
h := sha256.Sum256([]byte(req.Password))
passHash := hex.EncodeToString(h[:])
if acUser.Pwd == nil || passHash != *acUser.Pwd {
return nil, errors.New("password is error")
userDO.Pwd = &passHash
err := repository.GUser.Create(ctx, &userDO)
if err != nil {
return nil, errors.New("user Register err")
}
//再次查询
acUser = repository.GUser.FindByReq(ctx, req)
if acUser == nil || acUser.Deleted == true {
return nil, errors.New("user Register err2")
}
} else {
//2. email 和 手机号登录 和account 需要验证密码。其他情况不需要验证密码
if req.Account != nil || req.Email != nil || req.Phone != nil {
if acUser.Pwd == nil {
return nil, errors.New("password not set")
}
// 验证账号密码
h := sha256.Sum256([]byte(req.Password))
passHash := hex.EncodeToString(h[:])
if acUser.Pwd == nil || passHash != *acUser.Pwd {
return nil, errors.New("password is error")
}
}
}