chore: add documentation comments and stylecop rules
This commit is contained in:
@@ -10,8 +10,6 @@ namespace TakeoutSaaS.Application.Sms.Contracts;
|
||||
/// </remarks>
|
||||
public sealed class SendVerificationCodeRequest(string phoneNumber, string scene, SmsProviderKind? provider = null)
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 手机号(支持 +86 前缀或纯 11 位)。
|
||||
/// </summary>
|
||||
|
||||
@@ -8,8 +8,6 @@ namespace TakeoutSaaS.Application.Sms.Contracts;
|
||||
/// </remarks>
|
||||
public sealed class VerifyVerificationCodeRequest(string phoneNumber, string scene, string code)
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 手机号。
|
||||
/// </summary>
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user