feature: 发票预览文件下载

beta
kk 1 year ago
parent 6f23d300cc
commit 6a3101784e
  1. 60
      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. 19
      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. 30
      jianshui-ui/src/views/sdinvoicefile/index.vue

@ -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,14 +2101,13 @@ 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", "");
@ -2123,7 +2124,7 @@ public class InvoiceController {
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()){
@ -2131,20 +2132,45 @@ public class InvoiceController {
fileContent = fileItem.get("FILECONTENT").getAsString();
}
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();
Map<String,Object> resMap = new HashMap<>();
resMap.put("fileContent",fileContent);
resMap.put("ty","." + ty);
return R.ok(resMap);
}
// 根据文件类型获取文件扩展名
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;
}
}

@ -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

@ -610,7 +610,22 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService {
String fileName = invoice.getFphm();
if(ddfpxx.getPDFZJL() != null && StrUtil.isNotEmpty(ddfpxx.getPDFZJL())){
String fphm = ddfpxx.getFPHM();
if(StrUtil.isNotEmpty(fphm)){
InvoiceFile invoiceFile = new InvoiceFile();
invoiceFile.setFphm(ddfpxx.getFPHM());
List<InvoiceFile> 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);
@ -619,6 +634,7 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService {
invoiceFile.setIdentity(companyservice.getIdentity());
invoiceFile.setTax(companyservice.getSellertax());
invoiceFileMapper.insertInvoiceFile(invoiceFile);
}
// 2023/11/16 kk:上传OSS转下载地址关闭,改为直接返回大象的文件流
invoice.setInvoicePdfUrl(ELE_File_URL + fileId);
@ -627,8 +643,7 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService {
if (secretIdProp != null) {
invoice.setInvoicePdfUrl(secretIdProp.getValue() + fileId);
}
}
}
// end

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

@ -5,9 +5,9 @@
<div class="center-container">
<!-- 下载PDF按钮 -->
<div style="float:left;">
<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('xml')" class="download-button">下载XML</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('XML')" class="download-button">下载XML</el-button>
</div>
<img :src="invoiceImageUrl" alt="发票图片损坏" class="invoice-image">
@ -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);
}
});
},
},

Loading…
Cancel
Save