提交内容:获取token

备注:获取token
提交人:邹江涛
This commit is contained in:
zoujiangtao 2024-12-18 16:36:56 +08:00
parent 74c07044eb
commit d089d552e5
1 changed files with 79 additions and 12 deletions

View File

@ -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<String, Object> getSubmitRequestBody(String fullname, String billnum, String id) {
Map<String, Object> data = new HashMap<>();
Map<String, String> innerData = new HashMap<>();