diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/yunpian/YunpianSmsClient.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/yunpian/YunpianSmsClient.java index dfadd573a..00b64ad4c 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/yunpian/YunpianSmsClient.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/yunpian/YunpianSmsClient.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; import cn.iocoder.dashboard.framework.sms.core.SmsConstants; import cn.iocoder.dashboard.framework.sms.core.SmsResult; @@ -28,6 +29,8 @@ import java.util.List; import java.util.Map; import java.util.StringJoiner; +import static com.yunpian.sdk.constant.Code.*; + /** * 云片短信客户端的实现类 * @@ -51,7 +54,15 @@ public class YunpianSmsClient extends AbstractSmsClient { @Override public void doInit() { - client = new YunpianClient(properties.getApiKey()); + YunpianClient oldClient = client; + // 初始化新的客户端 + YunpianClient newClient = new YunpianClient(properties.getApiKey()); + newClient.init(); + this.client = newClient; + // 销毁老的客户端 + if (oldClient != null) { + oldClient.close(); + } } @Override @@ -71,9 +82,8 @@ public class YunpianSmsClient extends AbstractSmsClient { throw sendResult.getThrowable(); } // 解析结果 - SmsSingleSend data = sendResult.getData(); return SmsResult.success(parseSendFailureType(sendResult), // 将 API 短信平台,解析成统一的错误码 - String.valueOf(data.getCode()), data.getMsg(), null, String.valueOf(data.getSid())); + String.valueOf(sendResult.getCode()), formatResultMsg(sendResult), null, getApiSerialNo(sendResult)); } private static String formatTplValue(Map templateParams) { @@ -86,10 +96,31 @@ public class YunpianSmsClient extends AbstractSmsClient { return joiner.toString(); } + private static String formatResultMsg(Result sendResult) { + if (StrUtil.isEmpty(sendResult.getDetail())) { + return sendResult.getMsg(); + } + return sendResult.getMsg() + " => " + sendResult.getDetail(); + } + private static SmsSendFailureTypeEnum parseSendFailureType(Result sendResult) { + Integer code = sendResult.getCode(); + switch (code) { + case ARGUMENT_MISSING: return SmsSendFailureTypeEnum.SMS_API_PARAM_ERROR; + case BAD_ARGUMENT_FORMAT: return SmsSendFailureTypeEnum.SMS_TEMPLATE_PARAM_ERROR; + case TPL_NOT_FOUND: return SmsSendFailureTypeEnum.SMS_TEMPLATE_NOT_EXISTS; + case TPL_NOT_VALID: return SmsSendFailureTypeEnum.SMS_TMPLATE_INVALID; + } return SmsSendFailureTypeEnum.SMS_UNKNOWN; } + private static String getApiSerialNo(Result sendResult) { + if (sendResult.getData() == null) { + return null; + } + return String.valueOf(sendResult.getData().getSid()); + } + /** * 云片的比较复杂,又是加密又是套娃的 */ diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/enums/SmsSendFailureTypeEnum.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/enums/SmsSendFailureTypeEnum.java index 82ce1e836..d014f5966 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/enums/SmsSendFailureTypeEnum.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/enums/SmsSendFailureTypeEnum.java @@ -12,14 +12,19 @@ import lombok.Getter; @AllArgsConstructor public enum SmsSendFailureTypeEnum { - // ========== 模板相关(100 开头) ========== + // ========== 渠道相关(100 开头) ========== SMS_CHANNEL_CLIENT_NOT_EXISTS(100, "短信渠道的客户端不存在"), // ========== 模板相关(200 开头) ========== - SMS_TEMPLATE_DISABLE(200, "短信模板被禁用"), + SMS_TEMPLATE_NOT_EXISTS(200, "短信模板不存在"), + SMS_TEMPLATE_DISABLE(201, "短信模板被禁用"), // 例如说,我们在管理后台禁用了 + SMS_TMPLATE_INVALID(202, "短信模板不可用"), // 例如说,短信模板正在审核中 + SMS_TEMPLATE_PARAM_ERROR(203, "模板参数不正确"), // ========== 其它相关(900 开头) ========== - SMS_SEND_EXCEPTION(900, "发送异常"), + SMS_API_PARAM_ERROR(900, "请求参数缺失"), + + SMS_SEND_EXCEPTION(998, "发送异常"), SMS_UNKNOWN(999, "未知错误,需要解析") ; diff --git a/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java b/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java index 1c193bbf2..c976528ae 100644 --- a/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java +++ b/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java @@ -4,7 +4,7 @@ import cn.hutool.core.thread.ThreadUtil; import cn.iocoder.dashboard.BaseRedisIntegrationTest; import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils; import cn.iocoder.dashboard.modules.system.mq.consumer.mail.SysMailSendConsumer; -import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SysSmsSendConsumer; +import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SmsSendConsumer; import cn.iocoder.dashboard.modules.system.mq.message.mail.SysMailSendMessage; import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage; import org.junit.jupiter.api.Disabled; @@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit; public class RedisStreamTest { - @Import({SysSmsSendConsumer.class, SysMailSendConsumer.class}) + @Import({SmsSendConsumer.class, SysMailSendConsumer.class}) @Disabled public static class ConsumerTest extends BaseRedisIntegrationTest { @@ -43,7 +43,7 @@ public class RedisStreamTest { for (int i = 0; i < 100; i++) { // 创建消息 SysSmsSendMessage message = new SysSmsSendMessage(); - message.setMobile("15601691300").setTemplateCode("test:" + i); + message.setMobile("15601691300").setApiTemplateId("test:" + i); // 发送消息 RedisMessageUtils.sendStreamMessage(stringRedisTemplate, message); } diff --git a/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/impl/yunpian/YunpianSmsClientIntegrationTest.java b/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/impl/yunpian/YunpianSmsClientIntegrationTest.java new file mode 100644 index 000000000..af4a8b6a0 --- /dev/null +++ b/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/impl/yunpian/YunpianSmsClientIntegrationTest.java @@ -0,0 +1,36 @@ +package cn.iocoder.dashboard.framework.sms.core.client.impl.yunpian; + +import cn.iocoder.dashboard.framework.sms.core.SmsResult; +import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum; +import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * {@link YunpianSmsClient} 的集成测试 + */ +public class YunpianSmsClientIntegrationTest { + + @Test + public void testSend() { + // 创建配置类 + SmsChannelProperties properties = new SmsChannelProperties(); + properties.setId(1L); + properties.setSignature("芋道"); + properties.setCode(SmsChannelEnum.YUN_PIAN.getCode()); + properties.setApiKey("1555a14277cb8a608cf45a9e6a80d510"); + // 创建客户端 + YunpianSmsClient smsClient = new YunpianSmsClient(properties); + smsClient.init(); + // 发送短信 + Map templateParams = new HashMap<>(); + templateParams.put("code", "1024"); + templateParams.put("operation", "嘿嘿"); +// SmsResult result = smsClient.send(1L, "15601691399", "4372216", templateParams); + SmsResult result = smsClient.send(1L, "15601691399", "4383920", templateParams); + System.out.println(result); + } + +} diff --git a/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/package-info.java b/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/package-info.java new file mode 100644 index 000000000..037ce8ca2 --- /dev/null +++ b/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.dashboard.framework.sms.core.client;