diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/AuthService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/AuthService.cs index 5984e6d4..4b662b16 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/AuthService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/AuthService.cs @@ -134,7 +134,7 @@ public class AuthService : IAuthService UnionId = unionId, Nickname = $"用户{Random.Shared.Next(100000, 999999)}", Headimg = GenerateDefaultAvatar(openId), - Pid = pid ?? 0, + Pid = pid?.ToString(), ClickId = ParseClickId(clickId) }; @@ -252,7 +252,7 @@ public class AuthService : IAuthService Mobile = mobile, Nickname = $"用户{Random.Shared.Next(100000, 999999)}", Headimg = GenerateDefaultAvatar(mobile), - Pid = pid ?? 0, + Pid = pid?.ToString(), ClickId = ParseClickId(clickId) }; diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs index 33202330..f1bd66cf 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs @@ -65,6 +65,37 @@ public class UserService : BaseService, IUserService // Generate UID based on config var uid = await GenerateUidAsync(); + // 处理推荐人ID - 支持通过 ID 或 uid 查找 + var pid = 0; + if (!string.IsNullOrWhiteSpace(dto.Pid)) + { + // 先尝试作为用户ID查找(如果是数字) + if (int.TryParse(dto.Pid, out var pidInt) && pidInt > 0) + { + var inviterById = await _dbSet.FirstOrDefaultAsync(u => u.Id == pidInt); + if (inviterById != null) + { + pid = inviterById.Id; + _logger.LogInformation("通过用户ID找到推荐人: Pid={Pid}", pid); + } + } + + // 如果还没找到,尝试作为 uid 查找 + if (pid == 0) + { + var inviterByUid = await _dbSet.FirstOrDefaultAsync(u => u.Uid == dto.Pid); + if (inviterByUid != null) + { + pid = inviterByUid.Id; + _logger.LogInformation("通过UID找到推荐人: Uid={Uid}, Pid={Pid}", dto.Pid, pid); + } + else + { + _logger.LogWarning("未找到推荐人: Pid={Pid}", dto.Pid); + } + } + } + var user = new User { OpenId = dto.OpenId ?? string.Empty, @@ -73,7 +104,7 @@ public class UserService : BaseService, IUserService Uid = uid, Nickname = dto.Nickname ?? $"User{Random.Shared.Next(1000, 9999)}", HeadImg = dto.Headimg ?? string.Empty, - Pid = dto.Pid, + Pid = pid, ClickId = dto.ClickId, Money = 0, Money2 = 0, @@ -89,7 +120,8 @@ public class UserService : BaseService, IUserService await _dbSet.AddAsync(user); await _dbContext.SaveChangesAsync(); - _logger.LogInformation($"User created: Id={user.Id}, Uid={user.Uid}, OpenId={user.OpenId}"); + _logger.LogInformation("User created: Id={UserId}, Uid={Uid}, OpenId={OpenId}, Pid={Pid}", + user.Id, user.Uid, user.OpenId, user.Pid); return user; } diff --git a/server/HoneyBox/src/HoneyBox.Model/Models/Auth/CreateUserDto.cs b/server/HoneyBox/src/HoneyBox.Model/Models/Auth/CreateUserDto.cs index d88996fc..935d9b10 100644 --- a/server/HoneyBox/src/HoneyBox.Model/Models/Auth/CreateUserDto.cs +++ b/server/HoneyBox/src/HoneyBox.Model/Models/Auth/CreateUserDto.cs @@ -31,9 +31,9 @@ public class CreateUserDto public string? Headimg { get; set; } /// - /// 推荐人ID + /// 推荐人ID或UID /// - public int Pid { get; set; } + public string? Pid { get; set; } /// /// 点击ID