🎨 完善注册逻辑
This commit is contained in:
@@ -6,9 +6,11 @@ import (
|
|||||||
"git.echol.cn/loser/lckt/model/app/vo"
|
"git.echol.cn/loser/lckt/model/app/vo"
|
||||||
"git.echol.cn/loser/lckt/model/user"
|
"git.echol.cn/loser/lckt/model/user"
|
||||||
"git.echol.cn/loser/lckt/model/user/request"
|
"git.echol.cn/loser/lckt/model/user/request"
|
||||||
|
utils2 "git.echol.cn/loser/lckt/utils"
|
||||||
"github.com/ArtisanCloud/PowerSocialite/v3/src/providers"
|
"github.com/ArtisanCloud/PowerSocialite/v3/src/providers"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AppUserService struct{}
|
type AppUserService struct{}
|
||||||
@@ -110,3 +112,46 @@ func (u *AppUserService) GetUserInfo(id uint) (info vo.UserInfo, err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *AppUserService) Register(p request.RegisterReq) (userInfo user.User, err error) {
|
||||||
|
err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
// 1. 判断用户是否存在
|
||||||
|
var count int64
|
||||||
|
if err = tx.Model(&userInfo).Where("phone = ?", p.Phone).Count(&count).Error; err != nil {
|
||||||
|
global.GVA_LOG.Error("查询用户失败", zap.Error(err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
global.GVA_LOG.Error("用户已存在")
|
||||||
|
return fmt.Errorf("用户已存在")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 如果用户不存在,则创建用户
|
||||||
|
password, _ := bcrypt.GenerateFromPassword([]byte(p.Password), bcrypt.DefaultCost)
|
||||||
|
userInfo = user.User{
|
||||||
|
Phone: p.Phone,
|
||||||
|
Password: string(password),
|
||||||
|
NickName: p.NickName,
|
||||||
|
UserLabel: 1,
|
||||||
|
Status: 1,
|
||||||
|
UserType: p.UserType,
|
||||||
|
Avatar: "https://ui-avatars.com/api/?name=" + p.NickName + "&background=0081ff&color=ffffff&rounded=true",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存用户
|
||||||
|
if err = tx.Save(&userInfo).Error; err != nil {
|
||||||
|
global.GVA_LOG.Error("创建用户失败", zap.Error(err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成邀请码
|
||||||
|
code := utils2.GenerateInviteCode(userInfo.ID)
|
||||||
|
userInfo.InviteCode = &code
|
||||||
|
if err = tx.Model(&userInfo).Where("id = ?", userInfo.ID).Update("invite_code", code).Error; err != nil {
|
||||||
|
global.GVA_LOG.Error("更新邀请码失败", zap.Error(err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user