Browse Source

cugr: 图片上传PDF;当前特性没启用

Michael Wang 5 years ago
parent
commit
fe17984e74

+ 3 - 0
src/features/OfflineExam/OfflineExamList.vue

@@ -136,6 +136,9 @@ export default {
   computed: {
     ...globalMapState(["user", "timeDifference"]),
     ...mapGetters(["isCug"]),
+    // enableFeFeature() {
+    //   return !!!this.FE_FEATURE_IMAGE_PDF;
+    // },
   },
   methods: {
     async enterExam(course) {

+ 88 - 46
src/features/OfflineExam/OfflineExamUploadCug.vue

@@ -32,6 +32,10 @@
         {{ loadingStatus ? "上传中..." : "上传" }}
       </i-button>
     </div>
+    <Modal v-model="showPreview" fullscreen footer-hide :closable="false">
+      <!-- <div slot="header"></div> -->
+      <img id="previewId" />
+    </Modal>
   </div>
 </template>
 
@@ -59,6 +63,9 @@ export default {
       loadingStatus: false,
       uploadFileFormat: [],
       uploadFileAccept: "",
+      // imageSrc
+      previewImage: null,
+      showPreview: false,
     };
   },
   async created() {
@@ -72,13 +79,14 @@ export default {
       (res.data.OFFLINE_UPLOAD_FILE_TYPE &&
         JSON.parse(res.data.OFFLINE_UPLOAD_FILE_TYPE)) ||
       [];
-    this.uploadFileFormat.push(...["jpeg", "jpg"]);
-    this.uploadFileFormat = this.uploadFileFormat.map(v => v.toLowerCase());
     this.uploadFileAccept =
-      "image/jpeg," + this.uploadFileFormat.map(v => "application/" + v).join();
+      "image/jpeg," +
+      this.uploadFileFormat.map(v => "application/" + v.toLowerCase()).join();
     if (this.uploadFileAccept.includes("zip")) {
       this.uploadFileAccept = ".zip," + this.uploadFileAccept;
     }
+    this.uploadFileFormat.push(...["jpeg", "jpg"]);
+    this.uploadFileFormat = this.uploadFileFormat.map(v => v.toLowerCase());
   },
   methods: {
     fileFormatCheck(file, resolve, reject) {
@@ -216,6 +224,7 @@ export default {
         closable: true,
       });
       this.$emit("reloadList");
+      this.showPreview = false;
     },
     handleError(error, file) {
       window._hmt.push(["_trackEvent", "离线考试页面", "上传作答", "上传失败"]);
@@ -227,6 +236,7 @@ export default {
         duration: 15,
         closable: true,
       });
+      this.showPreview = false;
     },
     handleFormatError(file) {
       this.file = null;
@@ -239,6 +249,7 @@ export default {
           this.uploadFileFormat.join(" 或 ") +
           "  文件。",
       });
+      this.showPreview = false;
     },
     handleMaxSize(file) {
       this.file = null;
@@ -247,6 +258,7 @@ export default {
         title: "超出文件大小限制",
         desc: file.name + " 太大,作答文件不能超过30M.",
       });
+      this.showPreview = false;
     },
     async handleBeforeUpload(file) {
       const suffix = file.name.split(".").pop();
@@ -268,53 +280,83 @@ export default {
           } else {
             resolve();
           }
-        }).then(() => {
-          return printCurrentPage()
-            .then(filename => {
-              const fs = window.nodeRequire("fs");
-              const path = window.nodeRequire("path");
-              // const fileNew = {
-              //   name: path.basename(filename),
-              //   // filename: path.basename(filename),
-              //   uri: "file://" + filename,
-              //   // path: filename,
-              //   type: "application/pdf",
-              // };
-              const fileNew = new File(
-                [fs.readFileSync(filename)],
-                path.basename(filename),
-                { type: "application/pdf" } // what I upload is image.
-              );
-              this.file = fileNew;
+        })
+          .then(() => {
+            this.showPreview = true;
+            this.$Message.info({
+              content: "正在准备将图片转为PDF...",
+              duration: 1.5,
+              closable: true,
+            });
 
-              var stats = fs.statSync(filename);
-              var fileSizeInBytes = stats["size"];
-              if (fileSizeInBytes > 1024 * 30 * 1024) {
-                this.handleMaxSize();
-                throw "exceed max size";
-              }
+            return new Promise(resolve => {
+              var reader = new FileReader();
+              reader.onload = function(e) {
+                document
+                  .getElementById("previewId")
+                  .setAttribute("src", e.target.result);
 
-              const formData = new FormData();
-              formData.append("fileType", "pdf");
-              formData.append("file", fileNew);
+                setTimeout(resolve, 2500);
+                // resolve();
+              };
+              //Imagepath.files[0] is blob type
+              reader.readAsDataURL(file);
+            });
+          })
+          .then(() => {
+            return printCurrentPage()
+              .then(filename => {
+                const fs = window.nodeRequire("fs");
+                const path = window.nodeRequire("path");
+                // const fileNew = {
+                //   name: path.basename(filename),
+                //   // filename: path.basename(filename),
+                //   uri: "file://" + filename,
+                //   // path: filename,
+                //   type: "application/pdf",
+                // };
+                const fileNew = new File(
+                  [fs.readFileSync(filename)],
+                  path.basename(filename),
+                  { type: "application/pdf" } // what I upload is image.
+                );
+                this.file = fileNew;
 
-              return this.$http
-                .post(
-                  "/api/ecs_oe_admin/offlineExam/submitPaper?examRecordDataId=" +
-                    this.course.examRecordDataId,
-                  formData
-                )
-                .then(() => {
-                  this.handleSuccess();
-                })
-                .catch(() => {
-                  this.handleError();
+                this.$Message.info({
+                  content: "图片转换成功,正在上传...",
+                  duration: 2,
+                  closable: true,
                 });
-            })
-            .catch(error => {
-              console.log(error);
-            });
-        });
+
+                var stats = fs.statSync(filename);
+                var fileSizeInBytes = stats["size"];
+                if (fileSizeInBytes > 1024 * 30 * 1024) {
+                  this.handleMaxSize();
+                  throw "exceed max size";
+                }
+
+                const formData = new FormData();
+                formData.append("fileType", "pdf");
+                formData.append("file", fileNew);
+
+                return this.$http
+                  .post(
+                    "/api/ecs_oe_admin/offlineExam/submitPaper?examRecordDataId=" +
+                      this.course.examRecordDataId,
+                    formData
+                  )
+                  .then(() => {
+                    this.handleSuccess();
+                  })
+                  .catch(() => {
+                    this.handleError();
+                  });
+              })
+              .catch(error => {
+                console.log(error);
+                this.handleError();
+              });
+          });
         return Promise.reject("图片另外路径上传");
       }
 

+ 0 - 6
src/features/OfflineExam/imageToPdf.js

@@ -11,12 +11,6 @@ export function printCurrentPage() {
       },
       (error, data) => {
         if (error) {
-          // console.log(error);
-          this.$alert(error, "失败", {
-            confirmButtonText: "确定",
-          });
-          this.isPrint = false;
-          // throw error;
           reject(error);
         }
 

+ 2 - 1
src/store.js

@@ -239,7 +239,8 @@ export default new Vuex.Store({
       return state.user.schoolDomain === EPCC_DOMAIN;
     },
     isCug(state) {
-      return state.user.schoolDomain === CUG_DOMAIN;
+      const FE_FEATURE_IMAGE_PDF = false;
+      return state.user.schoolDomain === CUG_DOMAIN && FE_FEATURE_IMAGE_PDF;
     },
   },
 });