From d089d552e5bdd65d994fd6865b60681a56b8e360 Mon Sep 17 00:00:00 2001 From: zoujiangtao Date: Wed, 18 Dec 2024 16:36:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=86=85=E5=AE=B9=EF=BC=9A?= =?UTF-8?q?=E8=8E=B7=E5=8F=96token=20=E5=A4=87=E6=B3=A8=EF=BC=9A=E8=8E=B7?= =?UTF-8?q?=E5=8F=96token=20=E6=8F=90=E4=BA=A4=E4=BA=BA=EF=BC=9A=E9=82=B9?= =?UTF-8?q?=E6=B1=9F=E6=B6=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shkd/sys/sys/mservice/ApiService.java | 91 ++++++++++++++++--- 1 file changed, 79 insertions(+), 12 deletions(-) diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java index d2f42c9..8a7324e 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java @@ -19,6 +19,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.math.BigDecimal; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -162,6 +163,12 @@ public class ApiService { new QFilter("shkd_sourcebill", QCP.equals, billMark).and( new QFilter("shkd_targetsystem", QCP.equals, systemName)).toArray()); + + // 获取token + String domainName = objects[0].getString("shkd_domainname"); + String token = ApiService.getBIPToken(domainName); + logger.info("获取token:{}", token); + String saveUrl = objects[0].getString("shkd_url"); // 组装请求体数据 @@ -181,7 +188,7 @@ public class ApiService { // 付款处理 case "https://biptestctny.invest.com.cn:8002/iuap-api-auth/yonbip/fi/fundpayment/save": // 保存 - saveResponseBody = pushBill(objects[0], saveUrl, saveRequestBody); + saveResponseBody = pushBill(token, saveUrl, saveRequestBody); logger.info("付款处理 → BIP保存接口 → 返回结果:{}", saveResponseBody); jsonObject = JSON.parseObject(saveResponseBody); code = jsonObject.getString("code"); @@ -196,19 +203,20 @@ public class ApiService { submitUrl = objects[0].getString("shkd_submiturl"); submitRequestBody = getSubmitRequestBody("cmp.fundpayment.FundPayment", "cmp_fundpayment", data.getString("id")); + logger.info("付款处理 →\n提交URL为:{}\n提交请求body:{}", submitUrl, JSON.toJSONString(submitRequestBody)); // 提交 - submitResponseBody = pushBill(objects[0], submitUrl, JSON.toJSONString(submitRequestBody)); + submitResponseBody = submitBill(token, submitUrl, JSON.toJSONString(submitRequestBody)); logger.info("付款处理 → BIP提交接口 → 返回结果:{}", submitResponseBody); result = "推送 → 付款处理单 → 成功"; } else { - logger.info("付款处理 → BIP保存接口 → 成功\n失败单据编号:{}\n推送失败接口返回数据:{}", dynamic.getString("billno"), saveResponseBody); + logger.info("付款处理 → BIP保存接口 → 失败\n失败单据编号:{}\n推送失败接口返回数据:{}", dynamic.getString("billno"), saveResponseBody); result = "推送 → 付款处理单 → 失败"; } break; // 收款处理 case "https://biptestctny.invest.com.cn:8002/iuap-api-auth/yonbip/fi/fundcollection/save": - saveResponseBody = pushBill(objects[0], saveUrl, saveRequestBody); + saveResponseBody = pushBill(token, saveUrl, saveRequestBody); logger.info("收款处理 → BIP保存接口 → 返回结果:{}", saveResponseBody); jsonObject = JSON.parseObject(saveResponseBody); code = jsonObject.getString("code"); @@ -224,7 +232,7 @@ public class ApiService { submitUrl = objects[0].getString("shkd_submiturl"); submitRequestBody = getSubmitRequestBody("cmp.fundcollection.FundCollection", "cmp_fundcollection", data.getString("id")); // 提交 - submitResponseBody = pushBill(objects[0], submitUrl, JSON.toJSONString(submitRequestBody)); + submitResponseBody = submitBill(token, submitUrl, JSON.toJSONString(submitRequestBody)); logger.info("收款处理 → BIP提交接口 → 返回结果:{}", submitResponseBody); result = "推送 → 收款处理 → 成功"; } else { @@ -238,7 +246,7 @@ public class ApiService { break; // 支付结果 case "https://biptestctny.invest.com.cn:8002/iuap-api-auth/yonbip/ctm/api/settlement/feedback": - saveResponseBody = pushBill(objects[0], saveUrl, saveRequestBody); + saveResponseBody = pushBill(token, saveUrl, saveRequestBody); jsonObject = JSON.parseObject(saveResponseBody); code = jsonObject.getString("code"); if ("200".equals(code)) { @@ -511,19 +519,15 @@ public class ApiService { /** * 推送单条数据 * - * @param dataEntity API映射数据对象 + * @param token token * @param linkUrl 请求URL * @param requestBody 请求体数据 * @return 推送结果 */ - private static String pushBill(DynamicObject dataEntity, String linkUrl, String requestBody) { + private static String pushBill(String token, String linkUrl, String requestBody) { // 响应数据 String formattedContent; try { - // 域名 - String domainName = dataEntity.getString("shkd_domainname"); - String token = ApiService.getBIPToken(domainName); - logger.info("获取token:{}", token); // 请求URL URL url = new URL(linkUrl + "?access_token=" + token); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); @@ -574,6 +578,69 @@ public class ApiService { return formattedContent; } + + /** + * 提交接口调试 + * + * @param token token + * @param linkUrl 提交接口url + * @param requestBody 请求体数据 + * @return 调用接口返回信息 + */ + private static String submitBill(String token, String linkUrl, String requestBody) { + try { + // 请求URL + URL url = new URL(linkUrl + "?access_token=" + token); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + // 设置请求方法为POST + connection.setRequestMethod("POST"); + + // 设置请求头 + connection.setRequestProperty("Content-Type", "application/json"); + + // 允许输出 + connection.setDoOutput(true); + + // 写入请求体 + try (OutputStream os = connection.getOutputStream()) { + byte[] input = requestBody.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + + // 获取响应码 + int responseCode = connection.getResponseCode(); + + // 读取响应内容 + if (responseCode == HttpURLConnection.HTTP_OK) { // 成功响应 + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); + String inputLine; + StringBuilder content = new StringBuilder(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + // 替换 \n 为实际的换行符 + logger.info("提交接口调用成功,响应成功信息:{}", content); + return "提交接口 → 响应成功: " + content.toString(); + } else { + // 读取错误流 + BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)); + String errorLine; + StringBuilder errorContent = new StringBuilder(); + while ((errorLine = errorReader.readLine()) != null) { + errorContent.append(errorLine); + } + errorReader.close(); + logger.info("提交接口调用失败,响应失败信息:{}", errorReader); + return "提交接口 → 响应失败: " + errorContent; + } + } catch (Exception e) { + logger.info("请求失败,失败信息:{}", e); + return "提交接口 → 请求失败," + e.getMessage(); + } + } + public static Map getSubmitRequestBody(String fullname, String billnum, String id) { Map data = new HashMap<>(); Map innerData = new HashMap<>();