Commit 2f38261de837b55c86b34053768bed05139e1743
1 parent
21921292
阿里云短信fix urlencode问题
Showing
1 changed file
with
115 additions
and
260 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/AliyunSmsClient.java
| 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.date.format.FastDateFormat; | ||
| 3 | import cn.hutool.core.lang.Assert; | 4 | import cn.hutool.core.lang.Assert; |
| 4 | -import cn.hutool.core.util.HexUtil; | 5 | +import cn.hutool.core.util.IdUtil; |
| 6 | +import cn.hutool.core.util.ObjectUtil; | ||
| 5 | import cn.hutool.core.util.URLUtil; | 7 | import cn.hutool.core.util.URLUtil; |
| 6 | import cn.hutool.crypto.SecureUtil; | 8 | import cn.hutool.crypto.SecureUtil; |
| 7 | import cn.hutool.crypto.digest.DigestUtil; | 9 | import cn.hutool.crypto.digest.DigestUtil; |
| 8 | import cn.hutool.http.HttpRequest; | 10 | import cn.hutool.http.HttpRequest; |
| 9 | import cn.hutool.http.HttpResponse; | 11 | import cn.hutool.http.HttpResponse; |
| 12 | +import cn.hutool.json.JSONArray; | ||
| 10 | import cn.hutool.json.JSONObject; | 13 | import cn.hutool.json.JSONObject; |
| 11 | import cn.hutool.json.JSONUtil; | 14 | import cn.hutool.json.JSONUtil; |
| 12 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 15 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| @@ -17,23 +20,15 @@ import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespD | @@ -17,23 +20,15 @@ import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespD | ||
| 17 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; | 20 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; |
| 18 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; | 21 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
| 19 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; | 22 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; |
| 20 | -import com.fasterxml.jackson.annotation.JsonFormat; | ||
| 21 | -import com.fasterxml.jackson.annotation.JsonProperty; | ||
| 22 | import com.google.common.annotations.VisibleForTesting; | 23 | import com.google.common.annotations.VisibleForTesting; |
| 23 | -import lombok.Data; | ||
| 24 | import lombok.extern.slf4j.Slf4j; | 24 | import lombok.extern.slf4j.Slf4j; |
| 25 | 25 | ||
| 26 | -import java.io.IOException; | ||
| 27 | import java.io.UnsupportedEncodingException; | 26 | import java.io.UnsupportedEncodingException; |
| 28 | -import java.net.URISyntaxException; | ||
| 29 | import java.net.URLEncoder; | 27 | import java.net.URLEncoder; |
| 30 | -import java.time.LocalDateTime; | ||
| 31 | import java.util.*; | 28 | import java.util.*; |
| 29 | +import java.util.stream.Collectors; | ||
| 32 | 30 | ||
| 33 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 31 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 34 | -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | ||
| 35 | -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; | ||
| 36 | -import java.text.SimpleDateFormat; | ||
| 37 | 32 | ||
| 38 | /** | 33 | /** |
| 39 | * 阿里短信客户端的实现类 | 34 | * 阿里短信客户端的实现类 |
| @@ -44,6 +39,12 @@ import java.text.SimpleDateFormat; | @@ -44,6 +39,12 @@ import java.text.SimpleDateFormat; | ||
| 44 | @Slf4j | 39 | @Slf4j |
| 45 | public class AliyunSmsClient extends AbstractSmsClient { | 40 | public class AliyunSmsClient extends AbstractSmsClient { |
| 46 | 41 | ||
| 42 | + private static final String URL = "https://dysmsapi.aliyuncs.com"; | ||
| 43 | + private static final String HOST = "dysmsapi.aliyuncs.com"; | ||
| 44 | + private static final String VERSION = "2017-05-25"; | ||
| 45 | + | ||
| 46 | + private static final String RESPONSE_CODE_SUCCESS = "OK"; | ||
| 47 | + | ||
| 47 | public AliyunSmsClient(SmsChannelProperties properties) { | 48 | public AliyunSmsClient(SmsChannelProperties properties) { |
| 48 | super(properties); | 49 | super(properties); |
| 49 | Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空"); | 50 | Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空"); |
| @@ -52,138 +53,68 @@ public class AliyunSmsClient extends AbstractSmsClient { | @@ -52,138 +53,68 @@ public class AliyunSmsClient extends AbstractSmsClient { | ||
| 52 | 53 | ||
| 53 | @Override | 54 | @Override |
| 54 | protected void doInit() { | 55 | protected void doInit() { |
| 55 | -// IClientProfile profile = DefaultProfile.getProfile(ENDPOINT, properties.getApiKey(), properties.getApiSecret()); | ||
| 56 | -// client = new DefaultAcsClient(profile); | ||
| 57 | } | 56 | } |
| 58 | 57 | ||
| 59 | @Override | 58 | @Override |
| 60 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId, | 59 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId, |
| 61 | List<KeyValue<String, Object>> templateParams) throws Throwable { | 60 | List<KeyValue<String, Object>> templateParams) throws Throwable { |
| 62 | - | 61 | + // 1. 执行请求 |
| 62 | + // 参考链接 https://api.aliyun.com/document/Dysmsapi/2017-05-25/SendSms | ||
| 63 | TreeMap<String, Object> queryParam = new TreeMap<>(); | 63 | TreeMap<String, Object> queryParam = new TreeMap<>(); |
| 64 | queryParam.put("PhoneNumbers",mobile); | 64 | queryParam.put("PhoneNumbers",mobile); |
| 65 | - queryParam.put("SignName",properties.getSignature()); | ||
| 66 | - queryParam.put("TemplateCode",apiTemplateId); | ||
| 67 | - queryParam.put("TemplateParam",JsonUtils.toJsonString(MapUtils.convertMap(templateParams))); | ||
| 68 | - | ||
| 69 | - JSONObject response = sendSmsRequest(queryParam,"sendSms"); | ||
| 70 | - SmsResponse smsResponse = getSmsSendResponse(response); | ||
| 71 | - | ||
| 72 | - return new SmsSendRespDTO().setSuccess(smsResponse.success).setApiMsg(smsResponse.data.toString()); | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - JSONObject sendSmsRequest(TreeMap<String, Object> queryParam,String apiName) throws IOException, URISyntaxException { | ||
| 76 | - | ||
| 77 | - // ************* 步骤 1:拼接规范请求串 ************* | ||
| 78 | - String url = "https://dysmsapi.aliyuncs.com"; //APP接入地址+接口访问URI | ||
| 79 | - String httpMethod = "POST"; // 请求方式 | ||
| 80 | - String canonicalUri = "/"; | ||
| 81 | - // 请求参数,当请求的查询字符串为空时,使用空字符串作为规范化查询字符串 | ||
| 82 | - StringBuilder canonicalQueryString = new StringBuilder(); | ||
| 83 | - queryParam.entrySet().stream().map(entry -> percentCode(entry.getKey()) + "=" + percentCode(String.valueOf(entry.getValue()))).forEachOrdered(queryPart -> { | ||
| 84 | - // 如果canonicalQueryString已经不是空的,则在查询参数前添加"&" | ||
| 85 | - if (!canonicalQueryString.isEmpty()) { | ||
| 86 | - canonicalQueryString.append("&"); | ||
| 87 | - } | ||
| 88 | - canonicalQueryString.append(queryPart); | ||
| 89 | - System.out.println("canonicalQueryString=========>\n" + canonicalQueryString); | ||
| 90 | - }); | ||
| 91 | - | ||
| 92 | - SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | ||
| 93 | - SDF.setTimeZone(new SimpleTimeZone(0, "GMT")); | ||
| 94 | - String SdfTime = SDF.format(new Date()); | ||
| 95 | - String randomUUID = UUID.randomUUID().toString(); | ||
| 96 | - | ||
| 97 | - TreeMap<String, String> headers = new TreeMap<>(); | ||
| 98 | - headers.put("host", "dysmsapi.aliyuncs.com"); | ||
| 99 | - headers.put("x-acs-action", apiName); | ||
| 100 | - headers.put("x-acs-version", "2017-05-25"); | ||
| 101 | - headers.put("x-acs-date", SdfTime); | ||
| 102 | - headers.put("x-acs-signature-nonce", randomUUID); | ||
| 103 | -// headers.put("content-type", "application/json;charset=utf-8"); | ||
| 104 | - | ||
| 105 | - // 构造请求头,多个规范化消息头,按照消息头名称(小写)的字符代码顺序以升序排列后拼接在一起 | ||
| 106 | - StringBuilder canonicalHeaders = new StringBuilder(); | ||
| 107 | - // 已签名消息头列表,多个请求头名称(小写)按首字母升序排列并以英文分号(;)分隔 | ||
| 108 | - StringBuilder signedHeadersSb = new StringBuilder(); | ||
| 109 | - headers.entrySet().stream().filter(entry -> entry.getKey().toLowerCase().startsWith("x-acs-") || entry.getKey().equalsIgnoreCase("host") || entry.getKey().equalsIgnoreCase("content-type")).sorted(Map.Entry.comparingByKey()).forEach(entry -> { | ||
| 110 | - String lowerKey = entry.getKey().toLowerCase(); | ||
| 111 | - String value = String.valueOf(entry.getValue()).trim(); | ||
| 112 | - canonicalHeaders.append(lowerKey).append(":").append(value).append("\n"); | ||
| 113 | - signedHeadersSb.append(lowerKey).append(";"); | ||
| 114 | - }); | ||
| 115 | - String signedHeaders = signedHeadersSb.substring(0, signedHeadersSb.length() - 1); | ||
| 116 | - | ||
| 117 | - String body = "";//短信API为RPC接口,query parameters在uri中拼接,因此request body如果没有特殊要求,设置为空。 | ||
| 118 | - String hashedRequestBody = HexUtil.encodeHexStr(DigestUtil.sha256(body)); | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - String canonicalRequest = httpMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody; | ||
| 122 | - System.out.println("canonicalRequest=========>\n" + canonicalRequest); | ||
| 123 | - | ||
| 124 | - // ************* 步骤 2:拼接待签名字符串 ************* | ||
| 125 | - String hashedCanonicalRequest = HexUtil.encodeHexStr(DigestUtil.sha256(canonicalRequest)); | ||
| 126 | - String stringToSign = "ACS3-HMAC-SHA256" + "\n" + hashedCanonicalRequest; | ||
| 127 | - | ||
| 128 | - // ************* 步骤 3:计算签名 ************* | ||
| 129 | - String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign); | ||
| 130 | - | ||
| 131 | - // ************* 步骤 4:拼接 Authorization ************* | ||
| 132 | - String authorization = "ACS3-HMAC-SHA256" + " " + "Credential=" + properties.getApiKey() + ", " | ||
| 133 | - + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature; | ||
| 134 | - headers.put("Authorization", authorization); | ||
| 135 | - | ||
| 136 | - // ************* 步骤 5:构造HttpRequest 并执行request请求,获得response ************* | ||
| 137 | -// url = url + canonicalUri; | ||
| 138 | - String urlWithParams = url + "?" + URLUtil.buildQuery(queryParam, null); | ||
| 139 | - | ||
| 140 | - HttpResponse response = HttpRequest.post(urlWithParams) | ||
| 141 | - .addHeaders(headers) | ||
| 142 | - .body(body) | ||
| 143 | - .execute(); | ||
| 144 | -// URIBuilder uriBuilder = new URIBuilder(url); | ||
| 145 | -// // 添加请求参数 | ||
| 146 | -// for (Map.Entry<String, Object> entry : queryParam.entrySet()) { | ||
| 147 | -// uriBuilder.addParameter(entry.getKey(), String.valueOf(entry.getValue())); | ||
| 148 | -// } | ||
| 149 | -// HttpUriRequest httpRequest = new HttpPost(uriBuilder.build()); | ||
| 150 | -//// HttpPost httpPost = new HttpPost(uriBuilder.build()); | ||
| 151 | -//// httpRequest = httpPost; | ||
| 152 | -// | ||
| 153 | -// // 添加http请求头 | ||
| 154 | -// for (Map.Entry<String, Object> entry : headers.entrySet()) { | ||
| 155 | -// httpRequest.addHeader(entry.getKey(), String.valueOf(entry.getValue())); | ||
| 156 | -// } | ||
| 157 | -// | ||
| 158 | -// // 发送请求 | ||
| 159 | -// CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
| 160 | -// CloseableHttpResponse response = httpClient.execute(httpRequest); | ||
| 161 | - System.out.println("getEntity====="+response.body()); | ||
| 162 | - System.out.println("response====="+response); | ||
| 163 | - | ||
| 164 | - return JSONUtil.parseObj(response.body()); | 65 | + queryParam.put("SignName", properties.getSignature()); |
| 66 | + queryParam.put("TemplateCode", apiTemplateId); | ||
| 67 | + queryParam.put("TemplateParam", JsonUtils.toJsonString(MapUtils.convertMap(templateParams))); | ||
| 68 | + queryParam.put("OutId", sendLogId); | ||
| 69 | + JSONObject response = request("SendSms", queryParam); | ||
| 70 | + | ||
| 71 | + // 2. 解析请求 | ||
| 72 | + return new SmsSendRespDTO() | ||
| 73 | + .setSuccess(Objects.equals(response.getStr("Code"), RESPONSE_CODE_SUCCESS)) | ||
| 74 | + .setSerialNo(response.getStr("BizId")) | ||
| 75 | + .setApiRequestId(response.getStr("RequestId")) | ||
| 76 | + .setApiCode(response.getStr("Code")) | ||
| 77 | + .setApiMsg(response.getStr("Message")); | ||
| 165 | } | 78 | } |
| 166 | 79 | ||
| 167 | @Override | 80 | @Override |
| 168 | public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { | 81 | public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { |
| 169 | - List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class); | ||
| 170 | - return convertList(statuses, status -> new SmsReceiveRespDTO().setSuccess(status.getSuccess()) | ||
| 171 | - .setErrorCode(status.getErrCode()).setErrorMsg(status.getErrMsg()) | ||
| 172 | - .setMobile(status.getPhoneNumber()).setReceiveTime(status.getReportTime()) | ||
| 173 | - .setSerialNo(status.getBizId()).setLogId(Long.valueOf(status.getOutId()))); | 82 | + JSONArray statuses = JSONUtil.parseArray(text); |
| 83 | + // 字段参考 | ||
| 84 | + return convertList(statuses, status -> { | ||
| 85 | + JSONObject statusObj = (JSONObject) status; | ||
| 86 | + return new SmsReceiveRespDTO() | ||
| 87 | + .setSuccess(statusObj.getBool("success")) // 是否接收成功 | ||
| 88 | + .setErrorCode(statusObj.getStr("err_code")) // 状态报告编码 | ||
| 89 | + .setErrorMsg(statusObj.getStr("err_msg")) // 状态报告说明 | ||
| 90 | + .setMobile(statusObj.getStr("phone_number")) // 手机号 | ||
| 91 | + .setReceiveTime(statusObj.getLocalDateTime("report_time", null)) // 状态报告时间 | ||
| 92 | + .setSerialNo(statusObj.getStr("biz_id")) // 发送序列号 | ||
| 93 | + .setLogId(statusObj.getLong("out_id")); // 用户序列号 | ||
| 94 | + }); | ||
| 174 | } | 95 | } |
| 175 | 96 | ||
| 176 | @Override | 97 | @Override |
| 177 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { | 98 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { |
| 178 | - | 99 | + // 1. 执行请求 |
| 100 | + // 参考链接 https://api.aliyun.com/document/Dysmsapi/2017-05-25/QuerySmsTemplate | ||
| 179 | TreeMap<String, Object> queryParam = new TreeMap<>(); | 101 | TreeMap<String, Object> queryParam = new TreeMap<>(); |
| 180 | - queryParam.put("TemplateCode",apiTemplateId); | 102 | + queryParam.put("TemplateCode", apiTemplateId); |
| 103 | + JSONObject response = request("QuerySmsTemplate", queryParam); | ||
| 181 | 104 | ||
| 182 | - JSONObject response = sendSmsRequest(queryParam,"QuerySmsTemplate"); | ||
| 183 | - QuerySmsTemplateResponse smsTemplateResponse = getSmsTemplateResponse(response); | ||
| 184 | - return new SmsTemplateRespDTO().setId(smsTemplateResponse.getTemplateCode()).setContent(smsTemplateResponse.getTemplateContent()) | ||
| 185 | - .setAuditStatus(convertSmsTemplateAuditStatus(smsTemplateResponse.getTemplateStatus())).setAuditReason(smsTemplateResponse.getReason()); | 105 | + System.out.println("getSmsTemplate response is =====" + response.toString()); |
| 186 | 106 | ||
| 107 | + // 2.1 请求失败 | ||
| 108 | + String code = response.getStr("Code"); | ||
| 109 | + if (ObjectUtil.notEqual(code, RESPONSE_CODE_SUCCESS)) { | ||
| 110 | + log.error("[getSmsTemplate][模版编号({}) 响应不正确({})]", apiTemplateId, response); | ||
| 111 | + return null; | ||
| 112 | + } | ||
| 113 | + // 2.2 请求成功 | ||
| 114 | + return new SmsTemplateRespDTO().setId(apiTemplateId) | ||
| 115 | + .setContent(response.getStr("TemplateContent")) | ||
| 116 | + .setAuditStatus(convertSmsTemplateAuditStatus(response.getInt("TemplateStatus"))) | ||
| 117 | + .setAuditReason(response.getStr("Reason")); | ||
| 187 | } | 118 | } |
| 188 | 119 | ||
| 189 | @VisibleForTesting | 120 | @VisibleForTesting |
| @@ -196,13 +127,68 @@ public class AliyunSmsClient extends AbstractSmsClient { | @@ -196,13 +127,68 @@ public class AliyunSmsClient extends AbstractSmsClient { | ||
| 196 | } | 127 | } |
| 197 | } | 128 | } |
| 198 | 129 | ||
| 130 | + /** | ||
| 131 | + * 请求阿里云短信 | ||
| 132 | + * | ||
| 133 | + * @see <a href="https://help.aliyun.com/zh/sdk/product-overview/v3-request-structure-and-signature">V3 版本请求体&签名机制</> | ||
| 134 | + * @param apiName 请求的 API 名称 | ||
| 135 | + * @param queryParams 请求参数 | ||
| 136 | + * @return 请求结果 | ||
| 137 | + */ | ||
| 138 | + private JSONObject request(String apiName, TreeMap<String, Object> queryParams) { | ||
| 139 | + // 1. 请求参数 | ||
| 140 | + String queryString = queryParams.entrySet().stream() | ||
| 141 | + .map(entry -> percentCode(entry.getKey()) + "=" + percentCode(String.valueOf(entry.getValue()))) | ||
| 142 | + .collect(Collectors.joining("&")); | ||
| 143 | + | ||
| 144 | + // 2.1 请求 Header | ||
| 145 | + TreeMap<String, String> headers = new TreeMap<>(); | ||
| 146 | + headers.put("host", HOST); | ||
| 147 | + headers.put("x-acs-version", VERSION); | ||
| 148 | + headers.put("x-acs-action", apiName); | ||
| 149 | + headers.put("x-acs-date", FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("GMT")).format(new Date())); | ||
| 150 | + headers.put("x-acs-signature-nonce", IdUtil.randomUUID()); | ||
| 151 | + | ||
| 152 | + // 2.2 构建签名 Header | ||
| 153 | + StringBuilder canonicalHeaders = new StringBuilder(); // 构造请求头,多个规范化消息头,按照消息头名称(小写)的字符代码顺序以升序排列后拼接在一起 | ||
| 154 | + StringBuilder signedHeadersBuilder = new StringBuilder(); // 已签名消息头列表,多个请求头名称(小写)按首字母升序排列并以英文分号(;)分隔 | ||
| 155 | + headers.entrySet().stream().filter(entry -> entry.getKey().toLowerCase().startsWith("x-acs-") | ||
| 156 | + || entry.getKey().equalsIgnoreCase("host") | ||
| 157 | + || entry.getKey().equalsIgnoreCase("content-type")) | ||
| 158 | + .sorted(Map.Entry.comparingByKey()).forEach(entry -> { | ||
| 159 | + String lowerKey = entry.getKey().toLowerCase(); | ||
| 160 | + canonicalHeaders.append(lowerKey).append(":").append(String.valueOf(entry.getValue()).trim()).append("\n"); | ||
| 161 | + signedHeadersBuilder.append(lowerKey).append(";"); | ||
| 162 | + }); | ||
| 163 | + String signedHeaders = signedHeadersBuilder.substring(0, signedHeadersBuilder.length() - 1); | ||
| 164 | + | ||
| 165 | + // 3. 请求 Body | ||
| 166 | + String requestBody = ""; // 短信 API 为 RPC 接口,query parameters 在 uri 中拼接,因此 request body 如果没有特殊要求,设置为空。 | ||
| 167 | + String hashedRequestBody = DigestUtil.sha256Hex(requestBody); | ||
| 168 | + | ||
| 169 | + // 4. 构建 Authorization 签名 | ||
| 170 | + String canonicalRequest = "POST" + "\n" + "/" + "\n" + queryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody; | ||
| 171 | + String hashedCanonicalRequest = DigestUtil.sha256Hex(canonicalRequest); | ||
| 172 | + | ||
| 173 | + String stringToSign = "ACS3-HMAC-SHA256" + "\n" + hashedCanonicalRequest; | ||
| 174 | + String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign); // 计算签名 | ||
| 175 | + headers.put("Authorization", "ACS3-HMAC-SHA256" + " " + "Credential=" + properties.getApiKey() | ||
| 176 | + + ", " + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature); | ||
| 177 | + | ||
| 178 | + // 5. 发起请求 | ||
| 179 | + String urlWithParams = URL + "?" + queryString; | ||
| 180 | + | ||
| 181 | + System.out.println("urlWithParams ======" + urlWithParams); | ||
| 182 | + try (HttpResponse response = HttpRequest.post(urlWithParams).addHeaders(headers).body(requestBody).execute()) { | ||
| 183 | + return JSONUtil.parseObj(response.body()); | ||
| 184 | + } | ||
| 185 | + } | ||
| 199 | 186 | ||
| 200 | /** | 187 | /** |
| 201 | - * 对指定的字符串进行URL编码。 | ||
| 202 | - * 使用UTF-8编码字符集对字符串进行编码,并对特定的字符进行替换,以符合URL编码规范。 | 188 | + * 对指定的字符串进行 URL 编码,并对特定的字符进行替换,以符合URL编码规范 |
| 203 | * | 189 | * |
| 204 | - * @param str 需要进行URL编码的字符串。 | ||
| 205 | - * @return 编码后的字符串。其中,加号"+"被替换为"%20",星号"*"被替换为"%2A",波浪号"%7E"被替换为"~"。 | 190 | + * @param str 需要进行URL编码的字符串 |
| 191 | + * @return 编码后的字符串 | ||
| 206 | */ | 192 | */ |
| 207 | public static String percentCode(String str) { | 193 | public static String percentCode(String str) { |
| 208 | if (str == null) { | 194 | if (str == null) { |
| @@ -215,135 +201,4 @@ public class AliyunSmsClient extends AbstractSmsClient { | @@ -215,135 +201,4 @@ public class AliyunSmsClient extends AbstractSmsClient { | ||
| 215 | } | 201 | } |
| 216 | } | 202 | } |
| 217 | 203 | ||
| 218 | - private SmsResponse getSmsSendResponse(JSONObject resJson) { | ||
| 219 | - SmsResponse smsResponse = new SmsResponse(); | ||
| 220 | - smsResponse.setSuccess("OK".equals(resJson.getStr("Code"))); | ||
| 221 | - smsResponse.setData(resJson); | ||
| 222 | -// smsResponse.setConfigId(getConfigId()); | ||
| 223 | - return smsResponse; | ||
| 224 | - } | ||
| 225 | - | ||
| 226 | - private QuerySmsTemplateResponse getSmsTemplateResponse(JSONObject resJson) { | ||
| 227 | - | ||
| 228 | - QuerySmsTemplateResponse smsTemplateResponse = new QuerySmsTemplateResponse(); | ||
| 229 | - | ||
| 230 | - smsTemplateResponse.setRequestId(resJson.getStr("RequestId")); | ||
| 231 | - smsTemplateResponse.setTemplateContent(resJson.getStr("TemplateContent")); | ||
| 232 | - smsTemplateResponse.setReason(resJson.getStr("Reason")); | ||
| 233 | - smsTemplateResponse.setTemplateStatus(resJson.getInt("TemplateStatus")); | ||
| 234 | - | ||
| 235 | - return smsTemplateResponse; | ||
| 236 | - } | ||
| 237 | - | ||
| 238 | - /** | ||
| 239 | - * <p>类名: SmsResponse | ||
| 240 | - * <p>说明: 发送短信返回信息 | ||
| 241 | - * | ||
| 242 | - * @author :scholar | ||
| 243 | - * 2024/07/17 0:25 | ||
| 244 | - **/ | ||
| 245 | - @Data | ||
| 246 | - public static class SmsResponse { | ||
| 247 | - | ||
| 248 | - /** | ||
| 249 | - * 是否成功 | ||
| 250 | - */ | ||
| 251 | - private boolean success; | ||
| 252 | - | ||
| 253 | - /** | ||
| 254 | - * 厂商原返回体 | ||
| 255 | - */ | ||
| 256 | - private Object data; | ||
| 257 | - | ||
| 258 | - /** | ||
| 259 | - * 配置标识名 如未配置取对应渠道名例如 Alibaba | ||
| 260 | - */ | ||
| 261 | - private String configId; | ||
| 262 | - } | ||
| 263 | - | ||
| 264 | - | ||
| 265 | - /** | ||
| 266 | - * <p>类名: QuerySmsTemplateResponse | ||
| 267 | - * <p>说明: sms模板查询返回信息 | ||
| 268 | - * | ||
| 269 | - * @author :scholar | ||
| 270 | - * 2024/07/17 0:25 | ||
| 271 | - **/ | ||
| 272 | - @Data | ||
| 273 | - public static class QuerySmsTemplateResponse { | ||
| 274 | - private String requestId; | ||
| 275 | - private String code; | ||
| 276 | - private String message; | ||
| 277 | - private Integer templateStatus; | ||
| 278 | - private String reason; | ||
| 279 | - private String templateCode; | ||
| 280 | - private Integer templateType; | ||
| 281 | - private String templateName; | ||
| 282 | - private String templateContent; | ||
| 283 | - private String createDate; | ||
| 284 | - } | ||
| 285 | - | ||
| 286 | - /** | ||
| 287 | - * 短信接收状态 | ||
| 288 | - * | ||
| 289 | - * 参见 <a href="https://help.aliyun.com/document_detail/101867.html">文档</a> | ||
| 290 | - * | ||
| 291 | - * @author 润普源码 | ||
| 292 | - */ | ||
| 293 | - @Data | ||
| 294 | - public static class SmsReceiveStatus { | ||
| 295 | - | ||
| 296 | - /** | ||
| 297 | - * 手机号 | ||
| 298 | - */ | ||
| 299 | - @JsonProperty("phone_number") | ||
| 300 | - private String phoneNumber; | ||
| 301 | - /** | ||
| 302 | - * 发送时间 | ||
| 303 | - */ | ||
| 304 | - @JsonProperty("send_time") | ||
| 305 | - @JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT) | ||
| 306 | - private LocalDateTime sendTime; | ||
| 307 | - /** | ||
| 308 | - * 状态报告时间 | ||
| 309 | - */ | ||
| 310 | - @JsonProperty("report_time") | ||
| 311 | - @JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT) | ||
| 312 | - private LocalDateTime reportTime; | ||
| 313 | - /** | ||
| 314 | - * 是否接收成功 | ||
| 315 | - */ | ||
| 316 | - private Boolean success; | ||
| 317 | - /** | ||
| 318 | - * 状态报告说明 | ||
| 319 | - */ | ||
| 320 | - @JsonProperty("err_msg") | ||
| 321 | - private String errMsg; | ||
| 322 | - /** | ||
| 323 | - * 状态报告编码 | ||
| 324 | - */ | ||
| 325 | - @JsonProperty("err_code") | ||
| 326 | - private String errCode; | ||
| 327 | - /** | ||
| 328 | - * 发送序列号 | ||
| 329 | - */ | ||
| 330 | - @JsonProperty("biz_id") | ||
| 331 | - private String bizId; | ||
| 332 | - /** | ||
| 333 | - * 用户序列号 | ||
| 334 | - * | ||
| 335 | - * 这里我们传递的是 SysSmsLogDO 的日志编号 | ||
| 336 | - */ | ||
| 337 | - @JsonProperty("out_id") | ||
| 338 | - private String outId; | ||
| 339 | - /** | ||
| 340 | - * 短信长度,例如说 1、2、3 | ||
| 341 | - * | ||
| 342 | - * 140 字节算一条短信,短信长度超过 140 字节时会拆分成多条短信发送 | ||
| 343 | - */ | ||
| 344 | - @JsonProperty("sms_size") | ||
| 345 | - private Integer smsSize; | ||
| 346 | - | ||
| 347 | - } | ||
| 348 | - | ||
| 349 | -} | 204 | +} |
| 350 | \ No newline at end of file | 205 | \ No newline at end of file |