通过post请求获取发票下载链接实现打印发票-龚宇杰
This commit is contained in:
parent
c2387a2793
commit
47e773b52f
|
@ -1,5 +1,9 @@
|
|||
package zf47.jdgz1.fi.em.plugin.print;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import kd.bos.dataentity.entity.DynamicObject;
|
||||
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
||||
import kd.bos.fileservice.FileItem;
|
||||
|
@ -18,8 +22,17 @@ import kd.bos.service.attachment.FilePathService;
|
|||
import kd.bos.servicehelper.AttachmentServiceHelper;
|
||||
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
||||
import kd.bos.url.UrlService;
|
||||
import kd.fi.er.business.invoicecloud.cache.model.InvoiceCloudCfgBO;
|
||||
import kd.fi.er.business.invoicecloud.kingdee.KingdeeInvoiceCloudConfig;
|
||||
import kd.fi.er.common.utils.MD5;
|
||||
import kd.sdk.plugin.Plugin;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.ImageType;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
|
@ -30,10 +43,7 @@ import javax.imageio.ImageIO;
|
|||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.AffineTransformOp;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
|
@ -64,6 +74,7 @@ public class TripReimbursePrintPlugin extends AbstractPrintPlugin implements Plu
|
|||
log.info(evt.getDataSource().getDsName());
|
||||
//拿源单类型
|
||||
Object pkId = evt.getDataSource().getPkId();
|
||||
DynamicObject tripReimburse = BusinessDataServiceHelper.loadSingle(pkId, MAIN_DS, "costcompany");
|
||||
DynamicObject bill = BusinessDataServiceHelper.loadSingle(pkId, MAIN_DS);
|
||||
if (bill != null) {
|
||||
long billId = bill.getLong("id");
|
||||
|
@ -72,13 +83,12 @@ public class TripReimbursePrintPlugin extends AbstractPrintPlugin implements Plu
|
|||
if (srcInvEntries != null && !srcInvEntries.isEmpty()) {
|
||||
for (DynamicObject srcInvEntry : srcInvEntries) {
|
||||
String serialNo = srcInvEntry.getString("serialno");//发票序列号
|
||||
QFilter qInvFile = new QFilter("serial_no", QCP.equals, serialNo);
|
||||
DynamicObject[] invFiles = BusinessDataServiceHelper.load("rim_invoice_file",
|
||||
"image_url", new QFilter[]{qInvFile});
|
||||
for (DynamicObject invFile : invFiles) {
|
||||
String url = invFile.getString("image_url");
|
||||
if (StringUtils.isNotBlank(url)) {
|
||||
ArrayList<String> handleUrls = handlePicAttUrl(url);
|
||||
Long companyId = tripReimburse.getDynamicObject("costcompany").getLong("id");
|
||||
HashMap<String, String> result = null;
|
||||
try {
|
||||
result = getInvoiceDownloadUrlByPost(serialNo, companyId);
|
||||
if (!result.isEmpty()) {
|
||||
ArrayList<String> handleUrls = InvoiceToPic(result);
|
||||
for (String handleUrl : handleUrls) {
|
||||
DataRowSet dataRowSet = new DataRowSet();
|
||||
ImageField imageField = new ImageField(handleUrl);
|
||||
|
@ -86,6 +96,8 @@ public class TripReimbursePrintPlugin extends AbstractPrintPlugin implements Plu
|
|||
customDataRows.add(dataRowSet);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -143,6 +155,159 @@ public class TripReimbursePrintPlugin extends AbstractPrintPlugin implements Plu
|
|||
}
|
||||
}
|
||||
|
||||
private HashMap<String, String> getInvoiceDownloadUrlByPost(String serialNo, Long companyId) throws IOException {
|
||||
HashMap<String, String> result = new HashMap<>();
|
||||
QFilter urlQF = new QFilter("number", QCP.equals, "GetInvoiceDetailDataUrl");
|
||||
DynamicObject urlParam = BusinessDataServiceHelper.loadSingle("zf47_system_params", "zf47_val", urlQF.toArray());
|
||||
if (urlParam == null) {
|
||||
log.warn("TripReimbursePrintPlugin:GetInvoiceDetailDataUrl参数未配置");
|
||||
} else {
|
||||
String url = urlParam.getString("zf47_val");
|
||||
QFilter headerQF = new QFilter("number", QCP.equals, "GetInvoiceDetailDataHeader");
|
||||
DynamicObject headerParam = BusinessDataServiceHelper.loadSingle("zf47_system_params", "zf47_val", headerQF.toArray());
|
||||
if (headerParam != null) {
|
||||
HashMap<String, String> header = new HashMap<>();
|
||||
String val = headerParam.getString("zf47_val");
|
||||
if (val.contains(";")) {
|
||||
String[] splits = val.split(";");
|
||||
for (String split : splits) {
|
||||
String[] splits1 = split.split(",");
|
||||
header.put(splits1[0], splits1[1]);
|
||||
}
|
||||
} else {
|
||||
String[] splits = val.split(",");
|
||||
header.put(splits[0], splits[1]);
|
||||
}
|
||||
|
||||
HashMap<String, Object> body = new HashMap<>();
|
||||
InvoiceCloudCfgBO config = KingdeeInvoiceCloudConfig.getConfig(companyId);
|
||||
String client_id = config.getClientId();
|
||||
String client_secret = config.getClientSecret();
|
||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||
body.put("timestamp", timestamp);
|
||||
body.put("sign", MD5.md5crypt(client_id + client_secret + timestamp));
|
||||
body.put("client_id", client_id);
|
||||
body.put("fid", serialNo);
|
||||
|
||||
JSONObject obj = (JSONObject) JSONObject.parse(sendPost(url, header, body));
|
||||
JSONArray data = (JSONArray) obj.get("data");
|
||||
JSONObject invoice = (JSONObject) data.get(0);
|
||||
result.put("downloadUrl", (String) invoice.get("downloadUrl"));
|
||||
result.put("originalFileName", (String) invoice.get("originalFileName"));
|
||||
} else {
|
||||
log.warn("TripReimbursePrintPlugin:GetInvoiceDetailDataHeader参数未配置");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String sendPost(String url, HashMap header, HashMap body) throws IOException {
|
||||
String responseData = "";
|
||||
// 准备要发送的 JSON 数据
|
||||
// 使用 FastJSON 将 Map 转换为 JSON 字符串
|
||||
String jsonString = JSON.toJSONString(body, SerializerFeature.DisableCircularReferenceDetect);
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
|
||||
// 创建 HttpPost 请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
// 设置请求头
|
||||
// httpPost.setHeader("Content-Type", "application/json");
|
||||
for (Object o : header.keySet()) {
|
||||
httpPost.setHeader((String) o, (String) header.get(o));
|
||||
}
|
||||
// 设置请求体
|
||||
StringEntity jsonEntity = new StringEntity(jsonString, "UTF-8");
|
||||
httpPost.setEntity(jsonEntity);
|
||||
|
||||
// 发送请求并获取响应
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
|
||||
// 检查响应状态
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
// 获取响应数据
|
||||
responseData = EntityUtils.toString(response.getEntity());
|
||||
log.info("TripReimbursePrintPlugin,Response: " + responseData);
|
||||
} else {
|
||||
log.info("TripReimbursePrintPlugin,Error: " + response.getStatusLine().getStatusCode() + "," + jsonEntity);
|
||||
}
|
||||
return responseData;
|
||||
}
|
||||
|
||||
private ArrayList<String> InvoiceToPic(HashMap<String, String> result) {
|
||||
ArrayList<String> resUrls = new ArrayList<>();
|
||||
try {
|
||||
FileService fs = FileServiceFactory.getAttachmentFileService();
|
||||
String uploadPrefix = "/invoiceApi/tripreimburse/image/";
|
||||
String file = result.get("originalFileName");
|
||||
String url = result.get("downloadUrl");
|
||||
|
||||
String newFileName = "";
|
||||
int index = file.lastIndexOf(".");
|
||||
String fileType = file.substring(index + 1);
|
||||
String fileName = URLEncoder.encode(file.substring(0, index), "utf-8");
|
||||
URL Url = new URL(url);
|
||||
HttpURLConnection conn = (HttpURLConnection) Url.openConnection();
|
||||
InputStream inputStream = conn.getInputStream();
|
||||
if (inputStream.available() > 0) {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
if ("pdf".equals(fileType)) {
|
||||
PDDocument document = PDDocument.load(inputStream);
|
||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||
for (int page = 0; page < document.getNumberOfPages(); ++page) {
|
||||
newFileName = fileName + "-" + page + ".jpg";
|
||||
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 1000, ImageType.RGB);
|
||||
ImageIO.write(bim, "jpg", outputStream);
|
||||
inputStream.close();
|
||||
}
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||
FileItem fileItem = new FileItem(newFileName, uploadPrefix + newFileName, byteArrayInputStream);
|
||||
String upload = fs.upload(fileItem);
|
||||
deleteUrl.add(upload);
|
||||
String uploadUrl = UrlService.getAttachmentDownloadUrl(upload);
|
||||
String uploadEncreptURL = AttachmentServiceHelper.getEncreptURL(uploadUrl);
|
||||
byteArrayInputStream.close();
|
||||
resUrls.add(uploadEncreptURL);
|
||||
document.close();
|
||||
} else if ("ofd".equals(fileType)) {
|
||||
try (OFDReader reader = new OFDReader(inputStream);) {
|
||||
inputStream.close();
|
||||
ImageMaker imageMaker = new ImageMaker(reader, 15);
|
||||
for (int i = 0; i < imageMaker.pageSize(); i++) {
|
||||
newFileName = fileName + "-" + i + ".jpg";
|
||||
|
||||
// 4. 指定页码转换图片
|
||||
BufferedImage image = imageMaker.makePage(i);
|
||||
// 5. 存储为指定格式图片
|
||||
ImageIO.write(image, "JPG", outputStream);
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||
FileItem fileItem = new FileItem(newFileName, uploadPrefix + newFileName, byteArrayInputStream);
|
||||
String upload = fs.upload(fileItem);
|
||||
deleteUrl.add(upload);
|
||||
String uploadUrl = UrlService.getAttachmentDownloadUrl(upload);
|
||||
String uploadEncreptURL = AttachmentServiceHelper.getEncreptURL(uploadUrl);
|
||||
byteArrayInputStream.close();
|
||||
resUrls.add(uploadEncreptURL);
|
||||
}
|
||||
}
|
||||
} else if ("jpg".equalsIgnoreCase(fileType) || "jpeg".equalsIgnoreCase(fileType) || "png".equalsIgnoreCase(fileType)
|
||||
|| "bmp".equalsIgnoreCase(fileType) || "gif".equalsIgnoreCase(fileType)) {
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||
FileItem fileItem = new FileItem(fileName, uploadPrefix + fileName, byteArrayInputStream);
|
||||
String upload = fs.upload(fileItem);
|
||||
deleteUrl.add(upload);
|
||||
String uploadUrl = UrlService.getAttachmentDownloadUrl(upload);
|
||||
String uploadEncreptURL = AttachmentServiceHelper.getEncreptURL(uploadUrl);
|
||||
byteArrayInputStream.close();
|
||||
resUrls.add(uploadEncreptURL);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("message" + e.getMessage());
|
||||
}
|
||||
return resUrls;
|
||||
}
|
||||
|
||||
private ArrayList<String> PDFtoPic(String url, String type) throws Exception {
|
||||
ArrayList<String> resUrls = new ArrayList<>();
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue