完成阿里云短信功能并进行测试

This commit is contained in:
jianweie
2024-02-28 23:44:59 +08:00
parent 1d3df852a0
commit 36dfe3b5c4
10 changed files with 818 additions and 226 deletions

View File

@@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="2.0.24" />
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" />
<PackageReference Include="Essensoft.Paylink.Alipay" Version="4.1.3" />
<PackageReference Include="Essensoft.Paylink.WeChatPay" Version="4.1.3" />

View File

@@ -22,12 +22,16 @@ using CoreCms.Net.Model.ViewModels.Sms;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.WeChatPay.V3.Domain;
using Flurl.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing.Template;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Tea;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.WxaApiUserLogSearchResponse.Types.Data.Types.RealtimeLog.Types;
@@ -126,26 +130,125 @@ namespace CoreCms.Net.Services
{
code = codeNumber
};
switch (type)
if (type == "login")
{
case "login":
if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.KingTto)
{
oldLog.contentBody = "您本次登陆的验证码是:" + codeNumber + ",请不要将验证码泄露给他人!";
oldLog.parameters = JsonConvert.SerializeObject(obj);
break;
default:
}
else if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.AliYun)
{
oldLog.contentBody = smsOptions.SmsAliYunTplForLogin;
oldLog.parameters = JsonConvert.SerializeObject(new
{
code = codeNumber.ToString()
});
}
}
else
{
if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.KingTto)
{
oldLog.contentBody = "您验证码是:" + codeNumber + ",请不要将验证码泄露给他人!";
oldLog.parameters = JsonConvert.SerializeObject(obj);
break;
}
else if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.AliYun)
{
oldLog.contentBody = smsOptions.SmsAliYunTplForVeri;
oldLog.parameters = JsonConvert.SerializeObject(new
{
code = codeNumber.ToString()
});
}
}
await _dal.InsertAsync(oldLog);
}
var result = await SendSms(oldLog.mobile, oldLog.contentBody, smsOptions);
if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.KingTto)
{
var result = await SendSms(oldLog.mobile, oldLog.contentBody, smsOptions);
jm.status = result.IsSuccess;
jm.msg = result.IsSuccess ? "发送成功" : result.Message;
jm.otherData = result;
jm.status = result.IsSuccess;
jm.msg = result.IsSuccess ? "发送成功" : result.Message;
jm.otherData = result;
}
else if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.AliYun)
{
if (type == "login" && string.IsNullOrEmpty(smsOptions.SmsAliYunTplForLogin))
{
jm.msg = "阿里云登录短信模板未设置";
return jm;
}
if (type != "login" && string.IsNullOrEmpty(smsOptions.SmsAliYunTplForVeri))
{
jm.msg = "阿里云验证码短信模板未设置";
return jm;
}
var config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 必填,您的 AccessKey ID
AccessKeyId = smsOptions.Account,
// 必填,您的 AccessKey Secret
AccessKeySecret = smsOptions.Password,
//Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
Endpoint = smsOptions.ApiUrl,
};
var client = new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
var sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest();
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
sendSmsRequest.SignName = smsOptions.Signature;
sendSmsRequest.PhoneNumbers = mobile;
sendSmsRequest.TemplateCode =
type == "login" ? smsOptions.SmsAliYunTplForLogin : smsOptions.SmsAliYunTplForVeri;
sendSmsRequest.TemplateParam = JsonConvert.SerializeObject(new
{
code = codeNumber.ToString()
});
sendSmsRequest.OutId = Guid.NewGuid().ToString();
var result = await client.SendSmsWithOptionsAsync(sendSmsRequest, runtime);
if (result.Body.Code == "OK")
{
jm.status = true;
jm.msg = "发送成功";
}
else
{
jm.status = false;
jm.msg = "发送失败:" + result.Body.Message;
}
jm.otherData = result;
}
catch (TeaException error)
{
jm.status = false;
jm.msg = "发送失败:" + error.Message;
}
catch (Exception _error)
{
var error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
jm.status = false;
jm.msg = "发送失败:" + _error.Message;
jm.otherData = error.Data["Recommend"];
}
}
return jm;
}
#endregion
@@ -274,11 +377,11 @@ namespace CoreCms.Net.Services
return jm;
}
var isUsed = false;
string codeNumberStr = string.Empty;
if (code == GlobalEnumVars.SmsMessageTypes.Reg.ToString() || code == GlobalEnumVars.SmsMessageTypes.Login.ToString() || code == GlobalEnumVars.SmsMessageTypes.Veri.ToString())
{
var newCreateTime = DateTime.Now.AddSeconds(-60);
var smsInfo = await _dal.QueryByClauseAsync(p =>
p.mobile == mobile && p.code == code && p.createTime < newCreateTime && p.isUsed == false);
var smsInfo = await _dal.QueryByClauseAsync(p => p.mobile == mobile && p.code == code && p.createTime < newCreateTime && p.isUsed == false);
if (smsInfo != null)
{
var ts = dt - smsInfo.createTime;
@@ -288,6 +391,10 @@ namespace CoreCms.Net.Services
return jm;
}
parameters = JObject.Parse(smsInfo.parameters);
if (parameters.TryGetValue("code", out var parameter))
{
codeNumberStr = parameter?.ToString();
}
}
else
{
@@ -298,6 +405,7 @@ namespace CoreCms.Net.Services
parameters.Remove("code");
}
parameters.Add("code", codeNumber);
codeNumberStr = codeNumber.ToString();
}
isUsed = false;
}
@@ -309,132 +417,258 @@ namespace CoreCms.Net.Services
var str = string.Empty;
var allConfigs = await _settingServices.GetConfigDictionaries();
if (code == GlobalEnumVars.SmsMessageTypes.Reg.ToString())
if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.KingTto)
{
// 账户注册
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForReg);
if (!string.IsNullOrEmpty(msg))
if (code == GlobalEnumVars.SmsMessageTypes.Reg.ToString())
{
var sendCode = string.Empty;
if (parameters.ContainsKey("code"))
// 账户注册
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForReg);
if (!string.IsNullOrEmpty(msg))
{
sendCode = parameters["code"]?.ToString();
str = msg.Replace("{code}", codeNumberStr);
}
str = msg.Replace("{code}", sendCode);
}
}
else if (code == GlobalEnumVars.SmsMessageTypes.Login.ToString())
{
// 账户登录
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForLogin);
if (!string.IsNullOrEmpty(msg))
else if (code == GlobalEnumVars.SmsMessageTypes.Login.ToString())
{
var sendCode = string.Empty;
if (parameters.ContainsKey("code"))
// 账户登录
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForLogin);
if (!string.IsNullOrEmpty(msg))
{
sendCode = parameters["code"]?.ToString();
str = msg.Replace("{code}", codeNumberStr);
}
str = msg.Replace("{code}", sendCode);
}
}
else if (code == GlobalEnumVars.SmsMessageTypes.Veri.ToString())
{
// 验证验证码
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForVeri);
if (!string.IsNullOrEmpty(msg))
else if (code == GlobalEnumVars.SmsMessageTypes.Veri.ToString())
{
var sendCode = string.Empty;
if (parameters.ContainsKey("code"))
// 验证验证码
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForVeri);
if (!string.IsNullOrEmpty(msg))
{
sendCode = parameters["code"]?.ToString();
str = msg.Replace("{code}", codeNumberStr);
}
str = msg.Replace("{code}", sendCode);
}
}
else if (code == GlobalEnumVars.PlatformMessageTypes.CreateOrder.ToString())
{
// 订单创建
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForCreateOrder);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.OrderPayed.ToString())
{
// 订单支付通知买家
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForOrderPayed);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.RemindOrderPay.ToString())
{
// 未支付催单
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForRemindOrderPay);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.DeliveryNotice.ToString())
{
// 订单发货
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForDeliveryNotice);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.AfterSalesPass.ToString())
{
// 售后审核通过
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForAfterSalesPass);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.RefundSuccess.ToString())
{
// 退款已处理
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForRefundSuccess);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.SellerOrderNotice.ToString())
{
// 订单支付通知卖家
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForSellerOrderNotice);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.Common.ToString())
{
//通用类型
var tpl = string.Empty;
if (parameters.ContainsKey("tpl"))
else if (code == GlobalEnumVars.PlatformMessageTypes.CreateOrder.ToString())
{
tpl = parameters["tpl"]?.ToString();
}
str = tpl;
if (string.IsNullOrEmpty(str))
{
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForCommon);
// 订单创建
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForCreateOrder);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
}
else if (code == GlobalEnumVars.PlatformMessageTypes.OrderPayed.ToString())
{
// 订单支付通知买家
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForOrderPayed);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.RemindOrderPay.ToString())
{
// 未支付催单
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForRemindOrderPay);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.DeliveryNotice.ToString())
{
// 订单发货
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForDeliveryNotice);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.AfterSalesPass.ToString())
{
// 售后审核通过
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForAfterSalesPass);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.RefundSuccess.ToString())
{
// 退款已处理
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForRefundSuccess);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.SellerOrderNotice.ToString())
{
// 订单支付通知卖家
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForSellerOrderNotice);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.Common.ToString())
{
//通用类型
var tpl = string.Empty;
if (parameters.ContainsKey("tpl"))
{
tpl = parameters["tpl"]?.ToString();
}
str = tpl;
if (string.IsNullOrEmpty(str))
{
var msg = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.SmsTplForCommon);
str = !string.IsNullOrEmpty(msg) ? msg : string.Empty;
}
}
if (string.IsNullOrEmpty(str))
{
jm.msg = GlobalErrorCodeVars.Code10009;
return jm;
}
var oldLog = new CoreCmsSms();
oldLog.mobile = mobile;
oldLog.code = code;
oldLog.parameters = JsonConvert.SerializeObject(parameters);
oldLog.contentBody = str;
oldLog.createTime = dt;
oldLog.ip = ip;
oldLog.isUsed = isUsed;
await _dal.InsertAsync(oldLog);
var result = await SendSms(oldLog.mobile, oldLog.contentBody, smsOptions);
jm.status = result.IsSuccess;
jm.msg = result.IsSuccess ? "发送成功" : result.Message;
if (string.IsNullOrEmpty(str))
{
jm.msg = GlobalErrorCodeVars.Code10009;
return jm;
}
else if (smsOptions.SmsType == (int)GlobalEnumVars.SmsType.AliYun)
{
var templateCode = string.Empty;
object templateParam = null;
if (code == GlobalEnumVars.SmsMessageTypes.Reg.ToString())
{
templateCode = smsOptions.SmsAliYunTplForReg;
templateParam = new
{
code = codeNumberStr
};
}
else if (code == GlobalEnumVars.SmsMessageTypes.Login.ToString())
{
templateCode = smsOptions.SmsAliYunTplForLogin;
templateParam = new
{
code = codeNumberStr
};
}
else if (code == GlobalEnumVars.SmsMessageTypes.Veri.ToString())
{
templateCode = smsOptions.SmsAliYunTplForVeri;
templateParam = new
{
code = codeNumberStr
};
}
else if (code == GlobalEnumVars.PlatformMessageTypes.CreateOrder.ToString())
{
templateCode = smsOptions.SmsAliYunTplForCreateOrder;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.OrderPayed.ToString())
{
templateCode = smsOptions.SmsAliYunTplForOrderPayed;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.RemindOrderPay.ToString())
{
templateCode = smsOptions.SmsAliYunTplForRemindOrderPay;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.DeliveryNotice.ToString())
{
templateCode = smsOptions.SmsAliYunTplForDeliveryNotice;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.AfterSalesPass.ToString())
{
templateCode = smsOptions.SmsAliYunTplForAfterSalesPass;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.RefundSuccess.ToString())
{
templateCode = smsOptions.SmsAliYunTplForRefundSuccess;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.SellerOrderNotice.ToString())
{
templateCode = smsOptions.SmsAliYunTplForSellerOrderNotice;
}
else if (code == GlobalEnumVars.PlatformMessageTypes.Common.ToString())
{
templateCode = smsOptions.SmsAliYunTplForCommon;
}
var oldLog = new CoreCmsSms();
oldLog.mobile = mobile;
oldLog.code = code;
oldLog.parameters = JsonConvert.SerializeObject(parameters);
oldLog.contentBody = str;
oldLog.createTime = dt;
oldLog.ip = ip;
oldLog.isUsed = isUsed;
if (string.IsNullOrEmpty(templateCode))
{
jm.msg = GlobalErrorCodeVars.Code10009;
return jm;
}
await _dal.InsertAsync(oldLog);
var config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 必填,您的 AccessKey ID
AccessKeyId = smsOptions.Account,
// 必填,您的 AccessKey Secret
AccessKeySecret = smsOptions.Password,
//Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
Endpoint = smsOptions.ApiUrl,
var result = await SendSms(oldLog.mobile, oldLog.contentBody, smsOptions);
};
var client = new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
jm.status = result.IsSuccess;
jm.msg = result.IsSuccess ? "发送成功" : result.Message;
var sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest();
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
return jm;
try
{
sendSmsRequest.SignName = smsOptions.Signature;
sendSmsRequest.PhoneNumbers = mobile;
sendSmsRequest.TemplateCode = templateCode;
sendSmsRequest.TemplateParam = JsonConvert.SerializeObject(templateParam);
sendSmsRequest.OutId = Guid.NewGuid().ToString();
var result = await client.SendSmsWithOptionsAsync(sendSmsRequest, runtime);
if (result.Body.Code == "OK")
{
var smsInfo = new CoreCmsSms();
smsInfo.mobile = mobile;
smsInfo.parameters = JsonConvert.SerializeObject(result);
smsInfo.code = code;
smsInfo.contentBody = templateCode;
smsInfo.createTime = DateTime.Now;
smsInfo.ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress != null ? _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : "127.0.0.1";
smsInfo.isUsed = isUsed;
await _dal.InsertAsync(smsInfo);
jm.status = true;
jm.msg = "发送成功";
}
else
{
jm.status = false;
jm.msg = "发送失败:" + result.Body.Message;
}
}
catch (TeaException error)
{
jm.status = false;
jm.msg = "发送失败:" + error.Message;
}
catch (Exception _error)
{
var error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
jm.status = false;
jm.msg = "发送失败:" + _error.Message;
jm.otherData = error.Data["Recommend"];
}
return jm;
}
else
{
jm.status = false;
jm.msg = "未获取到发送方式";
return jm;
}
}
#endregion
}
}

View File

@@ -215,15 +215,28 @@ namespace CoreCms.Net.Services
var settings = await GetDatas();
sms.Enabled = GetValue(SystemSettingConstVars.SmsEnabled, configs, settings).ObjectToInt(1) == 1;
sms.SmsType = GetValue(SystemSettingConstVars.SmsType, configs, settings).ObjectToInt(1);
sms.UserId = GetValue(SystemSettingConstVars.SmsUserId, configs, settings);
sms.Account = GetValue(SystemSettingConstVars.SmsAccount, configs, settings);
sms.Password = GetValue(SystemSettingConstVars.SmsPassword, configs, settings);
sms.Signature = GetValue(SystemSettingConstVars.SmsSignature, configs, settings);
sms.ApiUrl = GetValue(SystemSettingConstVars.SmsApiUrl, configs, settings);
sms.SmsIpSendNumber= GetValue(SystemSettingConstVars.SmsIpSendNumber, configs, settings).ObjectToInt(20);
sms.SmsIpSendNumber = GetValue(SystemSettingConstVars.SmsIpSendNumber, configs, settings).ObjectToInt(20);
sms.SmsIpSendWhiteList = GetValue(SystemSettingConstVars.SmsIpSendWhiteList, configs, settings);
sms.SmsIpSendBlackList = GetValue(SystemSettingConstVars.SmsIpSendBlackList, configs, settings);
sms.SmsAliYunTplForReg = GetValue(SystemSettingConstVars.SmsAliYunTplForReg, configs, settings);
sms.SmsAliYunTplForLogin = GetValue(SystemSettingConstVars.SmsAliYunTplForLogin, configs, settings);
sms.SmsAliYunTplForVeri = GetValue(SystemSettingConstVars.SmsAliYunTplForVeri, configs, settings);
sms.SmsAliYunTplForCreateOrder = GetValue(SystemSettingConstVars.SmsAliYunTplForCreateOrder, configs, settings);
sms.SmsAliYunTplForOrderPayed = GetValue(SystemSettingConstVars.SmsAliYunTplForOrderPayed, configs, settings);
sms.SmsAliYunTplForRemindOrderPay = GetValue(SystemSettingConstVars.SmsAliYunTplForRemindOrderPay, configs, settings);
sms.SmsAliYunTplForDeliveryNotice = GetValue(SystemSettingConstVars.SmsAliYunTplForDeliveryNotice, configs, settings);
sms.SmsAliYunTplForAfterSalesPass = GetValue(SystemSettingConstVars.SmsAliYunTplForAfterSalesPass, configs, settings);
sms.SmsAliYunTplForRefundSuccess = GetValue(SystemSettingConstVars.SmsAliYunTplForRefundSuccess, configs, settings);
sms.SmsAliYunTplForSellerOrderNotice = GetValue(SystemSettingConstVars.SmsAliYunTplForSellerOrderNotice, configs, settings);
sms.SmsAliYunTplForCommon = GetValue(SystemSettingConstVars.SmsAliYunTplForCommon, configs, settings);
return sms;
}