feat:区分配置文件
This commit is contained in:
@@ -4,12 +4,24 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AlibabaCloud.SDK.Dysmsapi20170525;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Tea;
|
||||
|
||||
namespace Yi.Framework.Sms.Aliyun
|
||||
{
|
||||
public class SmsAliyunManager
|
||||
{
|
||||
public Client AliyunClient { get; set; }
|
||||
private ILogger<SmsAliyunManager> _logger;
|
||||
private SmsAliyunOptions Options { get; set; }
|
||||
public SmsAliyunManager(ILogger<SmsAliyunManager> logger,IOptions<SmsAliyunOptions> options)
|
||||
{
|
||||
_logger = logger;
|
||||
AliyunClient = CreateClient(Options.AccessKeyId, Options.AccessKeySecret);
|
||||
Options = options.Value;
|
||||
}
|
||||
|
||||
private static Client CreateClient(string accessKeyId, string accessKeySecret)
|
||||
{
|
||||
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
|
||||
@@ -24,12 +36,13 @@ namespace Yi.Framework.Sms.Aliyun
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
public Client AliyunClient { get; set; }
|
||||
public SmsAliyunManager() {
|
||||
|
||||
AliyunClient = CreateClient("accessKeyId", "accessKeySecret");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送短信
|
||||
/// </summary>
|
||||
/// <param name="phoneNumbers"></param>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Send(string phoneNumbers, string code)
|
||||
{
|
||||
try
|
||||
@@ -37,21 +50,17 @@ namespace Yi.Framework.Sms.Aliyun
|
||||
AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
|
||||
{
|
||||
PhoneNumbers = phoneNumbers,
|
||||
SignName = "",
|
||||
SignName = Options.SignName,
|
||||
TemplateCode = code,
|
||||
};
|
||||
|
||||
var response= await AliyunClient.SendSmsAsync(sendSmsRequest);
|
||||
var response = await AliyunClient.SendSmsAsync(sendSmsRequest);
|
||||
}
|
||||
catch (TeaException error)
|
||||
{
|
||||
|
||||
Console.WriteLine(error.Message);
|
||||
}
|
||||
catch (Exception _error)
|
||||
{
|
||||
Console.WriteLine(_error.Message);
|
||||
_logger.LogError(_error, _error.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,5 +8,9 @@ namespace Yi.Framework.Sms.Aliyun
|
||||
{
|
||||
public class SmsAliyunOptions
|
||||
{
|
||||
public string AccessKeyId { get; set; }
|
||||
public string AccessKeySecret { get; set; }
|
||||
|
||||
public string SignName { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
using Yi.Framework.Core.Configuration;
|
||||
|
||||
namespace Yi.Framework.Sms.Aliyun
|
||||
{
|
||||
[DependsOn(typeof(YiFrameworkCoreModule))]
|
||||
public class YiFrameworkSmsAliyunModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
@@ -13,7 +17,8 @@ namespace Yi.Framework.Sms.Aliyun
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
|
||||
services.Configure<SmsAliyunOptions>(Appsettings.appConfiguration("SmsAliyunOptions"));
|
||||
services.AddSingleton<SmsAliyunManager>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user