mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 08:58:38 +08:00
统一工具类风格为 XxxUtils
This commit is contained in:
parent
9614f1d094
commit
08ccedc6ff
@ -10,7 +10,7 @@ import java.util.Objects;
|
||||
* @author :Wind
|
||||
* 2023/4/8 14:29
|
||||
**/
|
||||
public class SettingUtil {
|
||||
public class SettingUtils {
|
||||
|
||||
/** 读取配置文件*/
|
||||
public static String getSetting(String path){
|
||||
@ -18,6 +18,6 @@ public class SettingUtil {
|
||||
}
|
||||
|
||||
public static String getSetting(){
|
||||
return getSetting(Objects.requireNonNull(SettingUtil.class.getResource("/smsConfig.json")).getPath());
|
||||
return getSetting(Objects.requireNonNull(SettingUtils.class.getResource("/smsConfig.json")).getPath());
|
||||
}
|
||||
}
|
||||
@ -9,16 +9,16 @@ import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class SmsHttpUtil {
|
||||
public class SmsHttpUtils {
|
||||
|
||||
private SmsHttpUtil() {
|
||||
private SmsHttpUtils() {
|
||||
}
|
||||
|
||||
private static class SmsHttpHolder {
|
||||
private static final SmsHttpUtil INSTANCE = new SmsHttpUtil();
|
||||
private static final SmsHttpUtils INSTANCE = new SmsHttpUtils();
|
||||
}
|
||||
|
||||
public static SmsHttpUtil instance() {
|
||||
public static SmsHttpUtils instance() {
|
||||
return SmsHttpHolder.INSTANCE;
|
||||
}
|
||||
|
||||
@ -15,8 +15,8 @@ import java.util.Map;
|
||||
/**
|
||||
* @author wind
|
||||
*/
|
||||
public class SmsUtil {
|
||||
private SmsUtil() {
|
||||
public class SmsUtils {
|
||||
private SmsUtils() {
|
||||
} //私有构造防止实例化
|
||||
|
||||
/**
|
||||
@ -5,7 +5,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
import org.dromara.sms4j.api.proxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
|
||||
@ -29,9 +29,9 @@ public class RestrictedProcessDefaultImpl implements RestrictedProcess {
|
||||
SmsConfig config = BeanFactory.getSmsConfig();
|
||||
Integer accountMax = config.getAccountMax(); // 每日最大发送量
|
||||
Integer minuteMax = config.getMinuteMax(); // 每分钟最大发送量
|
||||
if (SmsUtil.isNotEmpty(accountMax)) { // 是否配置了每日限制
|
||||
if (SmsUtils.isNotEmpty(accountMax)) { // 是否配置了每日限制
|
||||
Integer i = (Integer) smsDao.get(phone + "max");
|
||||
if (SmsUtil.isEmpty(i)) {
|
||||
if (SmsUtils.isEmpty(i)) {
|
||||
smsDao.set(phone + "max", 1, accTimer);
|
||||
} else if (i >= accountMax) {
|
||||
log.info("The phone:" + phone + ",number of short messages reached the maximum today");
|
||||
@ -40,9 +40,9 @@ public class RestrictedProcessDefaultImpl implements RestrictedProcess {
|
||||
smsDao.set(phone + "max", i + 1, accTimer);
|
||||
}
|
||||
}
|
||||
if (SmsUtil.isNotEmpty(minuteMax)) { // 是否配置了每分钟最大限制
|
||||
if (SmsUtils.isNotEmpty(minuteMax)) { // 是否配置了每分钟最大限制
|
||||
Integer o = (Integer) smsDao.get(phone);
|
||||
if (SmsUtil.isNotEmpty(o)) {
|
||||
if (SmsUtils.isNotEmpty(o)) {
|
||||
if (o < minuteMax) {
|
||||
smsDao.set(phone, o + 1, minTimer);
|
||||
} else {
|
||||
|
||||
@ -17,14 +17,14 @@ import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
import org.dromara.sms4j.cloopen.config.CloopenFactory;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.dromara.sms4j.core.proxy.RestrictedProcessDefaultImpl;
|
||||
import org.dromara.sms4j.core.proxy.SmsInvocationHandler;
|
||||
import org.dromara.sms4j.ctyun.config.CtyunFactory;
|
||||
import org.dromara.sms4j.emay.config.EmayFactory;
|
||||
import org.dromara.sms4j.huawei.config.HuaweiFactory;
|
||||
import org.dromara.sms4j.javase.util.YamlUtil;
|
||||
import org.dromara.sms4j.javase.util.YamlUtils;
|
||||
import org.dromara.sms4j.jdcloud.config.JdCloudFactory;
|
||||
import org.dromara.sms4j.netease.config.NeteaseFactory;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
@ -65,7 +65,7 @@ public class SEInitializer {
|
||||
* @param yaml yaml配置字符串
|
||||
*/
|
||||
public void fromYaml(String yaml) {
|
||||
InitConfig config = YamlUtil.toBean(yaml, InitConfig.class);
|
||||
InitConfig config = YamlUtils.toBean(yaml, InitConfig.class);
|
||||
this.initConfig(config);
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ public class SEInitializer {
|
||||
continue;
|
||||
}
|
||||
configMap.put("config-id", configId);
|
||||
SmsUtil.replaceKeysSeperator(configMap, "-", "_");
|
||||
SmsUtils.replaceKeysSeperator(configMap, "-", "_");
|
||||
JSONObject configJson = new JSONObject(configMap);
|
||||
SupplierConfig supplierConfig = JSONUtil.toBean(configJson, providerFactory.getConfigClass());
|
||||
if(Boolean.TRUE.equals(smsConfig.getRestricted())) {
|
||||
|
||||
@ -7,7 +7,7 @@ import org.yaml.snakeyaml.constructor.Constructor;
|
||||
import org.yaml.snakeyaml.introspector.Property;
|
||||
import org.yaml.snakeyaml.introspector.PropertyUtils;
|
||||
|
||||
public class YamlUtil {
|
||||
public class YamlUtils {
|
||||
|
||||
/**
|
||||
* 构建Yaml实例
|
||||
@ -38,7 +38,7 @@ public class YamlUtil {
|
||||
* @return 读取内容
|
||||
*/
|
||||
public static <T> T toBean(String content, Class<T> cls) {
|
||||
Yaml yaml = YamlUtil.buildYamlInstance(cls);
|
||||
Yaml yaml = YamlUtils.buildYamlInstance(cls);
|
||||
return yaml.loadAs(content, cls);
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.provider.service.AbstractSmsBlend;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@ -76,7 +76,7 @@ public class AlibabaSmsImpl extends AbstractSmsBlend<AlibabaConfig> {
|
||||
@Override
|
||||
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
|
||||
String messageStr = JSONUtil.toJsonStr(messages);
|
||||
return getSmsResponse(SmsUtil.arrayToString(phones), messageStr, templateId);
|
||||
return getSmsResponse(SmsUtils.arrayToString(phones), messageStr, templateId);
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String phone, String message, String templateId) {
|
||||
|
||||
@ -10,7 +10,7 @@ import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.cloopen.config.CloopenConfig;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsHttpUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsHttpUtils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -26,10 +26,10 @@ import java.util.Map;
|
||||
public class CloopenHelper {
|
||||
|
||||
private final CloopenConfig config;
|
||||
private final SmsHttpUtil http;
|
||||
private final SmsHttpUtils http;
|
||||
private int retry = 0;
|
||||
|
||||
public CloopenHelper(CloopenConfig config, SmsHttpUtil http) {
|
||||
public CloopenHelper(CloopenConfig config, SmsHttpUtils http) {
|
||||
this.config = config;
|
||||
this.http = http;
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.ctyun.config.CtyunConfig;
|
||||
import org.dromara.sms4j.ctyun.utils.CtyunUtils;
|
||||
import org.dromara.sms4j.provider.service.AbstractSmsBlend;
|
||||
@ -64,7 +64,7 @@ public class CtyunSmsImpl extends AbstractSmsBlend<CtyunConfig> {
|
||||
@Override
|
||||
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
|
||||
String messageStr = JSONUtil.toJsonStr(messages);
|
||||
return getSmsResponse(SmsUtil.arrayToString(phones), messageStr, templateId);
|
||||
return getSmsResponse(SmsUtils.arrayToString(phones), messageStr, templateId);
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String phone, String message, String templateId) {
|
||||
|
||||
@ -7,7 +7,7 @@ import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.emay.config.EmayConfig;
|
||||
import org.dromara.sms4j.emay.util.EmayBuilder;
|
||||
import org.dromara.sms4j.provider.service.AbstractSmsBlend;
|
||||
@ -80,7 +80,7 @@ public class EmaySmsImpl extends AbstractSmsBlend<EmayConfig> {
|
||||
if (phones.size() > 500) {
|
||||
throw new SmsBlendException("单次发送超过最大发送上限,建议每次群发短信人数低于500");
|
||||
}
|
||||
return sendMessage(SmsUtil.listToString(phones), message);
|
||||
return sendMessage(SmsUtils.listToString(phones), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -92,7 +92,7 @@ public class EmaySmsImpl extends AbstractSmsBlend<EmayConfig> {
|
||||
for (Map.Entry<String, String> entry : messages.entrySet()) {
|
||||
list.add(entry.getValue());
|
||||
}
|
||||
return sendMessage(SmsUtil.listToString(phones), EmayBuilder.listToString(list));
|
||||
return sendMessage(SmsUtils.listToString(phones), EmayBuilder.listToString(list));
|
||||
}
|
||||
|
||||
private SmsResponse getResponse(JSONObject resJson) {
|
||||
|
||||
@ -7,7 +7,7 @@ import org.dromara.sms4j.api.callback.CallBack;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.utils.SmsHttpUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsHttpUtils;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@ -27,7 +27,7 @@ public abstract class AbstractSmsBlend<C extends SupplierConfig> implements SmsB
|
||||
|
||||
protected final DelayedTime delayed;
|
||||
|
||||
protected final SmsHttpUtil http = SmsHttpUtil.instance();
|
||||
protected final SmsHttpUtils http = SmsHttpUtils.instance();
|
||||
|
||||
protected AbstractSmsBlend(C config, Executor pool, DelayedTime delayed) {
|
||||
this.configId = StrUtil.isEmpty(config.getConfigId()) ? getSupplier() : config.getConfigId();
|
||||
|
||||
@ -8,7 +8,7 @@ import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.provider.service.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.tencent.config.TencentConfig;
|
||||
import org.dromara.sms4j.tencent.utils.TencentUtils;
|
||||
@ -77,7 +77,7 @@ public class TencentSmsImpl extends AbstractSmsBlend<TencentConfig> {
|
||||
list.add(entry.getValue());
|
||||
}
|
||||
String[] s = new String[list.size()];
|
||||
return getSmsResponse(SmsUtil.listToArray(phones), list.toArray(s), templateId);
|
||||
return getSmsResponse(SmsUtils.listToArray(phones), list.toArray(s), templateId);
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String[] phones, String[] messages, String templateId) {
|
||||
|
||||
@ -6,12 +6,11 @@ import cn.hutool.crypto.digest.HmacAlgorithm;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsHttpUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsHttpUtils;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
@ -28,7 +27,7 @@ public class UniClient {
|
||||
private final int retryInterval;
|
||||
private final int maxRetries;
|
||||
private int retry = 0;
|
||||
private final SmsHttpUtil http = SmsHttpUtil.instance();
|
||||
private final SmsHttpUtils http = SmsHttpUtils.instance();
|
||||
|
||||
protected UniClient(Builder b) {
|
||||
this.accessKeyId = b.accessKeyId;
|
||||
|
||||
@ -7,7 +7,7 @@ import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.provider.service.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.yunpian.config.YunpianConfig;
|
||||
|
||||
@ -103,7 +103,7 @@ public class YunPianSmsImpl extends AbstractSmsBlend<YunpianConfig> {
|
||||
if (phones.size() > 1000) {
|
||||
throw new SmsBlendException("单次发送超过最大发送上限,建议每次群发短信人数低于1000");
|
||||
}
|
||||
return sendMessage(SmsUtil.listToString(phones), message);
|
||||
return sendMessage(SmsUtils.listToString(phones), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -111,7 +111,7 @@ public class YunPianSmsImpl extends AbstractSmsBlend<YunpianConfig> {
|
||||
if (phones.size() > 1000) {
|
||||
throw new SmsBlendException("单次发送超过最大发送上限,建议每次群发短信人数低于1000");
|
||||
}
|
||||
return sendMessage(SmsUtil.listToString(phones), templateId, messages);
|
||||
return sendMessage(SmsUtils.listToString(phones), templateId, messages);
|
||||
}
|
||||
|
||||
private String formattingMap(Map<String, String> messages) {
|
||||
|
||||
@ -4,7 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
import org.dromara.sms4j.api.proxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.noear.solon.core.AopContext;
|
||||
@ -26,9 +26,9 @@ public class SolonRestrictedProcess implements RestrictedProcess {
|
||||
SmsConfig config = BeanFactory.getSmsConfig();
|
||||
Integer accountMax = config.getAccountMax(); // 每日最大发送量
|
||||
Integer minuteMax = config.getMinuteMax(); // 每分钟最大发送量
|
||||
if (SmsUtil.isNotEmpty(accountMax)) { // 是否配置了每日限制
|
||||
if (SmsUtils.isNotEmpty(accountMax)) { // 是否配置了每日限制
|
||||
Integer i = (Integer) smsDao.get(REDIS_KEY + phone + "max");
|
||||
if (SmsUtil.isEmpty(i)) {
|
||||
if (SmsUtils.isEmpty(i)) {
|
||||
smsDao.set(REDIS_KEY + phone + "max", 1, accTimer / 1000);
|
||||
} else if (i >= accountMax) {
|
||||
log.info("The phone:" + phone + ",number of short messages reached the maximum today");
|
||||
@ -37,9 +37,9 @@ public class SolonRestrictedProcess implements RestrictedProcess {
|
||||
smsDao.set(REDIS_KEY + phone + "max", i + 1, accTimer / 1000);
|
||||
}
|
||||
}
|
||||
if (SmsUtil.isNotEmpty(minuteMax)) { // 是否配置了每分钟最大限制
|
||||
if (SmsUtils.isNotEmpty(minuteMax)) { // 是否配置了每分钟最大限制
|
||||
Integer o = (Integer) smsDao.get(REDIS_KEY + phone);
|
||||
if (SmsUtil.isNotEmpty(o)) {
|
||||
if (SmsUtils.isNotEmpty(o)) {
|
||||
if (o < minuteMax) {
|
||||
smsDao.set(REDIS_KEY + phone, o + 1, minTimer / 1000);
|
||||
} else {
|
||||
|
||||
@ -7,7 +7,7 @@ import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.comm.constant.SupplierConstant;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@ -29,14 +29,14 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 通过负载均衡服务获取短信服务对象
|
||||
SmsResponse smsResponse = SmsFactory.getSmsBlend().sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getSmsBlend().sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alibabaSmsTest() {
|
||||
// 阿里
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.ALIBABA).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.ALIBABA).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 华为
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.HUAWEI).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.HUAWEI).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -57,7 +57,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 容联云
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.CLOOPEN).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.CLOOPEN).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -68,7 +68,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 亿美软通
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.EMAY).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.EMAY).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -79,7 +79,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 京东云
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.JDCLOUD).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.JDCLOUD).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -90,7 +90,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 云片
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.YUNPIAN).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.YUNPIAN).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -101,7 +101,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 腾讯
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.TENCENT).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.TENCENT).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -112,7 +112,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 合一
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.UNISMS).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.UNISMS).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -123,7 +123,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 天翼云
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.CTYUN).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.CTYUN).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -134,7 +134,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 网易云短信
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.NETEASE).sendMessage(PHONE, SmsUtil.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.NETEASE).sendMessage(PHONE, SmsUtils.getRandomInt(6));
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
}
|
||||
@ -148,7 +148,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 助通短信短信
|
||||
String msg = StrUtil.format("【图书商城】您好,你的验证码是{}:(5分钟失效)", SmsUtil.getRandomInt(6));
|
||||
String msg = StrUtil.format("【图书商城】您好,你的验证码是{}:(5分钟失效)", SmsUtils.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.ZHUTONG).sendMessage(PHONE, msg);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
@ -164,7 +164,7 @@ class Sms4jTest {
|
||||
}
|
||||
// 助通短信短信
|
||||
LinkedHashMap<String, String> messages = new LinkedHashMap<>(1);
|
||||
messages.put("code", SmsUtil.getRandomInt(6));
|
||||
messages.put("code", SmsUtils.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.ZHUTONG).sendMessage(PHONE, "59264", messages);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
@ -179,7 +179,7 @@ class Sms4jTest {
|
||||
return;
|
||||
}
|
||||
// 助通短信短信
|
||||
String msg = StrUtil.format("【图书商城】您好,你的验证码是{}:(5分钟失效)", SmsUtil.getRandomInt(6));
|
||||
String msg = StrUtil.format("【图书商城】您好,你的验证码是{}:(5分钟失效)", SmsUtils.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.ZHUTONG).massTexting(ListUtil.of(PHONE, "180****1111"), msg);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
@ -195,7 +195,7 @@ class Sms4jTest {
|
||||
}
|
||||
// 助通短信短信
|
||||
LinkedHashMap<String, String> messages = new LinkedHashMap<>(1);
|
||||
messages.put("code", SmsUtil.getRandomInt(6));
|
||||
messages.put("code", SmsUtils.getRandomInt(6));
|
||||
SmsResponse smsResponse = SmsFactory.getBySupplier(SupplierConstant.ZHUTONG).massTexting(ListUtil.of(PHONE, "180****1111"), "59264", messages);
|
||||
log.info(JSONUtil.toJsonStr(smsResponse));
|
||||
Assert.isTrue(smsResponse.isSuccess());
|
||||
|
||||
@ -3,7 +3,7 @@ package org.dromara.sms4j.example;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@ -15,56 +15,56 @@ import java.util.List;
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
public class SmsUtilTest {
|
||||
public class SmsUtilsTest {
|
||||
|
||||
@Test
|
||||
public void getRandomString() {
|
||||
String randomString = SmsUtil.getRandomString();
|
||||
String randomString = SmsUtils.getRandomString();
|
||||
log.info(randomString);
|
||||
Assert.isTrue(randomString.length() == 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRandomString() {
|
||||
String randomString = SmsUtil.getRandomString(4);
|
||||
String randomString = SmsUtils.getRandomString(4);
|
||||
log.info(randomString);
|
||||
Assert.isTrue(randomString.length() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRandomInt() {
|
||||
String randomInt = SmsUtil.getRandomInt(4);
|
||||
String randomInt = SmsUtils.getRandomInt(4);
|
||||
log.info(randomInt);
|
||||
Assert.isTrue(randomInt.length() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty() {
|
||||
Assert.isTrue(SmsUtil.isEmpty(""));
|
||||
Assert.isTrue(SmsUtils.isEmpty(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmpty() {
|
||||
Assert.isTrue(SmsUtil.isNotEmpty("not"));
|
||||
Assert.isTrue(SmsUtils.isNotEmpty("not"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonForObject() {
|
||||
AlibabaConfig alibabaConfig = SmsUtil.jsonForObject("{'templateName':'Test'}", AlibabaConfig.class);
|
||||
AlibabaConfig alibabaConfig = SmsUtils.jsonForObject("{'templateName':'Test'}", AlibabaConfig.class);
|
||||
Assert.isTrue(alibabaConfig.getTemplateName().equals("Test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyBean() {
|
||||
AlibabaConfig alibabaConfig = SmsUtil.jsonForObject("{'templateName':'Test'}", AlibabaConfig.class);
|
||||
AlibabaConfig alibabaConfig = SmsUtils.jsonForObject("{'templateName':'Test'}", AlibabaConfig.class);
|
||||
AlibabaConfig alibabaConfig1 = new AlibabaConfig();
|
||||
SmsUtil.copyBean(alibabaConfig, alibabaConfig1);
|
||||
SmsUtils.copyBean(alibabaConfig, alibabaConfig1);
|
||||
Assert.isTrue(alibabaConfig1.getTemplateName().equals("Test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNewMap() {
|
||||
SmsUtil.getNewMap();
|
||||
SmsUtils.getNewMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -72,7 +72,7 @@ public class SmsUtilTest {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("12312341234");
|
||||
list.add("12312341235");
|
||||
String str = SmsUtil.listToString(list);
|
||||
String str = SmsUtils.listToString(list);
|
||||
log.info(str);
|
||||
Assert.isTrue(str.equals("12312341234,12312341235"));
|
||||
}
|
||||
@ -82,7 +82,7 @@ public class SmsUtilTest {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("12312341234");
|
||||
list.add("12312341235");
|
||||
String str = SmsUtil.arrayToString(list);
|
||||
String str = SmsUtils.arrayToString(list);
|
||||
log.info(str);
|
||||
Assert.isTrue(str.equals("+8612312341234,+8612312341235"));
|
||||
}
|
||||
@ -92,7 +92,7 @@ public class SmsUtilTest {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("12312341234");
|
||||
list.add("12312341235");
|
||||
String[] str = SmsUtil.listToArray(list);
|
||||
String[] str = SmsUtils.listToArray(list);
|
||||
Assert.isTrue(str[0].equals("+8612312341234") && str[1].equals("+8612312341235"));
|
||||
}
|
||||
|
||||
@ -4,12 +4,10 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.dao.SmsDao;
|
||||
import org.dromara.sms4j.api.proxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtil;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtils;
|
||||
|
||||
@Slf4j
|
||||
public class SpringRestrictedProcess implements RestrictedProcess {
|
||||
@ -21,12 +19,12 @@ public class SpringRestrictedProcess implements RestrictedProcess {
|
||||
@Override
|
||||
public SmsBlendException process(String phone) {
|
||||
SmsConfig config = BeanFactory.getSmsConfig();
|
||||
SmsDao smsDao = SmsSpringUtil.getBean(SmsDao.class);
|
||||
SmsDao smsDao = SmsSpringUtils.getBean(SmsDao.class);
|
||||
Integer accountMax = config.getAccountMax(); // 每日最大发送量
|
||||
Integer minuteMax = config.getMinuteMax(); // 每分钟最大发送量
|
||||
if (SmsUtil.isNotEmpty(accountMax)) { // 是否配置了每日限制
|
||||
if (SmsUtils.isNotEmpty(accountMax)) { // 是否配置了每日限制
|
||||
Integer i = (Integer) smsDao.get(REDIS_KEY + phone + "max");
|
||||
if (SmsUtil.isEmpty(i)) {
|
||||
if (SmsUtils.isEmpty(i)) {
|
||||
smsDao.set(REDIS_KEY + phone + "max", 1, accTimer / 1000);
|
||||
} else if (i >= accountMax) {
|
||||
log.info("The phone:" + phone + ",number of short messages reached the maximum today");
|
||||
@ -35,9 +33,9 @@ public class SpringRestrictedProcess implements RestrictedProcess {
|
||||
smsDao.set(REDIS_KEY + phone + "max", i + 1, accTimer / 1000);
|
||||
}
|
||||
}
|
||||
if (SmsUtil.isNotEmpty(minuteMax)) { // 是否配置了每分钟最大限制
|
||||
if (SmsUtils.isNotEmpty(minuteMax)) { // 是否配置了每分钟最大限制
|
||||
Integer o = (Integer) smsDao.get(REDIS_KEY + phone);
|
||||
if (SmsUtil.isNotEmpty(o)) {
|
||||
if (SmsUtils.isNotEmpty(o)) {
|
||||
if (o < minuteMax) {
|
||||
smsDao.set(REDIS_KEY + phone, o + 1, minTimer / 1000);
|
||||
} else {
|
||||
|
||||
@ -6,8 +6,8 @@ import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.provider.config.SmsBanner;
|
||||
import org.dromara.sms4j.provider.config.SmsConfig;
|
||||
import org.dromara.sms4j.provider.factory.BeanFactory;
|
||||
import org.dromara.sms4j.starter.utils.ConfigUtil;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtil;
|
||||
import org.dromara.sms4j.starter.utils.ConfigUtils;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@ -22,10 +22,10 @@ import java.util.concurrent.Executor;
|
||||
@Slf4j
|
||||
public class SmsAutowiredConfig {
|
||||
|
||||
private final SmsSpringUtil smsSpringUtil;
|
||||
private final SmsSpringUtils smsSpringUtils;
|
||||
|
||||
public SmsAutowiredConfig(SmsSpringUtil smsSpringUtil) {
|
||||
this.smsSpringUtil = smsSpringUtil;
|
||||
public SmsAutowiredConfig(SmsSpringUtils smsSpringUtils) {
|
||||
this.smsSpringUtils = smsSpringUtils;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ -52,8 +52,8 @@ public class SmsAutowiredConfig {
|
||||
/** 注入一个配置文件读取工具*/
|
||||
@Bean("smsConfigUtil")
|
||||
@Lazy
|
||||
protected ConfigUtil configUtil(Environment environment){
|
||||
return new ConfigUtil(environment);
|
||||
protected ConfigUtils configUtil(Environment environment){
|
||||
return new ConfigUtils(environment);
|
||||
}
|
||||
|
||||
/** smsConfig参数意义为确保注入时smsConfig已经存在*/
|
||||
|
||||
@ -10,7 +10,7 @@ import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
import org.dromara.sms4j.cloopen.config.CloopenFactory;
|
||||
import org.dromara.sms4j.comm.constant.Constant;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtils;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.dromara.sms4j.core.proxy.SmsInvocationHandler;
|
||||
import org.dromara.sms4j.ctyun.config.CtyunFactory;
|
||||
@ -60,7 +60,7 @@ public class SmsBlendsInitializer implements ApplicationListener<ContextRefreshe
|
||||
continue;
|
||||
}
|
||||
configMap.put("config-id", configId);
|
||||
SmsUtil.replaceKeysSeperator(configMap, "-", "_");
|
||||
SmsUtils.replaceKeysSeperator(configMap, "-", "_");
|
||||
JSONObject configJson = new JSONObject(configMap);
|
||||
org.dromara.sms4j.api.universal.SupplierConfig supplierConfig = JSONUtil.toBean(configJson, providerFactory.getConfigClass());
|
||||
if(Boolean.TRUE.equals(smsConfig.getRestricted())) {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package org.dromara.sms4j.starter.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtil;
|
||||
import org.dromara.sms4j.starter.utils.SmsSpringUtils;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@ -10,14 +10,14 @@ import org.springframework.context.annotation.Bean;
|
||||
public class SmsMainConfig {
|
||||
|
||||
@Bean
|
||||
public SmsSpringUtil smsSpringUtil(DefaultListableBeanFactory defaultListableBeanFactory){
|
||||
return new SmsSpringUtil(defaultListableBeanFactory);
|
||||
public SmsSpringUtils smsSpringUtil(DefaultListableBeanFactory defaultListableBeanFactory){
|
||||
return new SmsSpringUtils(defaultListableBeanFactory);
|
||||
}
|
||||
|
||||
/** 主要配置注入 确保springUtil注入后再注入*/
|
||||
@Bean
|
||||
public SmsAutowiredConfig smsAutowiredConfig(SmsSpringUtil smsSpringUtil){
|
||||
return new SmsAutowiredConfig(smsSpringUtil);
|
||||
public SmsAutowiredConfig smsAutowiredConfig(SmsSpringUtils smsSpringUtils){
|
||||
return new SmsAutowiredConfig(smsSpringUtils);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -8,11 +8,11 @@ import org.springframework.core.env.Environment;
|
||||
* @author :Wind
|
||||
* 2023/4/7 21:39
|
||||
**/
|
||||
public class ConfigUtil {
|
||||
public class ConfigUtils {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
public ConfigUtil(Environment environment) {
|
||||
public ConfigUtils(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@ -17,13 +17,13 @@ import java.util.Map;
|
||||
* 2023/3/25 0:13
|
||||
**/
|
||||
@Slf4j
|
||||
public class SmsSpringUtil implements ApplicationContextAware {
|
||||
public class SmsSpringUtils implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
private final DefaultListableBeanFactory beanFactory;
|
||||
|
||||
public SmsSpringUtil(DefaultListableBeanFactory beanFactory) {
|
||||
public SmsSpringUtils(DefaultListableBeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@ -33,8 +33,8 @@ public class SmsSpringUtil implements ApplicationContextAware {
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (SmsSpringUtil.applicationContext == null) {
|
||||
SmsSpringUtil.applicationContext = applicationContext;
|
||||
if (SmsSpringUtils.applicationContext == null) {
|
||||
SmsSpringUtils.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user