perf:调整阿里云短信发送,方便扩展其他方法
This commit is contained in:
@@ -49,6 +49,48 @@ public class AliyunSmsSender implements TieshengMessageSender {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
*
|
||||
* @param action 业务类型
|
||||
* @param map 业务参数
|
||||
* @return 返回数据
|
||||
*/
|
||||
public String request(String action, SortedMap<String, String> map) {
|
||||
|
||||
SortedMap<String, String> queryMap = new TreeMap<>();
|
||||
|
||||
// 系统参数
|
||||
queryMap.put("AccessKeyId", getAccessKeyId());
|
||||
queryMap.put("SignatureMethod", "HMAC-SHA1");
|
||||
queryMap.put("SignatureNonce", IdUtil.randomUUID());
|
||||
queryMap.put("SignatureVersion", "1.0");
|
||||
SimpleDateFormat dateFormat = DateUtil.newSimpleFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.CHINESE, TimeZone.getTimeZone("0"));
|
||||
queryMap.put("Timestamp", DateUtil.format(DateUtil.date(), dateFormat));
|
||||
queryMap.put("Format", "json");
|
||||
queryMap.put("Action", action);
|
||||
queryMap.put("Version", "2017-05-25");
|
||||
queryMap.put("RegionId", "cn-hangzhou");
|
||||
if (map != null) {
|
||||
queryMap.putAll(map);
|
||||
}
|
||||
|
||||
// 构造带签名字符串
|
||||
StringBuilder sortQueryStringTmp = new StringBuilder();
|
||||
for (String key : queryMap.keySet()) {
|
||||
sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(queryMap.get(key)));
|
||||
}
|
||||
String sortedQueryString = sortQueryStringTmp.substring(1);
|
||||
String stringToSign = "GET" + "&" + specialUrlEncode("/") + "&" + specialUrlEncode(sortedQueryString);
|
||||
String digest = SecureUtil.hmacSha1((getAccessKeySecret() + "&").getBytes(StandardCharsets.UTF_8))
|
||||
.digestBase64(stringToSign, false);
|
||||
String signature = specialUrlEncode(digest);
|
||||
queryMap.put("Signature", signature);
|
||||
|
||||
return HttpUtil.get(ENDPOINT + "?Signature=" + signature + sortQueryStringTmp, 10 * 1000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建发送请求
|
||||
*
|
||||
@@ -69,21 +111,8 @@ public class AliyunSmsSender implements TieshengMessageSender {
|
||||
.putOpt("TemplateParam", params)
|
||||
.toString());
|
||||
|
||||
SortedMap<String, String> queryMap = new TreeMap<>();
|
||||
|
||||
// 系统参数
|
||||
queryMap.put("AccessKeyId", getAccessKeyId());
|
||||
queryMap.put("SignatureMethod", "HMAC-SHA1");
|
||||
queryMap.put("SignatureNonce", IdUtil.randomUUID());
|
||||
queryMap.put("SignatureVersion", "1.0");
|
||||
SimpleDateFormat dateFormat = DateUtil.newSimpleFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.CHINESE, TimeZone.getTimeZone("0"));
|
||||
queryMap.put("Timestamp", DateUtil.format(DateUtil.date(), dateFormat));
|
||||
queryMap.put("Format", "json");
|
||||
|
||||
// 业务API参数
|
||||
queryMap.put("Action", "SendSms");
|
||||
queryMap.put("Version", "2017-05-25");
|
||||
queryMap.put("RegionId", "cn-hangzhou");
|
||||
SortedMap<String, String> queryMap = new TreeMap<>();
|
||||
queryMap.put("PhoneNumbers", phoneNumbers);
|
||||
queryMap.put("SignName", getSignName());
|
||||
queryMap.put("TemplateCode", templateCode);
|
||||
@@ -91,22 +120,8 @@ public class AliyunSmsSender implements TieshengMessageSender {
|
||||
queryMap.put("TemplateParam", params.toString());
|
||||
}
|
||||
|
||||
// 构造带签名字符串
|
||||
StringBuilder sortQueryStringTmp = new StringBuilder();
|
||||
for (String key : queryMap.keySet()) {
|
||||
sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(queryMap.get(key)));
|
||||
}
|
||||
String sortedQueryString = sortQueryStringTmp.substring(1);
|
||||
String stringToSign = "GET" + "&" + specialUrlEncode("/") + "&" + specialUrlEncode(sortedQueryString);
|
||||
String digest = SecureUtil.hmacSha1((getAccessKeySecret() + "&").getBytes(StandardCharsets.UTF_8))
|
||||
.digestBase64(stringToSign, false);
|
||||
String signature = specialUrlEncode(digest);
|
||||
queryMap.put("Signature", signature);
|
||||
|
||||
// 发送请求
|
||||
String response = HttpUtil.get(ENDPOINT + "?Signature=" + signature + sortQueryStringTmp, 10 * 1000);
|
||||
reqResp.setRespBody(response);
|
||||
JSONObject respObj = JSONUtil.parseObj(response);
|
||||
reqResp.setRespBody(request("SendSms", queryMap));
|
||||
JSONObject respObj = JSONUtil.parseObj(reqResp.getRespBody());
|
||||
if (!Objects.equals(respObj.getStr("Code"), "OK")) {
|
||||
reqResp.setResult(0);
|
||||
reqResp.setToast(respObj.getStr("Message"));
|
||||
|
||||
Reference in New Issue
Block a user