|
|
|
@ -0,0 +1,129 @@
|
|
|
|
|
using AlibabaCloud.OpenApiClient.Models;
|
|
|
|
|
using AlibabaCloud.SDK.Dysmsapi20170525;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NPin.Framework.Upms.Domain.Repositories;
|
|
|
|
|
using NPin.Framework.Upms.Domain.Shared.Enums;
|
|
|
|
|
using NPin.Framework.Upms.Domain.Shared.Model;
|
|
|
|
|
using NPin.Framework.Upms.Domain.Shared.Options;
|
|
|
|
|
using TencentCloud.Common;
|
|
|
|
|
using TencentCloud.Common.Profile;
|
|
|
|
|
using TencentCloud.Sms.V20210111;
|
|
|
|
|
using TencentCloud.Sms.V20210111.Models;
|
|
|
|
|
using Volo.Abp.Caching;
|
|
|
|
|
using Volo.Abp.Domain.Services;
|
|
|
|
|
|
|
|
|
|
namespace NPin.Framework.Upms.Domain.Managers;
|
|
|
|
|
|
|
|
|
|
public class SmsManager : DomainService, ISms
|
|
|
|
|
{
|
|
|
|
|
private ILogger<SmsManager> _logger;
|
|
|
|
|
private IConfigRepository _configRepository;
|
|
|
|
|
private IDistributedCache<SmsConfigModel> _cache;
|
|
|
|
|
|
|
|
|
|
public SmsManager(ILogger<SmsManager> logger, IConfigRepository configRepository)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_configRepository = configRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SendSmsAsync(SmsTypeEnum smsType, string phoneNumbers, object templateParam)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var smsSettings = SmsOptions.Config[smsType];
|
|
|
|
|
if (!smsSettings.Enabled)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (smsSettings.Provider)
|
|
|
|
|
{
|
|
|
|
|
case SmsProviderEnum.Aliyun:
|
|
|
|
|
await SendAliyunSmsAsync(smsSettings, phoneNumbers, templateParam);
|
|
|
|
|
break;
|
|
|
|
|
case SmsProviderEnum.Tencent:
|
|
|
|
|
await SendTencentSmsAsync(smsSettings, phoneNumbers, templateParam);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Exception("未实现该服务提供商");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, $"短信发送失败: {ex.Message}");
|
|
|
|
|
throw new UserFriendlyException($"短信发送失败: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SendTencentSmsAsync(SmsSettings settings, string phoneNumbers,
|
|
|
|
|
object templateParam)
|
|
|
|
|
{
|
|
|
|
|
var client = SmsClientProvider.CreateClient(TencentOptions, settings);
|
|
|
|
|
|
|
|
|
|
var sendSmsRequest = new SendSmsRequest
|
|
|
|
|
{
|
|
|
|
|
PhoneNumberSet = phoneNumbers.Split(','),
|
|
|
|
|
SignName = settings.SignName,
|
|
|
|
|
TemplateId = settings.TemplateCode,
|
|
|
|
|
TemplateParamSet = templateParam as string[]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await client.SendSms(sendSmsRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SendAliyunSmsAsync(SmsSettings settings, string phoneNumbers,
|
|
|
|
|
object templateParam)
|
|
|
|
|
{
|
|
|
|
|
var client = SmsClientProvider.CreateClient(AliyunOptions, settings);
|
|
|
|
|
|
|
|
|
|
var sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
|
|
|
|
|
{
|
|
|
|
|
PhoneNumbers = phoneNumbers,
|
|
|
|
|
SignName = settings.SignName,
|
|
|
|
|
TemplateCode = settings.TemplateCode,
|
|
|
|
|
TemplateParam = JsonConvert.SerializeObject(templateParam)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await client.SendSmsAsync(sendSmsRequest);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class SmsClientProvider
|
|
|
|
|
{
|
|
|
|
|
public static Client CreateClient(AliyunOptions options, SmsSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var config = new Config()
|
|
|
|
|
{
|
|
|
|
|
AccessKeyId = options.AccessKeyId,
|
|
|
|
|
AccessKeySecret = options.AccessKeySecret,
|
|
|
|
|
Endpoint = settings.Endpoint,
|
|
|
|
|
RegionId = settings.RegionId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new Client(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SmsClient CreateClient(TencentOptions options, SmsSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var cred = new Credential
|
|
|
|
|
{
|
|
|
|
|
SecretId = options.SecretId,
|
|
|
|
|
SecretKey = options.SecretKey,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var httpProfile = new HttpProfile
|
|
|
|
|
{
|
|
|
|
|
Endpoint = settings.Endpoint,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var clientProfile = new ClientProfile()
|
|
|
|
|
{
|
|
|
|
|
HttpProfile = httpProfile
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new SmsClient(cred, settings.RegionId, clientProfile);
|
|
|
|
|
}
|
|
|
|
|
}
|