using Furion.FriendlyException;
|
using Furion.HttpRemote;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.Extensions.Options;
|
using System;
|
using System.Collections.Generic;
|
using System.Globalization;
|
using System.Linq;
|
using System.Security.Cryptography;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace FlexJobApi.Core
|
{
|
public class AliyunSmsUtils
|
{
|
private readonly IOptions<AliyunOptions> options;
|
private readonly IHttpRemoteService httpRemoteService;
|
private readonly IHttpContextAccessor httpContextAccessor;
|
|
public AliyunSmsUtils(
|
IOptions<AliyunOptions> options,
|
IHttpRemoteService httpRemoteService,
|
IHttpContextAccessor httpContextAccessor)
|
{
|
this.options = options;
|
this.httpRemoteService = httpRemoteService;
|
this.httpContextAccessor = httpContextAccessor;
|
}
|
|
/// <summary>
|
/// 发送短信
|
/// </summary>
|
/// <param name="phoneNumber">手机号码</param>
|
/// <param name="templateCode">模板代码</param>
|
/// <param name="templateParam">模板参数</param>
|
/// <param name="cancellationToken">取消令牌</param>
|
/// <returns></returns>
|
/// <exception cref="Oops"></exception>
|
//public async Task SendAsync(string phoneNumber, EnumSmsTemplateType templateCode, string templateParam, CancellationToken cancellationToken)
|
//{
|
// if (options.Value.Sms?.Enable != true)
|
// {
|
// httpContextAccessor.AddAdditionalData("TemplateParam", templateParam);
|
// return;
|
// }
|
// if (options.Value.Sms != null
|
// && options.Value.Sms.Version.IsNotNull()
|
// && options.Value.Sms.RegionId.IsNotNull()
|
// && options.Value.Sms.SignName.IsNotNull()
|
// && options.Value.AccessKeyId.IsNotNull()
|
// && options.Value.AccessKeySecret.IsNotNull())
|
// {
|
// var _templateCode = options.Value.Sms.TemplateCodes[templateCode.ToString()];
|
// var _params = new Dictionary<string, string>
|
// {
|
// {"Action", "SendSms"},
|
// {"Version", options.Value.Sms.Version},
|
// {"RegionId", options.Value.Sms.RegionId},
|
// {"PhoneNumbers", phoneNumber},
|
// {"SignName", options.Value.Sms.SignName},
|
// {"TemplateCode", _templateCode}
|
// };
|
// if (!string.IsNullOrWhiteSpace(templateParam))
|
// {
|
// _params.Add("TemplateParam", templateParam);
|
// }
|
// var timestamp = DateTime.Now.ToUniversalTime()
|
// .ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.CreateSpecificCulture("en-US"));
|
|
// _params.Add("AccessKeyId", options.Value.AccessKeyId);
|
// _params.Add("Timestamp", timestamp);
|
// _params.Add("Format", "JSON");
|
// _params.Add("SignatureMethod", "HMAC-SHA1");
|
// _params.Add("SignatureVersion", "1.0");
|
// _params.Add("SignatureNonce", Guid.NewGuid().ToString());
|
|
// //排序
|
// var sortDic = new SortedDictionary<string, string>(_params, StringComparer.Ordinal);
|
|
// //生成Url参数
|
// var urlParams = "";
|
// foreach (var dic in sortDic)
|
// {
|
// urlParams += $"{PercentEncode(dic.Key)}={PercentEncode(dic.Value)}&";
|
// }
|
// urlParams = urlParams.TrimEnd('&');
|
|
// //签名
|
// var stringToSign = $"GET&{PercentEncode("/")}&{PercentEncode(urlParams)}";
|
// string signature = PercentEncode(ToHmacsha1(stringToSign, options.Value.AccessKeySecret + "&"));
|
|
// var req = $"http://dysmsapi.aliyuncs.com/?Signature={signature}&{urlParams}"
|
// .SetMethod(HttpMethod.Get);
|
// var res = await http.SendAsync(req, cancellationToken);
|
// var callback = res.Callback?.JsonTo(new
|
// {
|
// Code = "",
|
// Message = "",
|
// RequestId = "",
|
// BizId = ""
|
// });
|
// if (callback == null || callback.Code != "OK")
|
// {
|
// throw new Oops(FriendlyCallbackCode.Error, "发送短信失败", $"发送短信异常:{callback?.Message}");
|
// }
|
// }
|
//}
|
|
/// <summary>
|
/// 排除敏感字符串
|
/// </summary>
|
/// <param name="value">文本</param>
|
/// <returns>排除后文本</returns>
|
private string PercentEncode(string value)
|
{
|
var stringBuilder = new StringBuilder();
|
var text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
|
var bytes = Encoding.GetEncoding("UTF-8").GetBytes(value);
|
foreach (var b in bytes)
|
{
|
var c = (char)b;
|
if (text.IndexOf(c) >= 0)
|
stringBuilder.Append(c);
|
else
|
stringBuilder.Append("%").Append(
|
string.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)c));
|
}
|
return stringBuilder.ToString();
|
}
|
|
/// <summary>
|
/// HMAC-SHA1加密
|
/// </summary>
|
/// <param name="content"></param>
|
/// <param name="key"></param>
|
/// <returns></returns>
|
private static string ToHmacsha1(string content, string key)
|
{
|
var hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(key));
|
var bytes = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(content));
|
return Convert.ToBase64String(bytes);
|
}
|
}
|
}
|