|
@@ -1,41 +1,35 @@
|
|
|
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.ListUtil;
|
|
|
+import cn.hutool.core.date.format.FastDateFormat;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
-import cn.hutool.http.HttpRequest;
|
|
|
-import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
|
|
-import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
|
+import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
|
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
|
|
|
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
|
|
|
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
|
|
import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
|
|
|
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.annotation.JsonFormat;
|
|
|
-import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
-import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import static cn.hutool.crypto.digest.DigestUtil.sha256Hex;
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
|
|
-import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
|
|
-import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT;
|
|
|
|
|
|
-// todo @scholar:参考阿里云在优化下
|
|
|
/**
|
|
|
* 华为短信客户端的实现类
|
|
|
*
|
|
@@ -45,182 +39,128 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DE
|
|
|
@Slf4j
|
|
|
public class HuaweiSmsClient extends AbstractSmsClient {
|
|
|
|
|
|
- /**
|
|
|
- * 调用成功 code
|
|
|
- */
|
|
|
- public static final String URL = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1";//APP接入地址+接口访问URI
|
|
|
- public static final String HOST = "smsapi.cn-north-4.myhuaweicloud.com:443";
|
|
|
- public static final String SIGNEDHEADERS = "content-type;host;x-sdk-date";
|
|
|
+ private static final String URL = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1";//APP接入地址+接口访问URI
|
|
|
+ private static final String HOST = "smsapi.cn-north-4.myhuaweicloud.com:443";
|
|
|
+ private static final String SIGNEDHEADERS = "content-type;host;x-sdk-date";
|
|
|
|
|
|
- @Override
|
|
|
- protected void doInit() {
|
|
|
- }
|
|
|
+ private static final String RESPONSE_CODE_SUCCESS = "000000";
|
|
|
|
|
|
public HuaweiSmsClient(SmsChannelProperties properties) {
|
|
|
super(properties);
|
|
|
Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空");
|
|
|
Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空");
|
|
|
+ validateSender(properties);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId,
|
|
|
- List<KeyValue<String, Object>> templateParams) throws Throwable {
|
|
|
- // 参考链接 https://support.huaweicloud.com/api-msgsms/sms_05_0001.html
|
|
|
- // 相比较阿里短信,华为短信发送的时候需要额外的参数“通道号”,考虑到不破坏原有的的结构
|
|
|
- // 所以将 通道号 拼接到 apiTemplateId 字段中,格式为 "apiTemplateId 通道号"。空格为分隔符。
|
|
|
- String sender = StrUtil.subAfter(apiTemplateId, " ", true); //中国大陆短信签名通道号或全球短信通道号
|
|
|
- String templateId = StrUtil.subBefore(apiTemplateId, " ", true); //模板ID
|
|
|
-
|
|
|
- //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
|
|
|
- String statusCallBack = properties.getCallbackUrl();
|
|
|
-
|
|
|
- List<String> templateParas = CollectionUtils.convertList(templateParams, kv -> String.valueOf(kv.getValue()));
|
|
|
-
|
|
|
- JSONObject JsonResponse = sendSmsRequest(sender,mobile,templateId,templateParas,statusCallBack);
|
|
|
- SmsResponse smsResponse = getSmsSendResponse(JsonResponse);
|
|
|
-
|
|
|
- return new SmsSendRespDTO().setSuccess(smsResponse.success).setApiMsg(smsResponse.data.toString());
|
|
|
+ /**
|
|
|
+ * 参数校验华为云的 sender 通道号
|
|
|
+ *
|
|
|
+ * 原因是:验华为云发放短信的时候,需要额外的参数 sender
|
|
|
+ *
|
|
|
+ * 解决方案:考虑到不破坏原有的 apiKey + apiSecret 的结构,所以将 secretId 拼接到 apiKey 字段中,格式为 "secretId sdkAppId"。
|
|
|
+ *
|
|
|
+ * @param properties 配置
|
|
|
+ */
|
|
|
+ private static void validateSender(SmsChannelProperties properties) {
|
|
|
+ String combineKey = properties.getApiKey();
|
|
|
+ Assert.notEmpty(combineKey, "apiKey 不能为空");
|
|
|
+ String[] keys = combineKey.trim().split(" ");
|
|
|
+ Assert.isTrue(keys.length == 2, "华为云短信 apiKey 配置格式错误,请配置 为[accessKeyId sender]");
|
|
|
}
|
|
|
|
|
|
- JSONObject sendSmsRequest(String sender,String mobile,String templateId,List<String> templateParas,String statusCallBack) throws UnsupportedEncodingException {
|
|
|
-
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.ENGLISH);
|
|
|
- sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
- String sdkDate = sdf.format(new Date());
|
|
|
-
|
|
|
- // ************* 步骤 1:拼接规范请求串 *************
|
|
|
- String httpRequestMethod = "POST";
|
|
|
- String canonicalUri = "/sms/batchSendSms/v1/";
|
|
|
- String canonicalQueryString = "";//查询参数为空
|
|
|
- String canonicalHeaders = "content-type:application/x-www-form-urlencoded\n"
|
|
|
- + "host:"+ HOST +"\n"
|
|
|
- + "x-sdk-date:" + sdkDate + "\n";
|
|
|
- //请求Body,不携带签名名称时,signature请填null
|
|
|
- String body = buildRequestBody(sender, mobile, templateId, templateParas, statusCallBack, null);
|
|
|
- if (null == body || body.isEmpty()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- String hashedRequestBody = sha256Hex(body);
|
|
|
- String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
|
|
|
- + canonicalHeaders + "\n" + SIGNEDHEADERS + "\n" + hashedRequestBody;
|
|
|
-
|
|
|
- // ************* 步骤 2:拼接待签名字符串 *************
|
|
|
- String hashedCanonicalRequest = sha256Hex(canonicalRequest);
|
|
|
- String stringToSign = "SDK-HMAC-SHA256" + "\n" + sdkDate + "\n" + hashedCanonicalRequest;
|
|
|
-
|
|
|
- // ************* 步骤 3:计算签名 *************
|
|
|
- String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign);
|
|
|
-
|
|
|
- // ************* 步骤 4:拼接 Authorization *************
|
|
|
- String authorization = "SDK-HMAC-SHA256" + " " + "Access=" + properties.getApiKey() + ", "
|
|
|
- + "SignedHeaders=" + SIGNEDHEADERS + ", " + "Signature=" + signature;
|
|
|
-
|
|
|
- // ************* 步骤 5:构造HttpRequest 并执行request请求,获得response *************
|
|
|
- HttpResponse response = HttpRequest.post(URL)
|
|
|
- .header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
- .header("X-Sdk-Date", sdkDate)
|
|
|
- .header("host",HOST)
|
|
|
- .header("Authorization", authorization)
|
|
|
- .body(body)
|
|
|
- .execute();
|
|
|
-
|
|
|
- return JSONUtil.parseObj(response.body());
|
|
|
+ private String getAccessKey() {
|
|
|
+ return StrUtil.subBefore(properties.getApiKey(), " ", true);
|
|
|
}
|
|
|
|
|
|
- private SmsResponse getSmsSendResponse(JSONObject resJson) {
|
|
|
- SmsResponse smsResponse = new SmsResponse();
|
|
|
- smsResponse.setSuccess("000000".equals(resJson.getStr("code")));
|
|
|
- smsResponse.setData(resJson);
|
|
|
- return smsResponse;
|
|
|
+ private String getSender() {
|
|
|
+ return StrUtil.subAfter(properties.getApiKey(), " ", true);
|
|
|
}
|
|
|
|
|
|
- static String buildRequestBody(String sender, String receiver, String templateId, List<String> templateParas,
|
|
|
- String statusCallBack, String signature) throws UnsupportedEncodingException {
|
|
|
- if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()
|
|
|
- || templateId.isEmpty()) {
|
|
|
- System.out.println("buildRequestBody(): sender, receiver or templateId is null.");
|
|
|
- return null;
|
|
|
+ @Override
|
|
|
+ public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId,
|
|
|
+ List<KeyValue<String, Object>> templateParams) throws Throwable {
|
|
|
+ StringBuilder requestBody = new StringBuilder();
|
|
|
+ appendToBody(requestBody, "from=", getSender());
|
|
|
+ appendToBody(requestBody, "&to=", mobile);
|
|
|
+ appendToBody(requestBody, "&templateId=", apiTemplateId);
|
|
|
+ appendToBody(requestBody, "&templateParas=", JsonUtils.toJsonString(
|
|
|
+ convertList(templateParams, kv -> String.valueOf(kv.getValue()))));
|
|
|
+ appendToBody(requestBody, "&statusCallback=", properties.getCallbackUrl());
|
|
|
+ appendToBody(requestBody, "&extend=", String.valueOf(sendLogId));
|
|
|
+ JSONObject response = request("/sms/batchSendSms/v1/", "POST", requestBody.toString());
|
|
|
+
|
|
|
+ // 2. 解析请求
|
|
|
+ if (!response.containsKey("result")) { // 例如说:密钥不正确
|
|
|
+ return new SmsSendRespDTO().setSuccess(false)
|
|
|
+ .setApiCode(response.getStr("code"))
|
|
|
+ .setApiMsg(response.getStr("description"));
|
|
|
}
|
|
|
-
|
|
|
- StringBuilder body = new StringBuilder();
|
|
|
- appendToBody(body, "from=", sender);
|
|
|
- appendToBody(body, "&to=", receiver);
|
|
|
- appendToBody(body, "&templateId=", templateId);
|
|
|
- appendToBody(body, "&templateParas=", JsonUtils.toJsonString(templateParas));
|
|
|
- appendToBody(body, "&statusCallback=", statusCallBack);
|
|
|
- appendToBody(body, "&signature=", signature);
|
|
|
- return body.toString();
|
|
|
+ JSONObject sendResult = response.getJSONArray("result").getJSONObject(0);
|
|
|
+ return new SmsSendRespDTO().setSuccess(RESPONSE_CODE_SUCCESS.equals(response.getStr("code")))
|
|
|
+ .setSerialNo(sendResult.getStr("smsMsgId")).setApiCode(sendResult.getStr("status"));
|
|
|
}
|
|
|
|
|
|
- private static void appendToBody(StringBuilder body, String key, String val) throws UnsupportedEncodingException {
|
|
|
- if (null != val && !val.isEmpty()) {
|
|
|
- body.append(key).append(URLEncoder.encode(val, "UTF-8"));
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 请求华为云短信
|
|
|
+ *
|
|
|
+ * @see <a href="认证鉴权">https://support.huaweicloud.com/api-msgsms/sms_05_0046.html</a>
|
|
|
+ * @param uri 请求 URI
|
|
|
+ * @param method 请求 Method
|
|
|
+ * @param requestBody 请求 Body
|
|
|
+ * @return 请求结果
|
|
|
+ */
|
|
|
+ private JSONObject request(String uri, String method, String requestBody) {
|
|
|
+ // 1.1 请求 Header
|
|
|
+ TreeMap<String, String> headers = new TreeMap<>();
|
|
|
+ headers.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+ String sdkDate = FastDateFormat.getInstance("yyyyMMdd'T'HHmmss'Z'", TimeZone.getTimeZone("UTC")).format(new Date());
|
|
|
+ headers.put("X-Sdk-Date", sdkDate);
|
|
|
+ headers.put("host", HOST);
|
|
|
+
|
|
|
+ // 1.2 构建签名 Header
|
|
|
+ String canonicalQueryString = ""; // 查询参数为空
|
|
|
+ String canonicalHeaders = "content-type:application/x-www-form-urlencoded\n"
|
|
|
+ + "host:"+ HOST +"\n" + "x-sdk-date:" + sdkDate + "\n";
|
|
|
+ String canonicalRequest = method + "\n" + uri + "\n" + canonicalQueryString + "\n"
|
|
|
+ + canonicalHeaders + "\n" + SIGNEDHEADERS + "\n" + sha256Hex(requestBody);
|
|
|
+ String stringToSign = "SDK-HMAC-SHA256" + "\n" + sdkDate + "\n" + sha256Hex(canonicalRequest);
|
|
|
+ String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign); // 计算签名
|
|
|
+ headers.put("Authorization", "SDK-HMAC-SHA256" + " " + "Access=" + getAccessKey()
|
|
|
+ + ", " + "SignedHeaders=" + SIGNEDHEADERS + ", " + "Signature=" + signature);
|
|
|
+
|
|
|
+ // 2. 发起请求
|
|
|
+ String responseBody = HttpUtils.post(URL, headers, requestBody);
|
|
|
+ return JSONUtil.parseObj(responseBody);
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
- public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) {
|
|
|
- List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class);
|
|
|
- return convertList(statuses, status -> new SmsReceiveRespDTO().setSuccess(Objects.equals(status.getStatus(),"DELIVRD"))
|
|
|
- .setErrorCode(status.getStatus()).setErrorMsg(status.getStatus())
|
|
|
- .setMobile(status.getPhoneNumber()).setReceiveTime(status.getUpdateTime())
|
|
|
- .setSerialNo(status.getSmsMsgId()));
|
|
|
+ public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String requestBody) {
|
|
|
+ Map<String, String> params = HttpUtil.decodeParamMap(requestBody, StandardCharsets.UTF_8);
|
|
|
+ // 字段参考 https://support.huaweicloud.com/api-msgsms/sms_05_0003.html
|
|
|
+ return ListUtil.of(new SmsReceiveRespDTO()
|
|
|
+ .setSuccess("DELIVRD".equals(params.get("status"))) // 是否接收成功
|
|
|
+ .setErrorCode(params.get("status")) // 状态报告编码
|
|
|
+ .setErrorMsg(params.get("statusDesc"))
|
|
|
+ .setMobile(params.get("to")) // 手机号
|
|
|
+ .setReceiveTime(LocalDateTime.ofInstant(Instant.parse(params.get("updateTime")), ZoneId.of("UTC"))) // 状态报告时间
|
|
|
+ .setSerialNo(params.get("smsMsgId")) // 发送序列号
|
|
|
+ .setLogId(Long.valueOf(params.get("extend")))); // 用户序列号
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable {
|
|
|
- //华为短信模板查询和发送短信,是不同的两套key和secret,与阿里、腾讯的区别较大,这里模板查询校验暂不实现。
|
|
|
- return new SmsTemplateRespDTO().setId(null).setContent(null)
|
|
|
+ // 华为短信模板查询和发送短信,是不同的两套 key 和 secret,与阿里、腾讯的区别较大,这里模板查询校验暂不实现
|
|
|
+ String[] strs = apiTemplateId.split(" ");
|
|
|
+ Assert.isTrue(strs.length == 2, "格式不正确,需要满足:apiTemplateId sender");
|
|
|
+ return new SmsTemplateRespDTO().setId(apiTemplateId).setContent(null)
|
|
|
.setAuditStatus(SmsTemplateAuditStatusEnum.SUCCESS.getStatus()).setAuditReason(null);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Data
|
|
|
- public static class SmsResponse {
|
|
|
-
|
|
|
- /**
|
|
|
- * 是否成功
|
|
|
- */
|
|
|
- private boolean success;
|
|
|
-
|
|
|
- /**
|
|
|
- * 厂商原返回体
|
|
|
- */
|
|
|
- private Object data;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 短信接收状态
|
|
|
- *
|
|
|
- * 参见 <a href="https://support.huaweicloud.com/api-msgsms/sms_05_0003.html">文档</a>
|
|
|
- *
|
|
|
- * @author scholar
|
|
|
- */
|
|
|
- @Data
|
|
|
- public static class SmsReceiveStatus {
|
|
|
-
|
|
|
- /**
|
|
|
- * 本条状态报告对应的短信的接收方号码,仅当状态报告中携带了extend参数时才会同时携带该参数
|
|
|
- */
|
|
|
- @JsonProperty("to")
|
|
|
- private String phoneNumber;
|
|
|
-
|
|
|
- /**
|
|
|
- * 短信资源的更新时间,通常为短信平台接收短信状态报告的时间
|
|
|
- */
|
|
|
- @JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
|
|
|
- private LocalDateTime updateTime;
|
|
|
-
|
|
|
- /**
|
|
|
- * 短信状态报告枚举值
|
|
|
- */
|
|
|
- private String status;
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送短信成功时返回的短信唯一标识。
|
|
|
- */
|
|
|
- private String smsMsgId;
|
|
|
+ @SuppressWarnings("CharsetObjectCanBeUsed")
|
|
|
+ private static void appendToBody(StringBuilder body, String key, String value) throws UnsupportedEncodingException {
|
|
|
+ if (StrUtil.isNotEmpty(value)) {
|
|
|
+ body.append(key).append(URLEncoder.encode(value, CharsetUtil.CHARSET_UTF_8.name()));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+}
|