diff --git a/server/HoneyBox/src/HoneyBox.Admin.Business/Services/AdminConfigService.cs b/server/HoneyBox/src/HoneyBox.Admin.Business/Services/AdminConfigService.cs index 80f47122..135c5c6e 100644 --- a/server/HoneyBox/src/HoneyBox.Admin.Business/Services/AdminConfigService.cs +++ b/server/HoneyBox/src/HoneyBox.Admin.Business/Services/AdminConfigService.cs @@ -119,6 +119,11 @@ public class AdminConfigService : IAdminConfigService if (existingConfig != null) { + // 如果值相同,直接返回成功(无需更新) + if (existingConfig.ConfigValue == jsonValue) + { + return true; + } // 更新现有配置 existingConfig.ConfigValue = jsonValue; } diff --git a/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs b/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs index 30670c77..049ef8eb 100644 --- a/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs +++ b/server/HoneyBox/src/HoneyBox.Core/Services/UserService.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using HoneyBox.Core.Interfaces; using HoneyBox.Model.Data; using HoneyBox.Model.Entities; @@ -13,11 +14,13 @@ namespace HoneyBox.Core.Services; public class UserService : BaseService, IUserService { private readonly ILogger _logger; + private readonly IConfigService _configService; - public UserService(HoneyBoxDbContext dbContext, ILogger logger) + public UserService(HoneyBoxDbContext dbContext, ILogger logger, IConfigService configService) : base(dbContext) { _logger = logger; + _configService = configService; } /// @@ -59,8 +62,8 @@ public class UserService : BaseService, IUserService if (dto == null) throw new ArgumentNullException(nameof(dto)); - // Generate UID - var uid = GenerateUid(); + // Generate UID based on config + var uid = await GenerateUidAsync(); var user = new User { @@ -273,12 +276,121 @@ public class UserService : BaseService, IUserService /// /// Generate a unique UID for new user /// - private string GenerateUid() + private async Task 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}"; + // 读取用户配置 + var userConfigJson = await _configService.GetConfigValueAsync("user_config"); + + int uidType = 2; // 默认数字ID + int uidLength = 6; // 默认6位 + + if (!string.IsNullOrEmpty(userConfigJson)) + { + try + { + var userConfig = JsonSerializer.Deserialize>(userConfigJson, + new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); + + if (userConfig != null) + { + if (userConfig.TryGetValue("uid_type", out var uidTypeStr) && int.TryParse(uidTypeStr, out var parsedType)) + { + uidType = parsedType; + } + if (userConfig.TryGetValue("uid_length", out var uidLengthStr) && int.TryParse(uidLengthStr, out var parsedLength)) + { + uidLength = parsedLength; + } + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to parse user_config, using defaults"); + } + } + + // 根据配置生成UID + string uid; + int maxAttempts = 100; + int attempt = 0; + + do + { + uid = uidType switch + { + 1 => await GenerateRealIdUidAsync(), // 真实ID(基于数据库自增) + 2 => GenerateNumericUid(uidLength), // 数字ID + 3 => GenerateAlphanumericUid(uidLength), // 随机字符和数字 + _ => GenerateNumericUid(uidLength) + }; + attempt++; + } + while (await UidExistsAsync(uid) && attempt < maxAttempts); + + if (attempt >= maxAttempts) + { + // 如果多次尝试都重复,使用时间戳+随机数作为后备方案 + uid = $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}{Random.Shared.Next(100, 999)}"; + } + + return uid; + } + + /// + /// 检查UID是否已存在 + /// + private async Task UidExistsAsync(string uid) + { + return await _dbSet.AnyAsync(u => u.Uid == uid); + } + + /// + /// 生成真实ID类型的UID(基于当前最大用户ID) + /// + private async Task GenerateRealIdUidAsync() + { + var maxId = await _dbSet.MaxAsync(u => (int?)u.Id) ?? 0; + return (maxId + 1).ToString(); + } + + /// + /// 生成纯数字UID + /// + private static string GenerateNumericUid(int length) + { + if (length < 4) length = 4; + if (length > 12) length = 12; + + // 第一位不能是0 + var firstDigit = Random.Shared.Next(1, 10).ToString(); + var remainingLength = length - 1; + + var remaining = new char[remainingLength]; + for (int i = 0; i < remainingLength; i++) + { + remaining[i] = (char)('0' + Random.Shared.Next(0, 10)); + } + + return firstDigit + new string(remaining); + } + + /// + /// 生成字母数字混合UID + /// + private static string GenerateAlphanumericUid(int length) + { + if (length < 4) length = 4; + if (length > 16) length = 16; + + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + var result = new char[length]; + + for (int i = 0; i < length; i++) + { + result[i] = chars[Random.Shared.Next(chars.Length)]; + } + + return new string(result); } ///