短信 code review 2020-02-25
parent
2a4d9f43eb
commit
cd2a01819b
|
@ -22,7 +22,7 @@ public abstract class AbstractSmsClient implements SmsClient {
|
||||||
protected final SmsChannelProperty channelVO;
|
protected final SmsChannelProperty channelVO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造阿里云短信发送处理
|
* 构造阿里云短信发送处理 TODO FROM 芋艿 to zzf:貌似注释不对
|
||||||
*
|
*
|
||||||
* @param property 阿里云短信配置
|
* @param property 阿里云短信配置
|
||||||
*/
|
*/
|
||||||
|
@ -30,13 +30,12 @@ public abstract class AbstractSmsClient implements SmsClient {
|
||||||
this.channelVO = property;
|
this.channelVO = property;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public SmsChannelProperty getProperty() {
|
public SmsChannelProperty getProperty() {
|
||||||
return channelVO;
|
return channelVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SmsResult send(String templateApiId, SmsBody smsBody, Collection<String> targets) {
|
public final SmsResult send(String templateApiId, SmsBody smsBody, Collection<String> targets) {
|
||||||
SmsResult result;
|
SmsResult result;
|
||||||
try {
|
try {
|
||||||
beforeSend(templateApiId, smsBody, targets);
|
beforeSend(templateApiId, smsBody, targets);
|
||||||
|
@ -50,13 +49,13 @@ public abstract class AbstractSmsClient implements SmsClient {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送消息
|
* 发送消息
|
||||||
*
|
*
|
||||||
* @param templateApiId 短信模板唯一标识
|
* @param templateApiId 短信模板唯一标识
|
||||||
* @param smsBody 消息内容
|
* @param smsBody 消息内容
|
||||||
* @param targets 发送对象列表
|
* @param targets 发送对象列表
|
||||||
|
* @throws Exception 调用发送失败,抛出异常
|
||||||
* @return 短信发送结果
|
* @return 短信发送结果
|
||||||
*/
|
*/
|
||||||
public abstract SmsResult doSend(String templateApiId, SmsBody smsBody, Collection<String> targets) throws Exception;
|
public abstract SmsResult doSend(String templateApiId, SmsBody smsBody, Collection<String> targets) throws Exception;
|
||||||
|
@ -67,7 +66,7 @@ public abstract class AbstractSmsClient implements SmsClient {
|
||||||
protected void afterSend(String templateApiId, SmsBody smsBody, Collection<String> targets, SmsResult result) throws Exception {
|
protected void afterSend(String templateApiId, SmsBody smsBody, Collection<String> targets, SmsResult result) throws Exception {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO FROM 芋艿 to zzf:可以考虑抽到 SmsResult 里
|
||||||
SmsResult failResult(String message) {
|
SmsResult failResult(String message) {
|
||||||
SmsResult resultBody = new SmsResult();
|
SmsResult resultBody = new SmsResult();
|
||||||
resultBody.setSuccess(false);
|
resultBody.setSuccess(false);
|
||||||
|
|
|
@ -58,16 +58,14 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||||
acsClient = new DefaultAcsClient(profile);
|
acsClient = new DefaultAcsClient(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SmsResult doSend(String templateApiId, SmsBody smsBody, Collection<String> targets) throws Exception {
|
public SmsResult doSend(String templateApiId, SmsBody smsBody, Collection<String> targets) throws Exception {
|
||||||
SendSmsRequest request = new SendSmsRequest();
|
SendSmsRequest request = new SendSmsRequest();
|
||||||
request.setSysMethod(MethodType.POST);
|
request.setSysMethod(MethodType.POST);
|
||||||
request.setPhoneNumbers(StringUtils.join(targets, ","));
|
request.setPhoneNumbers(StringUtils.join(targets, ",")); // TODO FROM 芋艿 to zzf:统一使用 Hutool 工具类嘿。
|
||||||
request.setSignName(channelVO.getApiSignatureId());
|
request.setSignName(channelVO.getApiSignatureId());
|
||||||
request.setTemplateCode(templateApiId);
|
request.setTemplateCode(templateApiId);
|
||||||
request.setTemplateParam(smsBody.getParamsStr());
|
request.setTemplateParam(smsBody.getParamsStr());
|
||||||
// TODO FROM 芋艿 TO zzf:try catch 咱是不是可以交给 abstract 来做。这样,异常处理,重试,限流等等,都可以酱紫 DONE
|
|
||||||
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
||||||
|
|
||||||
boolean result = OK.equals(sendSmsResponse.getCode());
|
boolean result = OK.equals(sendSmsResponse.getCode());
|
||||||
|
@ -78,7 +76,7 @@ public class AliyunSmsClient extends AbstractSmsClient {
|
||||||
resultBody.setSuccess(result);
|
resultBody.setSuccess(result);
|
||||||
QuerySendDetailsRequest querySendDetailsRequest = new QuerySendDetailsRequest();
|
QuerySendDetailsRequest querySendDetailsRequest = new QuerySendDetailsRequest();
|
||||||
querySendDetailsRequest.setBizId(sendSmsResponse.getBizId());
|
querySendDetailsRequest.setBizId(sendSmsResponse.getBizId());
|
||||||
|
// TODO FROM 芋艿 to zzf:发送完之后,基于短信平台回调,去更新回执状态。短信发送是否成功,和最终用户收到,是两个维度。这块有困惑,可以微信,我给个截图哈。
|
||||||
QuerySendDetailsResponse acsResponse = acsClient.getAcsResponse(querySendDetailsRequest);
|
QuerySendDetailsResponse acsResponse = acsClient.getAcsResponse(querySendDetailsRequest);
|
||||||
List<SmsResultDetail> resultDetailList = new ArrayList<>(Integer.parseInt(acsResponse.getTotalCount()));
|
List<SmsResultDetail> resultDetailList = new ArrayList<>(Integer.parseInt(acsResponse.getTotalCount()));
|
||||||
acsResponse.getSmsSendDetailDTOs().forEach(s -> {
|
acsResponse.getSmsSendDetailDTOs().forEach(s -> {
|
||||||
|
|
|
@ -42,7 +42,6 @@ public class SmsClientFactory {
|
||||||
* @return 客户端id(默认channelId)
|
* @return 客户端id(默认channelId)
|
||||||
*/
|
*/
|
||||||
public Long createClient(SmsChannelProperty propertyVO) {
|
public Long createClient(SmsChannelProperty propertyVO) {
|
||||||
// TODO FROM 芋艿 TO zzf:参数的校验,可以考虑统一使用 validation。 DONE
|
|
||||||
AbstractSmsClient sender = createClient(SmsChannelEnum.getByCode(propertyVO.getCode()), propertyVO);
|
AbstractSmsClient sender = createClient(SmsChannelEnum.getByCode(propertyVO.getCode()), propertyVO);
|
||||||
smsSenderMap.put(propertyVO.getId(), sender);
|
smsSenderMap.put(propertyVO.getId(), sender);
|
||||||
return propertyVO.getId();
|
return propertyVO.getId();
|
||||||
|
@ -77,7 +76,7 @@ public class SmsClientFactory {
|
||||||
* 添加或修改短信模板信息缓存
|
* 添加或修改短信模板信息缓存
|
||||||
*/
|
*/
|
||||||
public void addOrUpdateTemplateCache(Collection<SmsTemplateProperty> templateProperties) {
|
public void addOrUpdateTemplateCache(Collection<SmsTemplateProperty> templateProperties) {
|
||||||
templateProperties.forEach(s -> templatePropertyMap.put(s.getCode(), s));
|
templateProperties.forEach(s -> addOrUpdateTemplateCache(templateProperties));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -102,4 +101,5 @@ public class SmsClientFactory {
|
||||||
}
|
}
|
||||||
return smsTemplateProperty.getApiTemplateId();
|
return smsTemplateProperty.getApiTemplateId();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,6 @@ public class SmsResult implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 是否成功(发送短信的请求是否成功)
|
* 是否成功(发送短信的请求是否成功)
|
||||||
*/
|
*/
|
||||||
// TODO FROM 芋艿 to zzf:未来要加一个 code,将不同平台的短信失败的情况,做一次统一的收敛。 DONE
|
|
||||||
private Boolean success;
|
private Boolean success;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,7 +29,6 @@ public class SmsResult implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 返回值
|
* 返回值
|
||||||
*/
|
*/
|
||||||
// TODO FROM 芋艿 to zzf:是不是统一各个平台的返回结果,这样对调用方来说统一。因为作为统一的短信客户端,最好让上层不太需要知道太具体。黑河诶 DONE
|
|
||||||
private List<SmsResultDetail> result;
|
private List<SmsResultDetail> result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 短信渠道枚举 TODO FROM 芋艿 TO zzf:属于短信的枚举类,可以放到 framework/sms 下 DONE
|
* 短信渠道枚举
|
||||||
*
|
*
|
||||||
* @author zzf
|
* @author zzf
|
||||||
* @date 2021/1/25 10:56
|
* @date 2021/1/25 10:56
|
||||||
|
@ -16,7 +16,7 @@ public enum SmsChannelEnum {
|
||||||
ALI("ALI", "阿里"),
|
ALI("ALI", "阿里"),
|
||||||
HUA_WEI("HUA_WEI", "华为"),
|
HUA_WEI("HUA_WEI", "华为"),
|
||||||
QI_NIU("QI_NIU", "七牛"),
|
QI_NIU("QI_NIU", "七牛"),
|
||||||
TENCENT("TENCENT", "腾讯"); // TODO FROM 芋艿 to zzf:TEN 有后鼻音哈,要被马爸爸打了。。。 DONE
|
TENCENT("TENCENT", "腾讯");
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
|
|
||||||
|
@ -30,4 +30,5 @@ public enum SmsChannelEnum {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,6 @@ import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 渠道模板VO类
|
* 渠道模板VO类
|
||||||
* TODO FROM 芋艿 TO zzf:模板是不是不要提供到 client 里面,而是交给 factory 统一维护就好。不然,模板修改时候,刷新 client 会比较麻烦。 DONE
|
|
||||||
*
|
|
||||||
*
|
*
|
||||||
* @author zzf
|
* @author zzf
|
||||||
* @date 2021/1/25 17:03
|
* @date 2021/1/25 17:03
|
||||||
|
|
|
@ -37,12 +37,10 @@ public class SmsChannelController {
|
||||||
return success(service.getSmsChannelEnums());
|
return success(service.getSmsChannelEnums());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ApiOperation("添加消息渠道")
|
@ApiOperation("添加消息渠道")
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
public CommonResult<Long> add(@Validated @RequestBody SmsChannelCreateReqVO reqVO) {
|
public CommonResult<Long> add(@Validated @RequestBody SmsChannelCreateReqVO reqVO) {
|
||||||
return success(service.createSmsChannel(reqVO));
|
return success(service.createSmsChannel(reqVO));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class SmsSendConsumer extends AbstractChannelMessageListener<SmsSendMessa
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(SmsSendMessage message) {
|
public void onMessage(SmsSendMessage message) {
|
||||||
log.info("[onMessage][收到 发送短信 消息]");
|
log.info("[onMessage][收到 发送短信 消息]"); // TODO FROM 芋艿 to zzf:log 如果要打,这里要内容打上。其他 Consumer 没打的原因,是没内容。。。
|
||||||
SmsResult send = sysSmsService.send(message.getSmsBody(), message.getTargetPhones());
|
SmsResult send = sysSmsService.send(message.getSmsBody(), message.getTargetPhones());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,9 +18,6 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public interface SysSmsChannelService {
|
public interface SysSmsChannelService {
|
||||||
|
|
||||||
// TODO FROM 芋艿 to ZZF:SmsChannelService=》SysSmsChannelService,增加 Sys 前缀,算在系统模块里
|
|
||||||
// TODO FROM 芋艿 to ZZF:方法名,保持不去掉 Sms 前缀。虽然长点,嘿嘿
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化短信渠道
|
* 初始化短信渠道
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -42,6 +42,7 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private SmsClientFactory clientFactory;
|
private SmsClientFactory clientFactory;
|
||||||
|
|
||||||
|
// TODO FROM 芋艿 to zzf:方法要放在成员变量下面;
|
||||||
/**
|
/**
|
||||||
* 初始化短信客户端
|
* 初始化短信客户端
|
||||||
*/
|
*/
|
||||||
|
@ -65,7 +66,6 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO FROM 芋艿 to ZZF:channelMapper 嘿,保持命名统一。 DONE
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysSmsChannelMapper channelMapper;
|
private SysSmsChannelMapper channelMapper;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue