From c3372d4bd2b569f370e51ac5637e1993fffcd97d Mon Sep 17 00:00:00 2001 From: zengzefeng <986510453@qq.com> Date: Fri, 26 Feb 2021 11:30:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/client/AbstractSmsClient.java | 15 ++---- .../framework/sms/client/AliyunSmsClient.java | 4 +- .../framework/sms/core/SmsClientFactory.java | 2 +- .../framework/sms/core/SmsResult.java | 7 +++ .../convert/sms/SmsTemplateConvert.java | 3 ++ .../mq/consumer/sms/SmsSendConsumer.java | 2 +- .../service/sms/SysSmsChannelService.java | 4 +- .../sms/impl/SysSmsChannelServiceImpl.java | 51 +++++++++---------- 8 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/client/AbstractSmsClient.java b/src/main/java/cn/iocoder/dashboard/framework/sms/client/AbstractSmsClient.java index b5e2dbdb9..d40d636e4 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/client/AbstractSmsClient.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/client/AbstractSmsClient.java @@ -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 targets) throws Exception; @@ -66,11 +66,4 @@ public abstract class AbstractSmsClient implements SmsClient { protected void afterSend(String templateApiId, SmsBody smsBody, Collection 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; - } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/client/AliyunSmsClient.java b/src/main/java/cn/iocoder/dashboard/framework/sms/client/AliyunSmsClient.java index a2cbbdfd5..f88d6f291 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/client/AliyunSmsClient.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/client/AliyunSmsClient.java @@ -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 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()); diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsClientFactory.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsClientFactory.java index a54952701..88c02cb87 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsClientFactory.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsClientFactory.java @@ -76,7 +76,7 @@ public class SmsClientFactory { * 添加或修改短信模板信息缓存 */ public void addOrUpdateTemplateCache(Collection templateProperties) { - templateProperties.forEach(s -> addOrUpdateTemplateCache(templateProperties)); + templateProperties.forEach(this::addOrUpdateTemplateCache); } diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsResult.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsResult.java index dbc976421..46306322d 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsResult.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/SmsResult.java @@ -31,4 +31,11 @@ public class SmsResult implements Serializable { */ private List result; + + public static SmsResult failResult(String message) { + SmsResult resultBody = new SmsResult(); + resultBody.setSuccess(false); + resultBody.setMessage(message); + return resultBody; + } } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsTemplateConvert.java b/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsTemplateConvert.java index aaac0abdf..febac15fc 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsTemplateConvert.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsTemplateConvert.java @@ -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 convertProperty(List bean); + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SmsSendConsumer.java b/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SmsSendConsumer.java index 61c81f54c..f5f9109e2 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SmsSendConsumer.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SmsSendConsumer.java @@ -24,7 +24,7 @@ public class SmsSendConsumer extends AbstractChannelMessageListener templateCode2ChannelIdMap = new ConcurrentHashMap<>(32); - @Autowired + @Resource private SmsClientFactory clientFactory; - // TODO FROM 芋艿 to zzf:方法要放在成员变量下面; - /** - * 初始化短信客户端 - */ - @PostConstruct - @Override - public void initSmsClient() { - // 查询有效渠道信息 - List channelDOList = channelMapper.selectEnabledList(); - List propertyList = SmsChannelConvert.INSTANCE.convertProperties(channelDOList); - - // 遍历渠道生成client并获取模板缓存 - propertyList.forEach(channelProperty -> { - Long clientId = clientFactory.createClient(channelProperty); - List 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 channelDOList = channelMapper.selectEnabledList(); + List propertyList = SmsChannelConvert.INSTANCE.convertProperties(channelDOList); + + // 遍历渠道生成client、获取模板并缓存 + propertyList.forEach(channelProperty -> { + List templateDOList = templateMapper.selectListByChannelId(channelProperty.getId()); + if (ObjectUtil.isNotEmpty(templateDOList)) { + Long clientId = clientFactory.createClient(channelProperty); + templateDOList.forEach(template -> templateCode2ChannelIdMap.put(template.getCode(), clientId)); + + List templatePropertyList = SmsTemplateConvert.INSTANCE.convertProperty(templateDOList); + clientFactory.addOrUpdateTemplateCache(templatePropertyList); + } + }); + } + @Override public PageResult pageSmsChannels(SmsChannelPageReqVO reqVO) { return SmsChannelConvert.INSTANCE.convertPage(channelMapper.selectChannelPage(reqVO)); @@ -106,9 +105,7 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService { return null; } List channelAllVOList = SmsChannelConvert.INSTANCE.convert(channelDOList); - channelAllVOList.forEach(smsChannelDO -> { - List templateDOList = templateMapper.selectListByChannelId(smsChannelDO.getId()); if (ObjectUtil.isNull(templateDOList)) { templateDOList = new ArrayList<>();