From 60edd6148b128a755e61dd2c163d5eea3311e189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 9 Jun 2023 10:25:05 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20!pr367=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90=20sms4j=20=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/CaptchaController.java | 9 +++-- .../src/main/resources/application-dev.yml | 21 +++++++++--- .../src/main/resources/application-prod.yml | 33 ++++++++++++++----- ruoyi-common/ruoyi-common-sms/pom.xml | 11 ++++--- ruoyi-modules/ruoyi-demo/pom.xml | 10 ------ .../demo/controller/SmsController.java | 23 +++---------- 6 files changed, 57 insertions(+), 50 deletions(-) diff --git a/ruoyi-admin/src/main/java/org/dromara/web/controller/CaptchaController.java b/ruoyi-admin/src/main/java/org/dromara/web/controller/CaptchaController.java index 3977e307..8abada68 100644 --- a/ruoyi-admin/src/main/java/org/dromara/web/controller/CaptchaController.java +++ b/ruoyi-admin/src/main/java/org/dromara/web/controller/CaptchaController.java @@ -17,6 +17,7 @@ import org.dromara.common.redis.utils.RedisUtils; import org.dromara.common.sms.config.properties.SmsProperties; import org.dromara.common.web.config.properties.CaptchaProperties; import org.dromara.common.web.enums.CaptchaType; +import org.dromara.sms4j.api.SmsBlend; import org.dromara.sms4j.api.entity.SmsResponse; import org.dromara.sms4j.core.factory.SmsFactory; import org.dromara.sms4j.provider.enumerate.SupplierType; @@ -57,17 +58,15 @@ public class CaptchaController { */ @GetMapping("/resource/sms/code") public R smsCode(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) { - if (!smsProperties.getEnabled()) { - return R.fail("当前系统没有开启短信功能!"); - } String key = GlobalConstants.CAPTCHA_CODE_KEY + phonenumber; String code = RandomUtil.randomNumbers(4); RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION)); // 验证码模板id 自行处理 (查数据库或写死均可) - String templateId = "SMS_460945446"; + String templateId = ""; LinkedHashMap map = new LinkedHashMap<>(1); map.put("code", code); - SmsResponse smsResponse = SmsFactory.createSmsBlend(SupplierType.ALIBABA).sendMessage(phonenumber, templateId, map); + SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA); + SmsResponse smsResponse = smsBlend.sendMessage(phonenumber, templateId, map); if (!"OK".equals(smsResponse.getCode())) { log.error("验证码短信发送异常 => {}", smsResponse); return R.fail(smsResponse.getMessage()); diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml index f3dda8fa..5adc8aed 100644 --- a/ruoyi-admin/src/main/resources/application-dev.yml +++ b/ruoyi-admin/src/main/resources/application-dev.yml @@ -158,16 +158,29 @@ mail: # Socket连接超时值,单位毫秒,缺省值不超时 connectionTimeout: 0 ---- # sms 短信 https://wind.kim/doc/start/springboot.html +--- # sms 短信 支持 阿里云 腾讯云 云片 等等各式各样的短信服务商 +# https://wind.kim/doc/start 文档地址 各个厂商可同时使用 sms: - enabled: true # 阿里云 dysmsapi.aliyuncs.com alibaba: + #请求地址 默认为 dysmsapi.aliyuncs.com 如无特殊改变可以不用设置 + requestUrl: dysmsapi.aliyuncs.com #阿里云的accessKey accessKeyId: xxxxxxx #阿里云的accessKeySecret accessKeySecret: xxxxxxx #短信签名 signature: 测试 - #请求地址 默认为dysmsapi.aliyuncs.com 如无特殊改变可以不用设置 - requestUrl: dysmsapi.aliyuncs.com + tencent: + #请求地址默认为 sms.tencentcloudapi.com 如无特殊改变可不用设置 + requestUrl: sms.tencentcloudapi.com + #腾讯云的accessKey + accessKeyId: xxxxxxx + #腾讯云的accessKeySecret + accessKeySecret: xxxxxxx + #短信签名 + signature: 测试 + #短信sdkAppId + sdkAppId: appid + #地域信息默认为 ap-guangzhou 如无特殊改变可不用设置 + territory: ap-guangzhou diff --git a/ruoyi-admin/src/main/resources/application-prod.yml b/ruoyi-admin/src/main/resources/application-prod.yml index e760823a..4b29d1ce 100644 --- a/ruoyi-admin/src/main/resources/application-prod.yml +++ b/ruoyi-admin/src/main/resources/application-prod.yml @@ -161,14 +161,29 @@ mail: # Socket连接超时值,单位毫秒,缺省值不超时 connectionTimeout: 0 ---- # sms 短信 +--- # sms 短信 支持 阿里云 腾讯云 云片 等等各式各样的短信服务商 +# https://wind.kim/doc/start 文档地址 各个厂商可同时使用 sms: - enabled: false # 阿里云 dysmsapi.aliyuncs.com - # 腾讯云 sms.tencentcloudapi.com - endpoint: "dysmsapi.aliyuncs.com" - accessKeyId: xxxxxxx - accessKeySecret: xxxxxx - signName: 测试 - # 腾讯专用 - sdkAppId: + alibaba: + #请求地址 默认为 dysmsapi.aliyuncs.com 如无特殊改变可以不用设置 + requestUrl: dysmsapi.aliyuncs.com + #阿里云的accessKey + accessKeyId: xxxxxxx + #阿里云的accessKeySecret + accessKeySecret: xxxxxxx + #短信签名 + signature: 测试 + tencent: + #请求地址默认为 sms.tencentcloudapi.com 如无特殊改变可不用设置 + requestUrl: sms.tencentcloudapi.com + #腾讯云的accessKey + accessKeyId: xxxxxxx + #腾讯云的accessKeySecret + accessKeySecret: xxxxxxx + #短信签名 + signature: 测试 + #短信sdkAppId + sdkAppId: appid + #地域信息默认为 ap-guangzhou 如无特殊改变可不用设置 + territory: ap-guangzhou diff --git a/ruoyi-common/ruoyi-common-sms/pom.xml b/ruoyi-common/ruoyi-common-sms/pom.xml index 45b42563..c50f222a 100644 --- a/ruoyi-common/ruoyi-common-sms/pom.xml +++ b/ruoyi-common/ruoyi-common-sms/pom.xml @@ -16,14 +16,17 @@ - - org.dromara - ruoyi-common-json - org.dromara.sms4j sms4j-spring-boot-starter + + + + com.alibaba + fastjson + + diff --git a/ruoyi-modules/ruoyi-demo/pom.xml b/ruoyi-modules/ruoyi-demo/pom.xml index 09bf0bfa..4fd43bc5 100644 --- a/ruoyi-modules/ruoyi-demo/pom.xml +++ b/ruoyi-modules/ruoyi-demo/pom.xml @@ -97,16 +97,6 @@ org.dromara ruoyi-common-websocket - - - - - - - - - - diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java index bd16bd68..048b5141 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java @@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor; import org.dromara.common.core.domain.R; import org.dromara.common.core.utils.SpringUtils; import org.dromara.common.sms.config.properties.SmsProperties; +import org.dromara.sms4j.api.SmsBlend; import org.dromara.sms4j.api.entity.SmsResponse; import org.dromara.sms4j.core.factory.SmsFactory; import org.dromara.sms4j.provider.enumerate.SupplierType; @@ -27,10 +28,6 @@ import java.util.LinkedHashMap; @RequestMapping("/demo/sms") public class SmsController { - private final SmsProperties smsProperties; -// private final SmsTemplate smsTemplate; // 可以使用spring注入 -// private final AliyunSmsTemplate smsTemplate; // 也可以注入某个厂家的模板工具 - /** * 发送短信Aliyun * @@ -39,15 +36,10 @@ public class SmsController { */ @GetMapping("/sendAliyun") public R sendAliyun(String phones, String templateId) { - if (!smsProperties.getEnabled()) { - return R.fail("当前系统没有开启短信功能!"); - } - if (!SpringUtils.containsBean("aliyunSmsTemplate")) { - return R.fail("阿里云依赖未引入!"); - } LinkedHashMap map = new LinkedHashMap<>(1); map.put("code", "1234"); - SmsResponse smsResponse = SmsFactory.createSmsBlend(SupplierType.ALIBABA).sendMessage(phones, templateId, map); + SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA); + SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map); return R.ok(smsResponse); } @@ -59,16 +51,11 @@ public class SmsController { */ @GetMapping("/sendTencent") public R sendTencent(String phones, String templateId) { - if (!smsProperties.getEnabled()) { - return R.fail("当前系统没有开启短信功能!"); - } - if (!SpringUtils.containsBean("tencentSmsTemplate")) { - return R.fail("腾讯云依赖未引入!"); - } LinkedHashMap map = new LinkedHashMap<>(1); // map.put("2", "测试测试"); map.put("1", "1234"); - SmsResponse smsResponse = SmsFactory.createSmsBlend(SupplierType.TENCENT).sendMessage(phones, templateId, map); + SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.TENCENT); + SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map); return R.ok(smsResponse); }