From 0fd757fbbd74b3e068012c25b1b9cdbf4ba14445 Mon Sep 17 00:00:00 2001 From: zengzefeng <986510453@qq.com> Date: Thu, 4 Feb 2021 09:42:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SmsProperty=E7=B1=BB=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E9=99=A4smsFactory=E5=AF=B9module=E5=B1=82vo=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/client/AbstractSmsClient.java | 12 ++-- .../framework/sms/client/AliyunSmsClient.java | 4 +- .../framework/sms/core/SmsClientFactory.java | 6 +- .../sms/core/property/SmsChannelProperty.java | 58 +++++++++++++++++++ .../core/property/SmsTemplateProperty.java | 38 ++++++++++++ ...elPropertyVO.java => SmsChannelAllVO.java} | 2 +- .../system/convert/sms/SmsChannelConvert.java | 9 ++- .../system/service/sms/SmsChannelService.java | 4 +- .../sms/impl/SmsChannelServiceImpl.java | 18 +++--- .../service/sms/impl/SmsLogServiceImpl.java | 4 +- 10 files changed, 128 insertions(+), 27 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsChannelProperty.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsTemplateProperty.java rename src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/{SmsChannelPropertyVO.java => SmsChannelAllVO.java} (94%) 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 043dd2a50..0b4907fa2 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 @@ -1,6 +1,6 @@ package cn.iocoder.dashboard.framework.sms.client; -import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelPropertyVO; +import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty; /** * 抽象短息客户端 @@ -13,19 +13,19 @@ public abstract class AbstractSmsClient implements SmsClient { /** * 短信渠道参数 */ - protected final SmsChannelPropertyVO channelVO; + protected final SmsChannelProperty channelVO; /** * 构造阿里云短信发送处理 * - * @param channelVO 阿里云短信配置 + * @param property 阿里云短信配置 */ - public AbstractSmsClient(SmsChannelPropertyVO channelVO) { - this.channelVO = channelVO; + public AbstractSmsClient(SmsChannelProperty property) { + this.channelVO = property; } - public SmsChannelPropertyVO getProperty() { + public SmsChannelProperty getProperty() { return channelVO; } 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 39d359449..4a2ee6d70 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 @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.framework.sms.client; import cn.iocoder.dashboard.framework.sms.core.SmsBody; import cn.iocoder.dashboard.framework.sms.core.SmsResult; -import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelPropertyVO; +import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; @@ -39,7 +39,7 @@ public class AliyunSmsClient extends AbstractSmsClient { * * @param channelVO 阿里云短信配置 */ - public AliyunSmsClient(SmsChannelPropertyVO channelVO) { + public AliyunSmsClient(SmsChannelProperty channelVO) { super(channelVO); String accessKeyId = channelVO.getApiKey(); 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 6c1eaf07c..35f87bb98 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 @@ -7,7 +7,7 @@ import cn.iocoder.dashboard.common.exception.ServiceException; import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient; import cn.iocoder.dashboard.framework.sms.client.AliyunSmsClient; -import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelPropertyVO; +import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty; import org.springframework.stereotype.Component; import java.util.Map; @@ -32,7 +32,7 @@ public class SmsClientFactory { * @param propertyVO 参数对象 * @return 客户端id(默认channelId) */ - public Long createClient(SmsChannelPropertyVO propertyVO) { + public Long createClient(SmsChannelProperty propertyVO) { if (StrUtil.isBlank(propertyVO.getCode())) { throw ServiceExceptionUtil.exception(PARAM_VALUE_IS_NULL, "短信渠道编码"); } @@ -45,7 +45,7 @@ public class SmsClientFactory { return propertyVO.getId(); } - private AbstractSmsClient createClient(SmsChannelEnum channelEnum, SmsChannelPropertyVO channelVO) { + private AbstractSmsClient createClient(SmsChannelEnum channelEnum, SmsChannelProperty channelVO) { if (channelEnum == null) { throw new ServiceException(INVALID_CHANNEL_CODE); } diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsChannelProperty.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsChannelProperty.java new file mode 100644 index 000000000..402b0d7e7 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsChannelProperty.java @@ -0,0 +1,58 @@ +package cn.iocoder.dashboard.framework.sms.core.property; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serializable; +import java.util.List; + +/** + * 渠道(包含模板)信息VO类 + * + * @author zzf + * @date 2021/1/25 17:01 + */ +@Data +@EqualsAndHashCode +public class SmsChannelProperty implements Serializable { + + /** + * id + */ + private Long id; + + /** + * 编码(来自枚举类 阿里、华为、七牛等) + */ + private String code; + + /** + * 渠道账号id + */ + private String apiKey; + + /** + * 渠道账号秘钥 + */ + private String apiSecret; + + /** + * 实际渠道签名唯一标识 + */ + private String apiSignatureId; + + /** + * 签名值 + */ + private String signature; + + /** + * 该渠道名下的短信模板集合 + */ + private List templateList; + + public SmsTemplateProperty getTemplateByTemplateCode(String tempCode) { + return templateList.stream().filter(s -> s.getCode().equals(tempCode)).findFirst().get(); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsTemplateProperty.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsTemplateProperty.java new file mode 100644 index 000000000..8cfb20ba6 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/property/SmsTemplateProperty.java @@ -0,0 +1,38 @@ +package cn.iocoder.dashboard.framework.sms.core.property; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 渠道模板VO类 + * + * @author zzf + * @date 2021/1/25 17:03 + */ +@Data +@EqualsAndHashCode +public class SmsTemplateProperty { + + /** + * 业务编码(来自数据字典, 用户自定义业务场景 一个场景可以有多个模板) + */ + private String bizCode; + + /** + * 编码 + */ + private String code; + + /** + * 实际渠道模板唯一标识 + */ + private String apiTemplateId; + + /** + * 内容 + */ + private String content; + + + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/SmsChannelPropertyVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/SmsChannelAllVO.java similarity index 94% rename from src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/SmsChannelPropertyVO.java rename to src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/SmsChannelAllVO.java index ab4d73594..042cddaa2 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/SmsChannelPropertyVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/SmsChannelAllVO.java @@ -14,7 +14,7 @@ import java.util.List; */ @Data @EqualsAndHashCode -public class SmsChannelPropertyVO implements Serializable { +public class SmsChannelAllVO implements Serializable { /** * id diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsChannelConvert.java b/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsChannelConvert.java index 985de7576..b52167ef7 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsChannelConvert.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/convert/sms/SmsChannelConvert.java @@ -2,11 +2,12 @@ package cn.iocoder.dashboard.modules.system.convert.sms; import cn.iocoder.dashboard.common.enums.SmsChannelEnum; import cn.iocoder.dashboard.common.pojo.PageResult; -import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelPropertyVO; +import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty; +import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelAllVO; import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelCreateReqVO; import cn.iocoder.dashboard.modules.system.controller.sms.vo.resp.SmsChannelEnumRespVO; -import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SmsChannelDO; import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserUpdateReqVO; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SmsChannelDO; import com.baomidou.mybatisplus.core.metadata.IPage; import org.mapstruct.Mapper; import org.mapstruct.Mapping; @@ -28,7 +29,9 @@ public interface SmsChannelConvert { List convertEnum(List bean); - List convert(List bean); + List convert(List bean); + + List convertProperty(List list); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SmsChannelService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SmsChannelService.java index 5fd39c2cf..f30ea2b88 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SmsChannelService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SmsChannelService.java @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.modules.system.service.sms; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient; -import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelPropertyVO; +import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelAllVO; 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; @@ -59,5 +59,5 @@ public interface SmsChannelService { * * @return 渠道(包含名下模块)信息集合 */ - List listChannelAllEnabledInfo(); + List listChannelAllEnabledInfo(); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsChannelServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsChannelServiceImpl.java index 044a4e561..db36b86f4 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsChannelServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsChannelServiceImpl.java @@ -5,7 +5,8 @@ import cn.iocoder.dashboard.common.enums.SmsChannelEnum; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient; import cn.iocoder.dashboard.framework.sms.core.SmsClientFactory; -import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelPropertyVO; +import cn.iocoder.dashboard.framework.sms.core.property.SmsChannelProperty; +import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelAllVO; 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; @@ -47,13 +48,14 @@ public class SmsChannelServiceImpl implements SmsChannelService { @PostConstruct @Override public void initSmsClient() { - List smsChannelPropertyVOList = listChannelAllEnabledInfo(); - if (ObjectUtil.isEmpty(smsChannelPropertyVOList)) { + List smsChannelAllVOList = listChannelAllEnabledInfo(); + if (ObjectUtil.isEmpty(smsChannelAllVOList)) { return; } - smsChannelPropertyVOList.forEach(smsChannelPropertyVO -> { - Long clientId = smsClientFactory.createClient(smsChannelPropertyVO); - smsChannelPropertyVO.getTemplateList().forEach(smsTemplateVO -> { + List channelPropertyList = SmsChannelConvert.INSTANCE.convertProperty(smsChannelAllVOList); + channelPropertyList.forEach(smsChannelProperty -> { + Long clientId = smsClientFactory.createClient(smsChannelProperty); + smsChannelProperty.getTemplateList().forEach(smsTemplateVO -> { templateCode2ChannelIdMap.put(smsTemplateVO.getCode(), clientId); }); }); @@ -89,12 +91,12 @@ public class SmsChannelServiceImpl implements SmsChannelService { } @Override - public List listChannelAllEnabledInfo() { + public List listChannelAllEnabledInfo() { List channelDOList = mapper.selectEnabledList(); if (ObjectUtil.isNull(channelDOList)) { return null; } - List channelAllVOList = SmsChannelConvert.INSTANCE.convert(channelDOList); + List channelAllVOList = SmsChannelConvert.INSTANCE.convert(channelDOList); channelAllVOList.forEach(smsChannelDO -> { diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsLogServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsLogServiceImpl.java index 651917259..5853463e8 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsLogServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SmsLogServiceImpl.java @@ -3,7 +3,7 @@ package cn.iocoder.dashboard.modules.system.service.sms.impl; import cn.iocoder.dashboard.framework.sms.client.AbstractSmsClient; import cn.iocoder.dashboard.framework.sms.core.SmsBody; import cn.iocoder.dashboard.framework.sms.core.SmsResult; -import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsChannelPropertyVO; +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.dal.mysql.dao.sms.SmsLogMapper; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SmsLogDO; @@ -37,7 +37,7 @@ public class SmsLogServiceImpl implements SmsLogService { smsLogMapper.updateById(smsLog); return smsBody.getSmsLogId(); } else { - SmsChannelPropertyVO property = client.getProperty(); + SmsChannelAllVO property = client.getProperty(); SmsTemplateVO smsTemplate = property.getTemplateByTemplateCode(smsBody.getTemplateCode()); smsLog.setChannelCode(property.getCode())