mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-26 09:11:52 +08:00
完成todo部分的修改
This commit is contained in:
parent
f81d56eb88
commit
22ff197f02
@ -2,35 +2,29 @@ package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
|
|||||||
|
|
||||||
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.HexUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import cn.hutool.crypto.digest.DigestUtil;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.json.JSONArray;
|
import cn.hutool.http.HttpResponse;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
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.SmsReceiveRespDTO;
|
||||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
|
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.client.dto.SmsTemplateRespDTO;
|
||||||
import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
|
import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
|
||||||
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
|
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
|
||||||
import org.apache.http.client.methods.*;
|
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -38,6 +32,7 @@ import java.util.*;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
|
||||||
|
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.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.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT;
|
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT;
|
||||||
@ -55,88 +50,90 @@ public class HuaweiSmsClient extends AbstractSmsClient {
|
|||||||
/**
|
/**
|
||||||
* 调用成功 code
|
* 调用成功 code
|
||||||
*/
|
*/
|
||||||
public static final String API_CODE_SUCCESS = "OK";
|
public static final String URL = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1";//APP接入地址+接口访问URI
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(HuaweiSmsClient.class);
|
public static final String HOST = "smsapi.cn-north-4.myhuaweicloud.com:443";
|
||||||
|
public static final String SIGNEDHEADERS = "content-type;host;x-sdk-date";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doInit() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public HuaweiSmsClient(SmsChannelProperties properties) {
|
public HuaweiSmsClient(SmsChannelProperties properties) {
|
||||||
super(properties);
|
super(properties);
|
||||||
Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空");
|
Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空");
|
||||||
Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空");
|
Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空");
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
protected void doInit() {
|
|
||||||
|
|
||||||
}
|
|
||||||
@Override
|
@Override
|
||||||
public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId,
|
public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId,
|
||||||
List<KeyValue<String, Object>> templateParams) throws Throwable {
|
List<KeyValue<String, Object>> templateParams) throws Throwable {
|
||||||
String url = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1"; //APP接入地址+接口访问URI
|
|
||||||
// 相比较阿里短信,华为短信发送的时候需要额外的参数“通道号”,考虑到不破坏原有的的结构
|
// 相比较阿里短信,华为短信发送的时候需要额外的参数“通道号”,考虑到不破坏原有的的结构
|
||||||
// 所以将 通道号 拼接到 apiTemplateId 字段中,格式为 "apiTemplateId 通道号"。空格为分隔符。
|
// 所以将 通道号 拼接到 apiTemplateId 字段中,格式为 "apiTemplateId 通道号"。空格为分隔符。
|
||||||
String sender = StrUtil.subAfter(apiTemplateId, " ", true); //中国大陆短信签名通道号或全球短信通道号
|
String sender = StrUtil.subAfter(apiTemplateId, " ", true); //中国大陆短信签名通道号或全球短信通道号
|
||||||
String templateId = StrUtil.subBefore(apiTemplateId, " ", true); //模板ID
|
String templateId = StrUtil.subBefore(apiTemplateId, " ", true); //模板ID
|
||||||
|
|
||||||
//必填,全局号码格式(包含国家码),示例:+86151****6789,多个号码之间用英文逗号分隔
|
|
||||||
String receiver = mobile; //短信接收人号码
|
|
||||||
|
|
||||||
//选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
|
//选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
|
||||||
String statusCallBack = properties.getCallbackUrl();
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.ENGLISH);
|
||||||
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
String singerDate = sdf.format(new Date());
|
String sdkDate = sdf.format(new Date());
|
||||||
|
|
||||||
// ************* 步骤 1:拼接规范请求串 *************
|
// ************* 步骤 1:拼接规范请求串 *************
|
||||||
String httpRequestMethod = "POST";
|
String httpRequestMethod = "POST";
|
||||||
String canonicalUri = "/sms/batchSendSms/v1/";
|
String canonicalUri = "/sms/batchSendSms/v1/";
|
||||||
String canonicalQueryString = "";//查询参数为空
|
String canonicalQueryString = "";//查询参数为空
|
||||||
String canonicalHeaders = "content-type:application/x-www-form-urlencoded\n"
|
String canonicalHeaders = "content-type:application/x-www-form-urlencoded\n"
|
||||||
+ "host:smsapi.cn-north-4.myhuaweicloud.com:443\n"
|
+ "host:"+ HOST +"\n"
|
||||||
+ "x-sdk-date:" + singerDate + "\n";
|
+ "x-sdk-date:" + sdkDate + "\n";
|
||||||
String signedHeaders = "content-type;host;x-sdk-date";
|
|
||||||
/**
|
|
||||||
* 选填,使用无变量模板时请赋空值 String templateParas = "";
|
|
||||||
* 单变量模板示例:模板内容为"您的验证码是${NUM_6}"时,templateParas可填写为"[\"111111\"]"
|
|
||||||
* 双变量模板示例:模板内容为"您有${NUM_2}件快递请到${TXT_20}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]"
|
|
||||||
*/
|
|
||||||
List<String> templateParas = new ArrayList<>();
|
|
||||||
for (KeyValue<String, Object> kv : templateParams) {
|
|
||||||
templateParas.add(String.valueOf(kv.getValue()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//请求Body,不携带签名名称时,signature请填null
|
//请求Body,不携带签名名称时,signature请填null
|
||||||
String body = buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, null);
|
String body = buildRequestBody(sender, mobile, templateId, templateParas, statusCallBack, null);
|
||||||
if (null == body || body.isEmpty()) {
|
if (null == body || body.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String hashedRequestBody = HexUtil.encodeHexStr(DigestUtil.sha256(body));
|
String hashedRequestBody = sha256Hex(body);
|
||||||
String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
|
String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
|
||||||
+ canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody;
|
+ canonicalHeaders + "\n" + SIGNEDHEADERS + "\n" + hashedRequestBody;
|
||||||
|
|
||||||
// ************* 步骤 2:拼接待签名字符串 *************
|
// ************* 步骤 2:拼接待签名字符串 *************
|
||||||
String hashedCanonicalRequest = HexUtil.encodeHexStr(DigestUtil.sha256(canonicalRequest));
|
String hashedCanonicalRequest = sha256Hex(canonicalRequest);
|
||||||
String stringToSign = "SDK-HMAC-SHA256" + "\n" + singerDate + "\n" + hashedCanonicalRequest;
|
String stringToSign = "SDK-HMAC-SHA256" + "\n" + sdkDate + "\n" + hashedCanonicalRequest;
|
||||||
|
|
||||||
// ************* 步骤 3:计算签名 *************
|
// ************* 步骤 3:计算签名 *************
|
||||||
String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign);
|
String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign);
|
||||||
|
|
||||||
// ************* 步骤 4:拼接 Authorization *************
|
// ************* 步骤 4:拼接 Authorization *************
|
||||||
String authorization = "SDK-HMAC-SHA256" + " " + "Access=" + properties.getApiKey() + ", "
|
String authorization = "SDK-HMAC-SHA256" + " " + "Access=" + properties.getApiKey() + ", "
|
||||||
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
|
+ "SignedHeaders=" + SIGNEDHEADERS + ", " + "Signature=" + signature;
|
||||||
|
|
||||||
// ************* 步骤 5:构造HttpRequest 并执行request请求,获得response *************
|
// ************* 步骤 5:构造HttpRequest 并执行request请求,获得response *************
|
||||||
HttpUriRequest postMethod = RequestBuilder.post()
|
HttpResponse response = HttpRequest.post(URL)
|
||||||
.setUri(url)
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||||
.setEntity(new StringEntity(body, StandardCharsets.UTF_8))
|
.header("X-Sdk-Date", sdkDate)
|
||||||
.setHeader("Content-Type","application/x-www-form-urlencoded")
|
.header("host",HOST)
|
||||||
.setHeader("X-Sdk-Date",singerDate)
|
.header("Authorization", authorization)
|
||||||
.setHeader("Authorization",authorization)
|
.body(body)
|
||||||
.build();
|
.execute();
|
||||||
CloseableHttpClient client = HttpClientBuilder.create().build();
|
|
||||||
HttpResponse response = client.execute(postMethod);
|
|
||||||
|
|
||||||
return new SmsSendRespDTO().setSuccess(Objects.equals(response.getStatusLine().getReasonPhrase(), API_CODE_SUCCESS)).setSerialNo(Integer.toString(response.getStatusLine().getStatusCode()))
|
return JSONUtil.parseObj(response.body());
|
||||||
.setApiRequestId(null).setApiCode(null).setApiMsg(null);
|
}
|
||||||
|
|
||||||
|
private SmsResponse getSmsSendResponse(JSONObject resJson) {
|
||||||
|
SmsResponse smsResponse = new SmsResponse();
|
||||||
|
smsResponse.setSuccess("000000".equals(resJson.getStr("code")));
|
||||||
|
smsResponse.setData(resJson);
|
||||||
|
return smsResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String buildRequestBody(String sender, String receiver, String templateId, List<String> templateParas,
|
static String buildRequestBody(String sender, String receiver, String templateId, List<String> templateParas,
|
||||||
@ -151,7 +148,7 @@ public class HuaweiSmsClient extends AbstractSmsClient {
|
|||||||
appendToBody(body, "from=", sender);
|
appendToBody(body, "from=", sender);
|
||||||
appendToBody(body, "&to=", receiver);
|
appendToBody(body, "&to=", receiver);
|
||||||
appendToBody(body, "&templateId=", templateId);
|
appendToBody(body, "&templateId=", templateId);
|
||||||
appendToBody(body, "&templateParas=", new JSONArray(templateParas).toString());
|
appendToBody(body, "&templateParas=", JsonUtils.toJsonString(templateParas));
|
||||||
appendToBody(body, "&statusCallback=", statusCallBack);
|
appendToBody(body, "&statusCallback=", statusCallBack);
|
||||||
appendToBody(body, "&signature=", signature);
|
appendToBody(body, "&signature=", signature);
|
||||||
return body.toString();
|
return body.toString();
|
||||||
@ -179,6 +176,22 @@ public class HuaweiSmsClient extends AbstractSmsClient {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class SmsResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否成功
|
||||||
|
*/
|
||||||
|
private boolean success;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 厂商原返回体
|
||||||
|
*/
|
||||||
|
private Object data;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 短信接收状态
|
* 短信接收状态
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user