diff --git a/config/nacos/ruoyi-resource.yml b/config/nacos/ruoyi-resource.yml index 3e95bb38..4c8d0322 100644 --- a/config/nacos/ruoyi-resource.yml +++ b/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 diff --git a/pom.xml b/pom.xml index 40d51417..8651b5cd 100644 --- a/pom.xml +++ b/pom.xml @@ -58,8 +58,7 @@ 4.10.0 - 2.0.23 - 3.1.687 + 2.2.0 3.11.0 @@ -323,16 +322,11 @@ ${aws-java-sdk-s3.version} + - com.aliyun - dysmsapi20170525 - ${aliyun.sms.version} - - - - com.tencentcloudapi - tencentcloud-sdk-java-sms - ${tencent.sms.version} + org.dromara.sms4j + sms4j-spring-boot-starter + ${sms4j.version} diff --git a/ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteSmsService.java b/ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteSmsService.java index c1741cc7..ad4cbea2 100644 --- a/ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteSmsService.java +++ b/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 param) throws ServiceException; + RemoteSms send(String phones, String templateId, LinkedHashMap param) throws ServiceException; } diff --git a/ruoyi-common/ruoyi-common-sms/pom.xml b/ruoyi-common/ruoyi-common-sms/pom.xml index b628f41d..64e52425 100644 --- a/ruoyi-common/ruoyi-common-sms/pom.xml +++ b/ruoyi-common/ruoyi-common-sms/pom.xml @@ -18,20 +18,15 @@ - org.dromara - ruoyi-common-json - - - - com.aliyun - dysmsapi20170525 - true - - - - com.tencentcloudapi - tencentcloud-sdk-java-sms - true + org.dromara.sms4j + sms4j-spring-boot-starter + + + + com.alibaba + fastjson + + diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/SmsAutoConfiguration.java b/ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/SmsAutoConfiguration.java new file mode 100644 index 00000000..b9f94bb6 --- /dev/null +++ b/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 { + +} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/properties/SmsProperties.java b/ruoyi-common/ruoyi-common-sms/src/main/java/com/ruoyi/common/sms/config/properties/SmsProperties.java new file mode 100644 index 00000000..f0f9021d --- /dev/null +++ b/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; +// +//} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java deleted file mode 100644 index ce4bae73..00000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsAutoConfiguration.java +++ /dev/null @@ -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); - } - - } - -} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/properties/SmsProperties.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/properties/SmsProperties.java deleted file mode 100644 index ead39f43..00000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/properties/SmsProperties.java +++ /dev/null @@ -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; - -} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/AliyunSmsTemplate.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/AliyunSmsTemplate.java deleted file mode 100644 index a24e35ba..00000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/AliyunSmsTemplate.java +++ /dev/null @@ -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 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()); - } - } - -} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/SmsTemplate.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/SmsTemplate.java deleted file mode 100644 index eba38dff..00000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/SmsTemplate.java +++ /dev/null @@ -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 param); - -} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/TencentSmsTemplate.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/TencentSmsTemplate.java deleted file mode 100644 index 7f0cbd35..00000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/TencentSmsTemplate.java +++ /dev/null @@ -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 param) { - if (StringUtils.isBlank(phones)) { - throw new SmsException("手机号不能为空"); - } - if (StringUtils.isBlank(templateId)) { - throw new SmsException("模板ID不能为空"); - } - SendSmsRequest req = new SendSmsRequest(); - Set 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()); - } - } - -} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/entity/SmsResult.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/entity/SmsResult.java deleted file mode 100644 index 45aba064..00000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/entity/SmsResult.java +++ /dev/null @@ -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; - - /** - * 实际响应体 - *

- * 可自行转换为 SDK 对应的 SendSmsResponse - */ - private String response; -} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/exception/SmsException.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/exception/SmsException.java deleted file mode 100644 index eb7730ae..00000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/exception/SmsException.java +++ /dev/null @@ -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); - } - -} diff --git a/ruoyi-common/ruoyi-common-sms/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-sms/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 5919ce32..e4081a1f 100644 --- a/ruoyi-common/ruoyi-common-sms/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/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 diff --git a/ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java b/ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java index 89e4126f..f14fdb73 100644 --- a/ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java +++ b/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 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 map = new HashMap<>(1); + LinkedHashMap 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 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 map = new HashMap<>(1); + LinkedHashMap 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); } } diff --git a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysSmsController.java b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysSmsController.java index 5824bea7..21c2afd1 100644 --- a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/controller/SysSmsController.java +++ b/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 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 map = new HashMap<>(1); + LinkedHashMap 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(); } diff --git a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteSmsServiceImpl.java b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteSmsServiceImpl.java index fa105bcf..a8070ee3 100644 --- a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteSmsServiceImpl.java +++ b/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 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 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; } }