101 lines
3.8 KiB
Java
101 lines
3.8 KiB
Java
package com.ruoyi.webApi;
|
||
|
||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
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;
|
||
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;
|
||
|
||
/**
|
||
* 各个单据回传星空接口(单据更新,单据下推)
|
||
* @author 16358
|
||
* @date 2025/6/16
|
||
*/
|
||
@Component("apiPostBack")
|
||
public class ApiPostBack {
|
||
@Autowired
|
||
private ISysConfigService sysConfigService;
|
||
|
||
@Autowired
|
||
private ApiTask apiTask;
|
||
|
||
@Autowired
|
||
private IPoundBillService poundBillService;
|
||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||
|
||
public String makePoundBillFormData(String id) throws IOException {
|
||
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();
|
||
}
|
||
|
||
return sendPostRequest(url, jsonInputString);
|
||
}
|
||
|
||
private String sendPostRequest(String url, String jsonBody) throws IOException {
|
||
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");
|
||
|
||
String token = apiTask.getToken();
|
||
if (token == null || token.isEmpty()) {
|
||
throw new RuntimeException("获取到的Token为空,无法设置请求头。");
|
||
}
|
||
connection.setRequestProperty("kdservice-sessionid", token);
|
||
connection.setDoOutput(true);
|
||
|
||
try (OutputStream os = connection.getOutputStream()) {
|
||
byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8);
|
||
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();
|
||
}
|
||
} else {
|
||
return "请求失败,响应码:" + responseCode;
|
||
}
|
||
} catch (IOException e) {
|
||
return "回传数据失败!: " + e.getMessage();
|
||
}
|
||
}
|
||
|
||
|
||
} |