Commit ee2c731d25f8d0c74a0c1d269c2ae49aa27055ce
1 parent
3351ebbb
短信模板测试短信,检测模板状态与渠道状态,同时为开启时发送短信,否则只记录短信
Showing
1 changed file
with
21 additions
and
4 deletions
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,6 +11,7 @@ import cn.iocoder.yudao.framework.sms.core.client.SmsClientFactory; | ||
| 11 | import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult; | 11 | import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult; |
| 12 | import cn.iocoder.yudao.framework.sms.core.client.dto.SmsReceiveRespDTO; | 12 | import cn.iocoder.yudao.framework.sms.core.client.dto.SmsReceiveRespDTO; |
| 13 | import cn.iocoder.yudao.framework.sms.core.client.dto.SmsSendRespDTO; | 13 | import cn.iocoder.yudao.framework.sms.core.client.dto.SmsSendRespDTO; |
| 14 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | ||
| 14 | import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; | 15 | import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; |
| 15 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | 16 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 16 | import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage; | 17 | import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage; |
| @@ -40,7 +41,8 @@ public class SmsSendServiceImpl implements SmsSendService { | @@ -40,7 +41,8 @@ public class SmsSendServiceImpl implements SmsSendService { | ||
| 40 | private AdminUserService adminUserService; | 41 | private AdminUserService adminUserService; |
| 41 | @Resource | 42 | @Resource |
| 42 | private MemberService memberService; | 43 | private MemberService memberService; |
| 43 | - | 44 | + @Resource |
| 45 | + private SmsChannelService smsChannelService; | ||
| 44 | @Resource | 46 | @Resource |
| 45 | private SmsTemplateService smsTemplateService; | 47 | private SmsTemplateService smsTemplateService; |
| 46 | @Resource | 48 | @Resource |
| @@ -80,13 +82,18 @@ public class SmsSendServiceImpl implements SmsSendService { | @@ -80,13 +82,18 @@ public class SmsSendServiceImpl implements SmsSendService { | ||
| 80 | String templateCode, Map<String, Object> templateParams) { | 82 | String templateCode, Map<String, Object> templateParams) { |
| 81 | // 校验短信模板是否合法 | 83 | // 校验短信模板是否合法 |
| 82 | SmsTemplateDO template = this.checkSmsTemplateValid(templateCode); | 84 | SmsTemplateDO template = this.checkSmsTemplateValid(templateCode); |
| 85 | + // 校验短信渠道是否合法 | ||
| 86 | + SmsChannelDO smsChannel = this.checkSmsChannelValid(template.getChannelId()); | ||
| 87 | + | ||
| 83 | // 校验手机号码是否存在 | 88 | // 校验手机号码是否存在 |
| 84 | mobile = this.checkMobile(mobile); | 89 | mobile = this.checkMobile(mobile); |
| 85 | // 构建有序的模板参数。为什么放在这个位置,是提前保证模板参数的正确性,而不是到了插入发送日志 | 90 | // 构建有序的模板参数。为什么放在这个位置,是提前保证模板参数的正确性,而不是到了插入发送日志 |
| 86 | List<KeyValue<String, Object>> newTemplateParams = this.buildTemplateParams(template, templateParams); | 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 | String content = smsTemplateService.formatSmsTemplateContent(template.getContent(), templateParams); | 97 | String content = smsTemplateService.formatSmsTemplateContent(template.getContent(), templateParams); |
| 91 | Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams); | 98 | Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams); |
| 92 | 99 | ||
| @@ -98,6 +105,16 @@ public class SmsSendServiceImpl implements SmsSendService { | @@ -98,6 +105,16 @@ public class SmsSendServiceImpl implements SmsSendService { | ||
| 98 | return sendLogId; | 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 | @VisibleForTesting | 119 | @VisibleForTesting |
| 103 | public SmsTemplateDO checkSmsTemplateValid(String templateCode) { | 120 | public SmsTemplateDO checkSmsTemplateValid(String templateCode) { |
| @@ -112,10 +129,10 @@ public class SmsSendServiceImpl implements SmsSendService { | @@ -112,10 +129,10 @@ public class SmsSendServiceImpl implements SmsSendService { | ||
| 112 | 129 | ||
| 113 | /** | 130 | /** |
| 114 | * 将参数模板,处理成有序的 KeyValue 数组 | 131 | * 将参数模板,处理成有序的 KeyValue 数组 |
| 115 | - * | 132 | + * <p> |
| 116 | * 原因是,部分短信平台并不是使用 key 作为参数,而是数组下标,例如说腾讯云 https://cloud.tencent.com/document/product/382/39023 | 133 | * 原因是,部分短信平台并不是使用 key 作为参数,而是数组下标,例如说腾讯云 https://cloud.tencent.com/document/product/382/39023 |
| 117 | * | 134 | * |
| 118 | - * @param template 短信模板 | 135 | + * @param template 短信模板 |
| 119 | * @param templateParams 原始参数 | 136 | * @param templateParams 原始参数 |
| 120 | * @return 处理后的参数 | 137 | * @return 处理后的参数 |
| 121 | */ | 138 | */ |