feature: 发票预览文件下载

beta
kk 1 year ago
parent 6f23d300cc
commit 6a3101784e
  1. 94
      jianshui-admin/src/main/java/com/jianshui/api/controller/http/invoice/v1/InvoiceController.java
  2. 4
      jianshui-admin/src/main/resources/application-dev.yml
  3. 51
      jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java
  4. 2
      jianshui-invoice/src/main/resources/mapper/invoice/InvoiceFileMapper.xml
  5. 32
      jianshui-ui/src/views/sdinvoicefile/index.vue

@ -11,6 +11,7 @@ import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -53,6 +54,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.ui.Model; 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 = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
@ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)}) @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
@PostMapping({"/api/invoice/v1/QuerySdInvoiceFileDownload/{identity}", "/api/invoice/v1/QuerySdInvoiceFileDownload", "/invoice/QuerySdInvoiceFileDownload"}) @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)) { if (ObjectUtil.isEmpty(jsonParam)) {
return; return R.failed("参数为空!");
} }
String ty = jsonParam.get("ty") != null ? jsonParam.get("ty").toString() : ""; String ty = jsonParam.get("ty") != null ? jsonParam.get("ty").toString() : "";
@ -2099,52 +2101,76 @@ public class InvoiceController {
InvoiceFile invoiceFile = invoiceFileMapper.selectInvoiceFileById(id); InvoiceFile invoiceFile = invoiceFileMapper.selectInvoiceFileById(id);
if (BeanUtil.isEmpty(invoiceFile) || StrUtil.isEmpty(invoiceFile.getFileContent())) { if (BeanUtil.isEmpty(invoiceFile) || StrUtil.isEmpty(invoiceFile.getFileContent())) {
return; return R.failed("发票未查询到!");
} }
String fileName = invoiceFile.getFileName(); String fileName = invoiceFile.getFileName();
String fphm = invoiceFile.getFphm(); String fphm = invoiceFile.getFphm();
String tax = invoiceFile.getTax(); String tax = invoiceFile.getTax();
String identity = invoiceFile.getIdentity(); String identity = invoiceFile.getIdentity();
try { JSONObject decryptResult = new JSONObject();
JSONObject decryptResult = new JSONObject(); decryptResult.put("XHFSBH", tax);
decryptResult.put("XHFSBH", tax); decryptResult.put("ENTID", "");
decryptResult.put("ENTID", ""); decryptResult.put("SDFPHM", fphm);
decryptResult.put("SDFPHM", fphm); decryptResult.put("WJLX", ty);
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(); String data = result.get("data") != null ? result.get("data").toString() : "";
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() : ""; Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(data, JsonObject.class);
Gson gson = new Gson(); JsonArray fileItemList = jsonObject.getAsJsonArray("fileitemlist");
JsonObject jsonObject = gson.fromJson(data, JsonObject.class);
JsonArray fileItemList = jsonObject.getAsJsonArray("FILEITEMLIST"); String fileContent = "";
if(!fileItemList.isEmpty()){
JsonObject fileItem = fileItemList.get(0).getAsJsonObject();
fileContent = fileItem.get("FILECONTENT").getAsString();
}
String fileContent = ""; Map<String,Object> resMap = new HashMap<>();
if(!fileItemList.isEmpty()){ resMap.put("fileContent",fileContent);
JsonObject fileItem = fileItemList.get(0).getAsJsonObject(); resMap.put("ty","." + ty);
fileContent = fileItem.get("FILECONTENT").getAsString(); 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"); private String getFileExtensionByType(String type) {
response.addHeader("Content-Length", "" + buffer.length); if (StringUtils.isEmpty(type)) {
response.setContentType("application/pdf"); return "";
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); }
outputStream.write(buffer); switch (type) {
outputStream.flush(); case "pdf":
outputStream.close(); // 关闭输出流 return ".pdf";
} catch (IOException ex) { case "odf":
ex.printStackTrace(); 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;
}
} }

@ -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/ #elephant_invoice_file_dc: http://127.0.0.1:8088/sdFile/
# 大象V6数电对应平台地址 # 大象V6数电对应平台地址
ele_url_v6: https://js.ele12.com/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://192.168.3.60:18108/order-api
#ele_url_v6: http://127.0.0.1:18108/order-api #ele_url_v6: http://127.0.0.1:18108/order-api
# 大象纸票服务对应平台地址 # 大象纸票服务对应平台地址
ele_url: http://140.143.226.17:8087/order-api ele_url: http://140.143.226.17:8087/order-api

@ -610,25 +610,40 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService {
String fileName = invoice.getFphm(); String fileName = invoice.getFphm();
if(ddfpxx.getPDFZJL() != null && StrUtil.isNotEmpty(ddfpxx.getPDFZJL())){ if(ddfpxx.getPDFZJL() != null && StrUtil.isNotEmpty(ddfpxx.getPDFZJL())){
InvoiceFile invoiceFile = new InvoiceFile(); String fphm = ddfpxx.getFPHM();
invoiceFile.setId(fileId); if(StrUtil.isNotEmpty(fphm)){
invoiceFile.setFileContent(ddfpxx.getPDFZJL()); InvoiceFile invoiceFile = new InvoiceFile();
invoiceFile.setFileId(fileId); invoiceFile.setFphm(ddfpxx.getFPHM());
invoiceFile.setFileName(fileName); List<InvoiceFile> invoiceFileList = invoiceFileMapper.selectInvoiceFileList(invoiceFile);
invoiceFile.setCreateTime(new Date()); if(invoiceFileList != null && invoiceFileList.size() > 0){
invoiceFile.setIdentity(companyservice.getIdentity()); invoiceFile = invoiceFileList.get(0);
invoiceFile.setTax(companyservice.getSellertax()); invoiceFile.setFileContent(ddfpxx.getPDFZJL());
invoiceFileMapper.insertInvoiceFile(invoiceFile); invoiceFile.setFileName(fileName);
invoiceFile.setCreateTime(new Date());
// 2023/11/16 kk:上传OSS转下载地址关闭,改为直接返回大象的文件流 invoiceFile.setIdentity(companyservice.getIdentity());
invoice.setInvoicePdfUrl(ELE_File_URL + fileId); invoiceFile.setTax(companyservice.getSellertax());
ICompanyservicePropService companyserviceProp = SpringUtils.getBean(ICompanyservicePropService.class); fileId = invoiceFile.getFileId();
CompanyserviceProp secretIdProp = companyserviceProp.selectPropByKey(companyservice.getCompanyid(), "elephant_invoice_file_dc"); fileName = invoiceFile.getFileName();
if (secretIdProp != null) { invoiceFileMapper.updateInvoiceFile(invoiceFile);
invoice.setInvoicePdfUrl(secretIdProp.getValue() + fileId); }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 // end

@ -46,7 +46,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="createTime != null">create_time,</if> <if test="createTime != null">create_time,</if>
<if test="identity != null">identity,</if> <if test="identity != null">identity,</if>
<if test="tax != null">tax,</if> <if test="tax != null">tax,</if>
<if test="fphm != fphm">fphm,</if> <if test="fphm != null">fphm,</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if> <if test="id != null">#{id},</if>

@ -5,9 +5,9 @@
<div class="center-container"> <div class="center-container">
<!-- 下载PDF按钮 --> <!-- 下载PDF按钮 -->
<div style="float:left;"> <div style="float:left;">
<el-button type="primary" @click="download('pdf')" class="download-button">下载PDF</el-button> <el-button type="primary" @click="download('PDF')" class="download-button">下载PDF</el-button>
<el-button type="primary" @click="download('ofd')" class="download-button">下载OFD</el-button> <el-button type="primary" @click="download('OFD')" class="download-button">下载OFD</el-button>
<el-button type="primary" @click="download('xml')" class="download-button">下载XML</el-button> <el-button type="primary" @click="download('XML')" class="download-button">下载XML</el-button>
</div> </div>
<img :src="invoiceImageUrl" alt="发票图片损坏" class="invoice-image"> <img :src="invoiceImageUrl" alt="发票图片损坏" class="invoice-image">
@ -50,11 +50,27 @@ export default {
"id": this.id "id": this.id
} }
console.info("param=" + param) console.info("param=" + param)
downloadFile(param).then( downloadFile(param).then(response => {
response =>{ const content = atob(response.data.fileContent);
console.info("请求结束"); 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);
}
});
}, },
}, },

Loading…
Cancel
Save