diff --git a/jianshui-admin/src/main/java/com/jianshui/api/controller/http/invoice/v1/InvoiceController.java b/jianshui-admin/src/main/java/com/jianshui/api/controller/http/invoice/v1/InvoiceController.java index 6cbfc82..171762e 100644 --- a/jianshui-admin/src/main/java/com/jianshui/api/controller/http/invoice/v1/InvoiceController.java +++ b/jianshui-admin/src/main/java/com/jianshui/api/controller/http/invoice/v1/InvoiceController.java @@ -11,6 +11,7 @@ import cn.hutool.json.JSONArray; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.api.R; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -53,6 +54,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.ui.Model; @@ -2087,10 +2089,10 @@ public class InvoiceController { @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true), @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)}) @PostMapping({"/api/invoice/v1/QuerySdInvoiceFileDownload/{identity}", "/api/invoice/v1/QuerySdInvoiceFileDownload", "/invoice/QuerySdInvoiceFileDownload"}) - public void QuerySdInvoiceFileDownload(HttpServletResponse response, @RequestBody JSONObject jsonParam) throws Exception { + public R QuerySdInvoiceFileDownload(HttpServletResponse response, @RequestBody JSONObject jsonParam) throws Exception { if (ObjectUtil.isEmpty(jsonParam)) { - return; + return R.failed("参数为空!"); } String ty = jsonParam.get("ty") != null ? jsonParam.get("ty").toString() : ""; @@ -2099,52 +2101,76 @@ public class InvoiceController { InvoiceFile invoiceFile = invoiceFileMapper.selectInvoiceFileById(id); if (BeanUtil.isEmpty(invoiceFile) || StrUtil.isEmpty(invoiceFile.getFileContent())) { - return; + return R.failed("发票未查询到!"); } String fileName = invoiceFile.getFileName(); String fphm = invoiceFile.getFphm(); String tax = invoiceFile.getTax(); String identity = invoiceFile.getIdentity(); - try { - JSONObject decryptResult = new JSONObject(); - decryptResult.put("XHFSBH", tax); - decryptResult.put("ENTID", ""); - decryptResult.put("SDFPHM", fphm); - decryptResult.put("WJLX", ty); + JSONObject decryptResult = new JSONObject(); + decryptResult.put("XHFSBH", tax); + decryptResult.put("ENTID", ""); + decryptResult.put("SDFPHM", fphm); + decryptResult.put("WJLX", ty); + + IInvoiceApiService invoiceService = new ElephantInvoiceApiV6ServiceImpl(); + Companyservice companyservice = new Companyservice(); + companyservice.setCompanyid(Long.valueOf(identity)); + HXResponse result = invoiceService.QuerySdInvoiceFile(decryptResult, companyservice); - IInvoiceApiService invoiceService = new ElephantInvoiceApiV6ServiceImpl(); - Companyservice companyservice = new Companyservice(); - companyservice.setCompanyid(Long.valueOf(identity)); - HXResponse result = invoiceService.QuerySdInvoiceFile(decryptResult, companyservice); + String data = result.get("data") != null ? result.get("data").toString() : ""; - String data = result.get("data") != null ? result.get("data").toString() : ""; + Gson gson = new Gson(); + JsonObject jsonObject = gson.fromJson(data, JsonObject.class); - Gson gson = new Gson(); - JsonObject jsonObject = gson.fromJson(data, JsonObject.class); + JsonArray fileItemList = jsonObject.getAsJsonArray("fileitemlist"); - JsonArray fileItemList = jsonObject.getAsJsonArray("FILEITEMLIST"); + String fileContent = ""; + if(!fileItemList.isEmpty()){ + JsonObject fileItem = fileItemList.get(0).getAsJsonObject(); + fileContent = fileItem.get("FILECONTENT").getAsString(); + } - String fileContent = ""; - if(!fileItemList.isEmpty()){ - JsonObject fileItem = fileItemList.get(0).getAsJsonObject(); - fileContent = fileItem.get("FILECONTENT").getAsString(); - } + Map resMap = new HashMap<>(); + resMap.put("fileContent",fileContent); + resMap.put("ty","." + ty); + return R.ok(resMap); - byte[] buffer = Base64Decoder.decode(fileContent); // 将文件流字符串转换为字节数组 - response.reset(); - response.setCharacterEncoding("UTF-8"); - response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf"); - response.addHeader("Content-Length", "" + buffer.length); - response.setContentType("application/pdf"); - OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); - outputStream.write(buffer); - outputStream.flush(); - outputStream.close(); // 关闭输出流 - } catch (IOException ex) { - ex.printStackTrace(); + } + + // 根据文件类型获取文件扩展名 + private String getFileExtensionByType(String type) { + if (StringUtils.isEmpty(type)) { + return ""; + } + switch (type) { + case "pdf": + return ".pdf"; + case "odf": + return ".odf"; + case "xml": + return ".xml"; + default: + return ""; } + } + // 根据文件类型获取Content-Type + private String getContentTypeByType(String type) { + if (StringUtils.isEmpty(type)) { + return MediaType.APPLICATION_OCTET_STREAM_VALUE; + } + switch (type) { + case "pdf": + return MediaType.APPLICATION_PDF_VALUE; + case "odf": + return "application/vnd.oasis.opendocument.text"; + case "xml": + return MediaType.APPLICATION_XML_VALUE; + default: + return MediaType.APPLICATION_OCTET_STREAM_VALUE; + } } diff --git a/jianshui-admin/src/main/resources/application-dev.yml b/jianshui-admin/src/main/resources/application-dev.yml index fe1cfe0..58bb4da 100644 --- a/jianshui-admin/src/main/resources/application-dev.yml +++ b/jianshui-admin/src/main/resources/application-dev.yml @@ -94,8 +94,8 @@ elephant_invoice_file: http://127.0.0.1:8081/invoice/fileUrl/ # 德才定制 放到数据库 #elephant_invoice_file_dc: http://127.0.0.1:8088/sdFile/ # 大象V6数电对应平台地址 -ele_url_v6: https://js.ele12.com/order-api -#ele_url_v6: http://192.168.3.60:18108/order-api +#ele_url_v6: https://js.ele12.com/order-api +ele_url_v6: http://192.168.3.60:18108/order-api #ele_url_v6: http://127.0.0.1:18108/order-api # 大象纸票服务对应平台地址 ele_url: http://140.143.226.17:8087/order-api diff --git a/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java b/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java index bde248a..5414f43 100644 --- a/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java +++ b/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java @@ -610,25 +610,40 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService { String fileName = invoice.getFphm(); if(ddfpxx.getPDFZJL() != null && StrUtil.isNotEmpty(ddfpxx.getPDFZJL())){ - InvoiceFile invoiceFile = new InvoiceFile(); - invoiceFile.setId(fileId); - invoiceFile.setFileContent(ddfpxx.getPDFZJL()); - invoiceFile.setFileId(fileId); - invoiceFile.setFileName(fileName); - invoiceFile.setCreateTime(new Date()); - invoiceFile.setIdentity(companyservice.getIdentity()); - invoiceFile.setTax(companyservice.getSellertax()); - invoiceFileMapper.insertInvoiceFile(invoiceFile); - - // 2023/11/16 kk:上传OSS转下载地址关闭,改为直接返回大象的文件流 - invoice.setInvoicePdfUrl(ELE_File_URL + fileId); - ICompanyservicePropService companyserviceProp = SpringUtils.getBean(ICompanyservicePropService.class); - CompanyserviceProp secretIdProp = companyserviceProp.selectPropByKey(companyservice.getCompanyid(), "elephant_invoice_file_dc"); - if (secretIdProp != null) { - invoice.setInvoicePdfUrl(secretIdProp.getValue() + fileId); - } - + String fphm = ddfpxx.getFPHM(); + if(StrUtil.isNotEmpty(fphm)){ + InvoiceFile invoiceFile = new InvoiceFile(); + invoiceFile.setFphm(ddfpxx.getFPHM()); + List invoiceFileList = invoiceFileMapper.selectInvoiceFileList(invoiceFile); + if(invoiceFileList != null && invoiceFileList.size() > 0){ + invoiceFile = invoiceFileList.get(0); + invoiceFile.setFileContent(ddfpxx.getPDFZJL()); + invoiceFile.setFileName(fileName); + invoiceFile.setCreateTime(new Date()); + invoiceFile.setIdentity(companyservice.getIdentity()); + invoiceFile.setTax(companyservice.getSellertax()); + fileId = invoiceFile.getFileId(); + fileName = invoiceFile.getFileName(); + invoiceFileMapper.updateInvoiceFile(invoiceFile); + }else{ + invoiceFile.setId(fileId); + invoiceFile.setFileContent(ddfpxx.getPDFZJL()); + invoiceFile.setFileId(fileId); + invoiceFile.setFileName(fileName); + invoiceFile.setCreateTime(new Date()); + invoiceFile.setIdentity(companyservice.getIdentity()); + invoiceFile.setTax(companyservice.getSellertax()); + invoiceFileMapper.insertInvoiceFile(invoiceFile); + } + // 2023/11/16 kk:上传OSS转下载地址关闭,改为直接返回大象的文件流 + invoice.setInvoicePdfUrl(ELE_File_URL + fileId); + ICompanyservicePropService companyserviceProp = SpringUtils.getBean(ICompanyservicePropService.class); + CompanyserviceProp secretIdProp = companyserviceProp.selectPropByKey(companyservice.getCompanyid(), "elephant_invoice_file_dc"); + if (secretIdProp != null) { + invoice.setInvoicePdfUrl(secretIdProp.getValue() + fileId); + } + } } // end diff --git a/jianshui-invoice/src/main/resources/mapper/invoice/InvoiceFileMapper.xml b/jianshui-invoice/src/main/resources/mapper/invoice/InvoiceFileMapper.xml index c2e74c3..283e856 100644 --- a/jianshui-invoice/src/main/resources/mapper/invoice/InvoiceFileMapper.xml +++ b/jianshui-invoice/src/main/resources/mapper/invoice/InvoiceFileMapper.xml @@ -46,7 +46,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" create_time, identity, tax, - fphm, + fphm, #{id}, diff --git a/jianshui-ui/src/views/sdinvoicefile/index.vue b/jianshui-ui/src/views/sdinvoicefile/index.vue index fee16c2..389d360 100644 --- a/jianshui-ui/src/views/sdinvoicefile/index.vue +++ b/jianshui-ui/src/views/sdinvoicefile/index.vue @@ -5,9 +5,9 @@
- 下载PDF - 下载OFD - 下载XML + 下载PDF + 下载OFD + 下载XML
发票图片损坏 @@ -50,11 +50,27 @@ export default { "id": this.id } console.info("param=" + param) - downloadFile(param).then( - response =>{ - console.info("请求结束"); - } - ); + downloadFile(param).then(response => { + const content = atob(response.data.fileContent); + const bytes = new Uint8Array(content.length); + for (let i = 0; i < content.length; i++) { + bytes[i] = content.charCodeAt(i); + } + const blob = new Blob([bytes], { type: response.data.ty }); + const fileName = this.id; + if (window.navigator.msSaveOrOpenBlob) { + // 兼容IE浏览器 + window.navigator.msSaveOrOpenBlob(blob, fileName); + } else { + const link = document.createElement('a'); + const url = URL.createObjectURL(blob); + link.href = url; + link.download = fileName + response.data.ty; + link.click(); + URL.revokeObjectURL(url); + + } + }); }, },