using Aop.Api.Domain; using Azure; using Furion.DatabaseAccessor; using Furion.DependencyInjection; using Furion.FriendlyException; using Furion.HttpRemote; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; using static System.Net.WebRequestMethods; namespace ApiTools.Core { public class AliyunSmsService : ISmsService, ITransient { private readonly IRepository repThreeResourceLog; private readonly IOptions options; private readonly IHttpRemoteService httpRemoteService; public AliyunSmsService( IRepository repThreeResourceLog, IOptions options, IHttpRemoteService httpRemoteService) { this.repThreeResourceLog = repThreeResourceLog; this.options = options; this.httpRemoteService = httpRemoteService; } /// /// 发送短信 /// /// /// 手机号码 /// 模板代码 /// 模板参数 /// 取消令牌 /// /// public async Task SendAsync(string signName, string phoneNumber, EnumSmsTemplateCode templateCode, object templateParam, CancellationToken cancellationToken) { AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config { AccessKeyId = options.Value.SMS.AccessKeyId, AccessKeySecret = options.Value.SMS.AccessSecret, Endpoint = "dysmsapi.aliyuncs.com" }; var client = new AlibabaCloud.SDK.Dysmsapi20170525.Client(config); AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest { PhoneNumbers = phoneNumber, SignName = signName, TemplateCode = options.Value.SMS.TemplateCodes[templateCode.ToString()], TemplateParam = templateParam.ToJson() }; var log = new ThreeResourceLog { Method = EnumResourceMethod.Get, Domain = "http://dysmsapi.aliyuncs.com", Request = "SendSms", }; await repThreeResourceLog.InsertNowAsync(log); var stopwatch = Stopwatch.StartNew(); var response = client.SendSmsWithOptions(sendSmsRequest, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); log.UpdatedTime = DateTimeOffset.Now; stopwatch.Stop(); log.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds; log.IsSuccess = response.Body.Code == "OK"; await repThreeResourceLog.UpdateNowAsync(log); var result = new SmsResponse { Status = log.IsSuccess ? EnumSmsStatus.InProcess : EnumSmsStatus.Fail, Code = response.Body.Code, Message = response.Body.Message, RequestId = response.Body.BizId }; if (result.Status == EnumSmsStatus.Fail && result.Message.Contains("流控")) { result.Message = "操作频繁"; } //var response2 = client.QuerySendDetailsWithOptions(new AlibabaCloud.SDK.Dysmsapi20170525.Models.QuerySendDetailsRequest //{ // PhoneNumber = phoneNumber, // BizId = response1.Body.BizId, // SendDate = DateTime.Now.ToString("yyyyMMdd"), // PageSize = 20, // CurrentPage = 1 //}, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); //Console.WriteLine(); return result; } /// /// 排除敏感字符串 /// /// 文本 /// 排除后文本 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(); } /// /// HMAC-SHA1加密 /// /// /// /// 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); } } }