Kaynağa Gözat

add 新增 sms4j 短信融合 支持数十种短信服务商共用
remove 移除 原框架自带短信功能

疯狂的狮子Li 3 yıl önce
ebeveyn
işleme
5e57292840
17 değiştirilmiş dosya ile 121 ekleme ve 422 silme
  1. 24 8
      config/nacos/ruoyi-resource.yml
  2. 5 11
      pom.xml
  3. 2 2
      ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteSmsService.java
  4. 9 14
      ruoyi-common/ruoyi-common-sms/pom.xml
  5. 15 0
      ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/SmsAutoConfiguration.java
  6. 19 0
      ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/properties/SmsProperties.java
  7. 0 48
      ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java
  8. 0 46
      ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/properties/SmsProperties.java
  9. 0 65
      ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/AliyunSmsTemplate.java
  10. 0 26
      ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/SmsTemplate.java
  11. 0 81
      ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/TencentSmsTemplate.java
  12. 0 31
      ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/entity/SmsResult.java
  13. 0 19
      ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/exception/SmsException.java
  14. 1 1
      ruoyi-common/ruoyi-common-sms/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
  15. 16 33
      ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java
  16. 13 19
      ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysSmsController.java
  17. 17 18
      ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteSmsServiceImpl.java

+ 24 - 8
config/nacos/ruoyi-resource.yml

@@ -47,13 +47,29 @@ mail:
   # Socket连接超时值,单位毫秒,缺省值不超时
   connectionTimeout: 0
 
+# 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

+ 5 - 11
pom.xml

@@ -58,8 +58,7 @@
         <okhttp.version>4.10.0</okhttp.version>
 
         <!-- SMS 配置 -->
-        <aliyun.sms.version>2.0.23</aliyun.sms.version>
-        <tencent.sms.version>3.1.687</tencent.sms.version>
+        <sms4j.version>2.2.0</sms4j.version>
 
         <!-- 插件版本 -->
         <maven-compiler-plugin.verison>3.11.0</maven-compiler-plugin.verison>
@@ -323,16 +322,11 @@
                 <version>${aws-java-sdk-s3.version}</version>
             </dependency>
 
+            <!--短信sms4j-->
             <dependency>
-                <groupId>com.aliyun</groupId>
-                <artifactId>dysmsapi20170525</artifactId>
-                <version>${aliyun.sms.version}</version>
-            </dependency>
-
-            <dependency>
-                <groupId>com.tencentcloudapi</groupId>
-                <artifactId>tencentcloud-sdk-java-sms</artifactId>
-                <version>${tencent.sms.version}</version>
+                <groupId>org.dromara.sms4j</groupId>
+                <artifactId>sms4j-spring-boot-starter</artifactId>
+                <version>${sms4j.version}</version>
             </dependency>
 
             <!-- 离线IP地址定位库 ip2region -->

+ 2 - 2
ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteSmsService.java

@@ -3,7 +3,7 @@ package org.dromara.resource.api;
 import org.dromara.common.core.exception.ServiceException;
 import org.dromara.resource.api.domain.RemoteSms;
 
-import java.util.Map;
+import java.util.LinkedHashMap;
 
 /**
  * 短信服务
@@ -19,6 +19,6 @@ public interface RemoteSmsService {
      * @param templateId 模板id
      * @param param      模板对应参数
      */
-    RemoteSms send(String phones, String templateId, Map<String, String> param) throws ServiceException;
+    RemoteSms send(String phones, String templateId, LinkedHashMap<String, String> param) throws ServiceException;
 
 }

+ 9 - 14
ruoyi-common/ruoyi-common-sms/pom.xml

@@ -18,20 +18,15 @@
 	<dependencies>
 
         <dependency>
-            <groupId>org.dromara</groupId>
-            <artifactId>ruoyi-common-json</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.aliyun</groupId>
-            <artifactId>dysmsapi20170525</artifactId>
-            <optional>true</optional>
-        </dependency>
-
-        <dependency>
-            <groupId>com.tencentcloudapi</groupId>
-            <artifactId>tencentcloud-sdk-java-sms</artifactId>
-            <optional>true</optional>
+            <groupId>org.dromara.sms4j</groupId>
+            <artifactId>sms4j-spring-boot-starter</artifactId>
+            <exclusions>
+                <!-- 排除京东短信内存在的fastjson等待作者后续修复 -->
+                <exclusion>
+                    <groupId>com.alibaba</groupId>
+                    <artifactId>fastjson</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
 
     </dependencies>

+ 15 - 0
ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/SmsAutoConfiguration.java

@@ -0,0 +1,15 @@
+package com.ruoyi.common.sms.config;
+
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+
+/**
+ * 短信配置类
+ *
+ * @author Lion Li
+ * @version 4.2.0
+ */
+@AutoConfiguration
+//@EnableConfigurationProperties(SmsProperties.class)
+public class SmsAutoConfiguration {
+
+}

+ 19 - 0
ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/properties/SmsProperties.java

@@ -0,0 +1,19 @@
+package com.ruoyi.common.sms.config.properties;//package com.ruoyi.common.sms.config.properties;
+//
+//import lombok.Data;
+//import org.springframework.boot.context.properties.ConfigurationProperties;
+//import org.springframework.stereotype.Component;
+//
+///**
+// * SMS短信 配置属性
+// *
+// * @author Lion Li
+// * @version 4.2.0
+// */
+//@Data
+//@ConfigurationProperties(prefix = "sms")
+//public class SmsProperties {
+//
+//    private Boolean enabled;
+//
+//}

+ 0 - 48
ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java

@@ -1,48 +0,0 @@
-package org.dromara.common.sms.config;
-
-import org.dromara.common.sms.config.properties.SmsProperties;
-import org.dromara.common.sms.core.AliyunSmsTemplate;
-import org.dromara.common.sms.core.SmsTemplate;
-import org.dromara.common.sms.core.TencentSmsTemplate;
-import org.springframework.boot.autoconfigure.AutoConfiguration;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * 短信配置类
- *
- * @author Lion Li
- * @version 4.2.0
- */
-@AutoConfiguration
-@EnableConfigurationProperties(SmsProperties.class)
-public class SmsAutoConfiguration {
-
-    @Configuration
-    @ConditionalOnProperty(value = "sms.enabled", havingValue = "true")
-    @ConditionalOnClass(com.aliyun.dysmsapi20170525.Client.class)
-    static class AliyunSmsConfiguration {
-
-        @Bean
-        public SmsTemplate aliyunSmsTemplate(SmsProperties smsProperties) {
-            return new AliyunSmsTemplate(smsProperties);
-        }
-
-    }
-
-    @Configuration
-    @ConditionalOnProperty(value = "sms.enabled", havingValue = "true")
-    @ConditionalOnClass(com.tencentcloudapi.sms.v20190711.SmsClient.class)
-    static class TencentSmsConfiguration {
-
-        @Bean
-        public SmsTemplate tencentSmsTemplate(SmsProperties smsProperties) {
-            return new TencentSmsTemplate(smsProperties);
-        }
-
-    }
-
-}

+ 0 - 46
ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/properties/SmsProperties.java

@@ -1,46 +0,0 @@
-package org.dromara.common.sms.config.properties;
-
-import lombok.Data;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.stereotype.Component;
-
-/**
- * SMS短信 配置属性
- *
- * @author Lion Li
- * @version 4.2.0
- */
-@Data
-@ConfigurationProperties(prefix = "sms")
-public class SmsProperties {
-
-    private Boolean enabled;
-
-    /**
-     * 配置节点
-     * 阿里云 dysmsapi.aliyuncs.com
-     * 腾讯云 sms.tencentcloudapi.com
-     */
-    private String endpoint;
-
-    /**
-     * key
-     */
-    private String accessKeyId;
-
-    /**
-     * 密匙
-     */
-    private String accessKeySecret;
-
-    /*
-     * 短信签名
-     */
-    private String signName;
-
-    /**
-     * 短信应用ID (腾讯专属)
-     */
-    private String sdkAppId;
-
-}

+ 0 - 65
ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/AliyunSmsTemplate.java

@@ -1,65 +0,0 @@
-package org.dromara.common.sms.core;
-
-import com.aliyun.dysmsapi20170525.Client;
-import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
-import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
-import com.aliyun.teaopenapi.models.Config;
-import org.dromara.common.core.utils.StringUtils;
-import org.dromara.common.json.utils.JsonUtils;
-import org.dromara.common.sms.config.properties.SmsProperties;
-import org.dromara.common.sms.entity.SmsResult;
-import org.dromara.common.sms.exception.SmsException;
-import lombok.SneakyThrows;
-
-import java.util.Map;
-
-/**
- * Aliyun 短信模板
- *
- * @author Lion Li
- * @version 4.2.0
- */
-public class AliyunSmsTemplate implements SmsTemplate {
-
-    private SmsProperties properties;
-
-    private Client client;
-
-    @SneakyThrows(Exception.class)
-    public AliyunSmsTemplate(SmsProperties smsProperties) {
-        this.properties = smsProperties;
-        Config config = new Config()
-            // 您的AccessKey ID
-            .setAccessKeyId(smsProperties.getAccessKeyId())
-            // 您的AccessKey Secret
-            .setAccessKeySecret(smsProperties.getAccessKeySecret())
-            // 访问的域名
-            .setEndpoint(smsProperties.getEndpoint());
-        this.client = new Client(config);
-    }
-
-    public SmsResult send(String phones, String templateId, Map<String, String> param) {
-        if (StringUtils.isBlank(phones)) {
-            throw new SmsException("手机号不能为空");
-        }
-        if (StringUtils.isBlank(templateId)) {
-            throw new SmsException("模板ID不能为空");
-        }
-        SendSmsRequest req = new SendSmsRequest()
-            .setPhoneNumbers(phones)
-            .setSignName(properties.getSignName())
-            .setTemplateCode(templateId)
-            .setTemplateParam(JsonUtils.toJsonString(param));
-        try {
-            SendSmsResponse resp = client.sendSms(req);
-            return SmsResult.builder()
-                .isSuccess("OK".equals(resp.getBody().getCode()))
-                .message(resp.getBody().getMessage())
-                .response(JsonUtils.toJsonString(resp))
-                .build();
-        } catch (Exception e) {
-            throw new SmsException(e.getMessage());
-        }
-    }
-
-}

+ 0 - 26
ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/SmsTemplate.java

@@ -1,26 +0,0 @@
-package org.dromara.common.sms.core;
-
-import org.dromara.common.sms.entity.SmsResult;
-
-import java.util.Map;
-
-/**
- * 短信模板
- *
- * @author Lion Li
- * @version 4.2.0
- */
-public interface SmsTemplate {
-
-    /**
-     * 发送短信
-     *
-     * @param phones     电话号(多个逗号分割)
-     * @param templateId 模板id
-     * @param param      模板对应参数
-     *                   阿里 需使用 模板变量名称对应内容 例如: code=1234
-     *                   腾讯 需使用 模板变量顺序对应内容 例如: 1=1234, 1为模板内第一个参数
-     */
-    SmsResult send(String phones, String templateId, Map<String, String> param);
-
-}

+ 0 - 81
ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/TencentSmsTemplate.java

@@ -1,81 +0,0 @@
-package org.dromara.common.sms.core;
-
-import cn.hutool.core.collection.CollUtil;
-import cn.hutool.core.util.ArrayUtil;
-import org.dromara.common.core.utils.StringUtils;
-import org.dromara.common.json.utils.JsonUtils;
-import org.dromara.common.sms.config.properties.SmsProperties;
-import org.dromara.common.sms.entity.SmsResult;
-import org.dromara.common.sms.exception.SmsException;
-import com.tencentcloudapi.common.Credential;
-import com.tencentcloudapi.common.profile.ClientProfile;
-import com.tencentcloudapi.common.profile.HttpProfile;
-import com.tencentcloudapi.sms.v20190711.SmsClient;
-import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
-import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
-import com.tencentcloudapi.sms.v20190711.models.SendStatus;
-import lombok.SneakyThrows;
-
-import java.util.Arrays;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-/**
- * Tencent 短信模板
- *
- * @author Lion Li
- * @version 4.2.0
- */
-public class TencentSmsTemplate implements SmsTemplate {
-
-    private SmsProperties properties;
-
-    private SmsClient client;
-
-    @SneakyThrows(Exception.class)
-    public TencentSmsTemplate(SmsProperties smsProperties) {
-        this.properties = smsProperties;
-        Credential credential = new Credential(smsProperties.getAccessKeyId(), smsProperties.getAccessKeySecret());
-        HttpProfile httpProfile = new HttpProfile();
-        httpProfile.setEndpoint(smsProperties.getEndpoint());
-        ClientProfile clientProfile = new ClientProfile();
-        clientProfile.setHttpProfile(httpProfile);
-        this.client = new SmsClient(credential, "", clientProfile);
-    }
-
-    public SmsResult send(String phones, String templateId, Map<String, String> param) {
-        if (StringUtils.isBlank(phones)) {
-            throw new SmsException("手机号不能为空");
-        }
-        if (StringUtils.isBlank(templateId)) {
-            throw new SmsException("模板ID不能为空");
-        }
-        SendSmsRequest req = new SendSmsRequest();
-        Set<String> set = Arrays.stream(phones.split(StringUtils.SEPARATOR)).map(p -> "+86" + p).collect(Collectors.toSet());
-        req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class));
-        if (CollUtil.isNotEmpty(param)) {
-            req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class));
-        }
-        req.setTemplateID(templateId);
-        req.setSign(properties.getSignName());
-        req.setSmsSdkAppid(properties.getSdkAppId());
-        try {
-            SendSmsResponse resp = client.SendSms(req);
-            SmsResult.SmsResultBuilder builder = SmsResult.builder()
-                .isSuccess(true)
-                .message("send success")
-                .response(JsonUtils.toJsonString(resp));
-            for (SendStatus sendStatus : resp.getSendStatusSet()) {
-                if (!"Ok".equals(sendStatus.getCode())) {
-                    builder.isSuccess(false).message(sendStatus.getMessage());
-                    break;
-                }
-            }
-            return builder.build();
-        } catch (Exception e) {
-            throw new SmsException(e.getMessage());
-        }
-    }
-
-}

+ 0 - 31
ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/entity/SmsResult.java

@@ -1,31 +0,0 @@
-package org.dromara.common.sms.entity;
-
-import lombok.Builder;
-import lombok.Data;
-
-/**
- * 上传返回体
- *
- * @author Lion Li
- */
-@Data
-@Builder
-public class SmsResult {
-
-    /**
-     * 是否成功
-     */
-    private Boolean isSuccess;
-
-    /**
-     * 响应消息
-     */
-    private String message;
-
-    /**
-     * 实际响应体
-     * <p>
-     * 可自行转换为 SDK 对应的 SendSmsResponse
-     */
-    private String response;
-}

+ 0 - 19
ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/exception/SmsException.java

@@ -1,19 +0,0 @@
-package org.dromara.common.sms.exception;
-
-import java.io.Serial;
-
-/**
- * Sms异常类
- *
- * @author Lion Li
- */
-public class SmsException extends RuntimeException {
-
-    @Serial
-    private static final long serialVersionUID = 1L;
-
-    public SmsException(String msg) {
-        super(msg);
-    }
-
-}

+ 1 - 1
ruoyi-common/ruoyi-common-sms/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -1 +1 @@
-org.dromara.common.sms.config.SmsAutoConfiguration
+com.ruoyi.common.sms.config.SmsAutoConfiguration

+ 16 - 33
ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java

@@ -1,17 +1,17 @@
 package org.dromara.demo.controller;
 
-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.common.sms.core.SmsTemplate;
 import lombok.RequiredArgsConstructor;
+import org.dromara.common.core.domain.R;
+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;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.util.LinkedHashMap;
 
 /**
  * 短信演示案例
@@ -25,52 +25,35 @@ import java.util.Map;
 @RestController
 @RequestMapping("/sms")
 public class SmsController {
-
-    private final SmsProperties smsProperties;
-//    private final SmsTemplate smsTemplate; // 可以使用spring注入
-//    private final AliyunSmsTemplate smsTemplate; // 也可以注入某个厂家的模板工具
-
     /**
      * 发送短信Aliyun
      *
-     * @param phones 电话号
+     * @param phones     电话号
      * @param templateId 模板ID
      */
     @GetMapping("/sendAliyun")
     public R<Object> sendAliyun(String phones, String templateId) {
-        if (!smsProperties.getEnabled()) {
-            return R.fail("当前系统没有开启短信功能!");
-        }
-        if (!SpringUtils.containsBean("aliyunSmsTemplate")) {
-            return R.fail("阿里云依赖未引入!");
-        }
-        SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
-        Map<String, String> map = new HashMap<>(1);
+        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
         map.put("code", "1234");
-        Object send = smsTemplate.send(phones, templateId, map);
-        return R.ok(send);
+        SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA);
+        SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map);
+        return R.ok(smsResponse);
     }
 
     /**
      * 发送短信Tencent
      *
-     * @param phones 电话号
+     * @param phones     电话号
      * @param templateId 模板ID
      */
     @GetMapping("/sendTencent")
     public R<Object> sendTencent(String phones, String templateId) {
-        if (!smsProperties.getEnabled()) {
-            return R.fail("当前系统没有开启短信功能!");
-        }
-        if (!SpringUtils.containsBean("tencentSmsTemplate")) {
-            return R.fail("腾讯云依赖未引入!");
-        }
-        SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
-        Map<String, String> map = new HashMap<>(1);
+        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
 //        map.put("2", "测试测试");
         map.put("1", "1234");
-        Object send = smsTemplate.send(phones, templateId, map);
-        return R.ok(send);
+        SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.TENCENT);
+        SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map);
+        return R.ok(smsResponse);
     }
 
 }

+ 13 - 19
ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysSmsController.java

@@ -2,26 +2,25 @@ package org.dromara.resource.controller;
 
 
 import cn.hutool.core.util.RandomUtil;
+import jakarta.validation.constraints.NotBlank;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.dromara.common.core.constant.Constants;
 import org.dromara.common.core.constant.GlobalConstants;
 import org.dromara.common.core.domain.R;
-import org.dromara.common.core.utils.SpringUtils;
-import org.dromara.common.web.core.BaseController;
 import org.dromara.common.redis.utils.RedisUtils;
-import org.dromara.common.sms.config.properties.SmsProperties;
-import org.dromara.common.sms.core.SmsTemplate;
-import org.dromara.common.sms.entity.SmsResult;
+import org.dromara.common.web.core.BaseController;
+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;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import jakarta.validation.constraints.NotBlank;
 import java.time.Duration;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.LinkedHashMap;
 
 /**
  * 短信功能
@@ -35,8 +34,6 @@ import java.util.Map;
 @RequestMapping("/sms")
 public class SysSmsController extends BaseController {
 
-    private final SmsProperties smsProperties;
-
     /**
      * 短信验证码
      *
@@ -44,21 +41,18 @@ public class SysSmsController extends BaseController {
      */
     @GetMapping("/code")
     public R<Void> smsCaptcha(@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 = "";
-        Map<String, String> map = new HashMap<>(1);
+        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
         map.put("code", code);
-        SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
-        SmsResult result = smsTemplate.send(phonenumber, templateId, map);
-        if (!result.getIsSuccess()) {
-            log.error("验证码短信发送异常 => {}", result);
-            return R.fail(result.getMessage());
+        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());
         }
         return R.ok();
     }

+ 17 - 18
ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteSmsServiceImpl.java

@@ -1,19 +1,19 @@
 package org.dromara.resource.dubbo;
 
-import cn.hutool.core.bean.BeanUtil;
-import org.dromara.common.core.exception.ServiceException;
-import org.dromara.common.core.utils.SpringUtils;
-import org.dromara.common.sms.config.properties.SmsProperties;
-import org.dromara.common.sms.core.SmsTemplate;
-import org.dromara.common.sms.entity.SmsResult;
-import org.dromara.resource.api.RemoteSmsService;
-import org.dromara.resource.api.domain.RemoteSms;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.dubbo.config.annotation.DubboService;
+import org.dromara.common.core.exception.ServiceException;
+import org.dromara.common.json.utils.JsonUtils;
+import org.dromara.resource.api.RemoteSmsService;
+import org.dromara.resource.api.domain.RemoteSms;
+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;
 import org.springframework.stereotype.Service;
 
-import java.util.Map;
+import java.util.LinkedHashMap;
 
 /**
  * 短信服务
@@ -26,8 +26,6 @@ import java.util.Map;
 @DubboService
 public class RemoteSmsServiceImpl implements RemoteSmsService {
 
-    private final SmsProperties smsProperties;
-
     /**
      * 发送短信
      *
@@ -35,13 +33,14 @@ public class RemoteSmsServiceImpl implements RemoteSmsService {
      * @param templateId 模板id
      * @param param      模板对应参数
      */
-    public RemoteSms send(String phones, String templateId, Map<String, String> param) throws ServiceException {
-        if (!smsProperties.getEnabled()) {
-            throw new ServiceException("当前系统没有开启短信功能!");
-        }
-        SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
-        SmsResult smsResult = smsTemplate.send(phones, templateId, param);
-        return BeanUtil.toBean(smsResult, RemoteSms.class);
+    public RemoteSms send(String phones, String templateId, LinkedHashMap<String, String> param) throws ServiceException {
+        SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA);
+        SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, param);
+        RemoteSms sysSms = new RemoteSms();
+        sysSms.setIsSuccess(smsResponse.isSuccess());
+        sysSms.setMessage(smsResponse.getMessage());
+        sysSms.setResponse(JsonUtils.toJsonString(smsResponse));
+        return sysSms;
     }
 
 }