Explorar el Código

fix: export report with objectId & md5

ryanemax hace 2 meses
padre
commit
a2a222520c
Se han modificado 2 ficheros con 17 adiciones y 12 borrados
  1. 14 10
      server/cloud/tbook/func-tbook-export.js
  2. 3 2
      server/lib/docs/index.js

+ 14 - 10
server/cloud/tbook/func-tbook-export.js

@@ -9,7 +9,7 @@ if(!fs.existsSync(TemplateDocxDir)){
 }
 const tempDir = path.join(__dirname , "temp");
 
-
+const crypto = require('crypto');
 
 /**
  * 常用Word字符
@@ -212,7 +212,10 @@ export async function exportProcessReportDocs(processId,bookList) {
     if(docsList?.length){
         let now = new Date();
         let fileList = docsList?.map(item=>item?.pdfPath);
-        let zipName = `申报书导出-${now.getFullYear()}${now.getMonth()+1}${now.getDate()}-${now.getHours()}${now.getMinutes()}${now.getSeconds()}.zip`
+        // zip包md5生成
+        let md5 = crypto.createHash('md5');
+        let fileListMd5 = md5.update(fileList?.join("")).digest('hex')
+        let zipName = `申报书导出-${now.getFullYear()}${now.getMonth()+1}${now.getDate()}${now.getHours()}${now.getMinutes()}${now.getSeconds()}${now.getMilliseconds()}-${fileListMd5}.zip`
         zipPath = await createZip(fileList,zipName,{tempDir:null})
         if(zipPath){
             zipUrl = (await uploadFileToOSS(zipPath))?.url || null
@@ -222,7 +225,7 @@ export async function exportProcessReportDocs(processId,bookList) {
                 fs.rmSync(path.dirname(tempFile),{recursive:true,force:true});
             })
         }
-        docsList = docsList.map(item=>{return {code:item.code,title:item.title,url:item?.url,urlPdf:item?.urlPdf}})
+        docsList = docsList.map(item=>{return {code:item.code,objectId:item?.objectId,title:item.title,url:item?.url,urlPdf:item?.urlPdf}})
     }
 
     let result = {
@@ -534,7 +537,7 @@ function renderReportDocsByTextbook(textbook){
     }
 
  
-    let bookTempDir = path.join(tempDir,bookid)
+    let bookTempDir = path.join(tempDir,json?.objectId)
     if(!fs.existsSync(bookTempDir)) fs.mkdirSync(bookTempDir)
 
 
@@ -629,9 +632,9 @@ function renderReportDocsByTextbook(textbook){
 
     // mergeFiles 处理七八九是否签名或后置问题
     let lastPageList = []
-    let docx7 = await getPageDocx(bookid,"七",bookData);
-    let docx8 = await getPageDocx(bookid,"八",bookData);
-    let docx9 = await getPageDocx(bookid,"九",bookData);
+    let docx7 = await getPageDocx(json?.objectId,bookid,"七",bookData);
+    let docx8 = await getPageDocx(json?.objectId,bookid,"八",bookData);
+    let docx9 = await getPageDocx(json?.objectId,bookid,"九",bookData);
     lastPageList.push(json?.authorSignPDF?.url||docx7);
     lastPageList.push(json?.unitMaterial?.url||docx8);
     lastPageList.push(docx9);
@@ -668,13 +671,14 @@ function renderReportDocsByTextbook(textbook){
             pdfPath = await docxToPdf(filePath,pdfPath,options) || filePath // 成功用pdf,失败继续用docx
             console.log("PDF CREATED:",filePath)
             if(pdfPath){
-                urlPdf = (await uploadFileToOSS(pdfPath))?.url || null
+                urlPdf = (await uploadFileToOSS(pdfPath,json?.objectId))?.url || null
             }
         }catch(err){
             console.error(err)
         }
         resolve({
             code:bookid,
+            objectId:json?.objectId,
             title:json?.title,
             filePath,
             pdfPath,
@@ -685,10 +689,10 @@ function renderReportDocsByTextbook(textbook){
     })
 }
 
-async function getPageDocx(bookid,pageName,bookData){
+async function getPageDocx(objectId,bookid,pageName,bookData){
     let tplPath = path.join(TemplateDocxDir,`模板-本科教材申报书-${pageName}.docx`);
     let title = bookData?.title?.replaceAll("/","")
-    let tempFileName = path.join(`${bookid}_${title}_申报书及附件_${pageName}.docx`);
+    let tempFileName = path.join(objectId,`${bookid}_${title}_申报书及附件_${pageName}.docx`);
     let filePath = renderDocx(tplPath,tempFileName,bookData);
     return filePath;
     // 转pdf

+ 3 - 2
server/lib/docs/index.js

@@ -47,7 +47,8 @@ export async function toBarCode(text){
     })
 }
 
-export async function uploadFileToOSS(filePath){
+export async function uploadFileToOSS(filePath,uniqueId){
+    uniqueId = uniqueId || ""
     let client = new OSS({
         // yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。
         region: "oss-cn-beijing",
@@ -59,7 +60,7 @@ export async function uploadFileToOSS(filePath){
 
     let now = new Date();
     let fileName = getFileName(filePath);
-    let fileKey = `export/report/${fileName}`;
+    let fileKey = `export/report/${uniqueId}/${fileName}`;
     const r1 = await client?.put(fileKey, filePath);
     console.log('put success: %j', r1);
     return r1