Commit 2192129220388d4d877893e057bbd6f13ee935ef
1 parent
22ff197f
完成todo部分的修改
Showing
1 changed file
with
175 additions
and
37 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/TencentSmsClient.java
| ... | ... | @@ -2,6 +2,11 @@ package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.lang.Assert; |
| 4 | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | +import cn.hutool.http.HttpRequest; | |
| 6 | +import cn.hutool.http.HttpResponse; | |
| 7 | +import cn.hutool.json.JSONArray; | |
| 8 | +import cn.hutool.json.JSONObject; | |
| 9 | +import cn.hutool.json.JSONUtil; | |
| 5 | 10 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 6 | 11 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 7 | 12 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| ... | ... | @@ -13,15 +18,17 @@ import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProp |
| 13 | 18 | import com.fasterxml.jackson.annotation.JsonFormat; |
| 14 | 19 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 15 | 20 | import com.google.common.annotations.VisibleForTesting; |
| 16 | -import com.tencentcloudapi.common.Credential; | |
| 17 | -import com.tencentcloudapi.sms.v20210111.SmsClient; | |
| 18 | -import com.tencentcloudapi.sms.v20210111.models.*; | |
| 19 | 21 | import lombok.Data; |
| 20 | 22 | |
| 23 | +import javax.crypto.Mac; | |
| 24 | +import javax.crypto.spec.SecretKeySpec; | |
| 25 | +import javax.xml.bind.DatatypeConverter; | |
| 26 | +import java.nio.charset.StandardCharsets; | |
| 27 | +import java.text.SimpleDateFormat; | |
| 21 | 28 | import java.time.LocalDateTime; |
| 22 | -import java.util.List; | |
| 23 | -import java.util.Objects; | |
| 29 | +import java.util.*; | |
| 24 | 30 | |
| 31 | +import static cn.hutool.crypto.digest.DigestUtil.sha256Hex; | |
| 25 | 32 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 26 | 33 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; |
| 27 | 34 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; |
| ... | ... | @@ -41,11 +48,6 @@ public class TencentSmsClient extends AbstractSmsClient { |
| 41 | 48 | public static final String API_CODE_SUCCESS = "Ok"; |
| 42 | 49 | |
| 43 | 50 | /** |
| 44 | - * REGION,使用南京 | |
| 45 | - */ | |
| 46 | - private static final String ENDPOINT = "ap-nanjing"; | |
| 47 | - | |
| 48 | - /** | |
| 49 | 51 | * 是否国际/港澳台短信: |
| 50 | 52 | * |
| 51 | 53 | * 0:表示国内短信。 |
| ... | ... | @@ -53,7 +55,6 @@ public class TencentSmsClient extends AbstractSmsClient { |
| 53 | 55 | */ |
| 54 | 56 | private static final long INTERNATIONAL_CHINA = 0L; |
| 55 | 57 | |
| 56 | - private SmsClient client; | |
| 57 | 58 | |
| 58 | 59 | public TencentSmsClient(SmsChannelProperties properties) { |
| 59 | 60 | super(properties); |
| ... | ... | @@ -63,9 +64,7 @@ public class TencentSmsClient extends AbstractSmsClient { |
| 63 | 64 | |
| 64 | 65 | @Override |
| 65 | 66 | protected void doInit() { |
| 66 | - // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey | |
| 67 | - Credential credential = new Credential(getApiKey(), properties.getApiSecret()); | |
| 68 | - client = new SmsClient(credential, ENDPOINT); | |
| 67 | + | |
| 69 | 68 | } |
| 70 | 69 | |
| 71 | 70 | /** |
| ... | ... | @@ -96,18 +95,87 @@ public class TencentSmsClient extends AbstractSmsClient { |
| 96 | 95 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, |
| 97 | 96 | String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable { |
| 98 | 97 | // 构建请求 |
| 99 | - SendSmsRequest request = new SendSmsRequest(); | |
| 100 | - request.setSmsSdkAppId(getSdkAppId()); | |
| 101 | - request.setPhoneNumberSet(new String[]{mobile}); | |
| 102 | - request.setSignName(properties.getSignature()); | |
| 103 | - request.setTemplateId(apiTemplateId); | |
| 104 | - request.setTemplateParamSet(ArrayUtils.toArray(templateParams, e -> String.valueOf(e.getValue()))); | |
| 105 | - request.setSessionContext(JsonUtils.toJsonString(new SessionContext().setLogId(sendLogId))); | |
| 106 | - // 执行请求 | |
| 107 | - SendSmsResponse response = client.SendSms(request); | |
| 108 | - SendStatus status = response.getSendStatusSet()[0]; | |
| 109 | - return new SmsSendRespDTO().setSuccess(Objects.equals(status.getCode(), API_CODE_SUCCESS)).setSerialNo(status.getSerialNo()) | |
| 110 | - .setApiRequestId(response.getRequestId()).setApiCode(status.getCode()).setApiMsg(status.getMessage()); | |
| 98 | + TreeMap<String, Object> body = new TreeMap<>(); | |
| 99 | + String[] phones = {mobile}; | |
| 100 | + body.put("PhoneNumberSet",phones); | |
| 101 | + body.put("SmsSdkAppId",getSdkAppId()); | |
| 102 | + body.put("SignName",properties.getSignature()); | |
| 103 | + body.put("TemplateId",apiTemplateId); | |
| 104 | + body.put("TemplateParamSet",ArrayUtils.toArray(templateParams, e -> String.valueOf(e.getValue()))); | |
| 105 | + | |
| 106 | + JSONObject JsonResponse = sendSmsRequest(body,"SendSms","2021-01-11","ap-guangzhou"); | |
| 107 | + SmsResponse smsResponse = getSmsSendResponse(JsonResponse); | |
| 108 | + | |
| 109 | + return new SmsSendRespDTO().setSuccess(smsResponse.success).setApiMsg(smsResponse.data.toString()); | |
| 110 | + | |
| 111 | + } | |
| 112 | + | |
| 113 | + JSONObject sendSmsRequest(TreeMap<String, Object> body,String action,String version,String region) throws Exception { | |
| 114 | + | |
| 115 | + String timestamp = String.valueOf(System.currentTimeMillis() / 1000); | |
| 116 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
| 117 | + // 注意时区,否则容易出错 | |
| 118 | + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); | |
| 119 | + String date = sdf.format(new Date(Long.valueOf(timestamp + "000"))); | |
| 120 | + | |
| 121 | + // ************* 步骤 1:拼接规范请求串 ************* | |
| 122 | + String host = "sms.tencentcloudapi.com"; //APP接入地址+接口访问URI | |
| 123 | + String httpMethod = "POST"; // 请求方式 | |
| 124 | + String canonicalUri = "/"; | |
| 125 | + String canonicalQueryString = ""; | |
| 126 | + | |
| 127 | + String canonicalHeaders = "content-type:application/json; charset=utf-8\n" | |
| 128 | + + "host:" + host + "\n" + "x-tc-action:" + action.toLowerCase() + "\n"; | |
| 129 | + String signedHeaders = "content-type;host;x-tc-action"; | |
| 130 | + String hashedRequestBody = sha256Hex(JSONUtil.toJsonStr(body)); | |
| 131 | + String canonicalRequest = httpMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody; | |
| 132 | + | |
| 133 | + // ************* 步骤 2:拼接待签名字符串 ************* | |
| 134 | + String credentialScope = date + "/" + "sms" + "/" + "tc3_request"; | |
| 135 | + String hashedCanonicalRequest = sha256Hex(canonicalRequest); | |
| 136 | + String stringToSign = "TC3-HMAC-SHA256" + "\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest; | |
| 137 | + | |
| 138 | + // ************* 步骤 3:计算签名 ************* | |
| 139 | + byte[] secretDate = hmac256(("TC3" + properties.getApiSecret()).getBytes(StandardCharsets.UTF_8), date); | |
| 140 | + byte[] secretService = hmac256(secretDate, "sms"); | |
| 141 | + byte[] secretSigning = hmac256(secretService, "tc3_request"); | |
| 142 | + String signature = DatatypeConverter.printHexBinary(hmac256(secretSigning, stringToSign)).toLowerCase(); | |
| 143 | + | |
| 144 | + // ************* 步骤 4:拼接 Authorization ************* | |
| 145 | + String authorization = "TC3-HMAC-SHA256" + " " + "Credential=" + getApiKey() + "/" + credentialScope + ", " | |
| 146 | + + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature; | |
| 147 | + | |
| 148 | + // ************* 步骤 5:构造HttpRequest 并执行request请求,获得response ************* | |
| 149 | + Map<String, String> headers = new HashMap<>(); | |
| 150 | + headers.put("Authorization", authorization); | |
| 151 | + headers.put("Content-Type", "application/json; charset=utf-8"); | |
| 152 | + headers.put("Host", host); | |
| 153 | + headers.put("X-TC-Action", action); | |
| 154 | + headers.put("X-TC-Timestamp", timestamp); | |
| 155 | + headers.put("X-TC-Version", version); | |
| 156 | + headers.put("X-TC-Region", region); | |
| 157 | + | |
| 158 | + HttpResponse response = HttpRequest.post("https://"+host) | |
| 159 | + .addHeaders(headers) | |
| 160 | + .body(JSONUtil.toJsonStr(body)) | |
| 161 | + .execute(); | |
| 162 | + | |
| 163 | + return JSONUtil.parseObj(response.body()); | |
| 164 | + } | |
| 165 | + | |
| 166 | + public static byte[] hmac256(byte[] key, String msg) throws Exception { | |
| 167 | + Mac mac = Mac.getInstance("HmacSHA256"); | |
| 168 | + SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm()); | |
| 169 | + mac.init(secretKeySpec); | |
| 170 | + return mac.doFinal(msg.getBytes(StandardCharsets.UTF_8)); | |
| 171 | + } | |
| 172 | + | |
| 173 | + private SmsResponse getSmsSendResponse(JSONObject resJson) { | |
| 174 | + SmsResponse smsResponse = new SmsResponse(); | |
| 175 | + JSONArray statusJson =resJson.getJSONObject("Response").getJSONArray("SendStatusSet"); | |
| 176 | + smsResponse.setSuccess("Ok".equals(statusJson.getJSONObject(0).getStr("Code"))); | |
| 177 | + smsResponse.setData(resJson); | |
| 178 | + return smsResponse; | |
| 111 | 179 | } |
| 112 | 180 | |
| 113 | 181 | @Override |
| ... | ... | @@ -122,18 +190,49 @@ public class TencentSmsClient extends AbstractSmsClient { |
| 122 | 190 | |
| 123 | 191 | @Override |
| 124 | 192 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { |
| 193 | + | |
| 125 | 194 | // 构建请求 |
| 126 | - DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); | |
| 127 | - request.setTemplateIdSet(new Long[]{Long.parseLong(apiTemplateId)}); | |
| 128 | - request.setInternational(INTERNATIONAL_CHINA); | |
| 129 | - // 执行请求 | |
| 130 | - DescribeSmsTemplateListResponse response = client.DescribeSmsTemplateList(request); | |
| 131 | - DescribeTemplateListStatus status = response.getDescribeTemplateStatusSet()[0]; | |
| 132 | - if (status == null || status.getStatusCode() == null) { | |
| 133 | - return null; | |
| 134 | - } | |
| 135 | - return new SmsTemplateRespDTO().setId(status.getTemplateId().toString()).setContent(status.getTemplateContent()) | |
| 136 | - .setAuditStatus(convertSmsTemplateAuditStatus(status.getStatusCode().intValue())).setAuditReason(status.getReviewReply()); | |
| 195 | + TreeMap<String, Object> body = new TreeMap<>(); | |
| 196 | + body.put("International",0); | |
| 197 | + Integer[] templateIds = {Integer.valueOf(apiTemplateId)}; | |
| 198 | + body.put("TemplateIdSet",templateIds); | |
| 199 | + | |
| 200 | + JSONObject JsonResponse = sendSmsRequest(body,"DescribeSmsTemplateList","2021-01-11","ap-guangzhou"); | |
| 201 | + QuerySmsTemplateResponse smsTemplateResponse = getSmsTemplateResponse(JsonResponse); | |
| 202 | + String templateId = Integer.toString(smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getTemplateId()); | |
| 203 | + String content = smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getTemplateContent(); | |
| 204 | + Integer templateStatus = smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getStatusCode(); | |
| 205 | + String auditReason = smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getReviewReply(); | |
| 206 | + | |
| 207 | + return new SmsTemplateRespDTO().setId(templateId).setContent(content) | |
| 208 | + .setAuditStatus(convertSmsTemplateAuditStatus(templateStatus)).setAuditReason(auditReason); | |
| 209 | + } | |
| 210 | + | |
| 211 | + private QuerySmsTemplateResponse getSmsTemplateResponse(JSONObject resJson) { | |
| 212 | + | |
| 213 | + QuerySmsTemplateResponse smsTemplateResponse = new QuerySmsTemplateResponse(); | |
| 214 | + | |
| 215 | + smsTemplateResponse.setRequestId(resJson.getJSONObject("Response").getStr("RequestId")); | |
| 216 | + | |
| 217 | + smsTemplateResponse.setDescribeTemplateStatusSet(new ArrayList<>()); | |
| 218 | + | |
| 219 | + QuerySmsTemplateResponse.TemplateInfo templateInfo = new QuerySmsTemplateResponse.TemplateInfo(); | |
| 220 | + | |
| 221 | + Object statusObject = resJson.getJSONObject("Response").getJSONArray("DescribeTemplateStatusSet").getFirst(); | |
| 222 | + | |
| 223 | + JSONObject statusJSON = new JSONObject(statusObject); | |
| 224 | + | |
| 225 | + templateInfo.setTemplateContent(statusJSON.get("TemplateContent").toString()); | |
| 226 | + | |
| 227 | + templateInfo.setStatusCode(Integer.parseInt(statusJSON.get("StatusCode").toString())); | |
| 228 | + | |
| 229 | + templateInfo.setReviewReply(statusJSON.get("ReviewReply").toString()); | |
| 230 | + | |
| 231 | + templateInfo.setTemplateId(Integer.parseInt(statusJSON.get("TemplateId").toString())); | |
| 232 | + | |
| 233 | + smsTemplateResponse.getDescribeTemplateStatusSet().add(templateInfo); | |
| 234 | + | |
| 235 | + return smsTemplateResponse; | |
| 137 | 236 | } |
| 138 | 237 | |
| 139 | 238 | @VisibleForTesting |
| ... | ... | @@ -147,6 +246,45 @@ public class TencentSmsClient extends AbstractSmsClient { |
| 147 | 246 | } |
| 148 | 247 | |
| 149 | 248 | @Data |
| 249 | + public static class SmsResponse { | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * 是否成功 | |
| 253 | + */ | |
| 254 | + private boolean success; | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * 厂商原返回体 | |
| 258 | + */ | |
| 259 | + private Object data; | |
| 260 | + | |
| 261 | + } | |
| 262 | + | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * <p>类名: QuerySmsTemplateResponse | |
| 266 | + * <p>说明: sms模板查询返回信息 | |
| 267 | + * | |
| 268 | + * @author :scholar | |
| 269 | + * 2024/07/17 0:25 | |
| 270 | + **/ | |
| 271 | + @Data | |
| 272 | + public static class QuerySmsTemplateResponse { | |
| 273 | + private List<TemplateInfo> DescribeTemplateStatusSet; | |
| 274 | + private String RequestId; | |
| 275 | + @Data | |
| 276 | + static class TemplateInfo { | |
| 277 | + private String TemplateName; | |
| 278 | + private Integer TemplateId; | |
| 279 | + private Integer International; | |
| 280 | + private String ReviewReply; | |
| 281 | + private long CreateTime; | |
| 282 | + private String TemplateContent; | |
| 283 | + private Integer StatusCode; | |
| 284 | + } | |
| 285 | + } | |
| 286 | + | |
| 287 | + @Data | |
| 150 | 288 | private static class SmsReceiveStatus { |
| 151 | 289 | |
| 152 | 290 | /** | ... | ... |