39 lines
1.2 KiB
C#

using EasyCaching.Core;
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;
private readonly IEasyCachingProvider _cachingProvider;
public SmsService(AliyunSmsService smsService, IEasyCachingProvider cachingProvider)
{
_smsService = smsService;
_cachingProvider = cachingProvider;
}
[HttpPost("sendVerificationCode")]
[DisplayName("发送短信验证码")]
[AllowAnonymous]
public async Task SendVerificationCode([FromBody] SendVerificationCodeInput input)
{
var limitCacheKey = $"sms:code:limit:{input.Phone}";
if (_cachingProvider.Exists(limitCacheKey))
{
throw Oops.Bah("发送太频繁,请稍后再试");
}
string code = new Random().Next(100000, 999999).ToString();
await _smsService.SendVerificationCode(input.Phone, code);
var codeCacheKey = $"sms:code:{input.Phone}";
_cachingProvider.Set(codeCacheKey, code, TimeSpan.FromMinutes(5));
_cachingProvider.Set(limitCacheKey, "1", TimeSpan.FromMinutes(1));
}
}