!145 将现有sms4j版本升级到3X

Merge pull request !145 from AprilWind/feat/dev-sms3.x
2.X
疯狂的狮子Li 1 year ago committed by Gitee
commit 7df0ed34d8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -54,28 +54,59 @@ mail:
connectionTimeout: 0 connectionTimeout: 0
# sms 短信 支持 阿里云 腾讯云 云片 等等各式各样的短信服务商 # sms 短信 支持 阿里云 腾讯云 云片 等等各式各样的短信服务商
# https://wind.kim/doc/start 文档地址 各个厂商可同时使用 # https://sms4j.com/doc3/ 差异配置文档地址 支持单厂商多配置,可以配置多个同时使用
sms: sms:
# 阿里云 dysmsapi.aliyuncs.com # 配置源类型用于标定配置来源(interface,yaml)
alibaba: config-type: yaml
#请求地址 默认为 dysmsapi.aliyuncs.com 如无特殊改变可以不用设置 # 用于标定yml中的配置是否开启短信拦截接口配置不受此限制
requestUrl: dysmsapi.aliyuncs.com restricted: true
#阿里云的accessKey # 短信拦截限制单手机号每分钟最大发送,只对开启了拦截的配置有效
accessKeyId: xxxxxxx minute-max: 1
#阿里云的accessKeySecret # 短信拦截限制单手机号每日最大发送量,只对开启了拦截的配置有效
accessKeySecret: xxxxxxx account-max: 30
#短信签名 # 以下配置来自于 org.dromara.sms4j.provider.config.BaseConfig类中
signature: 测试 blends:
tencent: # 自定义的标识也就是configId这里可以是任意值最好不要是中文
#请求地址默认为 sms.tencentcloudapi.com 如无特殊改变可不用设置 tx1:
requestUrl: sms.tencentcloudapi.com # 框架定义的厂商名称标识,标定此配置是哪个厂商,详细请看厂商标识介绍部分
#腾讯云的accessKey supplier: tencent
accessKeyId: xxxxxxx # 有些称为accessKey有些称之为apiKey也有称为sdkKey或者appId。
#腾讯云的accessKeySecret access-key-id: 您的accessKey
accessKeySecret: xxxxxxx # 称为accessSecret有些称之为apiSecret
#短信签名 access-key-secret: 您的accessKeySecret
signature: 测试 # 您的短信签名
#短信sdkAppId signature: 您的短信签名
sdkAppId: appid #模板ID 如果不需要简化的sendMessage方法可以不配置如果使用sendMessage的快速发送需此配置
#地域信息默认为 ap-guangzhou 如无特殊改变可不用设置 template-id: xxxxxxxx
territory: ap-guangzhou #您的sdkAppId
sdk-app-id: 您的sdkAppId
# 随机权重负载均衡的权重值依赖于此默认为1如不需要负载均衡可不进行配置
weight: 1
#短信自动重试间隔时间 默认五秒
retry-interval: 3
# 短信重试次数默认0次不重试如果你需要短信重试则根据自己的需求修改值即可
max-retries: 1
# 当前厂商的发送数量上限,默认不设置上限
maximum: 10000
# 自定义的标识也就是configId这里可以是任意值最好不要是中文
tx2:
#厂商标识,标定此配置是哪个厂商,详细请看厂商标识介绍部分
supplier: tencent
#您的accessKey
access-key-id: 您的accessKey
#您的accessKeySecret
access-key-secret: 您的accessKeySecret
#您的短信签名
signature: 您的短信签名
#模板ID 非必须配置如果使用sendMessage的快速发送需此配置
template-id: xxxxxxxx
#您的sdkAppId
sdk-app-id: 您的sdkAppId
# 随机权重负载均衡的权重值依赖于此默认为1如不需要负载均衡可不进行配置
weight: 1
#短信自动重试间隔时间 默认五秒
retry-interval: 3
# 短信重试次数默认0次不重试如果你需要短信重试则根据自己的需求修改值即可
max-retries: 1
# 当前厂商的发送数量上限,默认不设置上限
maximum: 10000

@ -57,7 +57,7 @@
<okhttp.version>4.10.0</okhttp.version> <okhttp.version>4.10.0</okhttp.version>
<!-- SMS 配置 --> <!-- SMS 配置 -->
<sms4j.version>2.2.0</sms4j.version> <sms4j.version>3.1.1</sms4j.version>
<!-- 插件版本 --> <!-- 插件版本 -->
<maven-compiler-plugin.verison>3.11.0</maven-compiler-plugin.verison> <maven-compiler-plugin.verison>3.11.0</maven-compiler-plugin.verison>

@ -1,24 +1,145 @@
package org.dromara.resource.api; package org.dromara.resource.api;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.resource.api.domain.RemoteSms; import org.dromara.resource.api.domain.RemoteSms;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
/** /**
* *
* *
* @author Lion Li * @author Feng
*/ */
public interface RemoteSmsService { public interface RemoteSmsService {
/** /**
* *
* *
* @param phones () * @param phone
* @param templateId id * @param message
* @param param * @return RemoteSms
*/ */
RemoteSms send(String phones, String templateId, LinkedHashMap<String, String> param) throws ServiceException; RemoteSms sendMessage(String phone, String message);
/**
*
*
* @param phone
* @param messages 使 LinkedHashMap
* @return RemoteSms
*/
RemoteSms sendMessage(String phone, LinkedHashMap<String, String> messages);
/**
* 使
*
* @param phone
* @param templateId ID
* @param messages 使 LinkedHashMap
* @return RemoteSms
*/
RemoteSms sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages);
/**
*
*
* @param phones 1~1000
* @param message
* @return RemoteSms
*/
RemoteSms massTexting(List<String> phones, String message);
/**
* 使
*
* @param phones 1~10001~1000
* @param templateId ID
* @param messages 使 LinkedHashMap
* @return RemoteSms
*/
RemoteSms massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages);
/**
*
*
* @param phone
* @param message
*/
void sendMessageAsync(String phone, String message);
/**
* 使
*
* @param phone
* @param templateId ID
* @param messages 使 LinkedHashMap
*/
void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages);
/**
*
*
* @param phone
* @param message
* @param delayedTime
*/
void delayedMessage(String phone, String message, Long delayedTime);
/**
* 使
*
* @param phone
* @param templateId ID
* @param messages 使 LinkedHashMap
* @param delayedTime
*/
void delayedMessage(String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime);
/**
*
*
* @param phones 1~1000
* @param message
* @param delayedTime
*/
void delayMassTexting(List<String> phones, String message, Long delayedTime);
/**
* 使
*
* @param phones 1~1000
* @param templateId ID
* @param messages 使 LinkedHashMap
* @param delayedTime
*/
void delayMassTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime);
/**
*
*
* @param phone
*/
void joinInBlacklist(String phone);
/**
*
*
* @param phones
*/
void joinInBlacklist(List<String> phones);
/**
*
*
* @param phone
*/
void removeFromBlacklist(String phone);
/**
*
*
* @param phones
*/
void removeFromBlacklist(List<String> phones);
} }

@ -19,15 +19,15 @@ public class RemoteSms implements Serializable {
/** /**
* *
*/ */
private Boolean isSuccess; private Boolean success;
/** /**
* * Alibaba
*/ */
private String message; private String configId;
/** /**
* *
* <p> * <p>
* SDK SendSmsResponse * SDK SendSmsResponse
*/ */

@ -20,13 +20,12 @@
<dependency> <dependency>
<groupId>org.dromara.sms4j</groupId> <groupId>org.dromara.sms4j</groupId>
<artifactId>sms4j-spring-boot-starter</artifactId> <artifactId>sms4j-spring-boot-starter</artifactId>
<exclusions> </dependency>
<!-- 排除京东短信内存在的fastjson等待作者后续修复 -->
<exclusion> <!-- RuoYi Common Redis-->
<groupId>com.alibaba</groupId> <dependency>
<artifactId>fastjson</artifactId> <groupId>org.dromara</groupId>
</exclusion> <artifactId>ruoyi-common-redis</artifactId>
</exclusions>
</dependency> </dependency>
</dependencies> </dependencies>

@ -1,14 +1,24 @@
package org.dromara.common.sms.config; package org.dromara.common.sms.config;
import org.dromara.common.redis.config.RedisConfiguration;
import org.dromara.common.sms.core.dao.PlusSmsDao;
import org.dromara.sms4j.api.dao.SmsDao;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
/** /**
* ( ) *
* *
* @author Lion Li * @author Feng
* @version 4.2.0
*/ */
@AutoConfiguration @AutoConfiguration(after = {RedisConfiguration.class})
public class SmsAutoConfiguration { public class SmsAutoConfiguration {
@Primary
@Bean
public SmsDao smsDao() {
return new PlusSmsDao();
}
} }

@ -0,0 +1,72 @@
package org.dromara.common.sms.core.dao;
import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.sms4j.api.dao.SmsDao;
import java.time.Duration;
/**
* SmsDao (使RedisUtils )
* <p>
*
* @author Feng
*/
public class PlusSmsDao implements SmsDao {
/**
*
*
* @param key
* @param value
* @param cacheTime )
*/
@Override
public void set(String key, Object value, long cacheTime) {
RedisUtils.setCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key, value, Duration.ofSeconds(cacheTime));
}
/**
*
*
* @param key
* @param value
*/
@Override
public void set(String key, Object value) {
RedisUtils.setCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key, value, true);
}
/**
*
*
* @param key
* @return
*/
@Override
public Object get(String key) {
return RedisUtils.getCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key);
}
/**
* remove
* <p> key
*
* @param key
* @return value
* @author :Wind
*/
@Override
public Object remove(String key) {
return RedisUtils.deleteObject(GlobalConstants.GLOBAL_REDIS_KEY + key);
}
/**
*
*/
@Override
public void clean() {
RedisUtils.deleteObject(GlobalConstants.GLOBAL_REDIS_KEY + "sms:");
}
}

@ -5,7 +5,6 @@ import org.dromara.common.core.domain.R;
import org.dromara.sms4j.api.SmsBlend; import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse; import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory; import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.provider.enumerate.SupplierType;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -35,7 +34,7 @@ public class SmsController {
public R<Object> sendAliyun(String phones, String templateId) { public R<Object> sendAliyun(String phones, String templateId) {
LinkedHashMap<String, String> map = new LinkedHashMap<>(1); LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
map.put("code", "1234"); map.put("code", "1234");
SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA); SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map); SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map);
return R.ok(smsResponse); return R.ok(smsResponse);
} }
@ -51,9 +50,33 @@ public class SmsController {
LinkedHashMap<String, String> map = new LinkedHashMap<>(1); LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
// map.put("2", "测试测试"); // map.put("2", "测试测试");
map.put("1", "1234"); map.put("1", "1234");
SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.TENCENT); SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map); SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, map);
return R.ok(smsResponse); return R.ok(smsResponse);
} }
/**
*
*
* @param phone
*/
@GetMapping("/joinInBlacklist")
public R<Object> joinInBlacklist(String phone){
SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
smsBlend.joinInBlacklist(phone);
return R.ok();
}
/**
*
*
* @param phone
*/
@GetMapping("/removeFromBlacklist")
public R<Object> removeFromBlacklist(String phone){
SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
smsBlend.removeFromBlacklist(phone);
return R.ok();
}
} }

@ -14,7 +14,6 @@ import org.dromara.common.web.core.BaseController;
import org.dromara.sms4j.api.SmsBlend; import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse; import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory; import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.provider.enumerate.SupplierType;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -50,11 +49,11 @@ public class SysSmsController extends BaseController {
String templateId = ""; String templateId = "";
LinkedHashMap<String, String> map = new LinkedHashMap<>(1); LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
map.put("code", code); map.put("code", code);
SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA); SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
SmsResponse smsResponse = smsBlend.sendMessage(phonenumber, templateId, map); SmsResponse smsResponse = smsBlend.sendMessage(phonenumber, templateId, map);
if (!"OK".equals(smsResponse.getCode())) { if (!smsResponse.isSuccess()) {
log.error("验证码短信发送异常 => {}", smsResponse); log.error("验证码短信发送异常 => {}", smsResponse);
return R.fail(smsResponse.getMessage()); return R.fail(smsResponse.getData().toString());
} }
return R.ok(); return R.ok();
} }

@ -11,7 +11,7 @@ import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
/** /**
* *
* *
* @author Lion Li * @author Lion Li
*/ */

@ -3,22 +3,20 @@ package org.dromara.resource.dubbo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService; 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.RemoteSmsService;
import org.dromara.resource.api.domain.RemoteSms; import org.dromara.resource.api.domain.RemoteSms;
import org.dromara.sms4j.api.SmsBlend; import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse; import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory; import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.provider.enumerate.SupplierType;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
/** /**
* *
* *
* @author Lion Li * @author Feng
*/ */
@Slf4j @Slf4j
@RequiredArgsConstructor @RequiredArgsConstructor
@ -27,20 +25,211 @@ import java.util.LinkedHashMap;
public class RemoteSmsServiceImpl implements RemoteSmsService { public class RemoteSmsServiceImpl implements RemoteSmsService {
/** /**
* * SmsBlend
* *
* @param phones () * @return SmsBlend
* @param templateId id
* @param param
*/ */
public RemoteSms send(String phones, String templateId, LinkedHashMap<String, String> param) throws ServiceException { private SmsBlend getSmsBlend() {
SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA); return SmsFactory.getSmsBlend("tx1");
SmsResponse smsResponse = smsBlend.sendMessage(phones, templateId, param); }
RemoteSms sysSms = new RemoteSms();
sysSms.setIsSuccess(smsResponse.isSuccess()); /**
sysSms.setMessage(smsResponse.getMessage()); * SmsResponse RemoteSms
sysSms.setResponse(JsonUtils.toJsonString(smsResponse)); *
return sysSms; * @param smsResponse
* @return RemoteSms
*/
private RemoteSms getRemoteSms(SmsResponse smsResponse) {
// 创建一个 RemoteSms 对象,封装响应信息
RemoteSms sms = new RemoteSms();
sms.setSuccess(smsResponse.isSuccess());
sms.setResponse(smsResponse.getData().toString());
sms.setConfigId(smsResponse.getConfigId());
return sms;
}
/**
*
*
* @param phone
* @param message
* @return RemoteSms
*/
@Override
public RemoteSms sendMessage(String phone, String message) {
// 调用 getSmsBlend 方法获取对应短信供应商的 SmsBlend 实例
SmsResponse smsResponse = getSmsBlend().sendMessage(phone, message);
return getRemoteSms(smsResponse);
}
/**
*
*
* @param phone
* @param messages 使 LinkedHashMap
* @return RemoteSms
*/
@Override
public RemoteSms sendMessage(String phone, LinkedHashMap<String, String> messages) {
SmsResponse smsResponse = getSmsBlend().sendMessage(phone, messages);
return getRemoteSms(smsResponse);
}
/**
*
*
* @param phone
* @param templateId ID
* @param messages 使 LinkedHashMap
* @return RemoteSms
*/
@Override
public RemoteSms sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
// 调用 getSmsBlend 方法获取对应短信供应商的 SmsBlend 实例
SmsResponse smsResponse = getSmsBlend().sendMessage(phone, templateId, messages);
return getRemoteSms(smsResponse);
}
/**
*
*
* @param phones
* @param message
* @return RemoteSms
*/
@Override
public RemoteSms massTexting(List<String> phones, String message) {
// 调用 getSmsBlend 方法获取对应短信供应商的 SmsBlend 实例
SmsResponse smsResponse = getSmsBlend().massTexting(phones, message);
return getRemoteSms(smsResponse);
}
/**
*
*
* @param phones
* @param templateId ID
* @param messages 使 LinkedHashMap
* @return RemoteSms
*/
@Override
public RemoteSms massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
// 调用 getSmsBlend 方法获取对应短信供应商的 SmsBlend 实例
SmsResponse smsResponse = getSmsBlend().massTexting(phones, templateId, messages);
return getRemoteSms(smsResponse);
}
/**
*
*
* @param phone
* @param message
*/
@Override
public void sendMessageAsync(String phone, String message) {
getSmsBlend().sendMessageAsync(phone, message);
}
/**
*
*
* @param phone
* @param templateId ID
* @param messages 使 LinkedHashMap
*/
@Override
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages) {
getSmsBlend().sendMessageAsync(phone, templateId, messages);
}
/**
*
*
* @param phone
* @param message
* @param delayedTime
*/
@Override
public void delayedMessage(String phone, String message, Long delayedTime) {
getSmsBlend().delayedMessage(phone, message, delayedTime);
}
/**
*
*
* @param phone
* @param templateId ID
* @param messages 使 LinkedHashMap
* @param delayedTime
*/
@Override
public void delayedMessage(String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
getSmsBlend().delayedMessage(phone, templateId, messages, delayedTime);
}
/**
*
*
* @param phones
* @param message
* @param delayedTime
*/
@Override
public void delayMassTexting(List<String> phones, String message, Long delayedTime) {
getSmsBlend().delayMassTexting(phones, message, delayedTime);
}
/**
*
*
* @param phones
* @param templateId ID
* @param messages 使 LinkedHashMap
* @param delayedTime
*/
@Override
public void delayMassTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
getSmsBlend().delayMassTexting(phones, templateId, messages, delayedTime);
}
/**
*
*
* @param phone
*/
@Override
public void joinInBlacklist(String phone) {
getSmsBlend().joinInBlacklist(phone);
}
/**
*
*
* @param phones
*/
@Override
public void joinInBlacklist(List<String> phones) {
getSmsBlend().batchJoinBlacklist(phones);
}
/**
*
*
* @param phone
*/
@Override
public void removeFromBlacklist(String phone) {
getSmsBlend().removeFromBlacklist(phone);
}
/**
*
*
* @param phones
*/
@Override
public void removeFromBlacklist(List<String> phones) {
getSmsBlend().batchRemovalFromBlacklist(phones);
} }
} }

Loading…
Cancel
Save