Commit ee2c731d25f8d0c74a0c1d269c2ae49aa27055ce

Authored by yjb
1 parent 3351ebbb

短信模板测试短信,检测模板状态与渠道状态,同时为开启时发送短信,否则只记录短信

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImpl.java
... ... @@ -11,6 +11,7 @@ import cn.iocoder.yudao.framework.sms.core.client.SmsClientFactory;
11 11 import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult;
12 12 import cn.iocoder.yudao.framework.sms.core.client.dto.SmsReceiveRespDTO;
13 13 import cn.iocoder.yudao.framework.sms.core.client.dto.SmsSendRespDTO;
  14 +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO;
14 15 import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO;
15 16 import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
16 17 import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
... ... @@ -40,7 +41,8 @@ public class SmsSendServiceImpl implements SmsSendService {
40 41 private AdminUserService adminUserService;
41 42 @Resource
42 43 private MemberService memberService;
43   -
  44 + @Resource
  45 + private SmsChannelService smsChannelService;
44 46 @Resource
45 47 private SmsTemplateService smsTemplateService;
46 48 @Resource
... ... @@ -80,13 +82,18 @@ public class SmsSendServiceImpl implements SmsSendService {
80 82 String templateCode, Map<String, Object> templateParams) {
81 83 // 校验短信模板是否合法
82 84 SmsTemplateDO template = this.checkSmsTemplateValid(templateCode);
  85 + // 校验短信渠道是否合法
  86 + SmsChannelDO smsChannel = this.checkSmsChannelValid(template.getChannelId());
  87 +
83 88 // 校验手机号码是否存在
84 89 mobile = this.checkMobile(mobile);
85 90 // 构建有序的模板参数。为什么放在这个位置,是提前保证模板参数的正确性,而不是到了插入发送日志
86 91 List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams);
87 92  
88 93 // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志
89   - Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus());
  94 + Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus())
  95 + && CommonStatusEnum.ENABLE.getStatus().equals(smsChannel.getStatus());
  96 + ;
90 97 String content = smsTemplateService.formatSmsTemplateContent(template.getContent(), templateParams);
91 98 Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams);
92 99  
... ... @@ -98,6 +105,16 @@ public class SmsSendServiceImpl implements SmsSendService {
98 105 return sendLogId;
99 106 }
100 107  
  108 + @VisibleForTesting
  109 + public SmsChannelDO checkSmsChannelValid(Long channelId) {
  110 + // 获得短信模板。考虑到效率,从缓存中获取
  111 + SmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId);
  112 + // 短信模板不存在
  113 + if (channelDO == null) {
  114 + throw exception(SMS_SEND_TEMPLATE_NOT_EXISTS);
  115 + }
  116 + return channelDO;
  117 + }
101 118  
102 119 @VisibleForTesting
103 120 public SmsTemplateDO checkSmsTemplateValid(String templateCode) {
... ... @@ -112,10 +129,10 @@ public class SmsSendServiceImpl implements SmsSendService {
112 129  
113 130 /**
114 131 * 将参数模板,处理成有序的 KeyValue 数组
115   - *
  132 + * <p>
116 133 * 原因是,部分短信平台并不是使用 key 作为参数,而是数组下标,例如说腾讯云 https://cloud.tencent.com/document/product/382/39023
117 134 *
118   - * @param template 短信模板
  135 + * @param template 短信模板
119 136 * @param templateParams 原始参数
120 137 * @return 处理后的参数
121 138 */
... ...