Browse Source

feat: import tbook export report cloud code

ryanemax 7 months ago
parent
commit
b4f01cde0e
3 changed files with 43 additions and 7 deletions
  1. 36 2
      server/cloud/tbook/func-tbook-export.js
  2. 5 5
      server/lib/docs/index.js
  3. 2 0
      server/server.js

+ 36 - 2
server/cloud/tbook/func-tbook-export.js

@@ -1,8 +1,42 @@
-const { replaceDocx, docsToPdf, createZip, uploadFileToOSS } = require("../../lib/docs");
-const Parse = global.Parse;
+import { replaceDocx, docsToPdf, createZip, uploadFileToOSS } from "../../lib/docs";
+// const Parse = global.Parse;
 
 const path = require("path")
 const 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
+ */
+ export function defineTbookExportReport(){
+    Parse.Cloud.define("tbookExportReport", async (request) => {
+        let processId = request.params.processId;
+        if(processId) {
+            try{
+                let result = await exportProcessReportDocs()
+                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
+            }
+        }
+    });
+}
+  
+
+
 /**
  * 导出流程教材申报文件
  * @returns

+ 5 - 5
server/lib/docs/index.js

@@ -14,7 +14,7 @@ const ALI_OSS_BUCKET = process.env.ALI_OSS_BUCKET || "hep-textbook"
 const ALI_OSS_ACCESS_KEY_ID = process.env.ALI_OSS_ACCESS_KEY_ID || "LTAI5t6AbTiAvXmeoVdJZhL3"
 const ALI_OSS_ACCESS_KEY_SECRET = process.env.ALI_OSS_ACCESS_KEY_SECRET || "KLtQRdIW69KLP7jnzHNUf7eKmdptxH"
 
-async function uploadFileToOSS(filePath){
+export async function uploadFileToOSS(filePath){
     let client = new OSS({
         // yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。
         region: "oss-cn-beijing",
@@ -31,7 +31,7 @@ async function uploadFileToOSS(filePath){
     console.log('put success: %j', r1);
     return r1
 }
-function getFileName(filePath) {
+export function getFileName(filePath) {
     // 使用 '/' 或 '\' 作为分隔符,分割路径
     const parts = filePath.split(/[/\\]/);
     // 返回最后一个部分,即文件名
@@ -44,7 +44,7 @@ module.exports.uploadFileToOSS = uploadFileToOSS
  * @param {Array<string>} filePathList - 要打包的文件路径数组
  * @param {string} outputZipName - 输出的zip文件名称
  */
- function createZip(filePathList, outputZipName) {
+ export function createZip(filePathList, outputZipName) {
     let zipStream = new compressing.zip.Stream();
     return new Promise((resolve)=>{
         try {
@@ -97,7 +97,7 @@ module.exports.createZip = createZip
  * @param {string} outputPath - 输出 PDF 文件的路径
  * @returns {Promise<void>}
  */
- async function docsToPdf(docxPath, outputPath) {
+ export async function docsToPdf(docxPath, outputPath) {
     try {
         const files = [docxPath];
 
@@ -141,7 +141,7 @@ module.exports.docsToPdf = docsToPdf
     }
     replaceDocx(inputDocx, outputDocx, replaceData)
  */
-function replaceDocx(inputDocxPath, outputDocxPath, options,eventMap) {
+export function replaceDocx(inputDocxPath, outputDocxPath, options,eventMap) {
     return new Promise((resolve,reject)=>{
 
     // 解压出来的临时目录

+ 2 - 0
server/server.js

@@ -48,6 +48,7 @@ import { defineAuthingLogin, defineDepartmentTrigger, defineUserAfterDelete, def
 import { defineAuthingDepartSync } from "./cloud/authing"
 import { defineUserAfterSave } from "./cloud/authing"
 import { defineTbookISBN } from "./cloud/tbook"
+import { defineTbookExportReport } from "./cloud/tbook/func-tbook-export"
 import { importData } from "./db/func/import-data"
 
 
@@ -187,6 +188,7 @@ async function initParseAndDatabase(){
       defineUserAfterDelete();
       defineDepartmentTrigger();
       defineTbookISBN();
+      defineTbookExportReport();
     });
  
     console.log("正在启动管理看板...")