diff --git a/src/main/java/cn/iocoder/dashboard/common/exception/GlobalException.java b/src/main/java/cn/iocoder/dashboard/common/exception/GlobalException.java deleted file mode 100644 index d4f9c945e..000000000 --- a/src/main/java/cn/iocoder/dashboard/common/exception/GlobalException.java +++ /dev/null @@ -1,41 +0,0 @@ -package cn.iocoder.dashboard.common.exception; - -import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants; -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * 全局异常 Exception - */ -@Data -@EqualsAndHashCode(callSuper = true) -public class GlobalException extends RuntimeException { - - /** - * 全局错误码 - * - * @see GlobalErrorCodeConstants - */ - private Integer code; - /** - * 错误提示 - */ - private String message; - - /** - * 空构造方法,避免反序列化问题 - */ - public GlobalException() { - } - - public GlobalException(ErrorCode errorCode) { - this.code = errorCode.getCode(); - this.message = errorCode.getMessage(); - } - - public GlobalException(Integer code, String message) { - this.code = code; - this.message = message; - } - -} diff --git a/src/main/java/cn/iocoder/dashboard/common/pojo/CommonResult.java b/src/main/java/cn/iocoder/dashboard/common/pojo/CommonResult.java index 14e34070d..d6e50c65d 100644 --- a/src/main/java/cn/iocoder/dashboard/common/pojo/CommonResult.java +++ b/src/main/java/cn/iocoder/dashboard/common/pojo/CommonResult.java @@ -1,7 +1,6 @@ package cn.iocoder.dashboard.common.pojo; import cn.iocoder.dashboard.common.exception.ErrorCode; -import cn.iocoder.dashboard.common.exception.GlobalException; import cn.iocoder.dashboard.common.exception.ServiceException; import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -16,7 +15,7 @@ import java.io.Serializable; * @param 数据泛型 */ @Data -public final class CommonResult implements Serializable { +public class CommonResult implements Serializable { /** * 错误码 @@ -81,16 +80,12 @@ public final class CommonResult implements Serializable { // ========= 和 Exception 异常体系集成 ========= /** - * 判断是否有异常。如果有,则抛出 {@link GlobalException} 或 {@link ServiceException} 异常 + * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常 */ - public void checkError() throws GlobalException, ServiceException { + public void checkError() throws ServiceException { if (isSuccess()) { return; } - // 全局异常 - if (GlobalErrorCodeConstants.isMatch(code)) { - throw new GlobalException(code, msg); - } // 业务异常 throw new ServiceException(code, msg); } @@ -99,8 +94,4 @@ public final class CommonResult implements Serializable { return error(serviceException.getCode(), serviceException.getMessage()); } - public static CommonResult error(GlobalException globalException) { - return error(globalException.getCode(), globalException.getMessage()); - } - } diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsConstants.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsConstants.java index 139cc6736..c5351d68b 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsConstants.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsConstants.java @@ -8,9 +8,6 @@ package cn.iocoder.dashboard.framework.sms.core; */ public interface SmsConstants { - String OK = "OK"; - - String JING_HAO = "#"; String COMMA = ","; diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsResult.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsResult.java deleted file mode 100644 index 46c59033c..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsResult.java +++ /dev/null @@ -1,77 +0,0 @@ -package cn.iocoder.dashboard.framework.sms.core; - -import cn.hutool.core.exceptions.ExceptionUtil; -import cn.iocoder.dashboard.framework.sms.core.enums.SmsSendFailureTypeEnum; -import lombok.Data; - -import java.io.Serializable; - -/** - * 消息内容实体类 - */ -@Data -public class SmsResult implements Serializable { - - /** - * 是否成功 - * - * 注意,是调用 API 短信平台的请求是否成功 - */ - private Boolean success; - /** - * 发送失败的类型 - * - * 枚举 {@link SmsSendFailureTypeEnum#getType()} - */ - private Integer sendFailureType; - /** - * 发送失败的提示 - * - * 一般情况下,使用 {@link SmsSendFailureTypeEnum#getMsg()} - * 异常情况下,通过格式化 Exception 的提示存储 - */ - private String sendFailureMsg; - - /** - * 短信 API 发送的错误码 - * - * 由于第三方的错误码可能是字符串,所以使用 String 类型 - */ - private String apiSendCode; - /** - * 短信 API 发送的提示 - */ - private String apiSendMsg; - /** - * 短信 API 发送返回的唯一请求 ID - * - * 用于和短信 API 进行定位于排错 - */ - private String apiRequestId; - /** - * 短信 API 发送返回的序号 - * - * 用于和短信 API 平台的发送记录关联 - */ - private String apiSerialNo; - - private SmsResult() { - } - - public static SmsResult success(SmsSendFailureTypeEnum sendFailureType, - String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo) { - SmsResult result = new SmsResult().setSuccess(true).setApiSendCode(apiSendCode).setApiSendMsg(apiSendMsg) - .setApiRequestId(apiRequestId).setApiSerialNo(apiSerialNo); - if (sendFailureType != null) { - result.setSendFailureType(sendFailureType.getType()).setSendFailureMsg(sendFailureType.getMsg()); - } - return result; - } - - public static SmsResult error(Throwable ex) { - return new SmsResult().setSuccess(false) - .setSendFailureType(SmsSendFailureTypeEnum.SMS_SEND_EXCEPTION.getType()) - .setSendFailureMsg(ExceptionUtil.getRootCauseMessage(ex)); - } - -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/SmsClient.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/SmsClient.java index 2933a11a0..4a0d9aedc 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/SmsClient.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/SmsClient.java @@ -1,7 +1,7 @@ package cn.iocoder.dashboard.framework.sms.core.client; -import cn.iocoder.dashboard.framework.sms.core.SmsResult; import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail; +import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO; import javax.servlet.ServletRequest; import java.util.Map; @@ -30,7 +30,7 @@ public interface SmsClient { * @param templateParams 短信模板参数 * @return 短信发送结果 */ - SmsResult send(Long sendLogId, String mobile, String apiTemplateId, Map templateParams); + SmsCommonResult send(Long sendLogId, String mobile, String apiTemplateId, Map templateParams); // TODO FROM 芋艿 to ZZF:是不是可以改成意图更明确的解析返回结果,例如说 parseXXXX /** diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/SmsCommonResult.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/SmsCommonResult.java new file mode 100644 index 000000000..ffd8c5b0c --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/SmsCommonResult.java @@ -0,0 +1,59 @@ +package cn.iocoder.dashboard.framework.sms.core.client; + +import cn.hutool.core.exceptions.ExceptionUtil; +import cn.iocoder.dashboard.common.pojo.CommonResult; +import cn.iocoder.dashboard.framework.sms.core.enums.SmsSendFailureTypeEnum; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +/** + * 短信的 CommonResult 拓展类 + * + * 考虑到不同的平台,返回的 code 和 msg 是不同的,所以统一额外返回 {@link #apiCode} 和 {@link #apiMsg} 字段 + * + * 另外,一些短信平台(例如说阿里云、腾讯云)会返回一个请求编号,用于排查请求失败的问题,我们设置到 {@link #apiRequestId} 字段 + * + * @author 芋道源码 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SmsCommonResult extends CommonResult { + + /** + * API 返回错误码 + * + * 由于第三方的错误码可能是字符串,所以使用 String 类型 + */ + private String apiCode; + /** + * API 返回提示 + */ + private String apiMsg; + + /** + * API 请求编号 + */ + private String apiRequestId; + + private SmsCommonResult() { + } + + public static SmsCommonResult success(SmsSendFailureTypeEnum sendFailureType, + String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo) { + SmsCommonResult result = new SmsCommonResult().setSuccess(true).setApiSendCode(apiSendCode).setApiSendMsg(apiSendMsg) + .setApiRequestId(apiRequestId).setApiSerialNo(apiSerialNo); + if (sendFailureType != null) { + result.setSendFailureType(sendFailureType.getType()).setSendFailureMsg(sendFailureType.getMsg()); + } + return result; + } + + public static SmsCommonResult error(Throwable ex) { + return new SmsCommonResult().setSuccess(false) + .setSendFailureType(SmsSendFailureTypeEnum.SMS_SEND_EXCEPTION.getType()) + .setSendFailureMsg(ExceptionUtil.getRootCauseMessage(ex)); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsSendRespDTO.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsSendRespDTO.java new file mode 100644 index 000000000..c3a28cb93 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsSendRespDTO.java @@ -0,0 +1,15 @@ +package cn.iocoder.dashboard.framework.sms.core.client.dto; + +/** + * 短信发送响应 DTO + * + * @author 芋道源码 + */ +public class SmsSendRespDTO { + + /** + * 短信 API 发送返回的序号 + */ + private String serialNo; + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/AbstractSmsClient.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/AbstractSmsClient.java index c8bd94e7a..d2b008b73 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/AbstractSmsClient.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/AbstractSmsClient.java @@ -1,7 +1,8 @@ package cn.iocoder.dashboard.framework.sms.core.client.impl; -import cn.iocoder.dashboard.framework.sms.core.SmsResult; +import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult; import cn.iocoder.dashboard.framework.sms.core.client.SmsClient; +import cn.iocoder.dashboard.framework.sms.core.client.dto.SmsSendRespDTO; import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties; import lombok.extern.slf4j.Slf4j; @@ -60,8 +61,10 @@ public abstract class AbstractSmsClient implements SmsClient { } @Override - public final SmsResult send(Long sendLogId, String mobile, String apiTemplateId, Map templateParams) { - SmsResult result; + public final SmsCommonResult send(Long sendLogId, String mobile, + String apiTemplateId, Map templateParams) { + // 执行短信发送 + SmsCommonResult result; try { result = doSend(sendLogId, mobile, apiTemplateId, templateParams); } catch (Throwable ex) { @@ -69,7 +72,7 @@ public abstract class AbstractSmsClient implements SmsClient { log.error("[send][发送短信异常,sendLogId({}) mobile({}) apiTemplateId({}) templateParams({})]", sendLogId, mobile, apiTemplateId, templateParams, ex); // 封装返回 - return SmsResult.error(ex); + return SmsCommonResult.error(ex); } return result; } @@ -83,7 +86,7 @@ public abstract class AbstractSmsClient implements SmsClient { * @param templateParams 短信模板参数 * @return 短信发送结果 */ - protected abstract SmsResult doSend(Long sendLogId, String mobile, String apiTemplateId, Map templateParams) - throws Throwable; + protected abstract SmsCommonResult doSend(Long sendLogId, String mobile, + String apiTemplateId, Map templateParams) throws Throwable; } diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClient.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClient.java index f9f19a22e..a0f903871 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClient.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClient.java @@ -3,7 +3,7 @@ package cn.iocoder.dashboard.framework.sms.core.client.impl.aliyun; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; -import cn.iocoder.dashboard.framework.sms.core.SmsResult; +import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult; import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail; import cn.iocoder.dashboard.framework.sms.core.client.impl.AbstractSmsClient; import cn.iocoder.dashboard.framework.sms.core.enums.SmsSendFailureTypeEnum; @@ -59,7 +59,7 @@ public class AliyunSmsClient extends AbstractSmsClient { } @Override - protected SmsResult doSend(Long sendLogId, String mobile, String apiTemplateId, Map templateParams) throws Exception { + protected SmsCommonResult doSend(Long sendLogId, String mobile, String apiTemplateId, Map templateParams) throws Exception { // 构建参数 SendSmsRequest request = new SendSmsRequest(); request.setSysMethod(MethodType.POST); @@ -73,10 +73,10 @@ public class AliyunSmsClient extends AbstractSmsClient { // 执行发送 SendSmsResponse sendResult = acsClient.getAcsResponse(request); // 解析结果 - return SmsResult.success(parseSendFailureType(sendResult.getCode()), // 将 API 短信平台,解析成统一的错误码 + return SmsCommonResult.success(parseSendFailureType(sendResult.getCode()), // 将 API 短信平台,解析成统一的错误码 sendResult.getCode(), sendResult.getMessage(), sendResult.getRequestId(), sendResult.getBizId()); } catch (ClientException ex) { - return SmsResult.success(parseSendFailureType(ex.getErrCode()), // 将 API 短信平台,解析成统一的错误码 + return SmsCommonResult.success(parseSendFailureType(ex.getErrCode()), // 将 API 短信平台,解析成统一的错误码 ex.getErrCode(), formatResultMsg(ex), ex.getRequestId(), null); } } 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 9e840f6ae..0d6a51021 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 @@ -7,7 +7,7 @@ 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; +import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult; import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail; import cn.iocoder.dashboard.framework.sms.core.client.impl.AbstractSmsClient; import cn.iocoder.dashboard.framework.sms.core.enums.SmsSendFailureTypeEnum; @@ -66,7 +66,7 @@ public class YunpianSmsClient extends AbstractSmsClient { } @Override - protected SmsResult doSend(Long sendLogId, String mobile, String apiTemplateId, Map templateParams) throws Throwable { + protected SmsCommonResult doSend(Long sendLogId, String mobile, String apiTemplateId, Map templateParams) throws Throwable { // 构建参数 Map request = new HashMap<>(); request.put(YunpianConstant.APIKEY, properties.getApiKey()); @@ -82,7 +82,7 @@ public class YunpianSmsClient extends AbstractSmsClient { throw sendResult.getThrowable(); } // 解析结果 - return SmsResult.success(parseSendFailureType(sendResult), // 将 API 短信平台,解析成统一的错误码 + return SmsCommonResult.success(parseSendFailureType(sendResult), // 将 API 短信平台,解析成统一的错误码 String.valueOf(sendResult.getCode()), formatResultMsg(sendResult), null, getApiSerialNo(sendResult)); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java index 61a73ea71..e722b4499 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java @@ -4,7 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.enums.UserTypeEnum; -import cn.iocoder.dashboard.framework.sms.core.SmsResult; +import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult; import cn.iocoder.dashboard.framework.sms.core.client.SmsClient; import cn.iocoder.dashboard.framework.sms.core.client.SmsClientFactory; import cn.iocoder.dashboard.framework.sms.core.enums.SmsSendFailureTypeEnum; @@ -137,7 +137,7 @@ public class SysSmsServiceImpl implements SysSmsService { } // 发送短信 - SmsResult sendResult = smsClient.send(message.getSendLogId(), message.getMobile(), + SmsCommonResult sendResult = smsClient.send(message.getSendLogId(), message.getMobile(), message.getApiTemplateId(), message.getTemplateParams()); } diff --git a/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClientTest.java b/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClientTest.java index b042365e4..1c85031b6 100644 --- a/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClientTest.java +++ b/src/test-integration/java/cn/iocoder/dashboard/framework/sms/core/client/impl/aliyun/AliyunSmsClientTest.java @@ -1,6 +1,6 @@ package cn.iocoder.dashboard.framework.sms.core.client.impl.aliyun; -import cn.iocoder.dashboard.framework.sms.core.SmsResult; +import cn.iocoder.dashboard.framework.sms.core.client.SmsCommonResult; import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum; import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties; import org.junit.jupiter.api.Test; @@ -30,7 +30,7 @@ public class AliyunSmsClientTest { templateParams.put("code", "1024"); // templateParams.put("operation", "嘿嘿"); // SmsResult result = smsClient.send(1L, "15601691399", "4372216", templateParams); - SmsResult result = smsClient.send(1L, "15601691399", "SMS_207945135", templateParams); + SmsCommonResult result = smsClient.send(1L, "15601691399", "SMS_207945135", templateParams); System.out.println(result); } 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 index af4a8b6a0..028cd6c77 100644 --- 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 @@ -1,6 +1,6 @@ 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.client.SmsCommonResult; import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum; import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperties; import org.junit.jupiter.api.Test; @@ -29,7 +29,7 @@ public class YunpianSmsClientIntegrationTest { templateParams.put("code", "1024"); templateParams.put("operation", "嘿嘿"); // SmsResult result = smsClient.send(1L, "15601691399", "4372216", templateParams); - SmsResult result = smsClient.send(1L, "15601691399", "4383920", templateParams); + SmsCommonResult result = smsClient.send(1L, "15601691399", "4383920", templateParams); System.out.println(result); }