60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 发送验证码服务
|
|
/// </summary>
|
|
public class SMSBLL : ChouBoxCodeBase
|
|
{
|
|
public SMSBLL(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
{
|
|
}
|
|
/// <summary>
|
|
/// 发送验证码
|
|
/// </summary>
|
|
/// <param name="phone"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
/// <exception cref="Exception"></exception>
|
|
public async Task<string> 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<string>(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 "验证码已发送";
|
|
}
|
|
}
|