feat:add request kingdee api

This commit is contained in:
zzs 2024-09-24 17:14:38 +08:00
parent 47dbfcf6f0
commit 0c1b537fbf
6 changed files with 201 additions and 5 deletions

View File

@ -55,5 +55,15 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.ensign</groupId>
<artifactId>ensign-spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,7 +1,20 @@
package com.ensign.crm.module.crm.controller.crm;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ensign.crm.module.crm.exception.AllKingdeeException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
@ -13,12 +26,14 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @Description: TODO
@ -32,7 +47,28 @@ import java.util.*;
@Validated
public class ProxyController {
private String targetAddr = "http://122.4.221.133:8022";
@Value("${ensign.kingdee.is-public}")
private Boolean isPublic;
@Value("${ensign.kingdee.test-public-end-point}")
private String testPublicEndPoint;
@Value("${ensign.kingdee.test-inner-end-point}")
private String testInnerEndPoint;
@Value("${ensign.kingdee.prod-public-end-point}")
private String prodPublicEndPoint;
@Value("${ensign.kingdee.prod-inner-end-point}")
private String prodInnerEndPoint;
@Value("${ensign.kingdee.env}")
private String env;
private final String redisKingdeeKey = "kingdee:accessToken";
@Resource
private RedisTemplate<String, String> redisTemplate;
@RequestMapping(value = "/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Operation(summary = "转发接口")
@ -41,7 +77,7 @@ public class ProxyController {
URI uri = new URI(request.getRequestURI());
String path = uri.getPath();
String query = request.getQueryString();
String target = targetAddr + path.replace("/crm-api/proxy/do", "");
String target = initBasePath() + path.replace("/crm-api/proxy/do", "");
if (query != null && !query.isEmpty() && !query.equals("null")) {
target = target + "?" + query;
}
@ -64,6 +100,19 @@ public class ProxyController {
}
delegate.getHeaders().addAll(headerName, arr);
}
try {
delegate.getHeaders().add("access_token", initAccessToken());
} catch (AllKingdeeException e) {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Map<String, Object> body = new HashMap<>();
body.put("status", false);
body.put("message", e.getMessage());
response.getWriter().println(JSONUtil.toJsonStr(body));
response.getWriter().flush();
return;
}
StreamUtils.copy(request.getInputStream(), delegate.getBody());
// 执行远程调用
ClientHttpResponse clientHttpResponse = delegate.execute();
@ -74,4 +123,117 @@ public class ProxyController {
}));
StreamUtils.copy(clientHttpResponse.getBody(), response.getOutputStream());
}
private String initAccessToken() throws IOException, AllKingdeeException {
String accessToken = redisTemplate.opsForValue().get(redisKingdeeKey);
if (accessToken != null && !accessToken.isEmpty()) {
return accessToken;
}
final String END_POINT = initBasePath() + "/ierp/api/getAppToken.do";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(END_POINT);
Map<String, String> body = new HashMap<>();
body.put("appId", "CRM");
body.put("appSecuret", "yx.CRM.tksecurit@240924");
body.put("tenantid", "yxzg-topview-dev");
body.put("accountId", "1878420900609526784");
body.put("language", "zh_CN");
HttpEntity reqEntity = new StringEntity(JSONUtil.toJsonStr(body), "utf-8");
post.setEntity(reqEntity);
post.setConfig(initRequestConfig());
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
String responseData = EntityUtils.toString(resEntity, "utf-8");
JSONObject jsonObject = JSONUtil.parseObj(responseData);
Boolean status = (Boolean) jsonObject.get("status");
String state = (String) jsonObject.get("state");
if (status && "success".equals(state)) {
JSONObject data = (JSONObject) jsonObject.get("data");
String appToken = (String) data.get("app_token");
if (appToken != null && !appToken.isEmpty()) {
return doGetAccessToken(appToken);
}
}
}
throw new AllKingdeeException("获取AppToken失败");
}
private String doGetAccessToken(String appToken) throws IOException, AllKingdeeException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(initBasePath() + "/ierp/api/login.do");
Map<String, String> body = new HashMap<>();
body.put("user", "COSMIC");
body.put("apptoken", appToken);
body.put("tenantid", "yxgz-prod");
body.put("accountId", "1878420900609526784");
body.put("language", "zh_CN");
body.put("usertype", "UserName");
HttpEntity reqEntity = new StringEntity(JSONUtil.toJsonStr(body), "utf-8");
post.setEntity(reqEntity);
post.setConfig(initRequestConfig());
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
String responseData = EntityUtils.toString(resEntity, "utf-8");
JSONObject jsonObject = JSONUtil.parseObj(responseData);
Boolean status = (Boolean) jsonObject.get("status");
String state = (String) jsonObject.get("state");
if (status && "success".equals(state)) {
JSONObject data = (JSONObject) jsonObject.get("data");
String accessToken = (String) data.get("access_token");
if (accessToken != null && !accessToken.isEmpty()) {
Long expireTime = (Long) data.get("expire_time");
redisTemplate.opsForValue().set(redisKingdeeKey, accessToken, expireTime - 3000, TimeUnit.MICROSECONDS);
return accessToken;
}
}
}
throw new AllKingdeeException("获取AccessToken失败");
}
public RequestConfig initRequestConfig() {
return RequestConfig.custom()
.setConnectTimeout(5000)//连接超时connectionTimeout-->指的是连接一个url的连接等待时间
.setSocketTimeout(5000)// 读取数据超时SocketTimeout-->指的是连接上一个url获取response的返回等待时间
.setConnectionRequestTimeout(5000)
.build();
}
public String initBasePath() {
if ("test".equals(env)) {
if (isPublic) {
return testPublicEndPoint;
} else {
return testInnerEndPoint;
}
}
if ("prod".equals(env)) {
if (isPublic) {
return prodPublicEndPoint;
} else {
return prodInnerEndPoint;
}
}
return "";
}
}

View File

@ -0,0 +1,16 @@
package com.ensign.crm.module.crm.exception;
/**
* @Description TODO
* @Date 2024/4/1 10:43
* @Created by ZZSLL
*/
public class AllKingdeeException extends Exception
{
public AllKingdeeException(String message)
{
super(message);
}
}

View File

@ -167,9 +167,10 @@ ensign:
pay:
order-notify-url: http://yunai.natapp1.cc/admin-api/pay/notify/order # 支付渠道的【支付】回调地址
refund-notify-url: http://yunai.natapp1.cc/admin-api/pay/notify/refund # 支付渠道的【退款】回调地址
demo: true # 开启演示模式
demo: false # 开启演示模式
tencent-lbs-key: TVDBZ-TDILD-4ON4B-PFDZA-RNLKH-VVF6E # QQ 地图的密钥 https://lbs.qq.com/service/staticV2/staticGuide/staticDoc
kingdee:
is-public: false
justauth:
enabled: true
type:

View File

@ -227,6 +227,8 @@ ensign:
miniprogram-state: developer # 跳转小程序类型:开发版为 “developer”体验版为 “trial”为正式版为 “formal”
tencent-lbs-key: TVDBZ-TDILD-4ON4B-PFDZA-RNLKH-VVF6E # QQ 地图的密钥 https://lbs.qq.com/service/staticV2/staticGuide/staticDoc
kingdee:
is-public: true
justauth:
enabled: true
type:

View File

@ -266,5 +266,10 @@ ensign:
kd100:
key: pLXUGAwK5305
customer: E77DF18BE109F454A5CD319E44BF5177
kingdee:
env: test
test-public-end-point: 'http://122.4.221.133:8022'
test-inner-end-point: 'http://10.64.112.152:8022'
prod-public-end-point: 'http://122.4.221.130:8022'
prod-inner-end-point: 'http://10.64.111.134:8022'
debug: false