From bb587607e147fd92f9cd653f837075455444249f Mon Sep 17 00:00:00 2001 From: 友杰 <273666957@qq.com> Date: 星期五, 09 六月 2023 09:58:50 +0800 Subject: [PATCH] !367 集成sms4j短信 集成sms4j短信 --- /dev/null | 19 ------ ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsConfig.java | 31 ---------- ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/SmsController.java | 23 +++---- ruoyi-common/ruoyi-common-sms/pom.xml | 10 -- ruoyi-admin/src/main/java/org/dromara/web/controller/CaptchaController.java | 21 +++--- ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/properties/SmsProperties.java | 26 -------- pom.xml | 17 +---- ruoyi-admin/src/main/resources/application-dev.yml | 20 +++--- 8 files changed, 39 insertions(+), 128 deletions(-) diff --git a/pom.xml b/pom.xml index b20e7f0..18e8448 100644 --- a/pom.xml +++ b/pom.xml @@ -48,8 +48,7 @@ <!-- OSS 閰嶇疆 --> <aws-java-sdk-s3.version>1.12.400</aws-java-sdk-s3.version> <!-- SMS 閰嶇疆 --> - <aliyun.sms.version>2.0.23</aliyun.sms.version> - <tencent.sms.version>3.1.687</tencent.sms.version> + <sms4j.version>2.1.1</sms4j.version> <!-- 鎻掍欢鐗堟湰 --> <maven-jar-plugin.version>3.2.2</maven-jar-plugin.version> @@ -234,17 +233,11 @@ <artifactId>aws-java-sdk-s3</artifactId> <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> <dependency> 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 dd9fa19..3977e30 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 @@ -15,10 +15,11 @@ import org.dromara.common.mail.utils.MailUtils; 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.config.properties.CaptchaProperties; import org.dromara.common.web.enums.CaptchaType; +import org.dromara.sms4j.api.entity.SmsResponse; +import org.dromara.sms4j.core.factory.SmsFactory; +import org.dromara.sms4j.provider.enumerate.SupplierType; import org.dromara.web.domain.vo.CaptchaVo; import jakarta.validation.constraints.NotBlank; import lombok.RequiredArgsConstructor; @@ -31,8 +32,7 @@ import org.springframework.web.bind.annotation.RestController; import java.time.Duration; -import java.util.HashMap; -import java.util.Map; +import java.util.LinkedHashMap; /** * 楠岃瘉鐮佹搷浣滃鐞� @@ -64,14 +64,13 @@ String code = RandomUtil.randomNumbers(4); RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION)); // 楠岃瘉鐮佹ā鏉縤d 鑷澶勭悊 (鏌ユ暟鎹簱鎴栧啓姝诲潎鍙�) - String templateId = ""; - Map<String, String> map = new HashMap<>(1); + String templateId = "SMS_460945446"; + 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.isSuccess()) { - log.error("楠岃瘉鐮佺煭淇″彂閫佸紓甯� => {}", result); - return R.fail(result.getMessage()); + SmsResponse smsResponse = SmsFactory.createSmsBlend(SupplierType.ALIBABA).sendMessage(phonenumber, templateId, map); + if (!"OK".equals(smsResponse.getCode())) { + log.error("楠岃瘉鐮佺煭淇″彂閫佸紓甯� => {}", smsResponse); + return R.fail(smsResponse.getMessage()); } return R.ok(); } diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml index 92a3640..f3dda8f 100644 --- a/ruoyi-admin/src/main/resources/application-dev.yml +++ b/ruoyi-admin/src/main/resources/application-dev.yml @@ -158,14 +158,16 @@ # Socket杩炴帴瓒呮椂鍊硷紝鍗曚綅姣锛岀己鐪佸�间笉瓒呮椂 connectionTimeout: 0 ---- # sms 鐭俊 +--- # sms 鐭俊 https://wind.kim/doc/start/springboot.html sms: - enabled: false + enabled: true # 闃块噷浜� dysmsapi.aliyuncs.com - # 鑵捐浜� sms.tencentcloudapi.com - endpoint: "dysmsapi.aliyuncs.com" - accessKeyId: xxxxxxx - accessKeySecret: xxxxxx - signName: 娴嬭瘯 - # 鑵捐涓撶敤 - sdkAppId: + alibaba: + #闃块噷浜戠殑accessKey + accessKeyId: xxxxxxx + #闃块噷浜戠殑accessKeySecret + accessKeySecret: xxxxxxx + #鐭俊绛惧悕 + signature: 娴嬭瘯 + #璇锋眰鍦板潃 榛樿涓篸ysmsapi.aliyuncs.com 濡傛棤鐗规畩鏀瑰彉鍙互涓嶇敤璁剧疆 + requestUrl: dysmsapi.aliyuncs.com diff --git a/ruoyi-common/ruoyi-common-sms/pom.xml b/ruoyi-common/ruoyi-common-sms/pom.xml index b06c5ca..45b4256 100644 --- a/ruoyi-common/ruoyi-common-sms/pom.xml +++ b/ruoyi-common/ruoyi-common-sms/pom.xml @@ -22,16 +22,10 @@ </dependency> <dependency> - <groupId>com.aliyun</groupId> - <artifactId>dysmsapi20170525</artifactId> - <optional>true</optional> + <groupId>org.dromara.sms4j</groupId> + <artifactId>sms4j-spring-boot-starter</artifactId> </dependency> - <dependency> - <groupId>com.tencentcloudapi</groupId> - <artifactId>tencentcloud-sdk-java-sms</artifactId> - <optional>true</optional> - </dependency> </dependencies> </project> diff --git a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsConfig.java b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsConfig.java index 86881f1..eada97b 100644 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsConfig.java +++ b/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/config/SmsConfig.java @@ -1,15 +1,8 @@ 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; /** * 鐭俊閰嶇疆绫� @@ -20,29 +13,5 @@ @AutoConfiguration @EnableConfigurationProperties(SmsProperties.class) public class SmsConfig { - - @Configuration - @ConditionalOnProperty(value = "sms.enabled", havingValue = "true") - @ConditionalOnClass(com.aliyun.dysmsapi20170525.Client.class) - static class AliyunSmsConfig { - - @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 TencentSmsConfig { - - @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 index da6d940..a080a34 100644 --- 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 @@ -15,31 +15,5 @@ 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 00d8152..0000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/AliyunSmsTemplate.java +++ /dev/null @@ -1,66 +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); - } - - @Override - 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()); - } - } - -} 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 eba38df..0000000 --- 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<String, String> 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 18d7384..0000000 --- a/ruoyi-common/ruoyi-common-sms/src/main/java/org/dromara/common/sms/core/TencentSmsTemplate.java +++ /dev/null @@ -1,82 +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); - } - - @Override - 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()); - } - } - -} 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 232d612..0000000 --- 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; - - /** - * 瀹為檯鍝嶅簲浣� - * <p> - * 鍙嚜琛岃浆鎹负 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 eb7730a..0000000 --- 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-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 e130cae..bd16bd6 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 @@ -1,17 +1,18 @@ package org.dromara.demo.controller; +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.common.sms.core.SmsTemplate; -import lombok.RequiredArgsConstructor; +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; /** * 鐭俊婕旂ず妗堜緥 @@ -44,11 +45,10 @@ 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); + SmsResponse smsResponse = SmsFactory.createSmsBlend(SupplierType.ALIBABA).sendMessage(phones, templateId, map); + return R.ok(smsResponse); } /** @@ -65,12 +65,11 @@ 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); + SmsResponse smsResponse = SmsFactory.createSmsBlend(SupplierType.TENCENT).sendMessage(phones, templateId, map); + return R.ok(smsResponse); } } -- Gitblit v1.9.3