优化部分代码逻辑

This commit is contained in:
zengzefeng 2021-02-26 11:30:37 +08:00
parent cd2a01819b
commit c3372d4bd2
8 changed files with 45 additions and 43 deletions

View File

@ -22,9 +22,9 @@ public abstract class AbstractSmsClient implements SmsClient {
protected final SmsChannelProperty channelVO;
/**
* 构造阿里云短信发送处理 TODO FROM 芋艿 to zzf貌似注释不对
* 短信客户端有参构造函数
*
* @param property 阿里云短信配置
* @param property 短信配置
*/
public AbstractSmsClient(SmsChannelProperty property) {
this.channelVO = property;
@ -44,7 +44,7 @@ public abstract class AbstractSmsClient implements SmsClient {
} catch (Exception e) {
// exception handle
log.debug(e.getMessage(), e);
return failResult("发送异常: " + e.getMessage());
return SmsResult.failResult("发送异常: " + e.getMessage());
}
return result;
}
@ -55,8 +55,8 @@ public abstract class AbstractSmsClient implements SmsClient {
* @param templateApiId 短信模板唯一标识
* @param smsBody 消息内容
* @param targets 发送对象列表
* @throws Exception 调用发送失败抛出异常
* @return 短信发送结果
* @throws Exception 调用发送失败抛出异常
*/
public abstract SmsResult doSend(String templateApiId, SmsBody smsBody, Collection<String> targets) throws Exception;
@ -66,11 +66,4 @@ public abstract class AbstractSmsClient implements SmsClient {
protected void afterSend(String templateApiId, SmsBody smsBody, Collection<String> targets, SmsResult result) throws Exception {
}
// TODO FROM 芋艿 to zzf可以考虑抽到 SmsResult
SmsResult failResult(String message) {
SmsResult resultBody = new SmsResult();
resultBody.setSuccess(false);
resultBody.setMessage(message);
return resultBody;
}
}

View File

@ -1,6 +1,8 @@
package cn.iocoder.dashboard.framework.sms.client;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.framework.sms.core.SmsBody;
import cn.iocoder.dashboard.framework.sms.core.SmsResult;
import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail;
@ -62,7 +64,7 @@ public class AliyunSmsClient extends AbstractSmsClient {
public SmsResult doSend(String templateApiId, SmsBody smsBody, Collection<String> targets) throws Exception {
SendSmsRequest request = new SendSmsRequest();
request.setSysMethod(MethodType.POST);
request.setPhoneNumbers(StringUtils.join(targets, ",")); // TODO FROM 芋艿 to zzf统一使用 Hutool 工具类嘿
request.setPhoneNumbers(ArrayUtil.join(targets, ","));
request.setSignName(channelVO.getApiSignatureId());
request.setTemplateCode(templateApiId);
request.setTemplateParam(smsBody.getParamsStr());

View File

@ -76,7 +76,7 @@ public class SmsClientFactory {
* 添加或修改短信模板信息缓存
*/
public void addOrUpdateTemplateCache(Collection<SmsTemplateProperty> templateProperties) {
templateProperties.forEach(s -> addOrUpdateTemplateCache(templateProperties));
templateProperties.forEach(this::addOrUpdateTemplateCache);
}

View File

@ -31,4 +31,11 @@ public class SmsResult implements Serializable {
*/
private List<SmsResultDetail> result;
public static SmsResult failResult(String message) {
SmsResult resultBody = new SmsResult();
resultBody.setSuccess(false);
resultBody.setMessage(message);
return resultBody;
}
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.dashboard.modules.system.convert.sms;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.sms.core.property.SmsTemplateProperty;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsTemplateVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsTemplateDO;
@ -23,4 +24,6 @@ public interface SmsTemplateConvert {
SmsTemplateVO convert(SysSmsTemplateDO bean);
List<SmsTemplateProperty> convertProperty(List<SysSmsTemplateDO> bean);
}

View File

@ -24,7 +24,7 @@ public class SmsSendConsumer extends AbstractChannelMessageListener<SmsSendMessa
@Override
public void onMessage(SmsSendMessage message) {
log.info("[onMessage][收到 发送短信 消息]"); // TODO FROM 芋艿 to zzflog 如果要打这里要内容打上其他 Consumer 没打的原因是没内容
log.info("[onMessage][收到 发送短信 消息], content: " + message.toString());
SmsResult send = sysSmsService.send(message.getSmsBody(), message.getTargetPhones());
}

View File

@ -19,9 +19,9 @@ import java.util.List;
public interface SysSmsChannelService {
/**
* 初始化短信渠道
* 初始化短信渠道并缓存短信模板信息
*/
void initSmsClient();
void initSmsClientAndCacheSmsTemplate();
/**
* 分页查询短信渠道信息

View File

@ -6,7 +6,9 @@ import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient;
import cn.iocoder.dashboard.framework.sms.core.SmsClientFactory;
import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum;
import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty;
import cn.iocoder.dashboard.framework.sms.core.property.SmsTemplateProperty;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelAllVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsTemplateVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelPageReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.resp.SmsChannelEnumRespVO;
@ -39,39 +41,36 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService {
private final Map<String, Long> templateCode2ChannelIdMap = new ConcurrentHashMap<>(32);
@Autowired
@Resource
private SmsClientFactory clientFactory;
// TODO FROM 芋艿 to zzf方法要放在成员变量下面
/**
* 初始化短信客户端
*/
@PostConstruct
@Override
public void initSmsClient() {
// 查询有效渠道信息
List<SysSmsChannelDO> channelDOList = channelMapper.selectEnabledList();
List<SmsChannelProperty> propertyList = SmsChannelConvert.INSTANCE.convertProperties(channelDOList);
// 遍历渠道生成client并获取模板缓存
propertyList.forEach(channelProperty -> {
Long clientId = clientFactory.createClient(channelProperty);
List<SysSmsTemplateDO> templateDOList = templateMapper.selectListByChannelId(channelProperty.getId());
if (ObjectUtil.isNotEmpty(templateDOList)) {
templateDOList.forEach(template -> {
templateCode2ChannelIdMap.put(template.getCode(), clientId);
});
SmsTemplateConvert.INSTANCE.convert(templateDOList);
}
});
}
@Resource
private SysSmsChannelMapper channelMapper;
@Resource
private SysSmsTemplateMapper templateMapper;
@PostConstruct
@Override
public void initSmsClientAndCacheSmsTemplate() {
// 查询有效渠道信息
List<SysSmsChannelDO> channelDOList = channelMapper.selectEnabledList();
List<SmsChannelProperty> propertyList = SmsChannelConvert.INSTANCE.convertProperties(channelDOList);
// 遍历渠道生成client获取模板并缓存
propertyList.forEach(channelProperty -> {
List<SysSmsTemplateDO> templateDOList = templateMapper.selectListByChannelId(channelProperty.getId());
if (ObjectUtil.isNotEmpty(templateDOList)) {
Long clientId = clientFactory.createClient(channelProperty);
templateDOList.forEach(template -> templateCode2ChannelIdMap.put(template.getCode(), clientId));
List<SmsTemplateProperty> templatePropertyList = SmsTemplateConvert.INSTANCE.convertProperty(templateDOList);
clientFactory.addOrUpdateTemplateCache(templatePropertyList);
}
});
}
@Override
public PageResult<SysSmsChannelDO> pageSmsChannels(SmsChannelPageReqVO reqVO) {
return SmsChannelConvert.INSTANCE.convertPage(channelMapper.selectChannelPage(reqVO));
@ -106,9 +105,7 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService {
return null;
}
List<SmsChannelAllVO> channelAllVOList = SmsChannelConvert.INSTANCE.convert(channelDOList);
channelAllVOList.forEach(smsChannelDO -> {
List<SysSmsTemplateDO> templateDOList = templateMapper.selectListByChannelId(smsChannelDO.getId());
if (ObjectUtil.isNull(templateDOList)) {
templateDOList = new ArrayList<>();