This commit is contained in:
2025-12-11 19:01:28 +08:00
parent cff3a32da4
commit a1859fdeb9
4 changed files with 300 additions and 84 deletions

View File

@@ -142,12 +142,10 @@ func (s *UserService) getAuthTypeByPlatform(platform string) string {
}
// RegisterUser 注册用户返回用户ID
// 传入手机号自动注册如果ctx存在临时用户则临时用户转为正式用户
// 只负责创建新用户(手机号不存在时),不处理合并逻辑
// 如果有临时用户claims会将临时用户的认证绑定到新用户
func (s *UserService) RegisterUser(ctx context.Context, mobile string) (string, error) {
claims, err := ctxdata.GetClaimsFromCtx(ctx)
if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) {
return "", err
}
// 检查手机号是否已存在
user, err := s.userModel.FindOneByMobile(ctx, sql.NullString{String: mobile, Valid: true})
if err != nil && !errors.Is(err, model.ErrNotFound) {
return "", err
@@ -155,51 +153,60 @@ func (s *UserService) RegisterUser(ctx context.Context, mobile string) (string,
if user != nil {
return "", errors.New("用户已注册")
}
// 普通注册
if claims == nil {
var userId string
err = s.userModel.Trans(ctx, func(ctx context.Context, session sqlx.Session) error {
user := &model.User{Id: uuid.NewString(), Mobile: sql.NullString{String: mobile, Valid: true}}
if _, userInsertErr := s.userModel.Insert(ctx, session, user); userInsertErr != nil {
return userInsertErr
}
userId = user.Id
_, userAuthInsertErr := s.userAuthModel.Insert(ctx, session, &model.UserAuth{Id: uuid.NewString(), UserId: userId, AuthType: model.UserAuthTypeMobile, AuthKey: mobile})
if userAuthInsertErr != nil {
return userAuthInsertErr
}
return nil
})
if err != nil {
return "", err
}
return userId, nil
}
// 双重判断是否已经注册根据mobile判断而不是userType
currentUser, err := s.userModel.FindOne(ctx, claims.UserId)
if err != nil && !errors.Is(err, model.ErrNotFound) {
// 获取当前登录态(可能为空
claims, err := ctxdata.GetClaimsFromCtx(ctx)
if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) {
return "", err
}
if currentUser != nil && currentUser.Mobile.Valid && currentUser.Mobile.String != "" {
return "", errors.New("用户已注册")
}
var userId string
// 临时用户绑定mobile转正式注册
err = s.userModel.Trans(ctx, func(ctx context.Context, session sqlx.Session) error {
// 创建新用户
user := &model.User{Id: uuid.NewString(), Mobile: sql.NullString{String: mobile, Valid: true}}
if _, userInsertErr := s.userModel.Insert(ctx, session, user); userInsertErr != nil {
return userInsertErr
}
userId = user.Id
_, userAuthInsertErr := s.userAuthModel.Insert(ctx, session, &model.UserAuth{Id: uuid.NewString(), UserId: userId, AuthType: model.UserAuthTypeMobile, AuthKey: mobile})
// 创建 mobile 认证
_, userAuthInsertErr := s.userAuthModel.Insert(ctx, session, &model.UserAuth{
Id: uuid.NewString(),
UserId: userId,
AuthType: model.UserAuthTypeMobile,
AuthKey: mobile,
})
if userAuthInsertErr != nil {
return userAuthInsertErr
}
tempUserBindErr := s.TempUserBindUser(ctx, session, userId)
if tempUserBindErr != nil {
return tempUserBindErr
// 如果有临时用户,将临时用户的认证绑定到新用户
if claims != nil {
// 检查临时用户是否已有该认证类型
existingAuth, err := s.userAuthModel.FindOneByAuthTypeAuthKey(ctx, claims.AuthType, claims.AuthKey)
if err != nil && !errors.Is(err, model.ErrNotFound) {
return err
}
// 如果认证不存在,创建新的认证绑定
if existingAuth == nil {
_, err = s.userAuthModel.Insert(ctx, session, &model.UserAuth{
Id: uuid.NewString(),
UserId: userId,
AuthType: claims.AuthType,
AuthKey: claims.AuthKey,
})
if err != nil {
return err
}
} else if existingAuth.UserId != userId {
// 如果认证已存在但属于其他用户,迁移到新用户
existingAuth.UserId = userId
if _, err := s.userAuthModel.Update(ctx, session, existingAuth); err != nil {
return err
}
}
}
return nil
})
if err != nil {