chore: add documentation comments and stylecop rules

This commit is contained in:
2025-12-04 11:25:01 +08:00
parent 17d143a351
commit 8e4c2b0e45
142 changed files with 1309 additions and 439 deletions

View File

@@ -30,6 +30,7 @@ public sealed class VerificationCodeService(
/// <inheritdoc />
public async Task<SendVerificationCodeResponse> SendAsync(SendVerificationCodeRequest request, CancellationToken cancellationToken = default)
{
// 1. 参数校验
if (string.IsNullOrWhiteSpace(request.PhoneNumber))
{
throw new BusinessException(ErrorCodes.BadRequest, "手机号不能为空");
@@ -40,6 +41,7 @@ public sealed class VerificationCodeService(
throw new BusinessException(ErrorCodes.BadRequest, "场景不能为空");
}
// 2. 解析模板与缓存键
var smsOptions = smsOptionsMonitor.CurrentValue;
var codeOptions = codeOptionsMonitor.CurrentValue;
var templateCode = ResolveTemplate(request.Scene, smsOptions);
@@ -48,8 +50,10 @@ public sealed class VerificationCodeService(
var cacheKey = $"{codeOptions.CachePrefix}:{tenantKey}:{request.Scene}:{phone}";
var cooldownKey = $"{cacheKey}:cooldown";
// 3. 检查冷却期
await EnsureCooldownAsync(cooldownKey, codeOptions.CooldownSeconds, cancellationToken).ConfigureAwait(false);
// 4. 生成验证码并发送短信
var code = GenerateCode(codeOptions.CodeLength);
var variables = new Dictionary<string, string> { { "code", code } };
var sender = senderResolver.Resolve(request.Provider);
@@ -61,6 +65,7 @@ public sealed class VerificationCodeService(
throw new BusinessException(ErrorCodes.InternalServerError, $"短信发送失败:{smsResult.Message}");
}
// 5. 写入验证码与冷却缓存
var expiresAt = DateTimeOffset.UtcNow.AddMinutes(codeOptions.ExpireMinutes);
await cache.SetStringAsync(cacheKey, code, new DistributedCacheEntryOptions
{
@@ -83,11 +88,13 @@ public sealed class VerificationCodeService(
/// <inheritdoc />
public async Task<bool> VerifyAsync(VerifyVerificationCodeRequest request, CancellationToken cancellationToken = default)
{
// 1. 基础校验
if (string.IsNullOrWhiteSpace(request.Code))
{
return false;
}
// 2. 读取验证码
var codeOptions = codeOptionsMonitor.CurrentValue;
var phone = NormalizePhoneNumber(request.PhoneNumber);
var tenantKey = tenantProvider.GetCurrentTenantId() == 0 ? "platform" : tenantProvider.GetCurrentTenantId().ToString();
@@ -99,6 +106,7 @@ public sealed class VerificationCodeService(
return false;
}
// 3. 比对成功后清除缓存
var success = string.Equals(cachedCode, request.Code, StringComparison.Ordinal);
if (success)
{