优化部分代码逻辑
This commit is contained in:
parent
cd2a01819b
commit
c3372d4bd2
@ -22,9 +22,9 @@ public abstract class AbstractSmsClient implements SmsClient {
|
|||||||
protected final SmsChannelProperty channelVO;
|
protected final SmsChannelProperty channelVO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造阿里云短信发送处理 TODO FROM 芋艿 to zzf:貌似注释不对
|
* 短信客户端有参构造函数
|
||||||
*
|
*
|
||||||
* @param property 阿里云短信配置
|
* @param property 短信配置
|
||||||
*/
|
*/
|
||||||
public AbstractSmsClient(SmsChannelProperty property) {
|
public AbstractSmsClient(SmsChannelProperty property) {
|
||||||
this.channelVO = property;
|
this.channelVO = property;
|
||||||
@ -44,7 +44,7 @@ public abstract class AbstractSmsClient implements SmsClient {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// exception handle
|
// exception handle
|
||||||
log.debug(e.getMessage(), e);
|
log.debug(e.getMessage(), e);
|
||||||
return failResult("发送异常: " + e.getMessage());
|
return SmsResult.failResult("发送异常: " + e.getMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -55,8 +55,8 @@ public abstract class AbstractSmsClient implements SmsClient {
|
|||||||
* @param templateApiId 短信模板唯一标识
|
* @param templateApiId 短信模板唯一标识
|
||||||
* @param smsBody 消息内容
|
* @param smsBody 消息内容
|
||||||
* @param targets 发送对象列表
|
* @param targets 发送对象列表
|
||||||
* @throws Exception 调用发送失败,抛出异常
|
|
||||||
* @return 短信发送结果
|
* @return 短信发送结果
|
||||||
|
* @throws Exception 调用发送失败,抛出异常
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
|
|
||||||
@ -66,11 +66,4 @@ 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 resultBody = new SmsResult();
|
|
||||||
resultBody.setSuccess(false);
|
|
||||||
resultBody.setMessage(message);
|
|
||||||
return resultBody;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package cn.iocoder.dashboard.framework.sms.client;
|
package cn.iocoder.dashboard.framework.sms.client;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
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.SmsBody;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.SmsResult;
|
import cn.iocoder.dashboard.framework.sms.core.SmsResult;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.SmsResultDetail;
|
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 {
|
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, ",")); // TODO FROM 芋艿 to zzf:统一使用 Hutool 工具类嘿。
|
request.setPhoneNumbers(ArrayUtil.join(targets, ","));
|
||||||
request.setSignName(channelVO.getApiSignatureId());
|
request.setSignName(channelVO.getApiSignatureId());
|
||||||
request.setTemplateCode(templateApiId);
|
request.setTemplateCode(templateApiId);
|
||||||
request.setTemplateParam(smsBody.getParamsStr());
|
request.setTemplateParam(smsBody.getParamsStr());
|
||||||
|
@ -76,7 +76,7 @@ public class SmsClientFactory {
|
|||||||
* 添加或修改短信模板信息缓存
|
* 添加或修改短信模板信息缓存
|
||||||
*/
|
*/
|
||||||
public void addOrUpdateTemplateCache(Collection<SmsTemplateProperty> templateProperties) {
|
public void addOrUpdateTemplateCache(Collection<SmsTemplateProperty> templateProperties) {
|
||||||
templateProperties.forEach(s -> addOrUpdateTemplateCache(templateProperties));
|
templateProperties.forEach(this::addOrUpdateTemplateCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,4 +31,11 @@ public class SmsResult implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private List<SmsResultDetail> result;
|
private List<SmsResultDetail> result;
|
||||||
|
|
||||||
|
|
||||||
|
public static SmsResult failResult(String message) {
|
||||||
|
SmsResult resultBody = new SmsResult();
|
||||||
|
resultBody.setSuccess(false);
|
||||||
|
resultBody.setMessage(message);
|
||||||
|
return resultBody;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.iocoder.dashboard.modules.system.convert.sms;
|
package cn.iocoder.dashboard.modules.system.convert.sms;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
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.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.SysSmsChannelDO;
|
||||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsTemplateDO;
|
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.sms.SysSmsTemplateDO;
|
||||||
@ -23,4 +24,6 @@ public interface SmsTemplateConvert {
|
|||||||
|
|
||||||
SmsTemplateVO convert(SysSmsTemplateDO bean);
|
SmsTemplateVO convert(SysSmsTemplateDO bean);
|
||||||
|
|
||||||
|
List<SmsTemplateProperty> convertProperty(List<SysSmsTemplateDO> bean);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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][收到 发送短信 消息]"); // TODO FROM 芋艿 to zzf:log 如果要打,这里要内容打上。其他 Consumer 没打的原因,是没内容。。。
|
log.info("[onMessage][收到 发送短信 消息], content: " + message.toString());
|
||||||
SmsResult send = sysSmsService.send(message.getSmsBody(), message.getTargetPhones());
|
SmsResult send = sysSmsService.send(message.getSmsBody(), message.getTargetPhones());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,9 +19,9 @@ import java.util.List;
|
|||||||
public interface SysSmsChannelService {
|
public interface SysSmsChannelService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化短信渠道
|
* 初始化短信渠道并缓存短信模板信息
|
||||||
*/
|
*/
|
||||||
void initSmsClient();
|
void initSmsClientAndCacheSmsTemplate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页查询短信渠道信息
|
* 分页查询短信渠道信息
|
||||||
|
@ -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.SmsClientFactory;
|
||||||
import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum;
|
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.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.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.SmsChannelCreateReqVO;
|
||||||
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelPageReqVO;
|
import cn.iocoder.dashboard.modules.system.controller.sms.vo.req.SmsChannelPageReqVO;
|
||||||
import cn.iocoder.dashboard.modules.system.controller.sms.vo.resp.SmsChannelEnumRespVO;
|
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);
|
private final Map<String, Long> templateCode2ChannelIdMap = new ConcurrentHashMap<>(32);
|
||||||
|
|
||||||
@Autowired
|
@Resource
|
||||||
private SmsClientFactory clientFactory;
|
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
|
@Resource
|
||||||
private SysSmsChannelMapper channelMapper;
|
private SysSmsChannelMapper channelMapper;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysSmsTemplateMapper templateMapper;
|
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
|
@Override
|
||||||
public PageResult<SysSmsChannelDO> pageSmsChannels(SmsChannelPageReqVO reqVO) {
|
public PageResult<SysSmsChannelDO> pageSmsChannels(SmsChannelPageReqVO reqVO) {
|
||||||
return SmsChannelConvert.INSTANCE.convertPage(channelMapper.selectChannelPage(reqVO));
|
return SmsChannelConvert.INSTANCE.convertPage(channelMapper.selectChannelPage(reqVO));
|
||||||
@ -106,9 +105,7 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List<SmsChannelAllVO> channelAllVOList = SmsChannelConvert.INSTANCE.convert(channelDOList);
|
List<SmsChannelAllVO> channelAllVOList = SmsChannelConvert.INSTANCE.convert(channelDOList);
|
||||||
|
|
||||||
channelAllVOList.forEach(smsChannelDO -> {
|
channelAllVOList.forEach(smsChannelDO -> {
|
||||||
|
|
||||||
List<SysSmsTemplateDO> templateDOList = templateMapper.selectListByChannelId(smsChannelDO.getId());
|
List<SysSmsTemplateDO> templateDOList = templateMapper.selectListByChannelId(smsChannelDO.getId());
|
||||||
if (ObjectUtil.isNull(templateDOList)) {
|
if (ObjectUtil.isNull(templateDOList)) {
|
||||||
templateDOList = new ArrayList<>();
|
templateDOList = new ArrayList<>();
|
||||||
|
Loading…
Reference in New Issue
Block a user