refactor(auth): 调整代码格式和注释以提高可读性
Build docker and publish / build (20.15.1) (push) Successful in 7m20s
Build docker and publish / build (20.15.1) (push) Successful in 7m20s
style(user): 统一代码缩进和注释格式 fix(user): 修改设备冲突处理逻辑为更新归属而非删除 refactor(user): 移除不必要的用户删除逻辑和缓存清理
This commit is contained in:
@@ -15,15 +15,16 @@ import (
|
||||
// 负责处理设备与用户的绑定关系,包括新设备创建、设备重新绑定、数据迁移等核心功能
|
||||
// 主要解决设备用户与邮箱用户之间的数据合并问题
|
||||
type BindDeviceLogic struct {
|
||||
logger.Logger // 日志记录器,用于记录操作过程和错误信息
|
||||
ctx context.Context // 上下文,用于传递请求信息和控制超时
|
||||
svcCtx *svc.ServiceContext // 服务上下文,包含数据库连接、配置等依赖
|
||||
logger.Logger // 日志记录器,用于记录操作过程和错误信息
|
||||
ctx context.Context // 上下文,用于传递请求信息和控制超时
|
||||
svcCtx *svc.ServiceContext // 服务上下文,包含数据库连接、配置等依赖
|
||||
}
|
||||
|
||||
// NewBindDeviceLogic 创建设备绑定逻辑处理器实例
|
||||
// 参数:
|
||||
// - ctx: 请求上下文,用于传递请求信息和控制超时
|
||||
// - svcCtx: 服务上下文,包含数据库连接、配置等依赖
|
||||
//
|
||||
// 返回:
|
||||
// - *BindDeviceLogic: 设备绑定逻辑处理器实例
|
||||
func NewBindDeviceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindDeviceLogic {
|
||||
@@ -141,10 +142,10 @@ func (l *BindDeviceLogic) createDeviceForUser(identifier, ip, userAgent string,
|
||||
// 第一步:创建设备认证方法记录
|
||||
// 在auth_methods表中记录该设备的认证信息
|
||||
authMethod := &user.AuthMethods{
|
||||
UserId: userId, // 关联的用户ID
|
||||
AuthType: "device", // 认证类型为设备认证
|
||||
AuthIdentifier: identifier, // 设备标识符
|
||||
Verified: true, // 设备认证默认为已验证状态
|
||||
UserId: userId, // 关联的用户ID
|
||||
AuthType: "device", // 认证类型为设备认证
|
||||
AuthIdentifier: identifier, // 设备标识符
|
||||
Verified: true, // 设备认证默认为已验证状态
|
||||
}
|
||||
if err := db.Create(authMethod).Error; err != nil {
|
||||
l.Errorw("failed to create device auth method",
|
||||
@@ -158,12 +159,12 @@ func (l *BindDeviceLogic) createDeviceForUser(identifier, ip, userAgent string,
|
||||
// 第二步:创建设备信息记录
|
||||
// 在device表中记录设备的详细信息
|
||||
deviceInfo := &user.Device{
|
||||
Ip: ip, // 设备IP地址
|
||||
UserId: userId, // 关联的用户ID
|
||||
UserAgent: userAgent, // 设备User-Agent信息
|
||||
Identifier: identifier, // 设备唯一标识符
|
||||
Enabled: true, // 设备默认启用状态
|
||||
Online: false, // 设备默认离线状态
|
||||
Ip: ip, // 设备IP地址
|
||||
UserId: userId, // 关联的用户ID
|
||||
UserAgent: userAgent, // 设备User-Agent信息
|
||||
Identifier: identifier, // 设备唯一标识符
|
||||
Enabled: true, // 设备默认启用状态
|
||||
Online: false, // 设备默认离线状态
|
||||
}
|
||||
if err := db.Create(deviceInfo).Error; err != nil {
|
||||
l.Errorw("failed to create device",
|
||||
@@ -224,11 +225,12 @@ func (l *BindDeviceLogic) createDeviceForUser(identifier, ip, userAgent string,
|
||||
//
|
||||
// 核心判断逻辑:
|
||||
// 1. 如果原用户是"纯设备用户"(只有设备认证,无邮箱等其他认证方式):
|
||||
// - 执行完整数据迁移(订单、订阅、余额、赠送金额)
|
||||
// - 禁用原用户账户
|
||||
// - 执行完整数据迁移(订单、订阅、余额、赠送金额)
|
||||
// - 禁用原用户账户
|
||||
//
|
||||
// 2. 如果原用户有其他认证方式(如邮箱、手机等):
|
||||
// - 只转移设备绑定关系
|
||||
// - 保留原用户账户和数据
|
||||
// - 只转移设备绑定关系
|
||||
// - 保留原用户账户和数据
|
||||
//
|
||||
// 参数:
|
||||
// - deviceInfo: 现有的设备信息记录
|
||||
@@ -268,7 +270,7 @@ func (l *BindDeviceLogic) rebindDeviceToNewUser(deviceInfo *user.Device, ip, use
|
||||
// 第三步:根据用户类型执行不同的处理逻辑
|
||||
if nonDeviceAuthCount == 0 {
|
||||
// 原用户是"纯设备用户",执行完整的数据迁移和用户禁用
|
||||
|
||||
|
||||
// 3.1 先执行数据迁移(订单、订阅、余额等)
|
||||
if err := l.migrateUserData(db, oldUserId, newUserId); err != nil {
|
||||
l.Errorw("failed to migrate user data",
|
||||
@@ -317,9 +319,9 @@ func (l *BindDeviceLogic) rebindDeviceToNewUser(deviceInfo *user.Device, ip, use
|
||||
// 第五步:更新设备记录信息
|
||||
// 更新device表中的设备信息,包括用户ID、IP、UserAgent等
|
||||
deviceInfo.UserId = newUserId // 更新设备归属用户
|
||||
deviceInfo.Ip = ip // 更新设备IP
|
||||
deviceInfo.Ip = ip // 更新设备IP
|
||||
deviceInfo.UserAgent = userAgent // 更新设备UserAgent
|
||||
deviceInfo.Enabled = true // 确保设备处于启用状态
|
||||
deviceInfo.Enabled = true // 确保设备处于启用状态
|
||||
|
||||
if err := db.Save(deviceInfo).Error; err != nil {
|
||||
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 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), // 累加赠送金额
|
||||
}).Error; err != nil {
|
||||
l.Errorw("failed to migrate user balance and gift",
|
||||
|
||||
Reference in New Issue
Block a user