【代码优化】SYSTEM:去除阿里云短信的 maven 依赖

This commit is contained in:
YunaiV 2024-08-07 13:03:42 +08:00
parent 2dd66847cf
commit 30c6f1eb77
7 changed files with 609 additions and 403 deletions

View File

@ -69,8 +69,6 @@
<okhttp3.version>4.11.0</okhttp3.version> <okhttp3.version>4.11.0</okhttp3.version>
<commons-io.version>2.15.1</commons-io.version> <commons-io.version>2.15.1</commons-io.version>
<minio.version>8.5.7</minio.version> <minio.version>8.5.7</minio.version>
<aliyun-java-sdk-core.version>4.6.4</aliyun-java-sdk-core.version>
<aliyun-java-sdk-dysmsapi.version>2.2.1</aliyun-java-sdk-dysmsapi.version>
<tencentcloud-sdk-java.version>3.1.880</tencentcloud-sdk-java.version> <tencentcloud-sdk-java.version>3.1.880</tencentcloud-sdk-java.version>
<justauth.version>2.0.5</justauth.version> <justauth.version>2.0.5</justauth.version>
<jimureport.version>1.7.8</jimureport.version> <jimureport.version>1.7.8</jimureport.version>
@ -548,26 +546,6 @@
</dependency> </dependency>
<!-- SMS SDK begin --> <!-- SMS SDK begin -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun-java-sdk-core.version}</version>
<exclusions>
<exclusion>
<artifactId>opentracing-api</artifactId>
<groupId>io.opentracing</groupId>
</exclusion>
<exclusion>
<artifactId>opentracing-util</artifactId>
<groupId>io.opentracing</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>${aliyun-java-sdk-dysmsapi.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.tencentcloudapi</groupId> <groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId> <artifactId>tencentcloud-sdk-java-sms</artifactId>

View File

@ -110,19 +110,6 @@
<artifactId>wx-java-miniapp-spring-boot-starter</artifactId> <!-- 微信登录(小程序) --> <artifactId>wx-java-miniapp-spring-boot-starter</artifactId> <!-- 微信登录(小程序) -->
</dependency> </dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId> <!-- 短信(阿里云) -->
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId> <!-- 短信(阿里云) -->
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId> <!-- 短信(腾讯云) -->
</dependency>
<dependency> <dependency>
<groupId>com.xingyuv</groupId> <groupId>com.xingyuv</groupId>
<artifactId>spring-boot-starter-captcha-plus</artifactId> <!-- 验证码,一般用于登录使用 --> <artifactId>spring-boot-starter-captcha-plus</artifactId> <!-- 验证码,一般用于登录使用 -->

View File

@ -46,6 +46,8 @@ public interface SmsClient {
/** /**
* 查询指定的短信模板 * 查询指定的短信模板
* *
* 如果查询失败则返回 null
*
* @param apiTemplateId 短信 API 的模板编号 * @param apiTemplateId 短信 API 的模板编号
* @return 短信模板 * @return 短信模板
*/ */

View File

@ -1,12 +1,15 @@
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil; import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil; import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
@ -17,23 +20,13 @@ import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespD
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 com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
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.TIME_ZONE_DEFAULT;
import java.text.SimpleDateFormat;
/** /**
* 阿里短信客户端的实现类 * 阿里短信客户端的实现类
@ -44,6 +37,12 @@ import java.text.SimpleDateFormat;
@Slf4j @Slf4j
public class AliyunSmsClient extends AbstractSmsClient { public class AliyunSmsClient extends AbstractSmsClient {
private static final String URL = "https://dysmsapi.aliyuncs.com";
private static final String HOST = "dysmsapi.aliyuncs.com";
private static final String VERSION = "2017-05-25";
private static final String RESPONSE_CODE_SUCCESS = "OK";
public AliyunSmsClient(SmsChannelProperties properties) { public AliyunSmsClient(SmsChannelProperties properties) {
super(properties); super(properties);
Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空"); Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空");
@ -52,138 +51,66 @@ public class AliyunSmsClient extends AbstractSmsClient {
@Override @Override
protected void doInit() { protected void doInit() {
// IClientProfile profile = DefaultProfile.getProfile(ENDPOINT, properties.getApiKey(), properties.getApiSecret());
// client = new DefaultAcsClient(profile);
} }
@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 {
// 1. 执行请求
// 参考链接 https://api.aliyun.com/document/Dysmsapi/2017-05-25/SendSms
TreeMap<String, Object> queryParam = new TreeMap<>(); TreeMap<String, Object> queryParam = new TreeMap<>();
queryParam.put("PhoneNumbers",mobile); queryParam.put("PhoneNumbers",mobile);
queryParam.put("SignName",properties.getSignature()); queryParam.put("SignName", properties.getSignature());
queryParam.put("TemplateCode",apiTemplateId); queryParam.put("TemplateCode", apiTemplateId);
queryParam.put("TemplateParam",JsonUtils.toJsonString(MapUtils.convertMap(templateParams))); queryParam.put("TemplateParam", JsonUtils.toJsonString(MapUtils.convertMap(templateParams)));
queryParam.put("OutId", sendLogId);
JSONObject response = request("sendSms", queryParam);
JSONObject response = sendSmsRequest(queryParam,"sendSms"); // 2. 解析请求
SmsResponse smsResponse = getSmsSendResponse(response); return new SmsSendRespDTO()
.setSuccess(Objects.equals(response.getStr("Code"), RESPONSE_CODE_SUCCESS))
return new SmsSendRespDTO().setSuccess(smsResponse.success).setApiMsg(smsResponse.data.toString()); .setSerialNo(response.getStr("BizId"))
} .setApiRequestId(response.getStr("RequestId"))
.setApiCode(response.getStr("Code"))
JSONObject sendSmsRequest(TreeMap<String, Object> queryParam,String apiName) throws IOException, URISyntaxException { .setApiMsg(response.getStr("Message"));
// ************* 步骤 1拼接规范请求串 *************
String url = "https://dysmsapi.aliyuncs.com"; //APP接入地址+接口访问URI
String httpMethod = "POST"; // 请求方式
String canonicalUri = "/";
// 请求参数当请求的查询字符串为空时使用空字符串作为规范化查询字符串
StringBuilder canonicalQueryString = new StringBuilder();
queryParam.entrySet().stream().map(entry -> percentCode(entry.getKey()) + "=" + percentCode(String.valueOf(entry.getValue()))).forEachOrdered(queryPart -> {
// 如果canonicalQueryString已经不是空的则在查询参数前添加"&"
if (!canonicalQueryString.isEmpty()) {
canonicalQueryString.append("&");
}
canonicalQueryString.append(queryPart);
System.out.println("canonicalQueryString=========>\n" + canonicalQueryString);
});
SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SDF.setTimeZone(new SimpleTimeZone(0, "GMT"));
String SdfTime = SDF.format(new Date());
String randomUUID = UUID.randomUUID().toString();
TreeMap<String, String> headers = new TreeMap<>();
headers.put("host", "dysmsapi.aliyuncs.com");
headers.put("x-acs-action", apiName);
headers.put("x-acs-version", "2017-05-25");
headers.put("x-acs-date", SdfTime);
headers.put("x-acs-signature-nonce", randomUUID);
// headers.put("content-type", "application/json;charset=utf-8");
// 构造请求头多个规范化消息头按照消息头名称小写的字符代码顺序以升序排列后拼接在一起
StringBuilder canonicalHeaders = new StringBuilder();
// 已签名消息头列表多个请求头名称小写按首字母升序排列并以英文分号;分隔
StringBuilder signedHeadersSb = new StringBuilder();
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 -> {
String lowerKey = entry.getKey().toLowerCase();
String value = String.valueOf(entry.getValue()).trim();
canonicalHeaders.append(lowerKey).append(":").append(value).append("\n");
signedHeadersSb.append(lowerKey).append(";");
});
String signedHeaders = signedHeadersSb.substring(0, signedHeadersSb.length() - 1);
String body = "";//短信API为RPC接口query parameters在uri中拼接因此request body如果没有特殊要求设置为空
String hashedRequestBody = HexUtil.encodeHexStr(DigestUtil.sha256(body));
String canonicalRequest = httpMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody;
System.out.println("canonicalRequest=========>\n" + canonicalRequest);
// ************* 步骤 2拼接待签名字符串 *************
String hashedCanonicalRequest = HexUtil.encodeHexStr(DigestUtil.sha256(canonicalRequest));
String stringToSign = "ACS3-HMAC-SHA256" + "\n" + hashedCanonicalRequest;
// ************* 步骤 3计算签名 *************
String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign);
// ************* 步骤 4拼接 Authorization *************
String authorization = "ACS3-HMAC-SHA256" + " " + "Credential=" + properties.getApiKey() + ", "
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
headers.put("Authorization", authorization);
// ************* 步骤 5构造HttpRequest 并执行request请求获得response *************
// url = url + canonicalUri;
String urlWithParams = url + "?" + URLUtil.buildQuery(queryParam, null);
HttpResponse response = HttpRequest.post(urlWithParams)
.addHeaders(headers)
.body(body)
.execute();
// URIBuilder uriBuilder = new URIBuilder(url);
// // 添加请求参数
// for (Map.Entry<String, Object> entry : queryParam.entrySet()) {
// uriBuilder.addParameter(entry.getKey(), String.valueOf(entry.getValue()));
// }
// HttpUriRequest httpRequest = new HttpPost(uriBuilder.build());
//// HttpPost httpPost = new HttpPost(uriBuilder.build());
//// httpRequest = httpPost;
//
// // 添加http请求头
// for (Map.Entry<String, Object> entry : headers.entrySet()) {
// httpRequest.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
// }
//
// // 发送请求
// CloseableHttpClient httpClient = HttpClients.createDefault();
// CloseableHttpResponse response = httpClient.execute(httpRequest);
System.out.println("getEntity====="+response.body());
System.out.println("response====="+response);
return JSONUtil.parseObj(response.body());
} }
@Override @Override
public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) {
List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class); JSONArray statuses = JSONUtil.parseArray(text);
return convertList(statuses, status -> new SmsReceiveRespDTO().setSuccess(status.getSuccess()) // 字段参考
.setErrorCode(status.getErrCode()).setErrorMsg(status.getErrMsg()) return convertList(statuses, status -> {
.setMobile(status.getPhoneNumber()).setReceiveTime(status.getReportTime()) JSONObject statusObj = (JSONObject) status;
.setSerialNo(status.getBizId()).setLogId(Long.valueOf(status.getOutId()))); return new SmsReceiveRespDTO()
.setSuccess(statusObj.getBool("success")) // 是否接收成功
.setErrorCode(statusObj.getStr("err_code")) // 状态报告编码
.setErrorMsg(statusObj.getStr("err_msg")) // 状态报告说明
.setMobile(statusObj.getStr("phone_number")) // 手机号
.setReceiveTime(statusObj.getLocalDateTime("report_time", null)) // 状态报告时间
.setSerialNo(statusObj.getStr("biz_id")) // 发送序列号
.setLogId(statusObj.getLong("out_id")); // 用户序列号
});
} }
@Override @Override
public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable {
// 1. 执行请求
// 参考链接 https://api.aliyun.com/document/Dysmsapi/2017-05-25/QuerySmsTemplate
TreeMap<String, Object> queryParam = new TreeMap<>(); TreeMap<String, Object> queryParam = new TreeMap<>();
queryParam.put("TemplateCode",apiTemplateId); queryParam.put("TemplateCode", apiTemplateId);
JSONObject response = request("QuerySmsTemplate", queryParam);
JSONObject response = sendSmsRequest(queryParam,"QuerySmsTemplate");
QuerySmsTemplateResponse smsTemplateResponse = getSmsTemplateResponse(response);
return new SmsTemplateRespDTO().setId(smsTemplateResponse.getTemplateCode()).setContent(smsTemplateResponse.getTemplateContent())
.setAuditStatus(convertSmsTemplateAuditStatus(smsTemplateResponse.getTemplateStatus())).setAuditReason(smsTemplateResponse.getReason());
// 2.1 请求失败
String code = response.getStr("Code");
if (ObjectUtil.notEqual(code, RESPONSE_CODE_SUCCESS)) {
log.error("[getSmsTemplate][模版编号({}) 响应不正确({})]", apiTemplateId, response);
return null;
}
// 2.2 请求成功
return new SmsTemplateRespDTO().setId(apiTemplateId)
.setContent(response.getStr("TemplateContent"))
.setAuditStatus(convertSmsTemplateAuditStatus(response.getInt("TemplateStatus")))
.setAuditReason(response.getStr("Reason"));
} }
@VisibleForTesting @VisibleForTesting
@ -196,154 +123,72 @@ public class AliyunSmsClient extends AbstractSmsClient {
} }
} }
/** /**
* 对指定的字符串进行URL编码 * 请求阿里云短信
* 使用UTF-8编码字符集对字符串进行编码并对特定的字符进行替换以符合URL编码规范
* *
* @param str 需要进行URL编码的字符串 * @see <a href="https://help.aliyun.com/zh/sdk/product-overview/v3-request-structure-and-signature">V3 版本请求体&签名机制</>
* @return 编码后的字符串其中加号"+"被替换为"%20"星号"*"被替换为"%2A"波浪号"%7E"被替换为"~" * @param apiName 请求的 API 名称
* @param queryParams 请求参数
* @return 请求结果
*/ */
public static String percentCode(String str) { private JSONObject request(String apiName, TreeMap<String, Object> queryParams) {
if (str == null) { // 1. 请求参数
throw new IllegalArgumentException("输入字符串不可为null"); String queryString = queryParams.entrySet().stream()
} .map(entry -> percentCode(entry.getKey()) + "=" + percentCode(String.valueOf(entry.getValue())))
try { .collect(Collectors.joining("&"));
return URLEncoder.encode(str, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
} catch (UnsupportedEncodingException e) { // 2.1 请求 Header
throw new RuntimeException("UTF-8编码不被支持", e); TreeMap<String, String> headers = new TreeMap<>();
headers.put("host", HOST);
headers.put("x-acs-version", VERSION);
headers.put("x-acs-action", apiName);
headers.put("x-acs-date", FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("GMT")).format(new Date()));
headers.put("x-acs-signature-nonce", IdUtil.randomUUID());
// 2.2 构建签名 Header
StringBuilder canonicalHeaders = new StringBuilder(); // 构造请求头多个规范化消息头按照消息头名称小写的字符代码顺序以升序排列后拼接在一起
StringBuilder signedHeadersBuilder = new StringBuilder(); // 已签名消息头列表多个请求头名称小写按首字母升序排列并以英文分号;分隔
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 -> {
String lowerKey = entry.getKey().toLowerCase();
canonicalHeaders.append(lowerKey).append(":").append(String.valueOf(entry.getValue()).trim()).append("\n");
signedHeadersBuilder.append(lowerKey).append(";");
});
String signedHeaders = signedHeadersBuilder.substring(0, signedHeadersBuilder.length() - 1);
// 3. 请求 Body
String requestBody = ""; // 短信 API RPC 接口query parameters uri 中拼接因此 request body 如果没有特殊要求设置为空
String hashedRequestBody = DigestUtil.sha256Hex(requestBody);
// 4. 构建 Authorization 签名
String hashedCanonicalRequest = DigestUtil.sha256Hex("POST" // httpMethod
+ "\n" + "/" // canonicalUri
+ "\n" + queryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody);
String stringToSign = "ACS3-HMAC-SHA256" + "\n" + hashedCanonicalRequest;
String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign); // 计算签名
headers.put("Authorization", "ACS3-HMAC-SHA256" + " " + "Credential=" + properties.getApiKey()
+ ", " + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature);
// 5. 发起请求
String urlWithParams = URL + "?" + URLUtil.buildQuery(queryParams, null);
try (HttpResponse response = HttpRequest.post(urlWithParams).addHeaders(headers).body(requestBody).execute()) {
return JSONUtil.parseObj(response.body());
} }
} }
private SmsResponse getSmsSendResponse(JSONObject resJson) {
SmsResponse smsResponse = new SmsResponse();
smsResponse.setSuccess("OK".equals(resJson.getStr("Code")));
smsResponse.setData(resJson);
// smsResponse.setConfigId(getConfigId());
return smsResponse;
}
private QuerySmsTemplateResponse getSmsTemplateResponse(JSONObject resJson) {
QuerySmsTemplateResponse smsTemplateResponse = new QuerySmsTemplateResponse();
smsTemplateResponse.setRequestId(resJson.getStr("RequestId"));
smsTemplateResponse.setTemplateContent(resJson.getStr("TemplateContent"));
smsTemplateResponse.setReason(resJson.getStr("Reason"));
smsTemplateResponse.setTemplateStatus(resJson.getInt("TemplateStatus"));
return smsTemplateResponse;
}
/** /**
* <p>类名: SmsResponse * 对指定的字符串进行 URL 编码并对特定的字符进行替换以符合URL编码规范
* <p>说明 发送短信返回信息
* *
* @author :scholar * @param str 需要进行URL编码的字符串
* 2024/07/17 0:25 * @return 编码后的字符串
**/
@Data
public static class SmsResponse {
/**
* 是否成功
*/
private boolean success;
/**
* 厂商原返回体
*/
private Object data;
/**
* 配置标识名 如未配置取对应渠道名例如 Alibaba
*/
private String configId;
}
/**
* <p>类名: QuerySmsTemplateResponse
* <p>说明 sms模板查询返回信息
*
* @author :scholar
* 2024/07/17 0:25
**/
@Data
public static class QuerySmsTemplateResponse {
private String requestId;
private String code;
private String message;
private Integer templateStatus;
private String reason;
private String templateCode;
private Integer templateType;
private String templateName;
private String templateContent;
private String createDate;
}
/**
* 短信接收状态
*
* 参见 <a href="https://help.aliyun.com/document_detail/101867.html">文档</a>
*
* @author 润普源码
*/ */
@Data private static String percentCode(String str) {
public static class SmsReceiveStatus { Assert.notNull(str, "str 不能为空");
return URLUtil.encode(str)
/** .replace("+", "%20") // 加号 "+" 被替换为 "%20"
* 手机号 .replace("*", "%2A") // 星号 "*" 被替换为 "%2A"
*/ .replace("%7E", "~"); // 波浪号 "%7E" 被替换为 "~"
@JsonProperty("phone_number")
private String phoneNumber;
/**
* 发送时间
*/
@JsonProperty("send_time")
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
private LocalDateTime sendTime;
/**
* 状态报告时间
*/
@JsonProperty("report_time")
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
private LocalDateTime reportTime;
/**
* 是否接收成功
*/
private Boolean success;
/**
* 状态报告说明
*/
@JsonProperty("err_msg")
private String errMsg;
/**
* 状态报告编码
*/
@JsonProperty("err_code")
private String errCode;
/**
* 发送序列号
*/
@JsonProperty("biz_id")
private String bizId;
/**
* 用户序列号
*
* 这里我们传递的是 SysSmsLogDO 的日志编号
*/
@JsonProperty("out_id")
private String outId;
/**
* 短信长度例如说 123
*
* 140 字节算一条短信短信长度超过 140 字节时会拆分成多条短信发送
*/
@JsonProperty("sms_size")
private Integer smsSize;
} }
} }

View File

@ -0,0 +1,349 @@
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
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.util.collection.MapUtils;
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 com.google.common.annotations.VisibleForTesting;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
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;
/**
* 阿里短信客户端的实现类
*
* @author zzf
* @since 2021/1/25 14:17
*/
@Slf4j
public class AliyunSmsClient_old extends AbstractSmsClient {
public AliyunSmsClient_old(SmsChannelProperties properties) {
super(properties);
Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空");
Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空");
}
@Override
protected void doInit() {
// IClientProfile profile = DefaultProfile.getProfile(ENDPOINT, properties.getApiKey(), properties.getApiSecret());
// client = new DefaultAcsClient(profile);
}
@Override
public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId,
List<KeyValue<String, Object>> templateParams) throws Throwable {
TreeMap<String, Object> queryParam = new TreeMap<>();
queryParam.put("PhoneNumbers",mobile);
queryParam.put("SignName",properties.getSignature());
queryParam.put("TemplateCode",apiTemplateId);
queryParam.put("TemplateParam",JsonUtils.toJsonString(MapUtils.convertMap(templateParams)));
JSONObject response = sendSmsRequest(queryParam,"sendSms");
SmsResponse smsResponse = getSmsSendResponse(response);
return new SmsSendRespDTO().setSuccess(smsResponse.success).setApiMsg(smsResponse.data.toString());
}
JSONObject sendSmsRequest(TreeMap<String, Object> queryParam,String apiName) throws IOException, URISyntaxException {
// ************* 步骤 1拼接规范请求串 *************
String url = "https://dysmsapi.aliyuncs.com"; //APP接入地址+接口访问URI
String httpMethod = "POST"; // 请求方式
String canonicalUri = "/";
// 请求参数当请求的查询字符串为空时使用空字符串作为规范化查询字符串
StringBuilder canonicalQueryString = new StringBuilder();
queryParam.entrySet().stream().map(entry -> percentCode(entry.getKey()) + "=" + percentCode(String.valueOf(entry.getValue()))).forEachOrdered(queryPart -> {
// 如果canonicalQueryString已经不是空的则在查询参数前添加"&"
if (!canonicalQueryString.isEmpty()) {
canonicalQueryString.append("&");
}
canonicalQueryString.append(queryPart);
System.out.println("canonicalQueryString=========>\n" + canonicalQueryString);
});
SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SDF.setTimeZone(new SimpleTimeZone(0, "GMT"));
String SdfTime = SDF.format(new Date());
String randomUUID = UUID.randomUUID().toString();
TreeMap<String, String> headers = new TreeMap<>();
headers.put("host", "dysmsapi.aliyuncs.com");
headers.put("x-acs-action", apiName);
headers.put("x-acs-version", "2017-05-25");
headers.put("x-acs-date", SdfTime);
headers.put("x-acs-signature-nonce", randomUUID);
// headers.put("content-type", "application/json;charset=utf-8");
// 构造请求头多个规范化消息头按照消息头名称小写的字符代码顺序以升序排列后拼接在一起
StringBuilder canonicalHeaders = new StringBuilder();
// 已签名消息头列表多个请求头名称小写按首字母升序排列并以英文分号;分隔
StringBuilder signedHeadersSb = new StringBuilder();
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 -> {
String lowerKey = entry.getKey().toLowerCase();
String value = String.valueOf(entry.getValue()).trim();
canonicalHeaders.append(lowerKey).append(":").append(value).append("\n");
signedHeadersSb.append(lowerKey).append(";");
});
String signedHeaders = signedHeadersSb.substring(0, signedHeadersSb.length() - 1);
String body = "";//短信API为RPC接口query parameters在uri中拼接因此request body如果没有特殊要求设置为空
String hashedRequestBody = HexUtil.encodeHexStr(DigestUtil.sha256(body));
String canonicalRequest = httpMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody;
System.out.println("canonicalRequest=========>\n" + canonicalRequest);
// ************* 步骤 2拼接待签名字符串 *************
String hashedCanonicalRequest = HexUtil.encodeHexStr(DigestUtil.sha256(canonicalRequest));
String stringToSign = "ACS3-HMAC-SHA256" + "\n" + hashedCanonicalRequest;
// ************* 步骤 3计算签名 *************
String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign);
// ************* 步骤 4拼接 Authorization *************
String authorization = "ACS3-HMAC-SHA256" + " " + "Credential=" + properties.getApiKey() + ", "
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
headers.put("Authorization", authorization);
// ************* 步骤 5构造HttpRequest 并执行request请求获得response *************
// url = url + canonicalUri;
String urlWithParams = url + "?" + URLUtil.buildQuery(queryParam, null);
HttpResponse response = HttpRequest.post(urlWithParams)
.addHeaders(headers)
.body(body)
.execute();
// URIBuilder uriBuilder = new URIBuilder(url);
// // 添加请求参数
// for (Map.Entry<String, Object> entry : queryParam.entrySet()) {
// uriBuilder.addParameter(entry.getKey(), String.valueOf(entry.getValue()));
// }
// HttpUriRequest httpRequest = new HttpPost(uriBuilder.build());
//// HttpPost httpPost = new HttpPost(uriBuilder.build());
//// httpRequest = httpPost;
//
// // 添加http请求头
// for (Map.Entry<String, Object> entry : headers.entrySet()) {
// httpRequest.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
// }
//
// // 发送请求
// CloseableHttpClient httpClient = HttpClients.createDefault();
// CloseableHttpResponse response = httpClient.execute(httpRequest);
System.out.println("getEntity====="+response.body());
System.out.println("response====="+response);
return JSONUtil.parseObj(response.body());
}
@Override
public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) {
List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class);
return convertList(statuses, status -> new SmsReceiveRespDTO().setSuccess(status.getSuccess())
.setErrorCode(status.getErrCode()).setErrorMsg(status.getErrMsg())
.setMobile(status.getPhoneNumber()).setReceiveTime(status.getReportTime())
.setSerialNo(status.getBizId()).setLogId(Long.valueOf(status.getOutId())));
}
@Override
public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable {
TreeMap<String, Object> queryParam = new TreeMap<>();
queryParam.put("TemplateCode",apiTemplateId);
JSONObject response = sendSmsRequest(queryParam,"QuerySmsTemplate");
QuerySmsTemplateResponse smsTemplateResponse = getSmsTemplateResponse(response);
return new SmsTemplateRespDTO().setId(smsTemplateResponse.getTemplateCode()).setContent(smsTemplateResponse.getTemplateContent())
.setAuditStatus(convertSmsTemplateAuditStatus(smsTemplateResponse.getTemplateStatus())).setAuditReason(smsTemplateResponse.getReason());
}
@VisibleForTesting
Integer convertSmsTemplateAuditStatus(Integer templateStatus) {
switch (templateStatus) {
case 0: return SmsTemplateAuditStatusEnum.CHECKING.getStatus();
case 1: return SmsTemplateAuditStatusEnum.SUCCESS.getStatus();
case 2: return SmsTemplateAuditStatusEnum.FAIL.getStatus();
default: throw new IllegalArgumentException(String.format("未知审核状态(%d)", templateStatus));
}
}
/**
* 对指定的字符串进行URL编码
* 使用UTF-8编码字符集对字符串进行编码并对特定的字符进行替换以符合URL编码规范
*
* @param str 需要进行URL编码的字符串
* @return 编码后的字符串其中加号"+"被替换为"%20"星号"*"被替换为"%2A"波浪号"%7E"被替换为"~"
*/
public static String percentCode(String str) {
if (str == null) {
throw new IllegalArgumentException("输入字符串不可为null");
}
try {
return URLEncoder.encode(str, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8编码不被支持", e);
}
}
private SmsResponse getSmsSendResponse(JSONObject resJson) {
SmsResponse smsResponse = new SmsResponse();
smsResponse.setSuccess("OK".equals(resJson.getStr("Code")));
smsResponse.setData(resJson);
// smsResponse.setConfigId(getConfigId());
return smsResponse;
}
private QuerySmsTemplateResponse getSmsTemplateResponse(JSONObject resJson) {
QuerySmsTemplateResponse smsTemplateResponse = new QuerySmsTemplateResponse();
smsTemplateResponse.setRequestId(resJson.getStr("RequestId"));
smsTemplateResponse.setTemplateContent(resJson.getStr("TemplateContent"));
smsTemplateResponse.setReason(resJson.getStr("Reason"));
smsTemplateResponse.setTemplateStatus(resJson.getInt("TemplateStatus"));
return smsTemplateResponse;
}
/**
* <p>类名: SmsResponse
* <p>说明 发送短信返回信息
*
* @author :scholar
* 2024/07/17 0:25
**/
@Data
public static class SmsResponse {
/**
* 是否成功
*/
private boolean success;
/**
* 厂商原返回体
*/
private Object data;
/**
* 配置标识名 如未配置取对应渠道名例如 Alibaba
*/
private String configId;
}
/**
* <p>类名: QuerySmsTemplateResponse
* <p>说明 sms模板查询返回信息
*
* @author :scholar
* 2024/07/17 0:25
**/
@Data
public static class QuerySmsTemplateResponse {
private String requestId;
private String code;
private String message;
private Integer templateStatus;
private String reason;
private String templateCode;
private Integer templateType;
private String templateName;
private String templateContent;
private String createDate;
}
/**
* 短信接收状态
*
* 参见 <a href="https://help.aliyun.com/document_detail/101867.html">文档</a>
*
* @author 润普源码
*/
@Data
public static class SmsReceiveStatus {
/**
* 手机号
*/
@JsonProperty("phone_number")
private String phoneNumber;
/**
* 发送时间
*/
@JsonProperty("send_time")
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
private LocalDateTime sendTime;
/**
* 状态报告时间
*/
@JsonProperty("report_time")
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
private LocalDateTime reportTime;
/**
* 是否接收成功
*/
private Boolean success;
/**
* 状态报告说明
*/
@JsonProperty("err_msg")
private String errMsg;
/**
* 状态报告编码
*/
@JsonProperty("err_code")
private String errCode;
/**
* 发送序列号
*/
@JsonProperty("biz_id")
private String bizId;
/**
* 用户序列号
*
* 这里我们传递的是 SysSmsLogDO 的日志编号
*/
@JsonProperty("out_id")
private String outId;
/**
* 短信长度例如说 123
*
* 140 字节算一条短信短信长度超过 140 字节时会拆分成多条短信发送
*/
@JsonProperty("sms_size")
private Integer smsSize;
}
}

View File

@ -1,36 +1,21 @@
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
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.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 com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.when;
// TODO 芋艿需要优化
/** /**
* {@link AliyunSmsClient} 的单元测试 * {@link cn.iocoder.yudao.module.system.framework.sms.core.client.impl.AliyunSmsClient_old} 的单元测试
* *
* @author 芋道源码 * @author 芋道源码
*/ */
@ -44,9 +29,6 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
@InjectMocks @InjectMocks
private final AliyunSmsClient smsClient = new AliyunSmsClient(properties); private final AliyunSmsClient smsClient = new AliyunSmsClient(properties);
@Mock
private IAcsClient client;
@Test @Test
public void testDoInit() { public void testDoInit() {
// 准备参数 // 准备参数
@ -54,68 +36,66 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
// 调用 // 调用
smsClient.doInit(); smsClient.doInit();
// 断言
assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "acsClient"));
} }
@Test // @Test
public void tesSendSms_success() throws Throwable { // public void tesSendSms_success() throws Throwable {
// 准备参数 // // 准备参数
Long sendLogId = randomLongId(); // Long sendLogId = randomLongId();
String mobile = randomString(); // String mobile = randomString();
String apiTemplateId = randomString(); // String apiTemplateId = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList( // List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("code", 1234), new KeyValue<>("op", "login")); // new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
// mock 方法 // // mock 方法
SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("OK")); // SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("OK"));
when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> { // when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> {
assertEquals(mobile, acsRequest.getPhoneNumbers()); // assertEquals(mobile, acsRequest.getPhoneNumbers());
assertEquals(properties.getSignature(), acsRequest.getSignName()); // assertEquals(properties.getSignature(), acsRequest.getSignName());
assertEquals(apiTemplateId, acsRequest.getTemplateCode()); // assertEquals(apiTemplateId, acsRequest.getTemplateCode());
assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam()); // assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam());
assertEquals(sendLogId.toString(), acsRequest.getOutId()); // assertEquals(sendLogId.toString(), acsRequest.getOutId());
return true; // return true;
}))).thenReturn(response); // }))).thenReturn(response);
//
// // 调用
// SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
// apiTemplateId, templateParams);
// // 断言
// assertTrue(result.getSuccess());
// assertEquals(response.getRequestId(), result.getApiRequestId());
// assertEquals(response.getCode(), result.getApiCode());
// assertEquals(response.getMessage(), result.getApiMsg());
// assertEquals(response.getBizId(), result.getSerialNo());
// }
// 调用 // @Test
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, // public void tesSendSms_fail() throws Throwable {
apiTemplateId, templateParams); // // 准备参数
// 断言 // Long sendLogId = randomLongId();
assertTrue(result.getSuccess()); // String mobile = randomString();
assertEquals(response.getRequestId(), result.getApiRequestId()); // String apiTemplateId = randomString();
assertEquals(response.getCode(), result.getApiCode()); // List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
assertEquals(response.getMessage(), result.getApiMsg()); // new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
assertEquals(response.getBizId(), result.getSerialNo()); // // mock 方法
} // SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("ERROR"));
// when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> {
@Test // assertEquals(mobile, acsRequest.getPhoneNumbers());
public void tesSendSms_fail() throws Throwable { // assertEquals(properties.getSignature(), acsRequest.getSignName());
// 准备参数 // assertEquals(apiTemplateId, acsRequest.getTemplateCode());
Long sendLogId = randomLongId(); // assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam());
String mobile = randomString(); // assertEquals(sendLogId.toString(), acsRequest.getOutId());
String apiTemplateId = randomString(); // return true;
List<KeyValue<String, Object>> templateParams = Lists.newArrayList( // }))).thenReturn(response);
new KeyValue<>("code", 1234), new KeyValue<>("op", "login")); //
// mock 方法 // // 调用
SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("ERROR")); // SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> { // // 断言
assertEquals(mobile, acsRequest.getPhoneNumbers()); // assertFalse(result.getSuccess());
assertEquals(properties.getSignature(), acsRequest.getSignName()); // assertEquals(response.getRequestId(), result.getApiRequestId());
assertEquals(apiTemplateId, acsRequest.getTemplateCode()); // assertEquals(response.getCode(), result.getApiCode());
assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam()); // assertEquals(response.getMessage(), result.getApiMsg());
assertEquals(sendLogId.toString(), acsRequest.getOutId()); // assertEquals(response.getBizId(), result.getSerialNo());
return true; // }
}))).thenReturn(response);
// 调用
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
// 断言
assertFalse(result.getSuccess());
assertEquals(response.getRequestId(), result.getApiRequestId());
assertEquals(response.getCode(), result.getApiCode());
assertEquals(response.getMessage(), result.getApiMsg());
assertEquals(response.getBizId(), result.getSerialNo());
}
@Test @Test
public void testParseSmsReceiveStatus() { public void testParseSmsReceiveStatus() {
@ -149,28 +129,28 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
assertEquals(67890L, statuses.get(0).getLogId()); assertEquals(67890L, statuses.get(0).getLogId());
} }
@Test // @Test
public void testGetSmsTemplate() throws Throwable { // public void testGetSmsTemplate() throws Throwable {
// 准备参数 // // 准备参数
String apiTemplateId = randomString(); // String apiTemplateId = randomString();
// mock 方法 // // mock 方法
QuerySmsTemplateResponse response = randomPojo(QuerySmsTemplateResponse.class, o -> { // QuerySmsTemplateResponse response = randomPojo(QuerySmsTemplateResponse.class, o -> {
o.setCode("OK"); // o.setCode("OK");
o.setTemplateStatus(1); // 设置模板通过 // o.setTemplateStatus(1); // 设置模板通过
}); // });
when(client.getAcsResponse(argThat((ArgumentMatcher<QuerySmsTemplateRequest>) acsRequest -> { // when(client.getAcsResponse(argThat((ArgumentMatcher<QuerySmsTemplateRequest>) acsRequest -> {
assertEquals(apiTemplateId, acsRequest.getTemplateCode()); // assertEquals(apiTemplateId, acsRequest.getTemplateCode());
return true; // return true;
}))).thenReturn(response); // }))).thenReturn(response);
//
// 调用 // // 调用
SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId); // SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId);
// 断言 // // 断言
assertEquals(response.getTemplateCode(), result.getId()); // assertEquals(response.getTemplateCode(), result.getId());
assertEquals(response.getTemplateContent(), result.getContent()); // assertEquals(response.getTemplateContent(), result.getContent());
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus()); // assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus());
assertEquals(response.getReason(), result.getAuditReason()); // assertEquals(response.getReason(), result.getAuditReason());
} // }
@Test @Test
public void testConvertSmsTemplateAuditStatus() { public void testConvertSmsTemplateAuditStatus() {

View File

@ -1,7 +1,9 @@
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
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.property.SmsChannelProperties; import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -17,7 +19,7 @@ public class SmsClientTests {
@Test @Test
@Disabled @Disabled
public void testHuaweiSmsClient() throws Throwable { public void testHuaweiSmsClient_sendSms() throws Throwable {
SmsChannelProperties properties = new SmsChannelProperties() SmsChannelProperties properties = new SmsChannelProperties()
.setApiKey("123") .setApiKey("123")
.setApiSecret("456"); .setApiSecret("456");
@ -33,4 +35,67 @@ public class SmsClientTests {
System.out.println(smsSendRespDTO); System.out.println(smsSendRespDTO);
} }
// ========== 阿里云 ==========
@Test
@Disabled
public void testAliyunSmsClient_getSmsTemplate() throws Throwable {
SmsChannelProperties properties = new SmsChannelProperties()
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR")
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz");
AliyunSmsClient client = new AliyunSmsClient(properties);
// 准备参数
String apiTemplateId = "SMS_207945135";
// 调用
SmsTemplateRespDTO template = client.getSmsTemplate(apiTemplateId);
// 打印结果
System.out.println(template);
}
@Test
@Disabled
public void testAliyunSmsClient_sendSms() throws Throwable {
SmsChannelProperties properties = new SmsChannelProperties()
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR")
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz");
AliyunSmsClient client = new AliyunSmsClient(properties);
// 准备参数
Long sendLogId = System.currentTimeMillis();
String mobile = "17321315478";
String apiTemplateId = "SMS_207945135";
// 调用
SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, List.of(new KeyValue<>("code", "1024")));
// 打印结果
System.out.println(sendRespDTO);
}
@Test
@Disabled
public void testAliyunSmsClient_parseSmsReceiveStatus() {
SmsChannelProperties properties = new SmsChannelProperties()
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR")
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz");
AliyunSmsClient client = new AliyunSmsClient(properties);
// 准备参数
String text = "[\n" +
" {\n" +
" \"phone_number\" : \"13900000001\",\n" +
" \"send_time\" : \"2017-01-01 11:12:13\",\n" +
" \"report_time\" : \"2017-02-02 22:23:24\",\n" +
" \"success\" : true,\n" +
" \"err_code\" : \"DELIVERED\",\n" +
" \"err_msg\" : \"用户接收成功\",\n" +
" \"sms_size\" : \"1\",\n" +
" \"biz_id\" : \"12345\",\n" +
" \"out_id\" : \"67890\"\n" +
" }\n" +
"]";
// mock 方法
// 调用
List<SmsReceiveRespDTO> statuses = client.parseSmsReceiveStatus(text);
// 打印结果
System.out.println(statuses);
}
} }