parent
bdbc5b2626
commit
f4aba034cf
|
@ -124,7 +124,7 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends AcsResponse, R> SmsCommonResult<R> invoke(AcsRequest<T> request, Function<T, R> consumer) {
|
private <T extends AcsResponse, R> SmsCommonResult<R> invoke(AcsRequest<T> request, Function<T, R> responseConsumer) {
|
||||||
try {
|
try {
|
||||||
// 执行发送. 由于阿里云 sms 短信没有统一的 Response,但是有统一的 code、message、requestId 属性,所以只好反射
|
// 执行发送. 由于阿里云 sms 短信没有统一的 Response,但是有统一的 code、message、requestId 属性,所以只好反射
|
||||||
T sendResult = acsClient.getAcsResponse(request);
|
T sendResult = acsClient.getAcsResponse(request);
|
||||||
|
@ -134,7 +134,7 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||||
// 解析结果
|
// 解析结果
|
||||||
R data = null;
|
R data = null;
|
||||||
if (Objects.equals(code, "OK")) { // 请求成功的情况下
|
if (Objects.equals(code, "OK")) { // 请求成功的情况下
|
||||||
data = consumer.apply(sendResult);
|
data = responseConsumer.apply(sendResult);
|
||||||
}
|
}
|
||||||
// 拼接结果
|
// 拼接结果
|
||||||
return SmsCommonResult.build(code, message, requestId, data, codeMapping);
|
return SmsCommonResult.build(code, message, requestId, data, codeMapping);
|
||||||
|
|
|
@ -10,6 +10,7 @@ import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsReceiveRespDTO;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO;
|
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.client.impl.AbstractSmsClient;
|
import cn.iocoder.dashboard.framework.sms.core.client.impl.AbstractSmsClient;
|
||||||
|
import cn.iocoder.dashboard.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties;
|
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties;
|
||||||
import cn.iocoder.dashboard.util.json.JsonUtils;
|
import cn.iocoder.dashboard.util.json.JsonUtils;
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
@ -17,11 +18,13 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.yunpian.sdk.YunpianClient;
|
import com.yunpian.sdk.YunpianClient;
|
||||||
import com.yunpian.sdk.constant.YunpianConstant;
|
import com.yunpian.sdk.constant.YunpianConstant;
|
||||||
import com.yunpian.sdk.model.Result;
|
import com.yunpian.sdk.model.Result;
|
||||||
import com.yunpian.sdk.model.SmsSingleSend;
|
import com.yunpian.sdk.model.Template;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||||
|
@ -62,27 +65,15 @@ public class YunpianSmsClient extends AbstractSmsClient {
|
||||||
@Override
|
@Override
|
||||||
protected SmsCommonResult<SmsSendRespDTO> doSendSms(Long sendLogId, String mobile,
|
protected SmsCommonResult<SmsSendRespDTO> doSendSms(Long sendLogId, String mobile,
|
||||||
String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable {
|
String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable {
|
||||||
// 构建参数
|
return invoke(() -> {
|
||||||
Map<String, String> request = new HashMap<>();
|
Map<String, String> request = new HashMap<>();
|
||||||
request.put(YunpianConstant.APIKEY, properties.getApiKey());
|
request.put(YunpianConstant.MOBILE, mobile);
|
||||||
request.put(YunpianConstant.MOBILE, mobile);
|
request.put(YunpianConstant.TPL_ID, apiTemplateId);
|
||||||
request.put(YunpianConstant.TPL_ID, apiTemplateId);
|
request.put(YunpianConstant.TPL_VALUE, formatTplValue(templateParams));
|
||||||
request.put(YunpianConstant.TPL_VALUE, formatTplValue(templateParams));
|
request.put(YunpianConstant.UID, String.valueOf(sendLogId));
|
||||||
request.put(YunpianConstant.UID, String.valueOf(sendLogId));
|
request.put(YunpianConstant.CALLBACK_URL, properties.getCallbackUrl());
|
||||||
request.put(YunpianConstant.CALLBACK_URL, properties.getCallbackUrl());
|
return client.sms().tpl_single_send(request);
|
||||||
|
}, response -> new SmsSendRespDTO().setSerialNo(String.valueOf(response.getSid())));
|
||||||
// 执行发送
|
|
||||||
Result<SmsSingleSend> sendResult = client.sms().tpl_single_send(request);
|
|
||||||
if (sendResult.getThrowable() != null) {
|
|
||||||
throw sendResult.getThrowable();
|
|
||||||
}
|
|
||||||
// 解析结果
|
|
||||||
SmsSendRespDTO data = null;
|
|
||||||
if (sendResult.getData() != null) {
|
|
||||||
data = new SmsSendRespDTO().setSerialNo(String.valueOf(sendResult.getData().getSid()));
|
|
||||||
}
|
|
||||||
return SmsCommonResult.build(String.valueOf(sendResult.getCode()), formatResultMsg(sendResult), null,
|
|
||||||
data, codeMapping);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String formatTplValue(List<KeyValue<String, Object>> templateParams) {
|
private static String formatTplValue(List<KeyValue<String, Object>> templateParams) {
|
||||||
|
@ -95,13 +86,6 @@ public class YunpianSmsClient extends AbstractSmsClient {
|
||||||
return joiner.toString();
|
return joiner.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String formatResultMsg(Result<SmsSingleSend> sendResult) {
|
|
||||||
if (StrUtil.isEmpty(sendResult.getDetail())) {
|
|
||||||
return sendResult.getMsg();
|
|
||||||
}
|
|
||||||
return sendResult.getMsg() + " => " + sendResult.getDetail();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<SmsReceiveRespDTO> doParseSmsReceiveStatus(String text) throws Throwable {
|
protected List<SmsReceiveRespDTO> doParseSmsReceiveStatus(String text) throws Throwable {
|
||||||
List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class);
|
List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class);
|
||||||
|
@ -117,7 +101,47 @@ public class YunpianSmsClient extends AbstractSmsClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected SmsCommonResult<SmsTemplateRespDTO> doGetSmsTemplate(String apiTemplateId) throws Throwable {
|
protected SmsCommonResult<SmsTemplateRespDTO> doGetSmsTemplate(String apiTemplateId) throws Throwable {
|
||||||
return null;
|
return invoke(() -> {
|
||||||
|
Map<String, String> request = new HashMap<>();
|
||||||
|
request.put(YunpianConstant.APIKEY, properties.getApiKey());
|
||||||
|
request.put(YunpianConstant.TPL_ID, apiTemplateId);
|
||||||
|
return client.tpl().get(request);
|
||||||
|
}, response -> {
|
||||||
|
Template template = response.get(0);
|
||||||
|
return new SmsTemplateRespDTO().setId(String.valueOf(template.getTpl_id())).setContent(template.getTpl_content())
|
||||||
|
.setAuditStatus(convertSmsTemplateAuditStatus(template.getCheck_status())).setAuditReason(template.getReason());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private Integer convertSmsTemplateAuditStatus(String checkStatus) {
|
||||||
|
switch (checkStatus) {
|
||||||
|
case "CHECKING": return SmsTemplateAuditStatusEnum.CHECKING.getStatus();
|
||||||
|
case "SUCCESS": return SmsTemplateAuditStatusEnum.SUCCESS.getStatus();
|
||||||
|
case "FAIL": return SmsTemplateAuditStatusEnum.FAIL.getStatus();
|
||||||
|
default: throw new IllegalArgumentException(String.format("未知审核状态(%s)", checkStatus));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T, R> SmsCommonResult<R> invoke(Supplier<Result<T>> requestConsumer, Function<T, R> responseConsumer) throws Throwable {
|
||||||
|
// 执行请求
|
||||||
|
Result<T> result = requestConsumer.get();
|
||||||
|
if (result.getThrowable() != null) {
|
||||||
|
throw result.getThrowable();
|
||||||
|
}
|
||||||
|
// 解析结果
|
||||||
|
R data = null;
|
||||||
|
if (result.getData() != null) {
|
||||||
|
data = responseConsumer.apply(result.getData());
|
||||||
|
}
|
||||||
|
// 拼接结果
|
||||||
|
return SmsCommonResult.build(String.valueOf(result.getCode()), formatResultMsg(result), null, data, codeMapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatResultMsg(Result<?> sendResult) {
|
||||||
|
if (StrUtil.isEmpty(sendResult.getDetail())) {
|
||||||
|
return sendResult.getMsg();
|
||||||
|
}
|
||||||
|
return sendResult.getMsg() + " => " + sendResult.getDetail();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,8 +3,10 @@ package cn.iocoder.dashboard.framework.sms.core.client.impl.yunpian;
|
||||||
import cn.iocoder.dashboard.common.core.KeyValue;
|
import cn.iocoder.dashboard.common.core.KeyValue;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult;
|
import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO;
|
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO;
|
||||||
|
import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum;
|
import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties;
|
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -15,8 +17,10 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class YunpianSmsClientIntegrationTest {
|
public class YunpianSmsClientIntegrationTest {
|
||||||
|
|
||||||
@Test
|
private static YunpianSmsClient smsClient;
|
||||||
public void testSend() {
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void init() {
|
||||||
// 创建配置类
|
// 创建配置类
|
||||||
SmsChannelProperties properties = new SmsChannelProperties();
|
SmsChannelProperties properties = new SmsChannelProperties();
|
||||||
properties.setId(1L);
|
properties.setId(1L);
|
||||||
|
@ -24,9 +28,12 @@ public class YunpianSmsClientIntegrationTest {
|
||||||
properties.setCode(SmsChannelEnum.YUN_PIAN.getCode());
|
properties.setCode(SmsChannelEnum.YUN_PIAN.getCode());
|
||||||
properties.setApiKey("1555a14277cb8a608cf45a9e6a80d510");
|
properties.setApiKey("1555a14277cb8a608cf45a9e6a80d510");
|
||||||
// 创建客户端
|
// 创建客户端
|
||||||
YunpianSmsClient smsClient = new YunpianSmsClient(properties);
|
smsClient = new YunpianSmsClient(properties);
|
||||||
smsClient.init();
|
smsClient.init();
|
||||||
// 发送短信
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendSms() {
|
||||||
List<KeyValue<String, Object>> templateParams = new ArrayList<>();
|
List<KeyValue<String, Object>> templateParams = new ArrayList<>();
|
||||||
templateParams.add(new KeyValue<>("code", "1024"));
|
templateParams.add(new KeyValue<>("code", "1024"));
|
||||||
templateParams.add(new KeyValue<>("operation", "嘿嘿"));
|
templateParams.add(new KeyValue<>("operation", "嘿嘿"));
|
||||||
|
@ -35,4 +42,11 @@ public class YunpianSmsClientIntegrationTest {
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSmsTemplate() {
|
||||||
|
String apiTemplateId = "4383920";
|
||||||
|
SmsCommonResult<SmsTemplateRespDTO> result = smsClient.getSmsTemplate(apiTemplateId);
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue