diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java index 8feaca916..aa55c925d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java @@ -45,7 +45,6 @@ public class CaptchaController { private final CaptchaProperties captchaProperties; private final SmsProperties smsProperties; - private final SmsTemplate smsTemplate; private final ISysConfigService configService; /** @@ -66,6 +65,7 @@ public class CaptchaController { String templateId = ""; Map map = new HashMap<>(1); map.put("code", code); + SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class); SmsResult result = smsTemplate.send(phonenumber, templateId, map); if (!result.isSuccess()) { log.error("验证码短信发送异常 => {}", result); diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/SmsController.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/SmsController.java index 51472de2b..833144534 100644 --- a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/SmsController.java +++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/SmsController.java @@ -1,6 +1,8 @@ package com.ruoyi.demo.controller; import com.ruoyi.common.core.domain.R; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.sms.config.properties.SmsProperties; import com.ruoyi.sms.core.SmsTemplate; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -27,12 +29,17 @@ import java.util.Map; @RequestMapping("/demo/sms") public class SmsController { - private final SmsTemplate smsTemplate; + private final SmsProperties smsProperties; +// private final SmsTemplate smsTemplate; // 可以使用spring注入 @ApiOperation("发送短信Aliyun") @GetMapping("/sendAliyun") public R sendAliyun(@ApiParam("电话号") String phones, @ApiParam("模板ID") String templateId) { + if (smsProperties.getEnabled()) { + R.fail("当前系统没有开启短信功能!"); + } + SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class); Map map = new HashMap<>(1); map.put("code", "1234"); Object send = smsTemplate.send(phones, templateId, map); @@ -43,6 +50,10 @@ public class SmsController { @GetMapping("/sendTencent") public R sendTencent(@ApiParam("电话号") String phones, @ApiParam("模板ID") String templateId) { + if (smsProperties.getEnabled()) { + R.fail("当前系统没有开启短信功能!"); + } + SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class); Map map = new HashMap<>(1); // map.put("2", "测试测试"); map.put("1", "1234"); diff --git a/ruoyi-sms/src/main/java/com/ruoyi/sms/config/SmsConfig.java b/ruoyi-sms/src/main/java/com/ruoyi/sms/config/SmsConfig.java index abc4bb97c..753773e87 100644 --- a/ruoyi-sms/src/main/java/com/ruoyi/sms/config/SmsConfig.java +++ b/ruoyi-sms/src/main/java/com/ruoyi/sms/config/SmsConfig.java @@ -16,10 +16,10 @@ import org.springframework.context.annotation.Configuration; * @version 4.2.0 */ @Configuration -@ConditionalOnProperty(value = "sms.enabled", havingValue = "true") public class SmsConfig { @Configuration + @ConditionalOnProperty(value = "sms.enabled", havingValue = "true") @ConditionalOnClass(com.aliyun.dysmsapi20170525.Client.class) static class AliyunSmsConfig { @@ -31,6 +31,7 @@ public class SmsConfig { } @Configuration + @ConditionalOnProperty(value = "sms.enabled", havingValue = "true") @ConditionalOnClass(com.tencentcloudapi.sms.v20190711.SmsClient.class) static class TencentSmsConfig {