Skip to content

Commit

Permalink
sms:移除 SmsCodeMapping + SmsCommonResult,简化短信的封装
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Nov 21, 2023
1 parent 4118f25 commit 562f825
Show file tree
Hide file tree
Showing 30 changed files with 338 additions and 866 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public interface SmsClient {
* @param templateParams 短信模板参数。通过 List 数组,保证参数的顺序
* @return 短信发送结果
*/
SmsCommonResult<SmsSendRespDTO> sendSms(Long logId, String mobile, String apiTemplateId,
List<KeyValue<String, Object>> templateParams);
SmsSendRespDTO sendSms(Long logId, String mobile, String apiTemplateId,
List<KeyValue<String, Object>> templateParams) throws Throwable;

/**
* 解析接收短信的接收结果
Expand All @@ -49,6 +49,6 @@ SmsCommonResult<SmsSendRespDTO> sendSms(Long logId, String mobile, String apiTem
* @param apiTemplateId 短信 API 的模板编号
* @return 短信模板
*/
SmsCommonResult<SmsTemplateRespDTO> getSmsTemplate(String apiTemplateId);
SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable;

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,34 @@
@Data
public class SmsSendRespDTO {

/**
* 是否成功
*/
private Boolean success;

/**
* API 请求编号
*/
private String apiRequestId;

// ==================== 成功时字段 ====================

/**
* 短信 API 发送返回的序号
*/
private String serialNo;

// ==================== 失败时字段 ====================

/**
* API 返回错误码
*
* 由于第三方的错误码可能是字符串,所以使用 String 类型
*/
private String apiCode;
/**
* API 返回提示
*/
private String apiMsg;

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package cn.iocoder.yudao.framework.sms.core.client.impl;

import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.sms.core.client.SmsClient;
import cn.iocoder.yudao.framework.sms.core.client.SmsCodeMapping;
import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult;
import cn.iocoder.yudao.framework.sms.core.client.dto.SmsReceiveRespDTO;
import cn.iocoder.yudao.framework.sms.core.client.dto.SmsSendRespDTO;
import cn.iocoder.yudao.framework.sms.core.client.dto.SmsTemplateRespDTO;
import cn.iocoder.yudao.framework.sms.core.property.SmsChannelProperties;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
* 短信客户端的抽象类,提供模板方法,减少子类的冗余代码
*
Expand All @@ -25,14 +17,9 @@ public abstract class AbstractSmsClient implements SmsClient {
* 短信渠道配置
*/
protected volatile SmsChannelProperties properties;
/**
* 错误码枚举类
*/
protected final SmsCodeMapping codeMapping;

public AbstractSmsClient(SmsChannelProperties properties, SmsCodeMapping codeMapping) {
this.properties = prepareProperties(properties);
this.codeMapping = codeMapping;
public AbstractSmsClient(SmsChannelProperties properties) {
this.properties = properties;
}

/**
Expand All @@ -54,74 +41,13 @@ public final void refresh(SmsChannelProperties properties) {
return;
}
log.info("[refresh][配置({})发生变化,重新初始化]", properties);
this.properties = prepareProperties(properties);
// 初始化
this.init();
}

/**
* 在赋值给{@link this#properties}前,子类可根据需要预处理短信渠道配置
*
* @param properties 数据库中存储的短信渠道配置
* @return 满足子类实现的短信渠道配置
*/
protected SmsChannelProperties prepareProperties(SmsChannelProperties properties) {
return properties;
}

@Override
public Long getId() {
return properties.getId();
}

@Override
public final SmsCommonResult<SmsSendRespDTO> sendSms(Long logId, String mobile,
String apiTemplateId, List<KeyValue<String, Object>> templateParams) {
// 执行短信发送
SmsCommonResult<SmsSendRespDTO> result;
try {
result = doSendSms(logId, mobile, apiTemplateId, templateParams);
} catch (Throwable ex) {
// 打印异常日志
log.error("[sendSms][发送短信异常,sendLogId({}) mobile({}) apiTemplateId({}) templateParams({})]",
logId, mobile, apiTemplateId, templateParams, ex);
// 封装返回
return SmsCommonResult.error(ex);
}
return result;
}

protected abstract SmsCommonResult<SmsSendRespDTO> doSendSms(Long sendLogId, String mobile,
String apiTemplateId, List<KeyValue<String, Object>> templateParams)
throws Throwable;

@Override
public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) throws Throwable {
try {
return doParseSmsReceiveStatus(text);
} catch (Throwable ex) {
log.error("[parseSmsReceiveStatus][text({}) 解析发生异常]", text, ex);
throw ex;
}
}

protected abstract List<SmsReceiveRespDTO> doParseSmsReceiveStatus(String text) throws Throwable;

@Override
public SmsCommonResult<SmsTemplateRespDTO> getSmsTemplate(String apiTemplateId) {
// 执行短信发送
SmsCommonResult<SmsTemplateRespDTO> result;
try {
result = doGetSmsTemplate(apiTemplateId);
} catch (Throwable ex) {
// 打印异常日志
log.error("[getSmsTemplate][获得短信模板({}) 发生异常]", apiTemplateId, ex);
// 封装返回
return SmsCommonResult.error(ex);
}
return result;
}

protected abstract SmsCommonResult<SmsTemplateRespDTO> doGetSmsTemplate(String apiTemplateId) throws Throwable;

}
Loading

0 comments on commit 562f825

Please sign in to comment.