Browse Source

feat: export with bookList params

MetaPunkGames 7 months ago
parent
commit
346589a1f6

+ 46 - 22
server/cloud/tbook/func-tbook-export.js

@@ -2,36 +2,47 @@ import { replaceDocx, docsToPdf, createZip, uploadFileToOSS } from "../../lib/do
 // const Parse = global.Parse;
 
 const path = require("path")
-const TemplateDocxPath = path.join(__dirname,"template/模板-推荐申报表.docx")
-
+const fs = require("fs")
+var TemplateDocxPath = path.join(__dirname,"template/模板-推荐申报表.docx")
+if(!fs.existsSync(TemplateDocxPath)){
+    TemplateDocxPath = path.join(__dirname,"../../template/模板-推荐申报表.docx")
+}
 /**
  * 定义导出申报合集文档云函数
  * @example
  * Cloud Code test
+ 导出流程
  curl -X POST -H "Content-Type: application/json" -H 'X-Parse-Application-Id: edu-textbook' -d '{ "processId": "Wz34loxdbO" }' http://127.0.0.1:61337/parse/functions/tbookExportReport
  curl -X POST -H "Content-Type: application/json" -H 'X-Parse-Application-Id: edu-textbook' -d '{ "processId": "Wz34loxdbO" }' http://8.140.98.43/parse/functions/tbookExportReport
+ 导出教材列表
+ curl -X POST -H "Content-Type: application/json" -H 'X-Parse-Application-Id: edu-textbook' -d '{ "bookList": ["2YBKitpCJL","xLdiEaHGrX"] }' http://127.0.0.1:61337/parse/functions/tbookExportReport
  */
  export function defineTbookExportReport(){
     Parse.Cloud.define("tbookExportReport", async (request) => {
         let processId = request.params.processId;
-        if(processId) {
-            try{
-                let result = await exportProcessReportDocs(processId)
-                if(result?.docsList?.length==0){
-                    throw new Parse.Error(404,"合集内无申报教材")
-                }
-                return result
-            }catch(err){
-                console.error(err)
-                throw new Parse.Error(404,"导出申报合集失败")
+        let bookList = request.params.bookList;
+        try{
+            let result
+            if(processId){
+                await exportProcessReportDocs(processId)
+            }
+            if(bookList?.length){
+                await exportProcessReportDocs(null,bookList)
             }
+            if(result?.docsList?.length==0){
+                throw new Parse.Error(404,"合集内无申报教材")
+            }
+            return result
+        }catch(err){
+            console.error(err)
+            throw new Parse.Error(404,"导出申报合集失败")
         }
         throw new Parse.Error(404,"未找到该流程合集")
     },{
         fields : {
             processId:{
-                required:true
-            }
+                required:false
+            },
         }
     });
 }
@@ -44,11 +55,22 @@ const TemplateDocxPath = path.join(__dirname,"template/模板-推荐申报表.do
  * docsList
  * zipUrl
  */
-async function exportProcessReportDocs(processId) {
-    if(!processId) return {}
-    let query = new Parse.Query("EduTextbook")
-    query.equalTo("eduProcess",processId);
-    let textbookList = await query.find();
+export async function exportProcessReportDocs(processId,bookList) {
+    if(!processId && !bookList?.length) return {}
+
+    let textbookList
+    if(processId){ // 流程读取教材列表
+        let query = new Parse.Query("EduTextbook")
+        query.equalTo("eduProcess",processId);
+        textbookList = await query.find();
+    }
+    if(bookList?.length){ // 直接导出教材列表
+        let query = new Parse.Query("EduTextbook")
+        query.containedIn("objectId",bookList);
+        textbookList = await query.find();
+    }
+
+    console.log(textbookList)
     let docsList = []
     for (let index = 0; index < textbookList.length; index++) {
         let textbook = textbookList[index];
@@ -57,7 +79,9 @@ async function exportProcessReportDocs(processId) {
     }
     let zipPath,zipUrl
     if(docsList?.length){
-        zipPath = await createZip(docsList?.map(item=>item?.filePath),`流程合集-${processId}.zip`)
+        let now = new Date();
+        let zipName = `申报书导出-${now.getFullYear()}${now.getMonth()+1}${now.getDate()}-${now.getHours()}${now.getMinutes()}${now.getSeconds()}.zip`
+        zipPath = await createZip(docsList?.map(item=>item?.filePath),zipName)
         if(zipPath){
             zipUrl = (await uploadFileToOSS(zipPath))?.url || null
         }
@@ -78,7 +102,7 @@ module.exports.exportProcessReportDocs = exportProcessReportDocs
 
 function renderReportDocsByTextbook(textbook){
     let json = textbook.toJSON();
-
+    console.log(json)
     // 圆圈选中未选 ○ 未选 ● 选中
     let circleCheck = ["○","●"];
     // 方块选中未选 ○ 未选 ● 选中
@@ -194,7 +218,7 @@ function padString(str,width) {
     str = str || ""
     str = String(str)
     width = width || 21 // 目标宽度为21个单位
-    spaceChar = "&#160;" // 占位符
+    let spaceChar = "&#160;" // 占位符
     // 计算字符串的宽度
     const charWidth = {
         'space': 1, // 空格占用1个单位

+ 17 - 2
server/cloud/tbook/test/test-export.js

@@ -3,9 +3,24 @@ Parse.initialize("edu-textbook");
 Parse.serverURL = "http://8.140.98.43/parse";
 global.Parse = Parse;
 
-const { exportProcessReportDocs } = require("../func-tbook-export");
+
+/**
+ * 
+ (npx vite build) -and (node temp/test-export.js)
+ npx vite build && node temp/test-export.js
+
+ */
+import { exportProcessReportDocs } from "../func-tbook-export";
 
 async function main(){
-    console.log(await exportProcessReportDocs("Wz34loxdbO"))
+    try{
+
+        // console.log("测试合集")
+        // console.log(await exportProcessReportDocs("FR7KZtefyR"))
+        console.log("测试列表")
+        console.log(await exportProcessReportDocs(null,["2YBKitpCJL","xLdiEaHGrX"]))
+    }catch(err){
+        console.error(err)
+    }
 }
 main()

+ 30 - 0
server/cloud/tbook/test/vite.config.js

@@ -0,0 +1,30 @@
+import { defineConfig } from 'vite';
+import { VitePluginNode } from 'vite-plugin-node';
+
+export default defineConfig({
+    server:{
+        port:61337
+    },
+    plugins: [
+        VitePluginNode({
+            adapter:"express",
+            appPath:"./test-export.js",
+            exportName:"TestExport",
+            // initAppOnBoot:false,
+        })
+    ],
+    build: {
+        lib: {
+            entry: 'test-export.js',  // 入口文件
+            name: 'temp-test-export',
+            fileName: (format) => `temp-test-export.${format}.js`
+        },
+        rollupOptions: {
+        input: 'test-export.js', // 你的入口文件
+        output: {
+            dir: 'temp',
+            format: 'cjs'
+        }
+        }
+    }
+});