Browse Source

采集流程调整

zhangjie 2 years ago
parent
commit
d5baf254aa

BIN
extra/database/client.rdb


BIN
extra/database/org.rdb


+ 81 - 66
src/modules/client/components/ScanTaskDialog.vue

@@ -6,6 +6,7 @@
     width="550px"
     :close-on-click-modal="false"
     :close-on-press-escape="false"
+    :show-close="false"
     append-to-body
     @open="visibleChange"
   >
@@ -32,22 +33,33 @@
         <span class="color-danger">
           {{ realScanCount }}
         </span>
+        <i v-if="scanStatus === 'START'" class="el-icon-loading"></i>
       </el-form-item>
     </el-form>
     <div slot="footer">
-      <el-button v-if="!isScaning" type="primary" @click="startTask"
+      <el-button v-if="scanStatus === 'INIT'" type="primary" @click="startTask"
         >开始扫描</el-button
       >
-      <el-button v-if="isScaning" type="primary" @click="confirm"
+      <el-button
+        v-if="scanStatus === 'PAUSE'"
+        type="primary"
+        @click="continueTask"
+        >继续扫描</el-button
+      >
+      <el-button v-if="scanStatus === 'PAUSE'" type="primary" @click="confirm"
         >确认</el-button
       >
-      <el-button @click="cancel">取消</el-button>
+      <el-button v-if="scanStatus === 'PAUSE'" @click="cancel">取消</el-button>
     </div>
   </el-dialog>
 </template>
 
 <script>
-import { getEarliestFile, saveOutputImage } from "../../../plugins/imageOcr";
+import {
+  getPreUploadFilesAutoSerial,
+  getPreUploadFileCount,
+  saveOutputImage
+} from "../../../plugins/imageOcr";
 import setTimeMixins from "../../../mixins/setTimeMixins";
 import db from "../../../plugins/db";
 const fs = require("fs");
@@ -67,10 +79,9 @@ export default {
     return {
       modalIsShow: false,
       loading: false,
-      isScaning: false,
-      canScan: true,
+      scanStatus: "INIT", // INIT:初始状态,START:已开始,PAUSE:已暂停
       modalForm: {
-        preScanCount: 0
+        preScanCount: undefined
       },
       rules: {
         preScanCount: [
@@ -81,14 +92,11 @@ export default {
           }
         ]
       },
-      scanList: [],
-      scaningImageList: []
+      scaningImageList: [],
+      realScanCount: 0
     };
   },
   computed: {
-    realScanCount() {
-      return this.scanList.length;
-    },
     user() {
       return this.$store.state.user;
     }
@@ -99,15 +107,17 @@ export default {
   methods: {
     initData() {
       this.loading = false;
-      this.modalForm.preScanCount = 0;
-      this.scanList = [];
-      this.isScaning = false;
+      this.modalForm.preScanCount = undefined;
+      this.scaningImageList = [];
+      this.scanStatus = "INIT";
+      this.realScanCount = 0;
     },
     visibleChange() {
       this.initData();
     },
     close() {
       this.modalIsShow = false;
+      this.$emit("on-close");
     },
     open() {
       this.modalIsShow = true;
@@ -119,12 +129,33 @@ export default {
       if (this.loading) return;
       this.loading = true;
 
-      this.startScan();
-      // TODO:唤起采集程序
+      this.scaningImageList = [];
+      this.scanStatus = "START";
+      this.getInitFile();
+
+      // 唤起采集程序
+      this.evokeScanExe();
+    },
+    async continueTask() {
+      this.scanStatus = "START";
+      this.getInitFile();
+
+      this.evokeScanExe();
+    },
+    evokeScanExe() {
+      console.log("唤起扫描仪");
+      setTimeout(() => {
+        this.scanStatus = "PAUSE";
+        console.log("扫描仪停止");
+      }, 5 * 1000);
     },
     async confirm() {
-      await this.addUploadTask();
-      this.pauseScan();
+      this.scaningImageList = getPreUploadFilesAutoSerial(this.GLOBAL.input);
+      if (!this.scaningImageList.length) {
+        this.$message.error("当前没有要保存的数据!");
+        return;
+      }
+      await this.saveScanImage();
 
       const confirm = await this.$confirm(`是否继续扫描?`, "提示", {
         type: "warning"
@@ -153,61 +184,45 @@ export default {
     // scan relate
     getInitFile() {
       this.clearSetTs();
-      if (!this.canScan) return;
-
-      const scaningImageList = getEarliestFile(this.GLOBAL.input);
-      this.scaningImageList = scaningImageList || [];
+      if (this.scanStatus !== "START") return;
 
-      if (this.scaningImageList.length) {
-        this.saveScanImage(this.scaningImageList);
-        this.addSetTime(() => {
-          this.getInitFile();
-        }, 200);
-      } else {
-        this.addSetTime(() => {
-          this.getInitFile();
-        }, 1000);
-      }
-    },
-    saveScanImage() {
-      const ouputImageList = saveOutputImage(this.scaningImageList);
-      this.scanList.push({
-        taskId: this.task.id,
-        taskName: this.task.name,
-        courseCode: this.task.courseCode,
-        courseName: this.task.courseName,
-        teachingClassName: this.task.teachingClassName,
-        frontOriginImgPath: ouputImageList[0],
-        versoOriginImgPath: ouputImageList[1],
-        clientUserId: this.user.userId,
-        clientUsername: this.user.name,
-        clientUserLoginTime: this.user.loginTime
-      });
-    },
-    pauseScan() {
-      this.canScan = false;
+      this.realScanCount = getPreUploadFileCount(this.GLOBAL.input);
+      this.addSetTime(() => {
+        this.getInitFile();
+      }, 500);
     },
-    startScan() {
-      this.scaningImageList = [];
-      this.canScan = true;
-      this.isScaning = true;
-      this.getInitFile();
-    },
-    async addUploadTask() {
-      // 添加上传任务
-      for (let i = 0, len = this.scanList.length; i < len; i++) {
-        await db.saveUploadInfo(this.scanList[i]).catch(err => {
+    async saveScanImage() {
+      for (let i = 0, len = this.scaningImageList.length; i < len; i++) {
+        const files = this.scaningImageList[i];
+        const ouputImageList = saveOutputImage([
+          files.frontFile,
+          files.versoFile
+        ]);
+        const fileInfo = {
+          taskId: this.task.id,
+          taskName: this.task.name,
+          courseCode: this.task.courseCode,
+          courseName: this.task.courseName,
+          teachingClassName: this.task.teachingClassName,
+          frontOriginImgPath: ouputImageList[0],
+          versoOriginImgPath: ouputImageList[1],
+          clientUserId: this.user.userId,
+          clientUsername: this.user.name,
+          clientUserLoginTime: this.user.loginTime
+        };
+
+        await db.saveUploadInfo(fileInfo).catch(err => {
           console.log(err);
         });
       }
     },
     clearScanList() {
-      for (let i = 0; i < this.scanList.length; i++) {
-        const item = this.scanList[i];
-        fs.unlinkSync(item.frontOriginImgPath);
-        fs.unlinkSync(item.versoOriginImgPath);
+      for (let i = 0; i < this.scaningImageList.length; i++) {
+        const item = this.scaningImageList[i];
+        fs.unlinkSync(item.frontFile);
+        fs.unlinkSync(item.versoFile);
       }
-      this.scanList = [];
+      this.scaningImageList = [];
     }
   }
 };

+ 235 - 0
src/modules/client/components/ScanTaskProcessDialog.vue

@@ -0,0 +1,235 @@
+<template>
+  <el-dialog
+    class="scan-task-dialog"
+    :visible.sync="modalIsShow"
+    top="10vh"
+    width="550px"
+    :close-on-click-modal="false"
+    :close-on-press-escape="false"
+    :show-close="false"
+    append-to-body
+    @open="visibleChange"
+  >
+    <el-form
+      ref="modalFormComp"
+      :model="modalForm"
+      :rules="rules"
+      label-width="120"
+    >
+      <el-form-item label="课程名称:">
+        {{ task.courseName }}({{ task.courseCode }})
+      </el-form-item>
+      <el-form-item prop="preScanCount" label="预扫张数:">
+        <el-input-number
+          v-model="modalForm.preScanCount"
+          :min="0"
+          :max="1000000"
+          :step="1"
+          step-strictly
+          :controls="false"
+        ></el-input-number>
+      </el-form-item>
+      <el-form-item label="实扫张数:">
+        <span class="color-danger">
+          {{ realScanCount }}
+        </span>
+      </el-form-item>
+    </el-form>
+    <div slot="footer">
+      <el-button v-if="scanStatus === 'INIT'" type="primary" @click="startTask"
+        >开始扫描</el-button
+      >
+      <el-button
+        v-if="scanStatus === 'PAUSE'"
+        type="primary"
+        @click="continueTask"
+        >继续扫描</el-button
+      >
+      <el-button v-if="scanStatus === 'PAUSE'" type="primary" @click="confirm"
+        >确认</el-button
+      >
+      <el-button v-if="scanStatus === 'PAUSE'" @click="cancel">取消</el-button>
+    </div>
+  </el-dialog>
+</template>
+
+<script>
+import {
+  getPreUploadFiles,
+  getPreUploadFileCount,
+  saveOutputImage
+} from "../../../plugins/imageOcr";
+import setTimeMixins from "../../../mixins/setTimeMixins";
+import db from "../../../plugins/db";
+const fs = require("fs");
+const childProcess = require("child_process");
+const util = require("util");
+const childProcessExec = util.promisify(childProcess.exec);
+
+export default {
+  name: "scan-task-dialog",
+  props: {
+    task: {
+      type: Object,
+      default() {
+        return {};
+      }
+    }
+  },
+  mixins: [setTimeMixins],
+  data() {
+    return {
+      modalIsShow: false,
+      loading: false,
+      scanStatus: "INIT", // INIT:初始状态,START:已开始,PAUSE:已暂停
+      modalForm: {
+        preScanCount: 0
+      },
+      rules: {
+        preScanCount: [
+          {
+            required: true,
+            message: "请输入预扫张数",
+            trigger: "change"
+          }
+        ]
+      },
+      scaningImageList: [],
+      realScanCount: 0
+    };
+  },
+  computed: {
+    user() {
+      return this.$store.state.user;
+    }
+  },
+  beforeDestroy() {
+    this.clearSetTs();
+  },
+  methods: {
+    initData() {
+      this.loading = false;
+      this.modalForm.preScanCount = undefined;
+      this.scaningImageList = [];
+      this.scanStatus = "INIT";
+      this.realScanCount = 0;
+    },
+    visibleChange() {
+      this.initData();
+    },
+    close() {
+      this.modalIsShow = false;
+      this.$emit("on-close");
+    },
+    open() {
+      this.modalIsShow = true;
+    },
+    async startTask() {
+      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
+      if (!valid) return;
+
+      if (this.loading) return;
+      this.loading = true;
+
+      this.scaningImageList = [];
+      this.scanStatus = "START";
+      this.getInitFile();
+
+      this.evokeScanExe();
+    },
+    async continueTask() {
+      this.scanStatus = "START";
+      this.getInitFile();
+
+      this.evokeScanExe();
+    },
+    async evokeScanExe() {
+      console.log("唤起扫描仪");
+      const { stdout, stderr } = await childProcessExec("").catch(error => {
+        console.log(error);
+      });
+
+      console.log("扫描仪停止");
+      this.scanStatus = "PAUSE";
+      console.log(stdout, stderr);
+    },
+    async confirm() {
+      const res = getPreUploadFiles(this.GLOBAL.input);
+      if (!res.succeed) {
+        this.$message.error(res.errorMsg);
+        return;
+      }
+
+      this.scaningImageList = res.data;
+      await this.saveScanImage();
+
+      const confirm = await this.$confirm(`是否继续扫描?`, "提示", {
+        type: "warning"
+      }).catch(() => {});
+
+      if (confirm !== "confirm") {
+        this.close();
+        return;
+      }
+
+      this.initData();
+    },
+    async cancel() {
+      const confirm = await this.$confirm(
+        `取消扫描后,当前未保存的数据将会被清空,确定要取消扫描吗?`,
+        "提示",
+        {
+          type: "warning"
+        }
+      ).catch(() => {});
+      if (confirm !== "confirm") return;
+
+      this.clearScanList();
+      this.close();
+    },
+    // scan relate
+    getInitFile() {
+      this.clearSetTs();
+      if (this.scanStatus !== "START") return;
+
+      this.realScanCount = getPreUploadFileCount(this.GLOBAL.input);
+      this.addSetTime(() => {
+        this.getInitFile();
+      }, 500);
+    },
+    async saveScanImage() {
+      for (let i = 0, len = this.scaningImageList.length; i < len; i++) {
+        const files = this.scaningImageList[i];
+        const ouputImageList = saveOutputImage([
+          files.frontFile,
+          files.versoFile
+        ]);
+        const fileInfo = {
+          taskId: this.task.id,
+          taskName: this.task.name,
+          courseCode: this.task.courseCode,
+          courseName: this.task.courseName,
+          teachingClassName: this.task.teachingClassName,
+          frontOriginImgPath: ouputImageList[0],
+          versoOriginImgPath: ouputImageList[1],
+          clientUserId: this.user.userId,
+          clientUsername: this.user.name,
+          clientUserLoginTime: this.user.loginTime
+        };
+
+        await db.saveUploadInfo(fileInfo).catch(err => {
+          console.log(err);
+        });
+      }
+    },
+    clearScanList() {
+      for (let i = 0; i < this.scaningImageList.length; i++) {
+        const item = this.scaningImageList[i];
+        fs.unlinkSync(item.frontFile);
+        fs.unlinkSync(item.versoFile);
+      }
+      this.scaningImageList = [];
+    }
+  }
+};
+</script>

+ 13 - 0
src/modules/client/views/TaskManage.vue

@@ -121,14 +121,26 @@
         </el-pagination>
       </div>
     </div>
+
+    <!-- ScanTaskProcessDialog -->
+    <scan-task-process-dialog
+      ref="ScanTaskProcessDialog"
+      :task="curRow"
+      @on-close="getList"
+    ></scan-task-process-dialog>
   </div>
 </template>
 
 <script>
 import { taskListPage } from "../api";
+// import ScanTaskProcessDialog from "../components/ScanTaskProcessDialog.vue";
+import ScanTaskProcessDialog from "../components/ScanTaskDialog.vue";
 
 export default {
   name: "task-manage",
+  components: {
+    ScanTaskProcessDialog
+  },
   data() {
     return {
       curTab: "all",
@@ -197,6 +209,7 @@ export default {
   },
   toScan(row) {
     this.curRow = row;
+    this.$refs.ScanTaskProcessDialog.open();
   }
 };
 </script>

+ 76 - 0
src/plugins/imageOcr.js

@@ -52,3 +52,79 @@ export function getEarliestFile(dir) {
     name: files[0].name
   };
 }
+
+export function getPreUploadFilesAutoSerial(dir) {
+  const ddir = dir || getInputDir();
+  const files = fs
+    .readdirSync(ddir)
+    .filter(fileName => fileName.toLowerCase().match(/\.(jpg|png|jpeg)/));
+
+  let imageList = [];
+  const len = Math.ceil(files.length / 2);
+  for (let i = 0; i < len; i++) {
+    const frontFile = files[i];
+    const versoFile = files[i + 1];
+    imageList.push({
+      frontFile,
+      versoFile
+    });
+  }
+  return imageList;
+}
+
+export function getPreUploadFileCount(dir) {
+  const ddir = dir || getInputDir();
+  const files = fs
+    .readdirSync(ddir)
+    .filter(fileName => fileName.toLowerCase().match(/\.(jpg|png|jpeg)/));
+
+  return Math.ceil(files.length / 2);
+}
+
+export function getPreUploadFiles(dir) {
+  const ddir = dir || getInputDir();
+  const files = fs
+    .readdirSync(ddir)
+    .filter(fileName => fileName.toLowerCase().match(/\.(json)/));
+
+  let imageList = [];
+  if (!files.length) return { succeed: false, errorMsg: "当前无扫描文件!" };
+
+  const fileCont = fs.readFileSync(path.join(dir, files[0]));
+  const fileInfo = JSON.parse(fileCont);
+
+  if (!fileInfo.succeed) {
+    return { succeed: false, errorMsg: fileInfo.errorMsg };
+  }
+
+  imageList = fileInfo.images.map(item => {
+    return {
+      frontFile: item.front,
+      versoFile: item.back
+    };
+  });
+  if (!imageList.length)
+    return { succeed: false, errorMsg: "当前无扫描文件!" };
+
+  return { succeed: true, data: imageList };
+}
+
+/**
+扫描仪返回的数据
+{
+	"images": [
+		{
+			"duplex": true,
+			"front": "D:/workspace/project/scan-library/test/00000001_F.jpg",
+			"back":"D:/workspace/project/scan-library/test/00000001_B.jpg"
+		},
+		{
+			"duplex": true,
+			"front": "D:/workspace/project/scan-library/test/00000001_B.jpg",
+			"back":"D:/workspace/project/scan-library/test/00000001_B.jpg"
+		}
+	],
+	"succeed": true,
+	"errorMsg":"请将试卷放置到扫描仪上,再进行扫描!"
+}
+ */