GyOpenApiUtil 更新
This commit is contained in:
parent
f1b034064b
commit
59b6e0e966
|
|
@ -0,0 +1,106 @@
|
|||
package tqq9.lc123.cloud.app.api.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import kd.bos.logging.Log;
|
||||
import kd.bos.logging.LogFactory;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class GyOpenApiUtil {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static String apiUrl = "http://v2.api.guanyierp.com/rest/erp_open";
|
||||
private static String appKey = "142232";
|
||||
private static String appSecret = "0140a81d7c814924a7148ff54c2dd859";
|
||||
private static String sessionKey = "1ac2c2eb69bc4626b13f2fa8255e728f";
|
||||
|
||||
private final static Log logger = LogFactory.getLog(GyOpenApiUtil.class);
|
||||
|
||||
/**
|
||||
* 生成管易云API签名
|
||||
*/
|
||||
public static String generateSign(Map<String, Object> params, String appSecret) {
|
||||
try {
|
||||
// 参数按字母顺序排序
|
||||
Map<String, Object> sortedParams = new TreeMap<>(params);
|
||||
// 转换为紧凑JSON
|
||||
String paramJson = objectMapper.writeValueAsString(sortedParams);
|
||||
// 生成签名:appSecret + JSON + appSecret,然后MD5加密并转为大写
|
||||
String signString = appSecret + paramJson + appSecret;
|
||||
return DigestUtils.md5Hex(signString).toUpperCase();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("签名生成失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建请求参数
|
||||
*/
|
||||
public static Map<String, Object> buildRequestParams(Map<String, Object> request) {
|
||||
request.put("session", sessionKey);
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
|
||||
params.putAll(request);
|
||||
|
||||
// 生成签名
|
||||
String sign = generateSign(params, appSecret);
|
||||
params.put("sign", sign);
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用管易云API
|
||||
*/
|
||||
public static String callGuanyiApi(Map<String, Object> params) {
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpPost httpPost = new HttpPost(apiUrl);
|
||||
|
||||
// 设置请求头
|
||||
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
|
||||
|
||||
// 设置请求体
|
||||
String jsonParams = objectMapper.writeValueAsString(params);
|
||||
logger.debug("管易云API请求参数: {}", jsonParams);
|
||||
|
||||
httpPost.setEntity(new StringEntity(jsonParams, "UTF-8"));
|
||||
|
||||
// 执行请求
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
String responseString = EntityUtils.toString(entity, "UTF-8");
|
||||
logger.debug("管易云API响应: {}", responseString);
|
||||
|
||||
if (response.getStatusLine().getStatusCode() != 200) {
|
||||
throw new RuntimeException("API调用失败,状态码: " + response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
return responseString;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("调用管易云API异常", e);
|
||||
throw new RuntimeException("API调用异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("method", "gy.erp.trade.get");
|
||||
|
||||
Map<String, Object> stringObjectMap = GyOpenApiUtil.buildRequestParams(params);
|
||||
String callGuanyiApi = GyOpenApiUtil.callGuanyiApi(stringObjectMap);
|
||||
System.out.println(callGuanyiApi);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue