From 4c43f221a0dcff9d9452e40d5f59c867edbb2433 Mon Sep 17 00:00:00 2001
From: coolcalf <28551@qq.com>
Date: Thu, 27 Jun 2024 13:54:02 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0SMS=E9=85=8D=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Configuration/SMS.json | 26 ++++++++++++++++---
Admin.NET/Admin.NET.Core/Option/SMSOptions.cs | 21 ++++++++++++---
.../Service/Message/SysSmsService.cs | 16 +++++++-----
3 files changed, 50 insertions(+), 13 deletions(-)
diff --git a/Admin.NET/Admin.NET.Application/Configuration/SMS.json b/Admin.NET/Admin.NET.Application/Configuration/SMS.json
index f126bff1..58a697db 100644
--- a/Admin.NET/Admin.NET.Application/Configuration/SMS.json
+++ b/Admin.NET/Admin.NET.Application/Configuration/SMS.json
@@ -5,15 +5,33 @@
"Aliyun": {
"AccessKeyId": "",
"AccessKeySecret": "",
- "SignName": "AdminNET 平台", // 短信签名
- "TemplateCode": "" // 短信模板
+ "Templates": [
+ {
+ "Id": "0",
+ "SignName": "AdminNET 平台",
+ "TemplateCode": "SMS_291005708",
+ "Content": "您的验证码为:${code},请勿泄露于他人!"
+ },
+ {
+ "Id": "1",
+ "SignName": "AdminNET 平台",
+ "TemplateCode": "SMS_462801755",
+ "Content": "注册成功,感谢您的注册,请妥善保管您的账户信息"
+ }
+ ]
},
"Tencentyun": {
"SdkAppId": "",
"AccessKeyId": "",
"AccessKeySecret": "",
- "SignName": "AdminNET 平台", // 短信签名
- "TemplateCode": "" // 短信模板
+ "Templates": [
+ {
+ "Id": "0",
+ "SignName": "AdminNET 平台",
+ "TemplateCode": "",
+ "Content": ""
+ }
+ ]
}
}
}
\ No newline at end of file
diff --git a/Admin.NET/Admin.NET.Core/Option/SMSOptions.cs b/Admin.NET/Admin.NET.Core/Option/SMSOptions.cs
index 757755f7..2011c11d 100644
--- a/Admin.NET/Admin.NET.Core/Option/SMSOptions.cs
+++ b/Admin.NET/Admin.NET.Core/Option/SMSOptions.cs
@@ -40,12 +40,27 @@ public sealed class SMSSettings
public string AccessKeySecret { get; set; }
///
- /// 短信签名
+ /// Templates
///
- public string SignName { get; set; }
+ public List Templates { get; set; }
///
- /// 短信模板
+ /// GetTemplate
///
+ public SmsTemplate GetTemplate(string id = "0")
+ {
+ foreach (var template in Templates)
+ {
+ if (template.Id == id) { return template; }
+ }
+ return null;
+ }
+}
+
+public class SmsTemplate
+{
+ public string Id { get; set; } = string.Empty;
+ public string SignName { get; set; }
public string TemplateCode { get; set; }
+ public string Content { get; set; }
}
\ No newline at end of file
diff --git a/Admin.NET/Admin.NET.Core/Service/Message/SysSmsService.cs b/Admin.NET/Admin.NET.Core/Service/Message/SysSmsService.cs
index 83dfadaf..a1443280 100644
--- a/Admin.NET/Admin.NET.Core/Service/Message/SysSmsService.cs
+++ b/Admin.NET/Admin.NET.Core/Service/Message/SysSmsService.cs
@@ -8,6 +8,7 @@ using AlibabaCloud.SDK.Dysmsapi20170525.Models;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20190711;
+using static SKIT.FlurlHttpClient.Wechat.Api.Models.ComponentTCBBatchCreateContainerServiceVersionRequest.Types;
namespace Admin.NET.Core.Service;
@@ -65,11 +66,12 @@ public class SysSmsService : IDynamicApiController, ITransient
});
var client = CreateAliyunClient();
+ var template = _smsOptions.Aliyun.GetTemplate();
var sendSmsRequest = new SendSmsRequest
{
PhoneNumbers = phoneNumber, // 待发送手机号, 多个以逗号分隔
- SignName = _smsOptions.Aliyun.SignName, // 短信签名
- TemplateCode = _smsOptions.Aliyun.TemplateCode, // 短信模板
+ SignName = template.SignName, // 短信签名
+ TemplateCode = template.TemplateCode, // 短信模板
TemplateParam = templateParam.ToString(), // 模板中的变量替换JSON串
OutId = YitIdHelper.NextId().ToString()
};
@@ -104,11 +106,12 @@ public class SysSmsService : IDynamicApiController, ITransient
throw Oops.Oh("短信内容不能为空");
var client = CreateAliyunClient();
+ var template = _smsOptions.Aliyun.GetTemplate();
var sendSmsRequest = new SendSmsRequest
{
PhoneNumbers = phoneNumber, // 待发送手机号, 多个以逗号分隔
- SignName = _smsOptions.Aliyun.SignName, // 短信签名
- TemplateCode = _smsOptions.Aliyun.TemplateCode, // 短信模板
+ SignName = template.SignName, // 短信签名
+ TemplateCode = template.TemplateCode, // 短信模板
TemplateParam = templateParam.ToString(), // 模板中的变量替换JSON串
OutId = YitIdHelper.NextId().ToString()
};
@@ -142,13 +145,14 @@ public class SysSmsService : IDynamicApiController, ITransient
// 实例化要请求产品的client对象,clientProfile是可选的
var client = new SmsClient(CreateTencentClient(), "ap-guangzhou", new ClientProfile() { HttpProfile = new HttpProfile() { Endpoint = ("sms.tencentcloudapi.com") } });
+ var template = _smsOptions.Tencentyun.GetTemplate();
// 实例化一个请求对象,每个接口都会对应一个request对象
var req = new TencentCloud.Sms.V20190711.Models.SendSmsRequest
{
PhoneNumberSet = new string[] { "+86" + phoneNumber.Trim(',') },
SmsSdkAppid = _smsOptions.Tencentyun.SdkAppId,
- Sign = _smsOptions.Tencentyun.SignName,
- TemplateID = _smsOptions.Tencentyun.TemplateCode,
+ Sign = template.SignName,
+ TemplateID = template.TemplateCode,
TemplateParamSet = new string[] { verifyCode.ToString() }
};