This commit is contained in:
2026-03-28 16:49:48 +08:00
parent f1b075d77d
commit a4dc154fca
2 changed files with 61 additions and 11 deletions

View File

@@ -6,11 +6,14 @@ import (
"sim-server/app/main/api/internal/svc"
"sim-server/app/main/api/internal/types"
"sim-server/app/main/model"
"sim-server/common/xerr"
"sim-server/pkg/lzkit/crypto"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type AdminUpdatePlatformUserLogic struct {
@@ -30,15 +33,17 @@ func NewAdminUpdatePlatformUserLogic(ctx context.Context, svcCtx *svc.ServiceCon
func (l *AdminUpdatePlatformUserLogic) AdminUpdatePlatformUser(req *types.AdminUpdatePlatformUserReq) (resp *types.AdminUpdatePlatformUserResp, err error) {
user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "用户不存在: %d, err: %v", req.Id, err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "用户不存在: %s, err: %v", req.Id, err)
}
var encryptedMobile string
if req.Mobile != nil {
key := l.svcCtx.Config.Encrypt.SecretKey
EncryptMobile, err := crypto.EncryptMobile(*req.Mobile, key)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败: %v", err)
em, encErr := crypto.EncryptMobile(*req.Mobile, key)
if encErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败: %v", encErr)
}
user.Mobile = sql.NullString{String: EncryptMobile, Valid: true}
encryptedMobile = em
user.Mobile = sql.NullString{String: encryptedMobile, Valid: true}
}
if req.Nickname != nil {
user.Nickname = sql.NullString{String: *req.Nickname, Valid: *req.Nickname != ""}
@@ -55,9 +60,41 @@ func (l *AdminUpdatePlatformUserLogic) AdminUpdatePlatformUser(req *types.AdminU
if req.Password != nil {
user.Password = sql.NullString{String: *req.Password, Valid: *req.Password != ""}
}
_, err = l.svcCtx.UserModel.Update(l.ctx, nil, user)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新用户失败: %v", err)
if req.Mobile != nil {
err = l.svcCtx.UserModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
if _, err := l.svcCtx.UserModel.Update(ctx, session, user); err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新用户失败: %v", err)
}
existing, findErr := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(ctx, user.Id, model.UserAuthTypeMobile)
if findErr != nil && !errors.Is(findErr, model.ErrNotFound) {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询手机号认证失败: %v", findErr)
}
if existing != nil {
if existing.AuthKey != encryptedMobile {
existing.AuthKey = encryptedMobile
if _, err := l.svcCtx.UserAuthModel.Update(ctx, session, existing); err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "同步手机号认证失败: %v", err)
}
}
} else {
if _, err := l.svcCtx.UserAuthModel.Insert(ctx, session, &model.UserAuth{
Id: uuid.NewString(),
UserId: user.Id,
AuthType: model.UserAuthTypeMobile,
AuthKey: encryptedMobile,
}); err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建手机号认证失败: %v", err)
}
}
return nil
})
if err != nil {
return nil, err
}
} else {
if _, err = l.svcCtx.UserModel.Update(l.ctx, nil, user); err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新用户失败: %v", err)
}
}
resp = &types.AdminUpdatePlatformUserResp{Success: true}
return resp, nil

View File

@@ -107,9 +107,22 @@ func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.Bind
if _, err := l.svcCtx.UserModel.Update(l.ctx, nil, currentUser); err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新手机号失败: %v", err)
}
// 记录mobile认证确保后续可通过手机号登录
if _, err := l.svcCtx.UserAuthModel.Insert(l.ctx, nil, &model.UserAuth{Id: uuid.NewString(), UserId: finalUserID, AuthType: model.UserAuthTypeMobile, AuthKey: encryptedMobile}); err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建手机号认证失败: %v", err)
// user_auth 上可能已有 mobile 行而 user.mobile 曾为空/不一致,直接 Insert 会违反 unique_userId_key
existingMobileAuth, authErr := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(l.ctx, finalUserID, model.UserAuthTypeMobile)
if authErr != nil && !errors.Is(authErr, model.ErrNotFound) {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询手机号认证失败: %v", authErr)
}
if existingMobileAuth != nil {
if existingMobileAuth.AuthKey != encryptedMobile {
existingMobileAuth.AuthKey = encryptedMobile
if _, err := l.svcCtx.UserAuthModel.Update(l.ctx, nil, existingMobileAuth); err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新手机号认证失败: %v", err)
}
}
} else {
if _, err := l.svcCtx.UserAuthModel.Insert(l.ctx, nil, &model.UserAuth{Id: uuid.NewString(), UserId: finalUserID, AuthType: model.UserAuthTypeMobile, AuthKey: encryptedMobile}); err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建手机号认证失败: %v", err)
}
}
// 自动创建代理记录(注册即成为代理)