fix(用户): 修复邮箱绑定逻辑中的错误处理
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m17s

重构邮箱绑定逻辑的错误处理流程,正确处理数据库查询错误和记录不存在的情况
优化设备查询方法,移除不必要的缓存键生成
This commit is contained in:
shanshanzhong 2025-10-29 01:38:49 -07:00
parent 02e76ebcf1
commit 1302accaf9
2 changed files with 14 additions and 9 deletions

View File

@ -51,21 +51,27 @@ func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.Bi
// 检查邮箱是否已被其他用户绑定
existingMethod, err := l.svcCtx.UserModel.FindUserAuthMethodByOpenID(l.ctx, "email", req.Email)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
l.Errorw("查询邮箱绑定状态失败", logger.Field("error", err.Error()))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "查询邮箱绑定状态失败")
}
var emailUserId int64
if existingMethod != nil {
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// 邮箱不存在,创建新的邮箱用户
l.Infow("邮箱未绑定,将创建新的邮箱用户", logger.Field("email", req.Email))
emailUserId, err = l.createEmailUser(req.Email)
} 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("邮箱已存在,将设备转移到现有邮箱用户",
logger.Field("email", req.Email),
logger.Field("email_user_id", emailUserId))
} else {
// 邮箱不存在,创建新的邮箱用户
// 这种情况理论上不应该发生(查询成功但返回零值结构体)
l.Infow("查询到邮箱记录但ID为0将创建新的邮箱用户", logger.Field("email", req.Email))
emailUserId, err = l.createEmailUser(req.Email)
if err != nil {
l.Errorw("创建邮箱用户失败", logger.Field("error", err.Error()))

View File

@ -23,9 +23,8 @@ func (m *customUserModel) FindOneDevice(ctx context.Context, id int64) (*Device,
}
func (m *customUserModel) FindOneDeviceByIdentifier(ctx context.Context, id string) (*Device, error) {
deviceIdKey := fmt.Sprintf("%s%v", cacheUserDeviceNumberPrefix, id)
var resp Device
err := m.QueryCtx(ctx, &resp, deviceIdKey, func(conn *gorm.DB, v interface{}) error {
err := m.QueryNoCacheCtx(ctx, &resp, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Device{}).Where("`identifier` = ?", id).First(&resp).Error
})
switch {