From 13877758f90ef0833dd7b9cd7beab36defd8882c Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Mon, 8 Feb 2021 10:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90sms=E3=80=91=E6=95=B4=E7=90=86?= =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E6=A8=A1=E5=9D=97=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/api/exception/SmsException.java | 9 ++--- .../roses/kernel/aliyun/AliyunSmsSender.java | 33 +++++++------------ .../kernel/sms/tencent/TencentSmsSender.java | 9 ++--- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/kernel-d-sms/sms-api/src/main/java/cn/stylefeng/roses/kernel/sms/api/exception/SmsException.java b/kernel-d-sms/sms-api/src/main/java/cn/stylefeng/roses/kernel/sms/api/exception/SmsException.java index a20725a71..1628b5dd9 100644 --- a/kernel-d-sms/sms-api/src/main/java/cn/stylefeng/roses/kernel/sms/api/exception/SmsException.java +++ b/kernel-d-sms/sms-api/src/main/java/cn/stylefeng/roses/kernel/sms/api/exception/SmsException.java @@ -1,5 +1,6 @@ package cn.stylefeng.roses.kernel.sms.api.exception; +import cn.hutool.core.util.StrUtil; import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum; import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; import cn.stylefeng.roses.kernel.sms.api.constants.SmsConstants; @@ -12,12 +13,12 @@ import cn.stylefeng.roses.kernel.sms.api.constants.SmsConstants; */ public class SmsException extends ServiceException { - public SmsException(String errorCode, String userTip) { - super(SmsConstants.SMS_MODULE_NAME, errorCode, userTip); - } - public SmsException(AbstractExceptionEnum exception) { super(SmsConstants.SMS_MODULE_NAME, exception); } + public SmsException(AbstractExceptionEnum exception, Object... params) { + super(SmsConstants.SMS_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params)); + } + } diff --git a/kernel-d-sms/sms-sdk-aliyun/src/main/java/cn/stylefeng/roses/kernel/aliyun/AliyunSmsSender.java b/kernel-d-sms/sms-sdk-aliyun/src/main/java/cn/stylefeng/roses/kernel/aliyun/AliyunSmsSender.java index 63927d71a..cedcb04e8 100644 --- a/kernel-d-sms/sms-sdk-aliyun/src/main/java/cn/stylefeng/roses/kernel/aliyun/AliyunSmsSender.java +++ b/kernel-d-sms/sms-sdk-aliyun/src/main/java/cn/stylefeng/roses/kernel/aliyun/AliyunSmsSender.java @@ -2,6 +2,12 @@ package cn.stylefeng.roses.kernel.aliyun; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; +import cn.stylefeng.roses.kernel.aliyun.enums.AliyunSmsResultEnum; +import cn.stylefeng.roses.kernel.aliyun.msign.MultiSignManager; +import cn.stylefeng.roses.kernel.sms.api.SmsSenderApi; +import cn.stylefeng.roses.kernel.sms.api.exception.SmsException; +import cn.stylefeng.roses.kernel.sms.api.exception.enums.SmsExceptionEnum; +import cn.stylefeng.roses.kernel.sms.api.pojo.AliyunSmsProperties; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.CommonRequest; @@ -10,12 +16,6 @@ import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; -import cn.stylefeng.roses.kernel.aliyun.enums.AliyunSmsResultEnum; -import cn.stylefeng.roses.kernel.aliyun.msign.MultiSignManager; -import cn.stylefeng.roses.kernel.sms.api.SmsSenderApi; -import cn.stylefeng.roses.kernel.sms.api.exception.SmsException; -import cn.stylefeng.roses.kernel.sms.api.exception.enums.SmsExceptionEnum; -import cn.stylefeng.roses.kernel.sms.api.pojo.AliyunSmsProperties; import lombok.extern.slf4j.Slf4j; import java.util.Map; @@ -68,9 +68,7 @@ public class AliyunSmsSender implements SmsSenderApi { } // 组装错误信息 - String userTip = SmsExceptionEnum.ALIYUN_SMS_ERROR.getUserTip(); - String finalUserTip = StrUtil.format(userTip, code, errorMessage); - throw new SmsException(SmsExceptionEnum.ALIYUN_SMS_ERROR.getErrorCode(), finalUserTip); + throw new SmsException(SmsExceptionEnum.ALIYUN_SMS_ERROR, code, errorMessage); } } @@ -125,9 +123,7 @@ public class AliyunSmsSender implements SmsSenderApi { log.error("初始化阿里云sms异常!可能是accessKey和secret错误!", e); // 组装错误信息 - String userTip = SmsExceptionEnum.ALIYUN_SMS_KEY_ERROR.getUserTip(); - String finalUserTip = StrUtil.format(userTip, aliyunSmsProperties.getAccessKeyId()); - throw new SmsException(SmsExceptionEnum.ALIYUN_SMS_ERROR.getErrorCode(), finalUserTip); + throw new SmsException(SmsExceptionEnum.ALIYUN_SMS_KEY_ERROR, aliyunSmsProperties.getAccessKeyId()); } } @@ -141,25 +137,20 @@ public class AliyunSmsSender implements SmsSenderApi { AliyunSmsProperties aliyunSmsProperties) { if (StrUtil.isBlank(phoneNumber)) { - String userTip = StrUtil.format(SEND_SMS_PARAM_NULL.getUserTip(), "电话号码"); - throw new SmsException(SEND_SMS_PARAM_NULL.getErrorCode(), userTip); + throw new SmsException(SEND_SMS_PARAM_NULL, "电话号码"); } if (StrUtil.isBlank(templateCode)) { - String userTip = StrUtil.format(SEND_SMS_PARAM_NULL.getUserTip(), "模板号templateCode"); - throw new SmsException(SEND_SMS_PARAM_NULL.getErrorCode(), userTip); + throw new SmsException(SEND_SMS_PARAM_NULL, "模板号templateCode"); } if (ObjectUtil.isEmpty(params)) { - String userTip = StrUtil.format(SEND_SMS_PARAM_NULL.getUserTip(), "模板参数"); - throw new SmsException(SEND_SMS_PARAM_NULL.getErrorCode(), userTip); + throw new SmsException(SEND_SMS_PARAM_NULL, "模板参数"); } if (ObjectUtil.isEmpty(aliyunSmsProperties)) { - String userTip = StrUtil.format(SEND_SMS_PARAM_NULL.getUserTip(), "短信配置properties"); - throw new SmsException(SEND_SMS_PARAM_NULL.getErrorCode(), userTip); + throw new SmsException(SEND_SMS_PARAM_NULL, "短信配置properties"); } - } /** diff --git a/kernel-d-sms/sms-sdk-tencent/src/main/java/cn/stylefeng/roses/kernel/sms/tencent/TencentSmsSender.java b/kernel-d-sms/sms-sdk-tencent/src/main/java/cn/stylefeng/roses/kernel/sms/tencent/TencentSmsSender.java index 22ad025ce..957949e03 100644 --- a/kernel-d-sms/sms-sdk-tencent/src/main/java/cn/stylefeng/roses/kernel/sms/tencent/TencentSmsSender.java +++ b/kernel-d-sms/sms-sdk-tencent/src/main/java/cn/stylefeng/roses/kernel/sms/tencent/TencentSmsSender.java @@ -1,7 +1,6 @@ package cn.stylefeng.roses.kernel.sms.tencent; import cn.hutool.core.util.ArrayUtil; -import cn.hutool.core.util.StrUtil; import cn.stylefeng.roses.kernel.sms.api.SmsSenderApi; import cn.stylefeng.roses.kernel.sms.api.exception.SmsException; import cn.stylefeng.roses.kernel.sms.api.pojo.TencentSmsProperties; @@ -82,13 +81,11 @@ public class TencentSmsSender implements SmsSenderApi { SendStatus[] sendStatusSet = res.getSendStatusSet(); if (sendStatusSet != null && sendStatusSet.length > 0) { if (!sendStatusSet[0].getCode().equals("Ok")) { - String userTip = StrUtil.format(TENCENT_SMS_PARAM_NULL.getUserTip(), sendStatusSet[0].getCode(), sendStatusSet[0].getMessage()); - throw new SmsException(TENCENT_SMS_PARAM_NULL.getErrorCode(), userTip); + throw new SmsException(TENCENT_SMS_PARAM_NULL, sendStatusSet[0].getCode(), sendStatusSet[0].getMessage()); } } } catch (TencentCloudSDKException e) { - String userTip = StrUtil.format(TENCENT_SMS_PARAM_NULL.getUserTip(), TENCENT_SMS_PARAM_NULL.getErrorCode(), e.getMessage()); - throw new SmsException(TENCENT_SMS_PARAM_NULL.getErrorCode(), userTip); + throw new SmsException(TENCENT_SMS_PARAM_NULL, TENCENT_SMS_PARAM_NULL.getErrorCode(), e.getMessage()); } } -} \ No newline at end of file +}