mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
update 移除forest
This commit is contained in:
parent
0d28d36895
commit
51af55c325
8
pom.xml
8
pom.xml
@ -58,7 +58,6 @@
|
||||
<solon.version>2.2.0</solon.version>
|
||||
<redisson.version>3.17.0</redisson.version>
|
||||
<jdcloud.version>1.3.3</jdcloud.version>
|
||||
<forest.version>1.5.30</forest.version>
|
||||
<hutool.version>5.8.18</hutool.version>
|
||||
<xmlblend.version>2.3.0</xmlblend.version>
|
||||
<activation.version>1.1.1</activation.version>
|
||||
@ -119,13 +118,6 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Forest依赖 声明式HTTP客户端框架-->
|
||||
<dependency>
|
||||
<groupId>com.dtflys.forest</groupId>
|
||||
<artifactId>forest-core</artifactId>
|
||||
<version>${forest.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- redis 缓存操作 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@ -19,5 +19,10 @@
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-comm</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-http</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package org.dromara.sms4j.api;
|
||||
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import org.dromara.sms4j.api.callback.CallBack;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.comm.annotation.Restricted;
|
||||
@ -18,7 +17,6 @@ public abstract class AbstractSmsBlend implements SmsBlend{
|
||||
protected final Executor pool;
|
||||
protected final DelayedTime delayed;
|
||||
|
||||
protected final ForestConfiguration http = BeanFactory.getForestConfiguration();
|
||||
protected AbstractSmsBlend(Executor pool, DelayedTime delayed) {
|
||||
this.pool = pool;
|
||||
this.delayed = delayed;
|
||||
|
||||
@ -15,9 +15,10 @@
|
||||
<description>sms4j-comm</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.dtflys.forest</groupId>
|
||||
<artifactId>forest-core</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package org.dromara.sms4j.comm.factory;
|
||||
|
||||
import com.dtflys.forest.Forest;
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.config.SmsSqlConfig;
|
||||
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
@ -36,23 +34,12 @@ public class BeanFactory {
|
||||
/** 数据库配置*/
|
||||
private static SmsSqlConfig smsSqlConfig;
|
||||
|
||||
/** 获取forest请求对象*/
|
||||
private static ForestConfiguration forestConfiguration;
|
||||
|
||||
/** 实例化自身对象防止被GC*/
|
||||
private static final BeanFactory beanFactory = new BeanFactory();
|
||||
|
||||
private BeanFactory() {
|
||||
}
|
||||
|
||||
/** 获取请求用的对象*/
|
||||
public static ForestConfiguration getForestConfiguration() {
|
||||
if (forestConfiguration == null){
|
||||
forestConfiguration = Forest.config().setBackendName("httpclient").setLogEnabled(getSmsConfig().getHttpLog());
|
||||
}
|
||||
return forestConfiguration;
|
||||
}
|
||||
|
||||
public static DelayedTime getDelayedTime() {
|
||||
if (delayedTime == null){
|
||||
delayedTime = new DelayedTime();
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
package org.dromara.sms4j.comm.utils;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.sun.org.slf4j.internal.Logger;
|
||||
import com.sun.org.slf4j.internal.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
|
||||
@ -22,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
**/
|
||||
@Slf4j
|
||||
public class TimeExpiredPoolCache {
|
||||
|
||||
/**
|
||||
* 持久化文件格式
|
||||
*/
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package org.dromara.sms4j.aliyun.service;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -15,7 +17,6 @@ import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* <p>类名: AlibabaSmsImpl
|
||||
@ -71,7 +72,6 @@ public class AlibabaSmsImpl extends AbstractSmsBlend {
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String phone, String message, String templateId) {
|
||||
AtomicReference<SmsResponse> reference = new AtomicReference<>();
|
||||
String requestUrl;
|
||||
String paramStr;
|
||||
try {
|
||||
@ -82,13 +82,13 @@ public class AlibabaSmsImpl extends AbstractSmsBlend {
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
}
|
||||
log.debug("requestUrl {}", requestUrl);
|
||||
super.http.post(requestUrl)
|
||||
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||
.addBody(paramStr)
|
||||
.onSuccess(((data, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
return reference.get();
|
||||
try(HttpResponse response = HttpRequest.post(requestUrl)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(paramStr)
|
||||
.execute()){
|
||||
JSONObject body = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(body);
|
||||
}
|
||||
}
|
||||
|
||||
private SmsResponse getResponse(JSONObject resJson) {
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
package org.dromara.sms4j.cloopen.api;
|
||||
|
||||
import com.dtflys.forest.annotation.Address;
|
||||
import com.dtflys.forest.annotation.BaseRequest;
|
||||
import com.dtflys.forest.annotation.Body;
|
||||
import com.dtflys.forest.annotation.PostRequest;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 容联云 REST API
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/4/17 20:57
|
||||
*/
|
||||
@Address(basePath = "{baseUrl}")
|
||||
@BaseRequest(headers = {
|
||||
"Accept: application/json",
|
||||
"Content-Type: application/json;charset=utf-8",
|
||||
"Authorization: {authorization}",
|
||||
})
|
||||
public interface CloopenRestApi {
|
||||
|
||||
/**
|
||||
* 发送模板短信
|
||||
*
|
||||
* @param paramMap 请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
@PostRequest("/Accounts/{accessKeyId}/SMS/TemplateSMS?sig={sign}")
|
||||
Map<String, Object> sendSms(@Body Map<String, Object> paramMap);
|
||||
}
|
||||
@ -2,11 +2,9 @@ package org.dromara.sms4j.cloopen.service;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.dtflys.forest.Forest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.cloopen.api.CloopenRestApi;
|
||||
import org.dromara.sms4j.cloopen.config.CloopenConfig;
|
||||
import org.dromara.sms4j.cloopen.util.CloopenHelper;
|
||||
import org.dromara.sms4j.comm.annotation.Restricted;
|
||||
@ -27,14 +25,11 @@ import java.util.concurrent.Executor;
|
||||
@Slf4j
|
||||
public class CloopenSmsImpl extends AbstractSmsBlend {
|
||||
|
||||
private final CloopenRestApi restApi;
|
||||
|
||||
private final CloopenConfig config;
|
||||
|
||||
public CloopenSmsImpl(CloopenConfig config, Executor pool, DelayedTime delayed) {
|
||||
super(pool,delayed);
|
||||
this.config = config;
|
||||
restApi = Forest.client(CloopenRestApi.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,6 +61,6 @@ public class CloopenSmsImpl extends AbstractSmsBlend {
|
||||
paramMap.put("appId", config.getAppId());
|
||||
paramMap.put("templateId", templateId);
|
||||
paramMap.put("datas", messages.keySet().stream().map(messages::get).toArray(String[]::new));
|
||||
return helper.request(restApi::sendSms, paramMap);
|
||||
return helper.smsResponse(paramMap);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,22 +1,18 @@
|
||||
package org.dromara.sms4j.cloopen.util;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.dtflys.forest.Forest;
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.cloopen.config.CloopenConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.RestApiFunction;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 容联云 Helper
|
||||
@ -32,38 +28,29 @@ public class CloopenHelper {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起 REST 请求
|
||||
*
|
||||
* @param restApiFunction REST API 函数式接口
|
||||
* @param paramMap 请求参数
|
||||
* @param <R> 响应类型
|
||||
* @return 响应信息
|
||||
*/
|
||||
public <R> SmsResponse request(RestApiFunction<Map<String, Object>, R> restApiFunction, Map<String, Object> paramMap) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
try {
|
||||
String timestamp = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
|
||||
// 设置全局变量
|
||||
ForestConfiguration forestConfiguration = Forest.config();
|
||||
forestConfiguration.setVariableValue("baseUrl", (method) -> config.getBaseUrl());
|
||||
forestConfiguration.setVariableValue("accessKeyId", (method) -> config.getAccessKeyId());
|
||||
forestConfiguration.setVariableValue("sign", this.generateSign(config.getAccessKeyId(), config.getAccessKeySecret(), timestamp));
|
||||
forestConfiguration.setVariableValue("authorization", this.generateAuthorization(config.getAccessKeyId(), timestamp));
|
||||
public SmsResponse smsResponse(Map<String, Object> paramMap){
|
||||
String timestamp = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
|
||||
|
||||
// 调用请求
|
||||
R response = restApiFunction.apply(paramMap);
|
||||
String url = String.format("%s/Accounts/%s/SMS/TemplateSMS?sig=%s",
|
||||
config.getBaseUrl(),
|
||||
config.getAccessKeyId(),
|
||||
this.generateSign(config.getAccessKeyId(), config.getAccessKeySecret(), timestamp));
|
||||
|
||||
// 解析结果
|
||||
Map<String, Object> responseMap = Optional.ofNullable(response)
|
||||
.map(JSONUtil::parseObj)
|
||||
.map(obj -> (Map<String, Object>) obj)
|
||||
.orElse(Collections.emptyMap());
|
||||
smsResponse.setSuccess("000000".equals(Convert.toStr(responseMap.get("statusCode"))));
|
||||
smsResponse.setData(JSONUtil.toJsonStr(response));
|
||||
} catch (Exception e) {
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
try(HttpResponse response = HttpRequest.post(url)
|
||||
.header("Accept", "application/json")
|
||||
.header("Content-Type", "application/json;charset=utf-8")
|
||||
.header("Authorization", this.generateAuthorization(config.getAccessKeyId(), timestamp))
|
||||
.body(JSONUtil.toJsonStr(paramMap))
|
||||
.execute()){
|
||||
JSONObject body = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(body);
|
||||
}
|
||||
}
|
||||
|
||||
private SmsResponse getResponse(JSONObject resJson) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
smsResponse.setSuccess("000000".equals(resJson.getStr("statusCode")));
|
||||
smsResponse.setData(resJson);
|
||||
return smsResponse;
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package org.dromara.sms4j.ctyun.service;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -15,7 +17,6 @@ import org.dromara.sms4j.ctyun.utils.CtyunUtils;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* <p>类名: CtyunSmsImpl
|
||||
@ -65,7 +66,6 @@ public class CtyunSmsImpl extends AbstractSmsBlend {
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String phone, String message, String templateId) {
|
||||
AtomicReference<SmsResponse> smsResponse = new AtomicReference<>();
|
||||
String requestUrl;
|
||||
String paramStr;
|
||||
try {
|
||||
@ -76,13 +76,13 @@ public class CtyunSmsImpl extends AbstractSmsBlend {
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
}
|
||||
log.debug("requestUrl {}", requestUrl);
|
||||
http.post(requestUrl)
|
||||
.addHeader(CtyunUtils.signHeader(paramStr, ctyunConfig.getAccessKeyId(), ctyunConfig.getAccessKeySecret()))
|
||||
.addBody(paramStr)
|
||||
.onSuccess(((data, req, res) -> smsResponse.set(this.getResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> smsResponse.set(this.getResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
return smsResponse.get();
|
||||
try(HttpResponse response = HttpRequest.post(requestUrl)
|
||||
.addHeaders(CtyunUtils.signHeader(paramStr, ctyunConfig.getAccessKeyId(), ctyunConfig.getAccessKeySecret()))
|
||||
.body(paramStr)
|
||||
.execute()){
|
||||
JSONObject body = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(body);
|
||||
}
|
||||
}
|
||||
|
||||
private SmsResponse getResponse(JSONObject resJson) {
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package org.dromara.sms4j.emay.service;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
@ -16,7 +19,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author Richard
|
||||
@ -79,16 +81,16 @@ public class EmaySmsImpl extends AbstractSmsBlend {
|
||||
}
|
||||
|
||||
private SmsResponse getSendResponse(Map<String, Object> body, String requestUrl) {
|
||||
AtomicReference<SmsResponse> smsResponse = new AtomicReference<>();
|
||||
http.post(requestUrl)
|
||||
.addBody(body)
|
||||
.onSuccess(((data, req, res) -> smsResponse.set(this.getSmsResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> smsResponse.set(this.getSmsResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
return smsResponse.get();
|
||||
try(HttpResponse response = HttpRequest.post(requestUrl)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(JSONUtil.toJsonStr(body))
|
||||
.execute()){
|
||||
JSONObject res = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(res);
|
||||
}
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(JSONObject resJson) {
|
||||
private SmsResponse getResponse(JSONObject resJson) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
smsResponse.setSuccess("success".equalsIgnoreCase(resJson.getStr("code")));
|
||||
smsResponse.setData(resJson);
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package org.dromara.sms4j.huawei.service;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
@ -16,7 +19,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.dromara.sms4j.huawei.utils.HuaweiBuilder.listToString;
|
||||
|
||||
@ -40,7 +42,6 @@ public class HuaweiSmsImpl extends AbstractSmsBlend {
|
||||
@Override
|
||||
@Restricted
|
||||
public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||
AtomicReference<SmsResponse> reference = new AtomicReference<>();
|
||||
String url = config.getUrl() + Constant.HUAWEI_REQUEST_URL;
|
||||
List<String> list = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : messages.entrySet()) {
|
||||
@ -52,13 +53,13 @@ public class HuaweiSmsImpl extends AbstractSmsBlend {
|
||||
headers.put("Authorization", Constant.HUAWEI_AUTH_HEADER_VALUE);
|
||||
headers.put("X-WSSE", HuaweiBuilder.buildWsseHeader(config.getAppKey(), config.getAppSecret()));
|
||||
headers.put("Content-Type", Constant.FROM_URLENCODED);
|
||||
http.post(url)
|
||||
.addHeader(headers)
|
||||
.addBody(requestBody)
|
||||
.onSuccess(((data, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
return reference.get();
|
||||
try(HttpResponse response = HttpRequest.post(url)
|
||||
.addHeaders(headers)
|
||||
.body(requestBody)
|
||||
.execute()){
|
||||
JSONObject body = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(body);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -2,6 +2,7 @@ package org.dromara.sms4j.netease.config;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import org.dromara.sms4j.api.universal.SupplierConfig;
|
||||
@ -10,6 +11,7 @@ import org.dromara.sms4j.comm.config.BaseConfig;
|
||||
/**
|
||||
* @author adam
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
|
||||
@ -2,9 +2,11 @@ package org.dromara.sms4j.netease.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
@ -20,7 +22,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
@ -93,31 +94,32 @@ public class NeteaseSmsImpl extends AbstractSmsBlend {
|
||||
}
|
||||
|
||||
private SmsResponse getSmsResponse(String requestUrl, List<String> phones, String message, String templateId) {
|
||||
AtomicReference<SmsResponse> reference = new AtomicReference<>();
|
||||
String nonce = IdUtil.fastSimpleUUID();
|
||||
String curTime = String.valueOf(DateUtil.currentSeconds());
|
||||
String checkSum = NeteaseUtils.getCheckSum(config.getAccessKeySecret(), nonce, curTime);
|
||||
Map<String, Object> body = new LinkedHashMap<>(4);
|
||||
body.put("templateid", templateId);
|
||||
body.put("mobiles", JSONArray.parseArray(JSON.toJSONString(phones)).toJSONString());
|
||||
JSONArray jsonArray = JSONUtil.createArray();
|
||||
jsonArray.addAll(phones);
|
||||
body.put("mobiles", jsonArray.toString());
|
||||
body.put("params", message);
|
||||
body.put("needUp", config.getNeedUp());
|
||||
http.post(requestUrl)
|
||||
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||
.addHeader("AppKey", config.getAccessKeyId())
|
||||
.addHeader("Nonce", nonce)
|
||||
.addHeader("CurTime", curTime)
|
||||
.addHeader("CheckSum", checkSum)
|
||||
.addBody(body)
|
||||
.onSuccess(((data, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
return reference.get();
|
||||
try(HttpResponse response = HttpRequest.post(requestUrl)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.header("AppKey", config.getAccessKeyId())
|
||||
.header("Nonce", nonce)
|
||||
.header("CurTime", curTime)
|
||||
.header("CheckSum", checkSum)
|
||||
.body(JSONUtil.toJsonStr(body))
|
||||
.execute()){
|
||||
JSONObject res = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(res);
|
||||
}
|
||||
}
|
||||
|
||||
private SmsResponse getResponse(JSONObject jsonObject) {
|
||||
SmsResponse response = new SmsResponse();
|
||||
response.setSuccess(jsonObject.getInteger("code") <= 200);
|
||||
response.setSuccess(jsonObject.getInt("code") <= 200);
|
||||
response.setData(jsonObject);
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package org.dromara.sms4j.tencent.service;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.jdcloud.sdk.utils.StringUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.AbstractSmsBlend;
|
||||
@ -85,29 +88,26 @@ public class TencentSmsImpl extends AbstractSmsBlend {
|
||||
log.error("tencent send message error", e);
|
||||
throw new SmsBlendException(e.getMessage());
|
||||
}
|
||||
Map<String, Object> headsMap = TencentUtils.generateHeadsMap(signature, timestamp, tencentSmsConfig.getAction(),
|
||||
Map<String, String> headsMap = TencentUtils.generateHeadsMap(signature, timestamp, tencentSmsConfig.getAction(),
|
||||
tencentSmsConfig.getVersion(), tencentSmsConfig.getTerritory(), tencentSmsConfig.getRequestUrl());
|
||||
Map<String, Object> requestBody = TencentUtils.generateRequestBody(phones, tencentSmsConfig.getSdkAppId(),
|
||||
tencentSmsConfig.getSignature(), templateId, messages);
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
String url = Constant.HTTPS_PREFIX + tencentSmsConfig.getRequestUrl();
|
||||
http.post(url)
|
||||
.addHeader(headsMap)
|
||||
.addBody(requestBody)
|
||||
.onSuccess(((data, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
JSONObject response = jsonBody.getJSONObject("Response");
|
||||
String error = response.getStr("Error");
|
||||
smsResponse.setSuccess(StringUtils.isBlank(error));
|
||||
smsResponse.setData(jsonBody);
|
||||
}))
|
||||
.onError((ex, req, res) -> {
|
||||
JSONObject jsonBody = res.get(JSONObject.class);
|
||||
smsResponse.setSuccess(false);
|
||||
smsResponse.setData(jsonBody);
|
||||
})
|
||||
.execute();
|
||||
return smsResponse;
|
||||
try(HttpResponse response = HttpRequest.post(url)
|
||||
.addHeaders(headsMap)
|
||||
.body(JSONUtil.toJsonStr(requestBody))
|
||||
.execute()){
|
||||
JSONObject body = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(body);
|
||||
}
|
||||
}
|
||||
|
||||
private SmsResponse getResponse(JSONObject resJson) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
JSONObject response = resJson.getJSONObject("Response");
|
||||
String error = response.getStr("Error");
|
||||
smsResponse.setSuccess(StringUtils.isBlank(error));
|
||||
smsResponse.setData(resJson);
|
||||
return smsResponse;
|
||||
}
|
||||
}
|
||||
@ -92,9 +92,9 @@ public class TencentUtils {
|
||||
* @param territory 服务器地区
|
||||
* @param requestUrl 请求地址
|
||||
*/
|
||||
public static Map<String, Object> generateHeadsMap(String authorization, String timestamp, String action,
|
||||
public static Map<String, String> generateHeadsMap(String authorization, String timestamp, String action,
|
||||
String version, String territory, String requestUrl) {
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Authorization", authorization);
|
||||
headers.put("Content-Type", CT_JSON);
|
||||
headers.put("Host", requestUrl);
|
||||
|
||||
@ -3,10 +3,10 @@ package org.dromara.sms4j.unisms.core;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.crypto.digest.HMac;
|
||||
import cn.hutool.crypto.digest.HmacAlgorithm;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
@ -20,8 +20,6 @@ import java.util.UUID;
|
||||
public class UniClient {
|
||||
|
||||
public static final String USER_AGENT = "uni-java-sdk" + "/" + Uni.VERSION;
|
||||
private final ForestConfiguration http = BeanFactory.getForestConfiguration();
|
||||
|
||||
private final String accessKeyId;
|
||||
private final String accessKeySecret;
|
||||
private final String endpoint;
|
||||
@ -81,27 +79,18 @@ public class UniClient {
|
||||
* @author :Wind
|
||||
*/
|
||||
public UniResponse request(final String action, final Map<String, Object> data) throws SmsBlendException {
|
||||
Map<String, Object> query = new HashMap<String, Object>();
|
||||
query.put("action", action);
|
||||
query.put("accessKeyId", this.accessKeyId);
|
||||
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("User-Agent", USER_AGENT);
|
||||
headers.put("Content-Type", "application/json;charset=utf-8");
|
||||
headers.put("Accept", "application/json");
|
||||
String str = http.post(this.endpoint)
|
||||
.addHeader(headers)
|
||||
.addQuery(this.sign(query))
|
||||
.addBody(JSONUtil.toJsonStr(data))
|
||||
.successWhen(((req, res) -> {
|
||||
return res.noException() && // 请求过程没有异常
|
||||
res.statusIsNot(500); // 不能是 500
|
||||
}))
|
||||
.onError((ex, req, res) -> {
|
||||
throw new SmsBlendException(ex.getMessage());
|
||||
})
|
||||
.executeAsString();
|
||||
return new UniResponse(JSONUtil.parseObj(str));
|
||||
|
||||
String url = this.endpoint + "?action=" + action + "&accessKeyId=" + this.accessKeyId;
|
||||
try(HttpResponse response = HttpRequest.post(url)
|
||||
.addHeaders(headers)
|
||||
.body(JSONUtil.toJsonStr(data))
|
||||
.execute()){
|
||||
return new UniResponse(JSONUtil.parseObj(response.body()));
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package org.dromara.sms4j.yunpian.service;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.dromara.sms4j.api.AbstractSmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.comm.annotation.Restricted;
|
||||
@ -15,7 +18,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author wind
|
||||
@ -29,7 +31,7 @@ public class YunPianSmsImpl extends AbstractSmsBlend {
|
||||
|
||||
private final YunpianConfig config;
|
||||
|
||||
private static SmsResponse getSmsResponse(JSONObject execute) {
|
||||
private static SmsResponse getResponse(JSONObject execute) {
|
||||
SmsResponse smsResponse = new SmsResponse();
|
||||
if (execute == null) {
|
||||
smsResponse.setSuccess(false);
|
||||
@ -111,14 +113,12 @@ public class YunPianSmsImpl extends AbstractSmsBlend {
|
||||
|
||||
private SmsResponse getSendResponse(Map<String, String> body) {
|
||||
Map<String, String> headers = getHeaders();
|
||||
AtomicReference<SmsResponse> smsResponse = new AtomicReference<>();
|
||||
http.post(Constant.YUNPIAN_URL + "/sms/tpl_single_send.json")
|
||||
.addHeader(headers)
|
||||
.addBody(body)
|
||||
.onSuccess(((data, req, res) -> smsResponse.set(getSmsResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> smsResponse.set(getSmsResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
|
||||
return smsResponse.get();
|
||||
try(HttpResponse response = HttpRequest.post(Constant.YUNPIAN_URL + "/sms/tpl_single_send.json")
|
||||
.addHeaders(headers)
|
||||
.body(JSONUtil.toJsonStr(body))
|
||||
.execute()){
|
||||
JSONObject res = JSONUtil.parseObj(response.body());
|
||||
return getResponse(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,8 @@ import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.text.StrPool;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
@ -22,7 +24,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* <p>助通短信发送
|
||||
@ -107,7 +108,7 @@ public class ZhutongSmsImpl extends AbstractSmsBlend {
|
||||
}
|
||||
|
||||
|
||||
String urls = requestUrl + "v2/sendSms";
|
||||
String url = requestUrl + "v2/sendSms";
|
||||
long tKey = System.currentTimeMillis() / 1000;
|
||||
Map<String, String> json = new HashMap<>(5);
|
||||
//账号
|
||||
@ -121,15 +122,13 @@ public class ZhutongSmsImpl extends AbstractSmsBlend {
|
||||
//内容
|
||||
json.put("content", content);
|
||||
|
||||
AtomicReference<SmsResponse> reference = new AtomicReference<>();
|
||||
http.post(urls)
|
||||
.addHeader("Content-Type", Constant.APPLICATION_JSON_UTF8)
|
||||
.addBody(JSONUtil.toJsonStr(json))
|
||||
.onSuccess(((data, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
log.info("助通短信 URL={} json={} 响应值为={}", urls, json, reference.get());
|
||||
return reference.get();
|
||||
try(HttpResponse response = HttpRequest.post(url)
|
||||
.header("Content-Type", Constant.APPLICATION_JSON_UTF8)
|
||||
.body(JSONUtil.toJsonStr(json))
|
||||
.execute()){
|
||||
JSONObject body = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(body);
|
||||
}
|
||||
}
|
||||
|
||||
protected SmsResponse getSmsResponse(String mobile, String content) {
|
||||
@ -161,49 +160,47 @@ public class ZhutongSmsImpl extends AbstractSmsBlend {
|
||||
}
|
||||
|
||||
//地址
|
||||
String urls = requestUrl + "v2/sendSmsTp";
|
||||
String url = requestUrl + "v2/sendSmsTp";
|
||||
//请求入参
|
||||
JSONObject requestJson = new JSONObject();
|
||||
//账号
|
||||
requestJson.put("username", username);
|
||||
requestJson.set("username", username);
|
||||
//tKey
|
||||
long tKey = System.currentTimeMillis() / 1000;
|
||||
requestJson.put("tKey", tKey);
|
||||
requestJson.set("tKey", tKey);
|
||||
//明文密码
|
||||
requestJson.put("password", SecureUtil.md5(SecureUtil.md5(password) + tKey));
|
||||
requestJson.set("password", SecureUtil.md5(SecureUtil.md5(password) + tKey));
|
||||
//模板ID
|
||||
requestJson.put("tpId", templateId);
|
||||
requestJson.set("tpId", templateId);
|
||||
//签名
|
||||
requestJson.put("signature", signature);
|
||||
requestJson.set("signature", signature);
|
||||
//扩展号
|
||||
requestJson.put("ext", "");
|
||||
requestJson.set("ext", "");
|
||||
//自定义参数
|
||||
requestJson.put("extend", "");
|
||||
requestJson.set("extend", "");
|
||||
//发送记录集合
|
||||
JSONArray records = new JSONArray();
|
||||
{
|
||||
for (String mobile : phones) {
|
||||
JSONObject record = new JSONObject();
|
||||
//手机号
|
||||
record.put("mobile", mobile);
|
||||
record.set("mobile", mobile);
|
||||
//替换变量
|
||||
JSONObject param = new JSONObject();
|
||||
param.putAll(messages);
|
||||
record.put("tpContent", param);
|
||||
record.set("tpContent", param);
|
||||
records.add(record);
|
||||
}
|
||||
}
|
||||
requestJson.put("records", records);
|
||||
requestJson.set("records", records);
|
||||
|
||||
AtomicReference<SmsResponse> reference = new AtomicReference<>();
|
||||
http.post(urls)
|
||||
.addHeader("Content-Type", Constant.APPLICATION_JSON_UTF8)
|
||||
.addBody(requestJson)
|
||||
.onSuccess(((data, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class)))))
|
||||
.onError((ex, req, res) -> reference.set(this.getResponse(res.get(JSONObject.class))))
|
||||
.execute();
|
||||
log.info("助通短信 URL={} json={} 响应值为={}", urls, requestJson, reference.get());
|
||||
return reference.get();
|
||||
try(HttpResponse response = HttpRequest.post(url)
|
||||
.header("Content-Type", Constant.APPLICATION_JSON_UTF8)
|
||||
.body(requestJson.toString())
|
||||
.execute()){
|
||||
JSONObject body = JSONUtil.parseObj(response.body());
|
||||
return this.getResponse(body);
|
||||
}
|
||||
}
|
||||
|
||||
protected SmsResponse getSmsResponseTemplate(String templateId, String mobile, LinkedHashMap<String, String> content) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user