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 {
// 用户已存在