using AutoMapper;
using ChouBox.Code.AppExtend;
using ChouBox.Code.TencentCloudExtend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChouBox.Code.Other;
///
/// 发送验证码服务
///
public class SMSBLL : ChouBoxCodeBase
{
public SMSBLL(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
///
/// 发送验证码
///
///
///
///
///
public async Task SendPhoneAsync(string phone)
{
if (string.IsNullOrEmpty(phone))
{
throw new ArgumentNullException("手机号不能为空");
}
var smsConfig = await this.GetTencentSMSConfigAsync();
if (smsConfig == null)
{
throw new ArgumentNullException("暂未开放发送验证码!");
}
Random random = new Random();
var verificationCode = random.Next(1000, 9999);
var redisKey = $"VerificationCode:{phone}";
var redisVerificationCode = RedisCache.StringGet(redisKey);
if (redisVerificationCode != null && !string.IsNullOrEmpty(redisVerificationCode))
{
throw new Exception("验证码已发送!");
}
TencentSMSSendVerificationCode tencentSMSSendVerificationCode = new TencentSMSSendVerificationCode(smsConfig, phone);
var result = await tencentSMSSendVerificationCode.SendVerificationCode(verificationCode.ToString(), 5);
if (!result)
{
throw new Exception("验证码发送失败");
}
await RedisCache.StringSetAsync(redisKey, verificationCode.ToString(), TimeSpan.FromMinutes(5));
return "验证码已发送";
}
}