yixindibang/measurement/src/main/java/com/ruoyi/webApi/ApiPostBack.java

101 lines
3.8 KiB
Java
Raw Normal View History

2025-06-17 09:33:30 +00:00
package com.ruoyi.webApi;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
2025-06-17 09:33:30 +00:00
import com.ruoyi.bill.domain.PoundBill;
import com.ruoyi.bill.service.IPoundBillService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.webApi.billHandlerUtil.BillHandler;
import com.ruoyi.webApi.billHandlerUtil.BillHandlers;
import com.ruoyi.webApi.requestbody.DynamicFormRequest;
2025-06-17 09:33:30 +00:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
2025-06-17 09:33:30 +00:00
/**
* 各个单据回传星空接口单据更新单据下推
* @author 16358
* @date 2025/6/16
*/
@Component("apiPostBack")
public class ApiPostBack {
@Autowired
private ISysConfigService sysConfigService;
@Autowired
2025-06-17 09:33:30 +00:00
private ApiTask apiTask;
@Autowired
2025-06-17 09:33:30 +00:00
private IPoundBillService poundBillService;
private static final ObjectMapper objectMapper = new ObjectMapper();
2025-06-17 09:33:30 +00:00
public String makePoundBillFormData(String id) throws IOException {
2025-06-17 09:33:30 +00:00
PoundBill poundBill = poundBillService.selectPoundBillById(id);
if (poundBill == null) throw new RuntimeException("磅单不存在!");
String url = sysConfigService.selectConfigByKey("OA_Url") +
"/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.save.common.kdsvc";
BillHandler handler = BillHandlers.getHandler(poundBill.getSrcblltype());
DynamicFormRequest request = handler.buildRequest(poundBill);
String jsonInputString;
try {
jsonInputString = objectMapper.writeValueAsString(request);
} catch (JsonProcessingException e) {
return "JSON序列化失败" + e.getMessage();
2025-06-17 09:33:30 +00:00
}
return sendPostRequest(url, jsonInputString);
}
private String sendPostRequest(String url, String jsonBody) throws IOException {
2025-06-17 09:33:30 +00:00
StringBuilder errorLog = new StringBuilder();
try {
URL apiUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
2025-06-17 09:33:30 +00:00
String token = apiTask.getToken();
if (token == null || token.isEmpty()) {
2025-06-17 09:33:30 +00:00
throw new RuntimeException("获取到的Token为空无法设置请求头。");
}
connection.setRequestProperty("kdservice-sessionid", token);
2025-06-17 09:33:30 +00:00
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8);
2025-06-17 09:33:30 +00:00
os.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
return response.toString();
2025-06-17 09:33:30 +00:00
}
} else {
return "请求失败,响应码:" + responseCode;
2025-06-17 09:33:30 +00:00
}
} catch (IOException e) {
return "回传数据失败!: " + e.getMessage();
2025-06-17 09:33:30 +00:00
}
}
2025-06-17 09:33:30 +00:00
}