f
This commit is contained in:
@@ -56,42 +56,55 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
|
||||
}
|
||||
}
|
||||
var userID string
|
||||
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if findUserErr != nil && findUserErr != model.ErrNotFound {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
|
||||
}
|
||||
if user == nil {
|
||||
// 用户不存在,自动注册新用户
|
||||
l.Infof("手机登录, 用户不存在,自动注册新用户: %s", encryptedMobile)
|
||||
|
||||
// 最多重试3次,每次间隔50ms
|
||||
maxRetries := 3
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if findUserErr != nil && findUserErr != model.ErrNotFound {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
|
||||
}
|
||||
|
||||
if user != nil {
|
||||
// 用户存在
|
||||
if user.Disable == 1 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_DISABLED), "用户已被封禁")
|
||||
}
|
||||
userID = user.Id
|
||||
break
|
||||
}
|
||||
|
||||
// 用户不存在,尝试注册
|
||||
l.Infof("手机登录, 用户不存在,尝试注册新用户 (尝试 %d/%d): %s", i+1, maxRetries, encryptedMobile)
|
||||
registeredUserID, registerErr := l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
|
||||
if registerErr != nil {
|
||||
// 检查是否是并发注册导致的唯一键冲突
|
||||
// MySQL错误码: 1062 (23000) - Duplicate entry
|
||||
errStr := registerErr.Error()
|
||||
if strings.Contains(errStr, "Duplicate entry") && strings.Contains(errStr, "user_auth.unique_type_key") {
|
||||
// 并发冲突,重新查询用户
|
||||
l.Infof("手机登录, 检测到并发注册冲突,重新查询用户: %s", encryptedMobile)
|
||||
user, retryErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if retryErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 并发冲突后重新查询用户失败, mobile: %s, err: %+v", encryptedMobile, retryErr)
|
||||
// 并发冲突,等待一小段时间后重试
|
||||
l.Infof("手机登录, 检测到并发注册冲突,等待后重试: %s", encryptedMobile)
|
||||
if i < maxRetries-1 {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
// 最后一次重试仍然失败,尝试最后一次查询
|
||||
user, lastRetryErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if lastRetryErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 并发冲突后重试查询用户失败, mobile: %s, err: %+v", encryptedMobile, lastRetryErr)
|
||||
}
|
||||
if user == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 并发冲突后仍未找到用户, mobile: %s", encryptedMobile)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 重试多次后仍未找到用户, mobile: %s", encryptedMobile)
|
||||
}
|
||||
userID = user.Id
|
||||
l.Infof("手机登录, 并发冲突后获取到已注册用户, userId: %s, mobile: %s", userID, encryptedMobile)
|
||||
l.Infof("手机登录, 并发冲突重试后获取到已注册用户, userId: %s, mobile: %s", userID, encryptedMobile)
|
||||
} else {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 自动注册用户失败, mobile: %s, err: %+v", encryptedMobile, registerErr)
|
||||
}
|
||||
} else {
|
||||
userID = registeredUserID
|
||||
l.Infof("手机登录, 自动注册用户成功, userId: %s, mobile: %s", userID, encryptedMobile)
|
||||
break
|
||||
}
|
||||
} else {
|
||||
if user.Disable == 1 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_DISABLED), "用户已被封禁")
|
||||
}
|
||||
userID = user.Id
|
||||
}
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user