|
|
|
@ -1,129 +1,78 @@
|
|
|
|
|
using AlibabaCloud.OpenApiClient.Models;
|
|
|
|
|
using AlibabaCloud.SDK.Dysmsapi20170525;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NPin.Framework.Upms.Domain.Repositories;
|
|
|
|
|
using NPin.Framework.SettingManagement.Domain;
|
|
|
|
|
using NPin.Framework.Upms.Domain.Settings;
|
|
|
|
|
using NPin.Framework.Upms.Domain.Shared.Consts;
|
|
|
|
|
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 NPin.Framework.Upms.Domain.Sms;
|
|
|
|
|
using Volo.Abp.Domain.Services;
|
|
|
|
|
|
|
|
|
|
namespace NPin.Framework.Upms.Domain.Managers;
|
|
|
|
|
|
|
|
|
|
public class SmsManager : DomainService, ISms
|
|
|
|
|
public class SmsManager : DomainService
|
|
|
|
|
{
|
|
|
|
|
private ILogger<SmsManager> _logger;
|
|
|
|
|
private IConfigRepository _configRepository;
|
|
|
|
|
private IDistributedCache<SmsConfigModel> _cache;
|
|
|
|
|
private readonly ILogger<SmsManager> _logger;
|
|
|
|
|
|
|
|
|
|
public SmsManager(ILogger<SmsManager> logger, IConfigRepository configRepository)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_configRepository = configRepository;
|
|
|
|
|
}
|
|
|
|
|
private readonly ISettingManager _settingManager;
|
|
|
|
|
|
|
|
|
|
public async Task SendSmsAsync(SmsTypeEnum smsType, string phoneNumbers, object templateParam)
|
|
|
|
|
{
|
|
|
|
|
private readonly IEnumerable<ISms> _smsProviders;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var smsSettings = SmsOptions.Config[smsType];
|
|
|
|
|
if (!smsSettings.Enabled)
|
|
|
|
|
public SmsManager(ILogger<SmsManager> logger, ISettingManager settingManager, IEnumerable<ISms> smsProviders)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_settingManager = settingManager;
|
|
|
|
|
_smsProviders = smsProviders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (smsSettings.Provider)
|
|
|
|
|
public async Task<SmsConfigModel> GetSmsConfigModelAsync()
|
|
|
|
|
{
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var configJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsConfigModelKey);
|
|
|
|
|
var settingsJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsSettingsKey);
|
|
|
|
|
var providersJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsProvidersKey);
|
|
|
|
|
var templateJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsTemplatesKey);
|
|
|
|
|
|
|
|
|
|
private async Task SendTencentSmsAsync(SmsSettings settings, string phoneNumbers,
|
|
|
|
|
object templateParam)
|
|
|
|
|
{
|
|
|
|
|
var client = SmsClientProvider.CreateClient(TencentOptions, settings);
|
|
|
|
|
var ret = JsonConvert.DeserializeObject<SmsConfigModel>(configJson ?? string.Empty) ?? new SmsConfigModel();
|
|
|
|
|
|
|
|
|
|
var sendSmsRequest = new SendSmsRequest
|
|
|
|
|
{
|
|
|
|
|
PhoneNumberSet = phoneNumbers.Split(','),
|
|
|
|
|
SignName = settings.SignName,
|
|
|
|
|
TemplateId = settings.TemplateCode,
|
|
|
|
|
TemplateParamSet = templateParam as string[]
|
|
|
|
|
};
|
|
|
|
|
ret.Settings =
|
|
|
|
|
JsonConvert.DeserializeObject<Dictionary<SmsTypeEnum, SmsSettings>>(settingsJson ?? string.Empty) ??
|
|
|
|
|
new Dictionary<SmsTypeEnum, SmsSettings>();
|
|
|
|
|
|
|
|
|
|
var response = await client.SendSms(sendSmsRequest);
|
|
|
|
|
}
|
|
|
|
|
ret.Providers = JsonConvert.DeserializeObject<List<SmsProvider>>(providersJson ?? string.Empty) ?? [];
|
|
|
|
|
ret.Templates = JsonConvert.DeserializeObject<List<SmsTemplate>>(templateJson ?? string.Empty) ?? [];
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class SmsClientProvider
|
|
|
|
|
public async Task SendSmsAsync(SmsTypeEnum smsType, string phoneNumbers, object templateParam)
|
|
|
|
|
{
|
|
|
|
|
public static Client CreateClient(AliyunOptions options, SmsSettings settings)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var config = new Config()
|
|
|
|
|
var smsConfig = await GetSmsConfigModelAsync();
|
|
|
|
|
if (!smsConfig.Enabled)
|
|
|
|
|
{
|
|
|
|
|
AccessKeyId = options.AccessKeyId,
|
|
|
|
|
AccessKeySecret = options.AccessKeySecret,
|
|
|
|
|
Endpoint = settings.Endpoint,
|
|
|
|
|
RegionId = settings.RegionId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new Client(config);
|
|
|
|
|
throw new UserFriendlyException("短信服务未启用,无法发送短信。");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SmsClient CreateClient(TencentOptions options, SmsSettings settings)
|
|
|
|
|
{
|
|
|
|
|
var cred = new Credential
|
|
|
|
|
var settings = smsConfig.GetSetting(smsType);
|
|
|
|
|
if (settings is null)
|
|
|
|
|
{
|
|
|
|
|
SecretId = options.SecretId,
|
|
|
|
|
SecretKey = options.SecretKey,
|
|
|
|
|
};
|
|
|
|
|
throw new UserFriendlyException("未找到对应短信类型,无法发送短信。");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var httpProfile = new HttpProfile
|
|
|
|
|
var providerConfig = smsConfig.GetProvider(settings);
|
|
|
|
|
var provider = _smsProviders.FirstOrDefault(i => i.ProviderType == providerConfig.ProviderType);
|
|
|
|
|
if (provider is null)
|
|
|
|
|
{
|
|
|
|
|
Endpoint = settings.Endpoint,
|
|
|
|
|
};
|
|
|
|
|
throw new NotImplementedException("未实现该方式的短信控制器");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clientProfile = new ClientProfile()
|
|
|
|
|
await provider.SendSmsAsync(smsConfig, settings, phoneNumbers, templateParam);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
HttpProfile = httpProfile
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new SmsClient(cred, settings.RegionId, clientProfile);
|
|
|
|
|
_logger.LogError(ex, $"短信发送失败: {ex.Message}");
|
|
|
|
|
throw new UserFriendlyException($"短信发送失败: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|