This commit is contained in:
Mrx
2026-02-28 17:36:39 +08:00
parent 85722a697a
commit 562ddf39d8
3 changed files with 76 additions and 14 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
"time"
"qnc-server/app/main/model"
"qnc-server/common/ctxdata"
@@ -83,7 +84,24 @@ func (l *ApplyForAgentLogic) ApplyForAgent(req *types.AgentApplyReq) (resp *type
}
userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "注册用户失败: %v", err)
// 检查是否是并发注册导致的唯一键冲突
// MySQL错误码: 1062 (23000) - Duplicate entry
errStr := err.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 errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "申请代理, 并发冲突后重新查询用户失败: %v", retryErr)
}
if user == nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "申请代理, 并发冲突后仍未找到用户")
}
userID = user.Id
l.Infof("申请代理, 并发冲突后获取到已注册用户, userId: %s, mobile: %s", userID, encryptedMobile)
} else {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "注册用户失败: %v", err)
}
}
} else {
// 用户已存在

View File

@@ -3,6 +3,7 @@ package user
import (
"context"
"database/sql"
stderrors "errors"
"fmt"
"os"
"qnc-server/app/main/api/internal/svc"
@@ -10,6 +11,7 @@ import (
"qnc-server/app/main/model"
"qnc-server/common/xerr"
"qnc-server/pkg/lzkit/crypto"
"strings"
"time"
"github.com/pkg/errors"
@@ -44,7 +46,7 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
redisKey := fmt.Sprintf("%s:%s", "login", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
if stderrors.Is(err, redis.Nil) {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机登录, 验证码过期: %s", encryptedMobile)
}
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
@@ -63,10 +65,28 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
l.Infof("手机登录, 用户不存在,自动注册新用户: %s", encryptedMobile)
registeredUserID, registerErr := l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
if registerErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 自动注册用户失败, mobile: %s, err: %+v", encryptedMobile, registerErr)
// 检查是否是并发注册导致的唯一键冲突
// 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)
}
if user == nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 并发冲突后仍未找到用户, mobile: %s", encryptedMobile)
}
userID = user.Id
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)
}
userID = registeredUserID
l.Infof("手机登录, 自动注册用户成功, userId: %s, mobile: %s", userID, encryptedMobile)
} else {
if user.Disable == 1 {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_DISABLED), "用户已被封禁")