From 56d640d0645f1c494fedcd9182e84f107c30e8de Mon Sep 17 00:00:00 2001 From: wxd <123@qq.com> Date: Fri, 12 Jul 2024 09:56:29 +0800 Subject: [PATCH] ali --- src/Shentun.Sms.HttpApi.Host/appsettings.json | 6 ++ .../Shentun.Sms.Service.csproj | 1 + src/Shentun.Sms.Service/Sms/AliyunSms.cs | 98 +++++++++++++++++++ src/Shentun.Sms.Service/Sms/SmsBase.cs | 1 + 4 files changed, 106 insertions(+) create mode 100644 src/Shentun.Sms.Service/Sms/AliyunSms.cs diff --git a/src/Shentun.Sms.HttpApi.Host/appsettings.json b/src/Shentun.Sms.HttpApi.Host/appsettings.json index 3c603a1..0ec1c3d 100644 --- a/src/Shentun.Sms.HttpApi.Host/appsettings.json +++ b/src/Shentun.Sms.HttpApi.Host/appsettings.json @@ -48,6 +48,12 @@ "SignName": "长沙神豚科技", //签名 "TemplateId": "785039", //校验短信模板ID "Timeout": 60 //超时 秒 + }, + "AliyunSms": { + "AccessKeyId": "AKIDvhbVOG0rhpvxw2z6AGQiBa3jQsIc1U4d", + "AccessKeySecret": "7gP5Z7mZN0Pgpqlkp07722zrbO7k9lSA", + "SignName": "", + "TemplateCode": "" } diff --git a/src/Shentun.Sms.Service/Shentun.Sms.Service.csproj b/src/Shentun.Sms.Service/Shentun.Sms.Service.csproj index 3f9f558..9865ad7 100644 --- a/src/Shentun.Sms.Service/Shentun.Sms.Service.csproj +++ b/src/Shentun.Sms.Service/Shentun.Sms.Service.csproj @@ -7,6 +7,7 @@ + diff --git a/src/Shentun.Sms.Service/Sms/AliyunSms.cs b/src/Shentun.Sms.Service/Sms/AliyunSms.cs new file mode 100644 index 0000000..f9d4fb5 --- /dev/null +++ b/src/Shentun.Sms.Service/Sms/AliyunSms.cs @@ -0,0 +1,98 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TencentCloud.Common.Profile; +using TencentCloud.Common; +using Tea; +using Tea.Utils; + +namespace Shentun.Sms.Service.Sms +{ + public class AliyunSms : SmsBase + { + private readonly IConfiguration _aliyunSmsConfig; + private readonly ILogger _logger; + + public AliyunSms( + IConfiguration aliyunSmsConfig, + ILogger logger) + { + _aliyunSmsConfig = aliyunSmsConfig; + _logger = logger; + } + + /// + /// + /// + /// 手机号前+86 + /// + public override void Send(string[] phoneNumbers, string[] templateParam) + { + AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient(); + AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest + { + PhoneNumbers = phoneNumbers[0], + SignName = _aliyunSmsConfig["AliyunSms:SignName"], + TemplateCode = _aliyunSmsConfig["AliyunSms:TemplateCode"], + TemplateParam = "{\"codestr\":\"2222\"}" + }; + try + { + // 复制代码运行请自行打印 API 的返回值 + var msg = client.SendSmsWithOptions(sendSmsRequest, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); + + _logger.LogInformation($"------短信发送记录=>{AlibabaCloud.TeaUtil.Common.AssertAsString(msg)}------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); + } + catch (TeaException error) + { + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + Console.WriteLine(error.Message); + // 诊断地址 + Console.WriteLine(error.Data["Recommend"]); + AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); + } + catch (Exception _error) + { + TeaException error = new TeaException(new Dictionary + { + { "message", _error.Message } + }); + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + Console.WriteLine(error.Message); + // 诊断地址 + Console.WriteLine(error.Data["Recommend"]); + AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); + } + + } + + /** + * 使用AK&SK初始化账号Client + * @return Client + * @throws Exception + */ + public AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient() + { + // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 + // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378671.html。 + AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config + { + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 + AccessKeyId = _aliyunSmsConfig["AliyunSms:AccessKeyId"], + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 + AccessKeySecret = _aliyunSmsConfig["AliyunSms:AccessKeySecret"] + }; + // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi + config.Endpoint = "dysmsapi.aliyuncs.com"; + return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config); + } + + + } +} diff --git a/src/Shentun.Sms.Service/Sms/SmsBase.cs b/src/Shentun.Sms.Service/Sms/SmsBase.cs index 43f89e1..4f2c094 100644 --- a/src/Shentun.Sms.Service/Sms/SmsBase.cs +++ b/src/Shentun.Sms.Service/Sms/SmsBase.cs @@ -14,5 +14,6 @@ namespace Shentun.Sms.Service.Sms /// 手机号码 /// 模板参数,跟模板ID对应的参数个数要一致 public abstract void Send(string[] phoneNumbers, string[] templateParam); + } }