丽知:新增推送u8c工具类(pk/code)。

This commit is contained in:
zhengyf 2024-08-07 14:35:36 +08:00
parent 83e3f2ba30
commit 06d9000c40
3 changed files with 256 additions and 0 deletions

View File

@ -0,0 +1,93 @@
package com.hzya.frame.plugin.lets.constant;
import java.util.HashMap;
import java.util.Map;
/**
* lets全局静态参数配置
*
*/
public class OverallConstant {
private static final Map<String, String> devOverAll = new HashMap<>();
private static final Map<String, String> testOverAll = new HashMap<>();
private static final Map<String, String> prodOverAll = new HashMap<>();
private static final Map<String, String> prodOverPublic = new HashMap<>();
static {
//dev
loadDev();
//prod
loadProd();
//公共部分
loadPublic();
}
private static void loadPublic() {
//存货分类
prodOverPublic.put("bdinvclSave", "/u8cloud/api/uapbd/bdinvcl/save");//存货分类新增
prodOverPublic.put("bdinvclUpdate", "/u8cloud/api/uapbd/bdinvcl/update");//存货分类修改
prodOverPublic.put("bdinvclDelete", "/u8cloud/api/uapbd/bdinvcl/delete");//存货分类删除
//存货基本档案
/**
* * 存货基本档案新增 /u8cloud/api/uapbd/invbasdoc/insert
* * 存货基本档案修改 /u8cloud/api/uapbd/invbasdoc/update
* * 存货基本档案封存 /u8cloud/api/uapbd/invbasdoc/seal
* * 存货基本档案取消封存 /u8cloud/api/uapbd/invbasdoc/unseal
*/
prodOverPublic.put("bdinvclSave", "/u8cloud/api/uapbd/bdinvcl/save");//存货分类新增
}
private static void loadDev() {
devOverAll.put("u8cApiUsercodePK", "admin");
devOverAll.put("u8cApiPasswordPK", "21232f297a57a5a743894a0e4a801fc3");
devOverAll.put("u8cApiTrantypePK", "pk");
devOverAll.put("u8cApiSystemPK", "lz");
devOverAll.put("u8cApiServiceNamePK", "http://39.170.109.90:9099");//测试U8C
devOverAll.put("u8cApiNeedStackTracePK", "Y");
devOverAll.put("u8cApiZdrPK", "0001A210000000000GVS");//单据制单人---测试环境郑一凡
devOverAll.put("u8cApiUsercodeCode", "admin1");
devOverAll.put("u8cApiPasswordCode", "e00cf25ad42683b3df678c61f42c6bda");
devOverAll.put("u8cApiTrantypeCode", "code");
devOverAll.put("u8cApiSystemCode", "lz1");
devOverAll.put("u8cApiServiceNameCode", "http://39.170.109.90:9099");//测试U8C
devOverAll.put("u8cApiNeedStackTraceCode", "Y");
devOverAll.put("u8cApiZdrCode", "15932295350");//单据制单人---测试环境郑一凡
}
private static void loadProd() {
}
/**
* 根据key获取全局变量值
*
* @param key key值
* @return 返回String
*/
public static String getOverAllValue(String key) {
String value = null;
if (ProfilesActiveConstant.LETS_PROFILES_ACTIVE.equals("dev")) {
value = devOverAll.get(key);
} else if (ProfilesActiveConstant.LETS_PROFILES_ACTIVE.equals("test")) {
value = testOverAll.get(key);
} else if (ProfilesActiveConstant.LETS_PROFILES_ACTIVE.equals("prod")) {
value = prodOverAll.get(key);
}
if (value == null) {
value = prodOverPublic.get(key);
}
return value;
}
}

View File

@ -0,0 +1,26 @@
package com.hzya.frame.plugin.lets.constant;
/**
* lets项目公共参数
*
*/
public class ProfilesActiveConstant {
public static final String LETS_PROFILES_ACTIVE = "dev";
public static final String LOG_STATUS_Y = "Y";
public static final String LOG_STATUS_N = "N";
public static final String LOG_STATUS_Y_H = "'Y','H'";
public static final String TYPE_DATE = "date";
public static final String TYPE_VBILLCODE = "vbillcode";
public static final String TYPE_DETAIL_ERROR = "details_error";
public static final String TYPE_TIME_FRAME = "time_frame";
public static final String TYPE_OTHER = "other";
}

View File

@ -0,0 +1,137 @@
package com.hzya.frame.plugin.lets.util;
import cn.hutool.core.lang.Assert;
import com.hzya.frame.plugin.lets.constant.OverallConstant;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
/**
* U8C推送工具类
*/
@Component
public class PushDataByU8cUtil {
/**
* 发起post请求传递json类型
*
* @param url 地址
* @param data 请求参数
* @return 返回消息
*/
public String pushU8CByPK(String url, String data) throws Exception {
Assert.notNull(url, "请求地址不能为空");
Assert.notNull(data, "请求参数不能为空(需要JSON格式)");
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
try {
// 设置连接超时和读取超时
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(300000) // 连接超时时间为60秒
.setSocketTimeout(300000) // 读取超时时间为60秒
.build();
HttpPost post = new HttpPost(OverallConstant.getOverAllValue("u8cApiServiceNamePK") + url);
post.setHeader("Content-Type", "application/json;charset=UTF-8");
post.setHeader("usercode", OverallConstant.getOverAllValue("u8cApiUsercodePK"));
post.setHeader("password", OverallConstant.getOverAllValue("u8cApiPasswordPK"));
post.setHeader("trantype", OverallConstant.getOverAllValue("u8cApiTrantypePK"));
post.setHeader("system", OverallConstant.getOverAllValue("u8cApiSystemPK"));
post.setHeader("needStackTrace", OverallConstant.getOverAllValue("u8cApiNeedStackTracePK"));
post.setConfig(requestConfig);
//post.setHeader("charset", "utf-8");
ByteArrayEntity entity = new ByteArrayEntity(data.getBytes("UTF-8"));
entity.setContentType("application/json");
post.setEntity(entity);
HttpResponse response = closeableHttpClient.execute(post);
// 处理返回结果乱码问题
if (response != null && response.getEntity() != null) {
String resultStr = EntityUtils.toString(response.getEntity(), "UTF-8");
return resultStr;
}
} catch (Exception e) {
e.printStackTrace();
Assert.state(false, "拉取/推送失败(pushU8CByPK),失败原因:{} 请求地址:{}", e.getMessage(), url);
} finally {
try {
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 发起post请求传递json类型
*
* u8c code模式
*
* @param url 地址
* @param data 请求参数
* @return 返回消息
*/
public String pushU8CByCode(String url, String data) throws Exception {
Assert.notNull(url, "请求地址不能为空");
Assert.notNull(data, "请求参数不能为空(需要JSON格式)");
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
try {
// 设置连接超时和读取超时
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(300000) // 连接超时时间为60秒
.setSocketTimeout(300000) // 读取超时时间为60秒
.build();
HttpPost post = new HttpPost(OverallConstant.getOverAllValue("u8cApiServiceNameCode") + url);
post.setHeader("Content-Type", "application/json;charset=UTF-8");
post.setHeader("usercode", OverallConstant.getOverAllValue("u8cApiUsercodeCode"));
post.setHeader("password", OverallConstant.getOverAllValue("u8cApiPasswordCode"));
post.setHeader("trantype", OverallConstant.getOverAllValue("u8cApiTrantypeCode"));
post.setHeader("system", OverallConstant.getOverAllValue("u8cApiSystemCode"));
post.setHeader("needStackTrace", OverallConstant.getOverAllValue("u8cApiNeedStackTraceCode"));
post.setConfig(requestConfig);
//post.setHeader("charset", "utf-8");
ByteArrayEntity entity = new ByteArrayEntity(data.getBytes("UTF-8"));
entity.setContentType("application/json");
post.setEntity(entity);
HttpResponse response = closeableHttpClient.execute(post);
// 处理返回结果乱码问题
if (response != null && response.getEntity() != null) {
String resultStr = EntityUtils.toString(response.getEntity(), "UTF-8");
return resultStr;
}
} catch (Exception e) {
e.printStackTrace();
Assert.state(false, "拉取/推送失败(pushU8CByCode),失败原因:{} 请求地址:{}", e.getMessage(), url);
} finally {
try {
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}