fix(auth): 修复新用户注册Uid超长导致截断错误
- GenerateUid生成的是timestamp+random(14位),超过数据库nvarchar(6)限制 - 改为生成6位随机数字,并查库确保唯一性
This commit is contained in:
parent
21e8ff5372
commit
26b471d691
|
|
@ -59,8 +59,8 @@ public class UserService : BaseService<User, int>, IUserService
|
|||
if (dto == null)
|
||||
throw new ArgumentNullException(nameof(dto));
|
||||
|
||||
// Generate UID
|
||||
var uid = GenerateUid();
|
||||
// 生成唯一UID
|
||||
var uid = await GenerateUidAsync();
|
||||
|
||||
var user = new User
|
||||
{
|
||||
|
|
@ -155,14 +155,20 @@ public class UserService : BaseService<User, int>, IUserService
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a unique UID for new user
|
||||
/// 生成6位唯一用户UID
|
||||
/// </summary>
|
||||
private string GenerateUid()
|
||||
private async Task<string> GenerateUidAsync()
|
||||
{
|
||||
// Generate a numeric UID based on timestamp and random number
|
||||
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
var random = Random.Shared.Next(1000, 9999);
|
||||
return $"{timestamp}{random}";
|
||||
const int maxAttempts = 10;
|
||||
for (var i = 0; i < maxAttempts; i++)
|
||||
{
|
||||
var uid = Random.Shared.Next(100000, 999999).ToString();
|
||||
var exists = await _dbSet.AnyAsync(u => u.Uid == uid);
|
||||
if (!exists)
|
||||
return uid;
|
||||
}
|
||||
// 兜底:使用时间戳后6位
|
||||
return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()[^6..];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user