短信提交 2021-03-31,重构返回的结果

pull/2/head
YunaiV 2021-03-31 00:46:20 +08:00
parent 60911c9f56
commit 9528698a5f
13 changed files with 101 additions and 154 deletions

View File

@ -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;
}
}

View File

@ -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 <T>
*/
@Data
public final class CommonResult<T> implements Serializable {
public class CommonResult<T> implements Serializable {
/**
*
@ -81,16 +80,12 @@ public final class CommonResult<T> 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<T> implements Serializable {
return error(serviceException.getCode(), serviceException.getMessage());
}
public static <T> CommonResult<T> error(GlobalException globalException) {
return error(globalException.getCode(), globalException.getMessage());
}
}

View File

@ -8,9 +8,6 @@ package cn.iocoder.dashboard.framework.sms.core;
*/
public interface SmsConstants {
String OK = "OK";
String JING_HAO = "#";
String COMMA = ",";

View File

@ -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));
}
}

View File

@ -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<String, Object> templateParams);
SmsCommonResult<SmsSendRespDTO> send(Long sendLogId, String mobile, String apiTemplateId, Map<String, Object> templateParams);
// TODO FROM 芋艿 to ZZF是不是可以改成意图更明确的解析返回结果例如说 parseXXXX
/**

View File

@ -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<T> extends CommonResult<T> {
/**
* 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));
}
}

View File

@ -0,0 +1,15 @@
package cn.iocoder.dashboard.framework.sms.core.client.dto;
/**
* DTO
*
* @author
*/
public class SmsSendRespDTO {
/**
* API
*/
private String serialNo;
}

View File

@ -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<String, Object> templateParams) {
SmsResult result;
public final SmsCommonResult<SmsSendRespDTO> send(Long sendLogId, String mobile,
String apiTemplateId, Map<String, Object> templateParams) {
// 执行短信发送
SmsCommonResult<SmsSendRespDTO> 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<String, Object> templateParams)
throws Throwable;
protected abstract SmsCommonResult<SmsSendRespDTO> doSend(Long sendLogId, String mobile,
String apiTemplateId, Map<String, Object> templateParams) throws Throwable;
}

View File

@ -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<String, Object> templateParams) throws Exception {
protected SmsCommonResult doSend(Long sendLogId, String mobile, String apiTemplateId, Map<String, Object> 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);
}
}

View File

@ -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<String, Object> templateParams) throws Throwable {
protected SmsCommonResult doSend(Long sendLogId, String mobile, String apiTemplateId, Map<String, Object> templateParams) throws Throwable {
// 构建参数
Map<String, String> 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));
}

View File

@ -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());
}

View File

@ -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);
}

View File

@ -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);
}