This commit is contained in:
zpc 2026-02-07 17:59:39 +08:00
parent 0fc9774d29
commit d55ac149d4
3 changed files with 38 additions and 6 deletions

View File

@ -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)
};

View File

@ -65,6 +65,37 @@ public class UserService : BaseService<User, int>, 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<User, int>, 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<User, int>, 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;
}

View File

@ -31,9 +31,9 @@ public class CreateUserDto
public string? Headimg { get; set; }
/// <summary>
/// 推荐人ID
/// 推荐人ID或UID
/// </summary>
public int Pid { get; set; }
public string? Pid { get; set; }
/// <summary>
/// 点击ID