|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+using System;
|
|
|
2
|
+using System.Collections.Generic;
|
|
|
3
|
+using System.Linq;
|
|
|
4
|
+using System.Net.Http;
|
|
|
5
|
+using System.Security.Cryptography;
|
|
|
6
|
+using System.Text;
|
|
|
7
|
+using System.Threading.Tasks;
|
|
|
8
|
+
|
|
|
9
|
+namespace CallCenter.Utility
|
|
|
10
|
+{
|
|
|
11
|
+ /// <summary>
|
|
|
12
|
+ /// 短信
|
|
|
13
|
+ /// </summary>
|
|
|
14
|
+ public static class SmsHelper
|
|
|
15
|
+ {
|
|
|
16
|
+ /// <summary>
|
|
|
17
|
+ /// 发送短信
|
|
|
18
|
+ /// </summary>
|
|
|
19
|
+ /// <param name="phones"></param>
|
|
|
20
|
+ /// <param name="templateCode"></param>
|
|
|
21
|
+ /// <param name="templateParam"></param>
|
|
|
22
|
+ /// <returns></returns>
|
|
|
23
|
+ public static string Send(string phones,string templateCode,string templateParam="")
|
|
|
24
|
+ {
|
|
|
25
|
+ string result = string.Empty;
|
|
|
26
|
+
|
|
|
27
|
+ try
|
|
|
28
|
+ {
|
|
|
29
|
+ string apiKey = Configs.GetValue("SmsNewApiKey");
|
|
|
30
|
+ string accessKey = Configs.GetValue("SmsNewSecretKey");
|
|
|
31
|
+ string authHost = Configs.GetValue("SmsNewUrl");
|
|
|
32
|
+
|
|
|
33
|
+ var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
|
34
|
+ var currentTime = Convert.ToInt64(ts.TotalMilliseconds).ToString();
|
|
|
35
|
+ HttpClient client = new HttpClient();
|
|
|
36
|
+
|
|
|
37
|
+ var requestHeaders = new Dictionary<string, string>();
|
|
|
38
|
+ //签名算法。支持HmacSHA256、HmacMD5、HmacSHA224、HmacSHA384、HmacSHA512
|
|
|
39
|
+ requestHeaders.Add("x-sign-method", "HmacSHA256");
|
|
|
40
|
+ requestHeaders.Add("x-api-key", $"{apiKey}");
|
|
|
41
|
+ //随机数。每次请求随机数建议不相同
|
|
|
42
|
+ requestHeaders.Add("x-nonce", $"{GetRandom()}");
|
|
|
43
|
+ //时间戳
|
|
|
44
|
+ requestHeaders.Add("x-timestamp", $"{currentTime}");
|
|
|
45
|
+
|
|
|
46
|
+
|
|
|
47
|
+ // client.DefaultRequestHeaders.Host = "recognition.image.myqcloud.com";
|
|
|
48
|
+ //设置headers请求参数
|
|
|
49
|
+ foreach (var item in requestHeaders)
|
|
|
50
|
+ {
|
|
|
51
|
+ client.DefaultRequestHeaders.Add(item.Key, item.Value);
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ //短信接口请求参数。参见接口规范
|
|
|
55
|
+ var jsArr = new Dictionary<string, string>();
|
|
|
56
|
+ jsArr.Add("phones", phones);
|
|
|
57
|
+ jsArr.Add("templateCode", templateCode);
|
|
|
58
|
+ if (!string.IsNullOrEmpty(templateParam))
|
|
|
59
|
+ {
|
|
|
60
|
+ jsArr.Add("templateParam", templateParam);
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ //将headers参与与接口报文参数合并、排序,并生成本次请求的签名信息
|
|
|
64
|
+ var signDic = jsArr.Union(requestHeaders).ToDictionary(k => k.Key, v => v.Value);
|
|
|
65
|
+ var dicStr = GetSignContent(signDic);
|
|
|
66
|
+ var sign = Hmacsha256Encrypt(dicStr, accessKey);
|
|
|
67
|
+ //在header中设置本次请求签名信息
|
|
|
68
|
+ client.DefaultRequestHeaders.Add("x-sign", sign);
|
|
|
69
|
+
|
|
|
70
|
+
|
|
|
71
|
+ HttpContent str = new StringContent(jsArr.ToJson());
|
|
|
72
|
+ str.Headers.Remove("Content-Type");
|
|
|
73
|
+ str.Headers.Add("Content-Type", "application/json");
|
|
|
74
|
+ HttpResponseMessage response = client.PostAsync(authHost, str).Result;
|
|
|
75
|
+ var result1 = response.Content.ReadAsStringAsync().Result;
|
|
|
76
|
+ var objresult = result1.ToJObject();
|
|
|
77
|
+ if (objresult["code"].ToString() != "200")
|
|
|
78
|
+ {
|
|
|
79
|
+ LogFactory.GetLogger("SmsHelper").Error(phones + "\r\n" + templateCode + "\r\n" + templateParam + "\r\n" + result1);
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ }
|
|
|
83
|
+ catch (Exception ex)
|
|
|
84
|
+ {
|
|
|
85
|
+ LogFactory.GetLogger("SmsHelper").Error(phones + "\r\n" + templateCode + "\r\n" + templateParam + "\r\n" + ex.ToString());
|
|
|
86
|
+ }
|
|
|
87
|
+ return result;
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ /// <summary>
|
|
|
91
|
+ /// Get Sign Content
|
|
|
92
|
+ /// </summary>
|
|
|
93
|
+ /// <param name="parameters"></param>
|
|
|
94
|
+ /// <returns></returns>
|
|
|
95
|
+ private static string GetSignContent(IDictionary<string, string> parameters)
|
|
|
96
|
+ {
|
|
|
97
|
+ // 第一步:把字典按Key的字母顺序排序
|
|
|
98
|
+ IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
|
|
|
99
|
+ IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
|
|
|
100
|
+
|
|
|
101
|
+ // 第二步:把所有参数名和参数值串在一起
|
|
|
102
|
+ StringBuilder query = new StringBuilder("");
|
|
|
103
|
+ while (dem.MoveNext())
|
|
|
104
|
+ {
|
|
|
105
|
+ string key = dem.Current.Key;
|
|
|
106
|
+ string value = dem.Current.Value;
|
|
|
107
|
+ if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
|
|
|
108
|
+ {
|
|
|
109
|
+ query.Append(key).Append("=").Append(value).Append("&");
|
|
|
110
|
+ }
|
|
|
111
|
+ }
|
|
|
112
|
+ string content = query.ToString().Substring(0, query.Length - 1);
|
|
|
113
|
+
|
|
|
114
|
+ return content;
|
|
|
115
|
+ }
|
|
|
116
|
+
|
|
|
117
|
+ /// <summary>
|
|
|
118
|
+ /// HMACSHA1算法加密
|
|
|
119
|
+ /// </summary>
|
|
|
120
|
+ private static string Hmacsha256Encrypt(string encryptText, string encryptKey)
|
|
|
121
|
+ {
|
|
|
122
|
+ var encoding = new System.Text.UTF8Encoding();
|
|
|
123
|
+ StringBuilder builder = new StringBuilder();
|
|
|
124
|
+
|
|
|
125
|
+ using (var hmacsha256 = new HMACSHA256(encoding.GetBytes(encryptKey)))
|
|
|
126
|
+ {
|
|
|
127
|
+ byte[] hashmessage = hmacsha256.ComputeHash(encoding.GetBytes(encryptText));
|
|
|
128
|
+ for (int i = 0; i < hashmessage.Length; i++)
|
|
|
129
|
+ {
|
|
|
130
|
+ builder.Append(hashmessage[i].ToString("x2"));
|
|
|
131
|
+ }
|
|
|
132
|
+ }
|
|
|
133
|
+ return builder.ToString();
|
|
|
134
|
+ }
|
|
|
135
|
+ /// <summary>
|
|
|
136
|
+ /// 获取随机数
|
|
|
137
|
+ /// </summary>
|
|
|
138
|
+ private static int GetRandom()
|
|
|
139
|
+ {
|
|
|
140
|
+ var random = new Random();
|
|
|
141
|
+ var rand = random.Next(10000, 999999999);
|
|
|
142
|
+ return rand;
|
|
|
143
|
+ }
|
|
|
144
|
+ }
|
|
|
145
|
+}
|