refactor(auth): 调整代码格式和注释以提高可读性
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m20s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m20s
style(user): 统一代码缩进和注释格式 fix(user): 修改设备冲突处理逻辑为更新归属而非删除 refactor(user): 移除不必要的用户删除逻辑和缓存清理
This commit is contained in:
parent
9d52826555
commit
e6bd78aa76
@ -15,15 +15,16 @@ import (
|
|||||||
// 负责处理设备与用户的绑定关系,包括新设备创建、设备重新绑定、数据迁移等核心功能
|
// 负责处理设备与用户的绑定关系,包括新设备创建、设备重新绑定、数据迁移等核心功能
|
||||||
// 主要解决设备用户与邮箱用户之间的数据合并问题
|
// 主要解决设备用户与邮箱用户之间的数据合并问题
|
||||||
type BindDeviceLogic struct {
|
type BindDeviceLogic struct {
|
||||||
logger.Logger // 日志记录器,用于记录操作过程和错误信息
|
logger.Logger // 日志记录器,用于记录操作过程和错误信息
|
||||||
ctx context.Context // 上下文,用于传递请求信息和控制超时
|
ctx context.Context // 上下文,用于传递请求信息和控制超时
|
||||||
svcCtx *svc.ServiceContext // 服务上下文,包含数据库连接、配置等依赖
|
svcCtx *svc.ServiceContext // 服务上下文,包含数据库连接、配置等依赖
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBindDeviceLogic 创建设备绑定逻辑处理器实例
|
// NewBindDeviceLogic 创建设备绑定逻辑处理器实例
|
||||||
// 参数:
|
// 参数:
|
||||||
// - ctx: 请求上下文,用于传递请求信息和控制超时
|
// - ctx: 请求上下文,用于传递请求信息和控制超时
|
||||||
// - svcCtx: 服务上下文,包含数据库连接、配置等依赖
|
// - svcCtx: 服务上下文,包含数据库连接、配置等依赖
|
||||||
|
//
|
||||||
// 返回:
|
// 返回:
|
||||||
// - *BindDeviceLogic: 设备绑定逻辑处理器实例
|
// - *BindDeviceLogic: 设备绑定逻辑处理器实例
|
||||||
func NewBindDeviceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindDeviceLogic {
|
func NewBindDeviceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindDeviceLogic {
|
||||||
@ -141,10 +142,10 @@ func (l *BindDeviceLogic) createDeviceForUser(identifier, ip, userAgent string,
|
|||||||
// 第一步:创建设备认证方法记录
|
// 第一步:创建设备认证方法记录
|
||||||
// 在auth_methods表中记录该设备的认证信息
|
// 在auth_methods表中记录该设备的认证信息
|
||||||
authMethod := &user.AuthMethods{
|
authMethod := &user.AuthMethods{
|
||||||
UserId: userId, // 关联的用户ID
|
UserId: userId, // 关联的用户ID
|
||||||
AuthType: "device", // 认证类型为设备认证
|
AuthType: "device", // 认证类型为设备认证
|
||||||
AuthIdentifier: identifier, // 设备标识符
|
AuthIdentifier: identifier, // 设备标识符
|
||||||
Verified: true, // 设备认证默认为已验证状态
|
Verified: true, // 设备认证默认为已验证状态
|
||||||
}
|
}
|
||||||
if err := db.Create(authMethod).Error; err != nil {
|
if err := db.Create(authMethod).Error; err != nil {
|
||||||
l.Errorw("failed to create device auth method",
|
l.Errorw("failed to create device auth method",
|
||||||
@ -158,12 +159,12 @@ func (l *BindDeviceLogic) createDeviceForUser(identifier, ip, userAgent string,
|
|||||||
// 第二步:创建设备信息记录
|
// 第二步:创建设备信息记录
|
||||||
// 在device表中记录设备的详细信息
|
// 在device表中记录设备的详细信息
|
||||||
deviceInfo := &user.Device{
|
deviceInfo := &user.Device{
|
||||||
Ip: ip, // 设备IP地址
|
Ip: ip, // 设备IP地址
|
||||||
UserId: userId, // 关联的用户ID
|
UserId: userId, // 关联的用户ID
|
||||||
UserAgent: userAgent, // 设备User-Agent信息
|
UserAgent: userAgent, // 设备User-Agent信息
|
||||||
Identifier: identifier, // 设备唯一标识符
|
Identifier: identifier, // 设备唯一标识符
|
||||||
Enabled: true, // 设备默认启用状态
|
Enabled: true, // 设备默认启用状态
|
||||||
Online: false, // 设备默认离线状态
|
Online: false, // 设备默认离线状态
|
||||||
}
|
}
|
||||||
if err := db.Create(deviceInfo).Error; err != nil {
|
if err := db.Create(deviceInfo).Error; err != nil {
|
||||||
l.Errorw("failed to create device",
|
l.Errorw("failed to create device",
|
||||||
@ -224,11 +225,12 @@ func (l *BindDeviceLogic) createDeviceForUser(identifier, ip, userAgent string,
|
|||||||
//
|
//
|
||||||
// 核心判断逻辑:
|
// 核心判断逻辑:
|
||||||
// 1. 如果原用户是"纯设备用户"(只有设备认证,无邮箱等其他认证方式):
|
// 1. 如果原用户是"纯设备用户"(只有设备认证,无邮箱等其他认证方式):
|
||||||
// - 执行完整数据迁移(订单、订阅、余额、赠送金额)
|
// - 执行完整数据迁移(订单、订阅、余额、赠送金额)
|
||||||
// - 禁用原用户账户
|
// - 禁用原用户账户
|
||||||
|
//
|
||||||
// 2. 如果原用户有其他认证方式(如邮箱、手机等):
|
// 2. 如果原用户有其他认证方式(如邮箱、手机等):
|
||||||
// - 只转移设备绑定关系
|
// - 只转移设备绑定关系
|
||||||
// - 保留原用户账户和数据
|
// - 保留原用户账户和数据
|
||||||
//
|
//
|
||||||
// 参数:
|
// 参数:
|
||||||
// - deviceInfo: 现有的设备信息记录
|
// - deviceInfo: 现有的设备信息记录
|
||||||
@ -317,9 +319,9 @@ func (l *BindDeviceLogic) rebindDeviceToNewUser(deviceInfo *user.Device, ip, use
|
|||||||
// 第五步:更新设备记录信息
|
// 第五步:更新设备记录信息
|
||||||
// 更新device表中的设备信息,包括用户ID、IP、UserAgent等
|
// 更新device表中的设备信息,包括用户ID、IP、UserAgent等
|
||||||
deviceInfo.UserId = newUserId // 更新设备归属用户
|
deviceInfo.UserId = newUserId // 更新设备归属用户
|
||||||
deviceInfo.Ip = ip // 更新设备IP
|
deviceInfo.Ip = ip // 更新设备IP
|
||||||
deviceInfo.UserAgent = userAgent // 更新设备UserAgent
|
deviceInfo.UserAgent = userAgent // 更新设备UserAgent
|
||||||
deviceInfo.Enabled = true // 确保设备处于启用状态
|
deviceInfo.Enabled = true // 确保设备处于启用状态
|
||||||
|
|
||||||
if err := db.Save(deviceInfo).Error; err != nil {
|
if err := db.Save(deviceInfo).Error; err != nil {
|
||||||
l.Errorw("failed to update device",
|
l.Errorw("failed to update device",
|
||||||
@ -468,7 +470,7 @@ func (l *BindDeviceLogic) migrateUserData(db *gorm.DB, oldUserId, newUserId int6
|
|||||||
// 只有当原用户有余额或赠送金额时才执行更新操作
|
// 只有当原用户有余额或赠送金额时才执行更新操作
|
||||||
if oldUser.Balance > 0 || oldUser.GiftAmount > 0 {
|
if oldUser.Balance > 0 || oldUser.GiftAmount > 0 {
|
||||||
if err := db.Model(&user.User{}).Where("id = ?", newUserId).Updates(map[string]interface{}{
|
if err := db.Model(&user.User{}).Where("id = ?", newUserId).Updates(map[string]interface{}{
|
||||||
"balance": gorm.Expr("balance + ?", oldUser.Balance), // 累加余额
|
"balance": gorm.Expr("balance + ?", oldUser.Balance), // 累加余额
|
||||||
"gift_amount": gorm.Expr("gift_amount + ?", oldUser.GiftAmount), // 累加赠送金额
|
"gift_amount": gorm.Expr("gift_amount + ?", oldUser.GiftAmount), // 累加赠送金额
|
||||||
}).Error; err != nil {
|
}).Error; err != nil {
|
||||||
l.Errorw("failed to migrate user balance and gift",
|
l.Errorw("failed to migrate user balance and gift",
|
||||||
|
|||||||
@ -119,7 +119,8 @@ func (l *BindEmailWithVerificationLogic) transferDeviceToEmailUser(deviceUserId,
|
|||||||
logger.Field("email_user_id", emailUserId),
|
logger.Field("email_user_id", emailUserId),
|
||||||
logger.Field("device_identifier", deviceIdentifier))
|
logger.Field("device_identifier", deviceIdentifier))
|
||||||
|
|
||||||
// 1. 先获取当前用户的SessionId,用于后续清理
|
// 1. 先获取当前用户的SessionId,用于后续清理 可以不需要
|
||||||
|
|
||||||
currentSessionId := ""
|
currentSessionId := ""
|
||||||
if sessionIdValue := l.ctx.Value(constant.CtxKeySessionID); sessionIdValue != nil {
|
if sessionIdValue := l.ctx.Value(constant.CtxKeySessionID); sessionIdValue != nil {
|
||||||
currentSessionId = sessionIdValue.(string)
|
currentSessionId = sessionIdValue.(string)
|
||||||
@ -191,11 +192,11 @@ func (l *BindEmailWithVerificationLogic) transferDeviceToEmailUser(deviceUserId,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. 处理设备冲突 - 删除目标用户的现有设备记录(如果存在)
|
// 6. 处理设备冲突 - 将现有设备记录的归属修改为邮箱用户
|
||||||
if existingDevice != nil && existingDevice.UserId != emailUserId {
|
if existingDevice != nil && existingDevice.UserId != emailUserId {
|
||||||
l.Infow("删除冲突的设备记录", logger.Field("existing_device_id", existingDevice.Id), logger.Field("existing_user_id", existingDevice.UserId))
|
l.Infow("更新冲突设备记录的归属", logger.Field("existing_device_id", existingDevice.Id), logger.Field("old_user_id", existingDevice.UserId), logger.Field("new_user_id", emailUserId))
|
||||||
if err := db.Where("identifier = ? AND user_id = ?", deviceIdentifier, existingDevice.UserId).Delete(&user.Device{}).Error; err != nil {
|
if err := db.Model(&user.Device{}).Where("identifier = ? AND user_id = ?", deviceIdentifier, existingDevice.UserId).Update("user_id", emailUserId).Error; err != nil {
|
||||||
l.Errorw("删除冲突设备记录失败", logger.Field("error", err.Error()))
|
l.Errorw("更新冲突设备记录归属失败", logger.Field("error", err.Error()))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -216,35 +217,6 @@ func (l *BindEmailWithVerificationLogic) transferDeviceToEmailUser(deviceUserId,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. 检查原始设备用户是否还有其他认证方式,如果没有则删除该用户
|
|
||||||
var remainingAuthMethods []user.AuthMethods
|
|
||||||
if err := db.Where("user_id = ?", deviceUserId).Find(&remainingAuthMethods).Error; err != nil {
|
|
||||||
l.Errorw("查询原始用户剩余认证方式失败", logger.Field("error", err.Error()), logger.Field("device_user_id", deviceUserId))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(remainingAuthMethods) == 0 {
|
|
||||||
// 获取原始用户信息用于清除缓存
|
|
||||||
deviceUser, _ := l.svcCtx.UserModel.FindOne(l.ctx, deviceUserId)
|
|
||||||
|
|
||||||
// 原始用户没有其他认证方式,可以安全删除
|
|
||||||
if err := db.Where("id = ?", deviceUserId).Delete(&user.User{}).Error; err != nil {
|
|
||||||
l.Errorw("删除原始设备用户失败", logger.Field("error", err.Error()), logger.Field("device_user_id", deviceUserId))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 清除已删除用户的缓存
|
|
||||||
if deviceUser != nil {
|
|
||||||
l.svcCtx.UserModel.ClearUserCache(l.ctx, deviceUser)
|
|
||||||
}
|
|
||||||
|
|
||||||
l.Infow("已删除原始设备用户", logger.Field("device_user_id", deviceUserId))
|
|
||||||
} else {
|
|
||||||
l.Infow("原始用户还有其他认证方式,保留用户记录",
|
|
||||||
logger.Field("device_user_id", deviceUserId),
|
|
||||||
logger.Field("remaining_auth_count", len(remainingAuthMethods)))
|
|
||||||
}
|
|
||||||
|
|
||||||
l.Infow("设备转移成功",
|
l.Infow("设备转移成功",
|
||||||
logger.Field("device_user_id", deviceUserId),
|
logger.Field("device_user_id", deviceUserId),
|
||||||
logger.Field("email_user_id", emailUserId),
|
logger.Field("email_user_id", emailUserId),
|
||||||
@ -284,7 +256,7 @@ func (l *BindEmailWithVerificationLogic) transferDeviceToEmailUser(deviceUserId,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 6. 清除设备相关缓存
|
// 6. 清除设备相关缓存
|
||||||
l.clearDeviceRelatedCache(deviceIdentifier, deviceUserId, emailUserId)
|
// l.clearDeviceRelatedCache(deviceIdentifier, deviceUserId, emailUserId)
|
||||||
|
|
||||||
return &types.BindEmailWithVerificationResponse{
|
return &types.BindEmailWithVerificationResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user