From 0c1b537fbf4611064a4136df69f2d061daebb04a Mon Sep 17 00:00:00 2001 From: zzs Date: Tue, 24 Sep 2024 17:14:38 +0800 Subject: [PATCH] feat:add request kingdee api --- .../ensign-module-crm-biz/pom.xml | 10 ++ .../crm/controller/crm/ProxyController.java | 166 +++++++++++++++++- .../crm/exception/AllKingdeeException.java | 16 ++ .../src/main/resources/application-dev.yaml | 5 +- .../src/main/resources/application-local.yaml | 2 + .../src/main/resources/application.yaml | 7 +- 6 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/exception/AllKingdeeException.java diff --git a/ensign-module-crm/ensign-module-crm-biz/pom.xml b/ensign-module-crm/ensign-module-crm-biz/pom.xml index bc981b8..5d565f3 100644 --- a/ensign-module-crm/ensign-module-crm-biz/pom.xml +++ b/ensign-module-crm/ensign-module-crm-biz/pom.xml @@ -55,5 +55,15 @@ ${revision} + + com.ensign + ensign-spring-boot-starter-redis + + + + org.apache.httpcomponents + httpclient + + diff --git a/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/ProxyController.java b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/ProxyController.java index 07faa6a..117300d 100644 --- a/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/ProxyController.java +++ b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/ProxyController.java @@ -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 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 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 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 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 ""; + } } diff --git a/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/exception/AllKingdeeException.java b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/exception/AllKingdeeException.java new file mode 100644 index 0000000..b5e27d7 --- /dev/null +++ b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/exception/AllKingdeeException.java @@ -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); + } +} diff --git a/ensign-server/src/main/resources/application-dev.yaml b/ensign-server/src/main/resources/application-dev.yaml index 5903faa..c357022 100644 --- a/ensign-server/src/main/resources/application-dev.yaml +++ b/ensign-server/src/main/resources/application-dev.yaml @@ -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: diff --git a/ensign-server/src/main/resources/application-local.yaml b/ensign-server/src/main/resources/application-local.yaml index 15f01c5..c3dceda 100644 --- a/ensign-server/src/main/resources/application-local.yaml +++ b/ensign-server/src/main/resources/application-local.yaml @@ -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: diff --git a/ensign-server/src/main/resources/application.yaml b/ensign-server/src/main/resources/application.yaml index 1b71c9e..1185acd 100644 --- a/ensign-server/src/main/resources/application.yaml +++ b/ensign-server/src/main/resources/application.yaml @@ -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 \ No newline at end of file