fix(用户绑定): 修复邮箱绑定流程中用户ID未正确赋值的问题
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m10s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m10s
在邮箱不存在的情况下,确保将当前设备用户的ID正确赋值给emailUserId变量,以保证后续token生成和返回结果正确
This commit is contained in:
parent
15f4e69dc3
commit
004acd03d2
@ -34,6 +34,12 @@ func NewBindEmailWithVerificationLogic(ctx context.Context, svcCtx *svc.ServiceC
|
||||
}
|
||||
}
|
||||
|
||||
// BindEmailWithVerification 处理邮箱绑定流程,并在邮箱已存在时转移设备归属
|
||||
// 参数说明:
|
||||
// - req: 绑定邮箱请求,包含邮箱地址等信息
|
||||
// 返回值:
|
||||
// - *types.BindEmailWithVerificationResponse: 包含绑定结果、消息、token、用户ID
|
||||
// - error: 发生错误时返回具体错误
|
||||
func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.BindEmailWithVerificationRequest) (*types.BindEmailWithVerificationResponse, error) {
|
||||
// 获取当前用户
|
||||
u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
|
||||
@ -63,21 +69,23 @@ func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.Bi
|
||||
// 检查邮箱是否已被其他用户绑定
|
||||
existingMethod, err := l.svcCtx.UserModel.FindUserAuthMethodByOpenID(l.ctx, "email", req.Email)
|
||||
var emailUserId int64
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 邮箱不存在,创建新的邮箱用户: 不需要创建邮箱用户
|
||||
l.Infow(" 为当前设备做 邮箱绑定操作; 在 user_auth_methods 中添加记录", logger.Field("email", req.Email))
|
||||
err = l.addAuthMethodForEmailUser(u.Id, req.Email)
|
||||
if err != nil {
|
||||
l.Errorw("添加邮箱用户认证方法失败", logger.Field("error", err.Error()), logger.Field("user_id", u.Id))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "添加邮箱用户认证方法失败")
|
||||
}
|
||||
} else {
|
||||
// 数据库查询错误
|
||||
l.Errorw("查询邮箱绑定状态失败", logger.Field("error", err.Error()))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "查询邮箱绑定状态失败")
|
||||
}
|
||||
} else if existingMethod.Id != 0 {
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 邮箱不存在,不创建新用户,直接将邮箱认证绑定到当前设备用户
|
||||
l.Infow(" 为当前设备做 邮箱绑定操作; 在 user_auth_methods 中添加记录", logger.Field("email", req.Email))
|
||||
err = l.addAuthMethodForEmailUser(u.Id, req.Email)
|
||||
if err != nil {
|
||||
l.Errorw("添加邮箱用户认证方法失败", logger.Field("error", err.Error()), logger.Field("user_id", u.Id))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "添加邮箱用户认证方法失败")
|
||||
}
|
||||
// 关键修复:为后续 token 生成与返回结果赋值绑定后的用户ID
|
||||
emailUserId = u.Id
|
||||
} else {
|
||||
// 数据库查询错误
|
||||
l.Errorw("查询邮箱绑定状态失败", logger.Field("error", err.Error()))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "查询邮箱绑定状态失败")
|
||||
}
|
||||
} else if existingMethod.Id != 0 {
|
||||
// 邮箱已存在,使用现有的邮箱用户
|
||||
emailUserId = existingMethod.UserId
|
||||
l.Infow("邮箱已存在,将设备转移到现有邮箱用户",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user