|
|
|
@ -0,0 +1,222 @@
|
|
|
|
|
package org.dromara.api.utils;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
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.api.domain.vo.SysErpConfigVo;
|
|
|
|
|
import org.dromara.common.core.exception.ServiceException;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ERP接口工具类
|
|
|
|
|
*
|
|
|
|
|
* @Author YinQ
|
|
|
|
|
* @create 2024-03-27 10:52
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class ErpApiUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ERP类型常量
|
|
|
|
|
*/
|
|
|
|
|
public static final String ERP_TYPE_K3 = "0"; // K3
|
|
|
|
|
public static final String ERP_TYPE_U8 = "1"; // U8
|
|
|
|
|
public static final String ERP_TYPE_SAP = "2"; // SAP
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送HTTP请求
|
|
|
|
|
*
|
|
|
|
|
* @param url 请求地址
|
|
|
|
|
* @param method 请求方法
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
* @param params 请求参数
|
|
|
|
|
* @return 响应结果
|
|
|
|
|
*/
|
|
|
|
|
public static String sendHttpRequest(String url, String method, Map<String, String> headers, Map<String, Object> params) {
|
|
|
|
|
try {
|
|
|
|
|
HttpRequest request = null;
|
|
|
|
|
switch (method.toUpperCase()) {
|
|
|
|
|
case "GET":
|
|
|
|
|
request = HttpRequest.get(url);
|
|
|
|
|
break;
|
|
|
|
|
case "POST":
|
|
|
|
|
request = HttpRequest.post(url);
|
|
|
|
|
break;
|
|
|
|
|
case "PUT":
|
|
|
|
|
request = HttpRequest.put(url);
|
|
|
|
|
break;
|
|
|
|
|
case "DELETE":
|
|
|
|
|
request = HttpRequest.delete(url);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ServiceException("不支持的请求方法:" + method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置请求头
|
|
|
|
|
if (headers != null) {
|
|
|
|
|
headers.forEach(request::header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置请求参数
|
|
|
|
|
if (params != null) {
|
|
|
|
|
if ("GET".equalsIgnoreCase(method)) {
|
|
|
|
|
params.forEach(request::form);
|
|
|
|
|
} else {
|
|
|
|
|
request.body(JSONUtil.toJsonStr(params));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
HttpResponse response = request.execute();
|
|
|
|
|
|
|
|
|
|
// 检查响应状态
|
|
|
|
|
if (!response.isOk()) {
|
|
|
|
|
throw new ServiceException("HTTP请求失败,状态码:" + response.getStatus());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.body();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("HTTP请求异常", e);
|
|
|
|
|
throw new ServiceException("HTTP请求异常:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取ERP Token
|
|
|
|
|
*
|
|
|
|
|
* @param config ERP配置信息
|
|
|
|
|
* @return Token信息
|
|
|
|
|
*/
|
|
|
|
|
public static String getErpToken(SysErpConfigVo config) {
|
|
|
|
|
if (config == null) {
|
|
|
|
|
throw new ServiceException("ERP配置信息不能为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String erpType = config.getErpType();
|
|
|
|
|
if (StringUtils.isEmpty(erpType)) {
|
|
|
|
|
throw new ServiceException("ERP类型不能为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (erpType) {
|
|
|
|
|
case ERP_TYPE_K3:
|
|
|
|
|
return getK3Token(config);
|
|
|
|
|
case ERP_TYPE_U8:
|
|
|
|
|
return getU8Token(config);
|
|
|
|
|
case ERP_TYPE_SAP:
|
|
|
|
|
return getSapToken(config);
|
|
|
|
|
default:
|
|
|
|
|
throw new ServiceException("不支持的ERP类型:" + erpType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取K3 Token
|
|
|
|
|
*
|
|
|
|
|
* @param config ERP配置信息
|
|
|
|
|
* @return Token信息
|
|
|
|
|
*/
|
|
|
|
|
private static String getK3Token(SysErpConfigVo config) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取U8 Token
|
|
|
|
|
*
|
|
|
|
|
* @param config ERP配置信息
|
|
|
|
|
* @return Token信息
|
|
|
|
|
*/
|
|
|
|
|
private static String getU8Token(SysErpConfigVo config) {
|
|
|
|
|
// 构建请求参数
|
|
|
|
|
Map<String, Object> params = Map.of(
|
|
|
|
|
"app_key", config.getAppKey(),
|
|
|
|
|
"app_secret", config.getAppSecret()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 发送请求获取token
|
|
|
|
|
String response = sendHttpRequest(
|
|
|
|
|
config.getTokenUrl(),
|
|
|
|
|
"GET",
|
|
|
|
|
null,
|
|
|
|
|
params
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 解析响应结果
|
|
|
|
|
JSONObject result = JSONUtil.parseObj(response);
|
|
|
|
|
|
|
|
|
|
// 检查错误码
|
|
|
|
|
String errcode = result.getStr("errcode");
|
|
|
|
|
if (!"0".equals(errcode)) {
|
|
|
|
|
String errmsg = result.getStr("errmsg");
|
|
|
|
|
throw new ServiceException("获取U8 Token失败:" + errmsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取token信息
|
|
|
|
|
JSONObject tokenInfo = result.getJSONObject("token");
|
|
|
|
|
if (tokenInfo == null) {
|
|
|
|
|
throw new ServiceException("获取U8 Token失败:返回数据格式错误");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tokenInfo.getStr("id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取SAP Token
|
|
|
|
|
*
|
|
|
|
|
* @param config ERP配置信息
|
|
|
|
|
* @return Token信息
|
|
|
|
|
*/
|
|
|
|
|
private static String getSapToken(SysErpConfigVo config) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 调用ERP接口
|
|
|
|
|
*
|
|
|
|
|
* @param config ERP配置信息
|
|
|
|
|
* @param token 访问令牌
|
|
|
|
|
* @param apiUrl 接口地址
|
|
|
|
|
* @param method 请求方法
|
|
|
|
|
* @param params 请求参数
|
|
|
|
|
* @return 响应结果
|
|
|
|
|
*/
|
|
|
|
|
public static String callErpApi(SysErpConfigVo config, String token, String apiUrl, String method, Map<String, Object> params) {
|
|
|
|
|
if (config == null) {
|
|
|
|
|
throw new ServiceException("ERP配置信息不能为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(token)) {
|
|
|
|
|
throw new ServiceException("访问令牌不能为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建完整URL
|
|
|
|
|
String fullUrl = StrUtil.format("{}{}", config.getServerUrl(), apiUrl);
|
|
|
|
|
|
|
|
|
|
// 构建请求头
|
|
|
|
|
Map<String, String> headers = Map.of(
|
|
|
|
|
"Authorization", "Bearer " + token,
|
|
|
|
|
"Content-Type", "application/json"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
String response = sendHttpRequest(fullUrl, method, headers, params);
|
|
|
|
|
|
|
|
|
|
// 解析响应结果
|
|
|
|
|
JSONObject result = JSONUtil.parseObj(response);
|
|
|
|
|
|
|
|
|
|
// 检查错误码
|
|
|
|
|
String errcode = result.getStr("errcode");
|
|
|
|
|
if (!"0".equals(errcode)) {
|
|
|
|
|
String errmsg = result.getStr("errmsg");
|
|
|
|
|
throw new ServiceException("调用ERP接口失败:" + errmsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|