39 lines
1.2 KiB
C#
Raw Normal View History

2026-03-31 15:54:27 +08:00
using EasyCaching.Core;
2026-03-30 20:47:40 +08:00
using YD_Event.Application.Common.Dtos;
using YD_Event.Application.Sms;
namespace YD_Event.Application.Common;
[ApiDescriptionSettings("通用服务")]
public class SmsService : IDynamicApiController, ITransient
{
private readonly AliyunSmsService _smsService;
2026-03-31 15:54:27 +08:00
private readonly IEasyCachingProvider _cachingProvider;
2026-03-30 20:47:40 +08:00
2026-03-31 15:54:27 +08:00
public SmsService(AliyunSmsService smsService, IEasyCachingProvider cachingProvider)
2026-03-30 20:47:40 +08:00
{
_smsService = smsService;
2026-03-31 15:54:27 +08:00
_cachingProvider = cachingProvider;
2026-03-30 20:47:40 +08:00
}
[HttpPost("sendVerificationCode")]
2026-03-30 20:47:40 +08:00
[DisplayName("发送短信验证码")]
[AllowAnonymous]
public async Task SendVerificationCode([FromBody] SendVerificationCodeInput input)
{
var limitCacheKey = $"sms:code:limit:{input.Phone}";
2026-03-31 15:54:27 +08:00
if (_cachingProvider.Exists(limitCacheKey))
2026-03-30 20:47:40 +08:00
{
throw Oops.Bah("发送太频繁,请稍后再试");
}
string code = new Random().Next(100000, 999999).ToString();
2026-03-30 20:47:40 +08:00
await _smsService.SendVerificationCode(input.Phone, code);
var codeCacheKey = $"sms:code:{input.Phone}";
2026-03-31 15:54:27 +08:00
_cachingProvider.Set(codeCacheKey, code, TimeSpan.FromMinutes(5));
_cachingProvider.Set(limitCacheKey, "1", TimeSpan.FromMinutes(1));
2026-03-30 20:47:40 +08:00
}
}