zhangjie hai 8 meses
pai
achega
c01ab6386b

+ 21 - 10
src/modules/client/views/ScanOther.vue

@@ -160,6 +160,7 @@ export default {
   },
   beforeDestroy() {
     this.stopLoopScaningFile();
+    this.clearSetTs();
     this.clearFiles();
   },
   methods: {
@@ -245,7 +246,9 @@ export default {
         console.error(error);
       });
       this.stopLoopScaningFile();
-      this.getScaningFile();
+    },
+    async evokeScanNotDelayOver() {
+      await this.getScaningFile();
 
       const scanCount = this.scanStageList.length - this.scanCount;
       this.scanCount = this.scanStageList.length;
@@ -263,7 +266,6 @@ export default {
       logger.info(`03扫描仪停止,扫描数:${scanCount}`);
     },
     stageScanImage(imageList) {
-      const batchNo = randomCode();
       for (let i = 0, len = imageList.length; i < len; i++) {
         const fileInfo = {
           id: randomCode(16),
@@ -284,7 +286,7 @@ export default {
           fileTypeId: this.modalForm.fileTypeId,
           fileTypeName: this.modalForm.fileTypeName,
           roomOrClass: this.modalForm.roomOrClass,
-          batchNo,
+          batchNo: "",
           clientUserId: this.user.id,
           clientUsername: this.user.loginName,
           clientUserLoginTime: this.user.loginTime,
@@ -304,8 +306,10 @@ export default {
 
       logger.info(`04-1开始保存数据`);
       try {
+        const batchNo = randomCode();
         const datas = this.scanStageList.map((item, index) => {
           item.studentPaperNo = index + 1;
+          item.batchNo = batchNo;
           return item;
         });
         this.scanStageList = await batchSaveImages(datas, this.task.courseCode);
@@ -326,20 +330,27 @@ export default {
     },
     // delay mode
     // 实时获取扫描图片
-    loopScaningFile() {
+    async loopScaningFile() {
       this.clearSetTs();
-      if (!this.looping) return;
-      this.getScaningFile();
+      if (!this.looping) {
+        this.evokeScanNotDelayOver();
+        return;
+      }
+      await this.getScaningFile();
 
       this.addSetTime(this.loopScaningFile, 1 * 1000);
     },
     stopLoopScaningFile() {
-      this.clearSetTs();
+      // this.clearSetTs();
       this.looping = false;
     },
-    getScaningFile() {
-      const newScanFiles = getDirScanFile(this.GLOBAL.input);
-      this.stageScanImage(newScanFiles);
+    async getScaningFile() {
+      const newScanFiles = await getDirScanFile(this.GLOBAL.input).catch(
+        (e) => {
+          logger.error(`loop 遍历文件错误,${e.message || "error"}`);
+        }
+      );
+      this.stageScanImage(newScanFiles || []);
     },
     // table action
     async clearStage() {

+ 14 - 4
src/modules/client/views/ScanPaper.vue

@@ -208,6 +208,7 @@ export default {
   },
   beforeDestroy() {
     this.stopLoopScaningFile();
+    this.clearSetTs();
     this.clearFiles();
   },
   methods: {
@@ -296,6 +297,8 @@ export default {
         console.error(error);
       });
       this.stopLoopScaningFile();
+    },
+    async evokeScanNotDelayOver() {
       await this.getScaningFile();
 
       const scanCount = this.scanStageList.length - this.scanCount;
@@ -466,7 +469,10 @@ export default {
     // 实时获取扫描图片
     async loopScaningFile() {
       this.clearSetTs();
-      if (!this.looping) return;
+      if (!this.looping) {
+        this.evokeScanNotDelayOver();
+        return;
+      }
       // const st = Date.now();
       await this.getScaningFile();
       // console.log(`耗时:${Date.now() - st}ms`);
@@ -474,12 +480,16 @@ export default {
       this.addSetTime(this.loopScaningFile, 1 * 1000);
     },
     stopLoopScaningFile() {
-      this.clearSetTs();
+      // this.clearSetTs();
       this.looping = false;
     },
     async getScaningFile() {
-      const newScanFiles = getDirScanFile(this.GLOBAL.input);
-      await this.stageScanImage(newScanFiles);
+      const newScanFiles = await getDirScanFile(this.GLOBAL.input).catch(
+        (e) => {
+          logger.error(`loop 遍历文件错误,${e.message || "error"}`);
+        }
+      );
+      await this.stageScanImage(newScanFiles || []);
     },
     // table action
     toBind() {

+ 75 - 8
src/plugins/imageOcr.js

@@ -7,12 +7,14 @@ import {
   getTmpDir,
 } from "./env";
 import { randomCode, formatDate } from "./utils";
-import usePQueue from "../plugins/pQueue";
+import usePQueue from "./pQueue";
 import gm from "./gm";
 const fs = window.nodeRequire("fs");
 const path = window.nodeRequire("path");
 const childProcess = window.nodeRequire("child_process");
 const isWindowsPlatform = process.platform === "win32";
+import log4js from "./logger";
+const logger = log4js.getLogger("scan");
 
 /**
  * 旋转图片,并保存为正式文件
@@ -186,18 +188,51 @@ export function renamePreUploadJsonFile(dir) {
   });
 }
 
+export async function moveFile(src, dest, count = 1) {
+  let result = true;
+  await fs.promises.rename(src, dest).catch((e) => {
+    logger.error(
+      `loop-03-01 转存文件失败,rename:${count}:${e.message || "rename error"}`
+    );
+    result = false;
+  });
+  if (result) return;
+
+  if (count < 3) {
+    count++;
+    await moveFile(src, dest, count);
+  } else {
+    try {
+      await fs.promises.copyFile(src, dest);
+      await fs.promises.unlink(src);
+    } catch (e) {
+      logger.error(
+        `loop-03-01 转存文件失败:copy:${e.message || "copy unlink error"}`
+      );
+      return Promise.reject(e);
+    }
+  }
+}
+
 /**
  * 获取最早添加的文件
  * @param {String} dir 图片目录
  */
-export function getDirScanFile(dir) {
+export async function getDirScanFile(dir) {
   // const st = Date.now();
   const stageDdir = getStageDir();
   const ddir = dir || getInputDir();
-  const files = fs
+  logger.info(`loop-01 开始遍历目录`);
+
+  let files = fs
     .readdirSync(ddir)
-    // .filter((fileName) => fileName.toLowerCase().match(/\.(jpg|png|jpeg)/))
-    .filter((fileName) => fileName.toLowerCase().match(/^.*\.(jpg|png|jpeg)$/))
+    .filter((fileName) =>
+      /^\d{13}_\d{8}_(F|B)\.(jpg|png|jpeg)$/.test(fileName)
+    );
+
+  logger.info(`loop-02 目录结果:${files.join()}`);
+
+  files = files
     .map((fileName) => {
       return {
         name: fileName,
@@ -206,6 +241,8 @@ export function getDirScanFile(dir) {
     })
     .sort((a, b) => a.time - b.time);
 
+  logger.info(`loop-03 目录排序结果:$${JSON.stringify(files)}`);
+
   const getbasename = (name) => {
     return path
       .basename(name, path.extname(name))
@@ -236,8 +273,33 @@ export function getDirScanFile(dir) {
     const frontFile = path.join(stageDdir, frontName);
     const versoFile = path.join(stageDdir, versoName);
 
-    fs.renameSync(path.join(ddir, frontName), frontFile);
-    fs.renameSync(path.join(ddir, versoName), versoFile);
+    logger.info(`loop-03-01 开始转存文件:${frontName},${versoName}`);
+
+    const inFrontFile = path.join(ddir, frontName);
+    const inVersoFile = path.join(ddir, versoName);
+    // if (!fs.existsSync(inFrontFile) || !fs.existsSync(inVersoFile)) {
+    //   logger.error(
+    //     `loop-03-01 转存文件失败:${frontName},${versoName}有文件不存在,退出!`
+    //   );
+    //   break;
+    // }
+
+    let result = true;
+    try {
+      await moveFile(inFrontFile, frontFile);
+      await moveFile(inVersoFile, versoFile);
+    } catch (error) {
+      result = false;
+      logger.info(
+        `loop-03-02 转存文件失败:${frontName},${versoName},原因:${
+          error.message || "move file failed"
+        }`
+      );
+    }
+
+    if (!result) break;
+
+    logger.info(`loop-03-02 转存文件成功:${frontName},${versoName}`);
 
     nFiles[i] = {
       frontFile,
@@ -245,6 +307,11 @@ export function getDirScanFile(dir) {
       basename: getScanFileBasename(frontName),
     };
   }
+
+  logger.info(
+    `loop-04 完成遍历,结果:${nFiles.map((item) => item.basename).join()}`
+  );
+
   // console.log(nFiles);
   // console.log(`getDirScanFile耗时:${Date.now() - st}`);
   return nFiles;
@@ -357,7 +424,7 @@ export async function batchSaveImages(imageList, courseCode) {
     return async () => {
       const outputOriginPath = path.join(outputDir, path.basename(item.url));
       let result = true;
-      await fs.promises.rename(item.url, outputOriginPath).catch((err) => {
+      await moveFile(item.url, outputOriginPath).catch((err) => {
         console.error(`Error moving file: ${item.url}, ${err.message}`);
         result = false;
       });