Browse Source

重构交卷逻辑:提取公共方法

Michael Wang 5 years ago
parent
commit
7b8c91351f

+ 12 - 74
src/features/Login/Login.vue

@@ -320,7 +320,18 @@ export default {
             "登录",
             this.$route.query.LogoutReason,
           ]);
-          await this.checkExamInProgress();
+          const alreadyInExam = await this.checkExamInProgress();
+          if (alreadyInExam) {
+            window._hmt.push([
+              "_trackEvent",
+              "登录页面",
+              "断点续考",
+              "重新登录",
+            ]);
+            this.logout("?LogoutReason=登录页面获取断点续考信息异常");
+            return;
+          }
+          this.$router.push("/online-exam");
           window._hmt.push(["_trackEvent", "登录页面", "登录成功"]);
         } catch (error) {
           window._hmt.push([
@@ -340,79 +351,6 @@ export default {
         this.errorInfo = data.desc;
       }
     },
-    async checkExamInProgress() {
-      try {
-        // 断点续考
-        const examingRes = (await this.$http.get(
-          "/api/ecs_oe_student/examControl/checkExamInProgress"
-        )).data;
-        if (examingRes.isExceed) {
-          // 超出断点续考次数的逻辑,仅此block
-          this.$Message.info({
-            content: `超出最大断点续考次数(${
-              examingRes.maxInterruptNum
-            }),正在自动交卷...`,
-            duration: 15,
-            closable: true,
-          });
-          this.$Spin.show({
-            render: () => {
-              return (
-                <div style="font-size: 24px">
-                  超出最大断点续考次数({examingRes.maxInterruptNum}
-                  ),正在自动交卷...
-                </div>
-              );
-            },
-          });
-          const res = await this.$http.get(
-            "/api/ecs_oe_student/examControl/endExam"
-          );
-          if (res.status === 200) {
-            this.$router.replace({
-              path: `/online-exam/exam/${examingRes.examId}/examRecordData/${
-                examingRes.examRecordDataId
-              }/end`,
-            });
-            this.$Spin.hide();
-          } else {
-            this.$Message.error({
-              content: "交卷失败",
-              duration: 15,
-              closable: true,
-            });
-          }
-          return;
-        }
-        if (examingRes) {
-          this.$Spin.show({
-            render: () => {
-              return <div style="font-size: 24px">正在进入断点续考...</div>;
-            },
-          });
-          window._hmt.push(["_trackEvent", "登录页面", "断点续考", "重新登录"]);
-          this.$router.push(
-            `/online-exam/exam/${examingRes.examId}/examRecordData/${
-              examingRes.examRecordDataId
-            }/order/1` +
-              (examingRes.faceVerifyMinute
-                ? `?faceVerifyMinute=${examingRes.faceVerifyMinute}`
-                : "")
-          );
-          setTimeout(() => this.$Spin.hide(), 1000);
-          return;
-        }
-        this.$router.push("/online-exam");
-      } catch (error) {
-        this.$Message.error({
-          content: "获取断点续考信息异常,退出登录",
-          duration: 15,
-          closable: true,
-        });
-        this.logout("?LogoutReason=登录页面获取断点续考信息异常");
-        return;
-      }
-    },
     async checkNewVersion() {
       let myHeaders = new Headers();
       myHeaders.append("Content-Type", "application/javascript");

+ 7 - 79
src/features/OnlineExam/OnlineExamList.vue

@@ -110,91 +110,19 @@ export default {
         moment(course.endTime)
       );
     },
-    async checkExamInProgress() {
-      try {
-        // 断点续考
-        const examingRes = (await this.$http.get(
-          "/api/ecs_oe_student/examControl/checkExamInProgress"
-        )).data;
-
-        if (examingRes.isExceed) {
-          // 超出断点续考次数的逻辑,仅此block
-          this.$Message.info({
-            content: `超出最大断点续考次数(${
-              examingRes.maxInterruptNum
-            }),正在自动交卷...`,
-            duration: 30,
-            closable: true,
-          });
-          this.$Spin.show({
-            render: () => {
-              return (
-                <div style="font-size: 24px">
-                  超出最大断点续考次数({examingRes.maxInterruptNum}
-                  ),正在自动交卷...
-                </div>
-              );
-            },
-          });
-          const res = await this.$http.get(
-            "/api/ecs_oe_student/examControl/endExam"
-          );
-          this.$Message.destroy();
-          if (res.status === 200) {
-            this.$router.replace({
-              path: `/online-exam/exam/${examingRes.examId}/examRecordData/${
-                examingRes.examRecordDataId
-              }/end`,
-            });
-            this.$Spin.hide();
-          } else {
-            this.$Message.error({
-              content: "交卷失败",
-              duration: 15,
-              closable: true,
-            });
-          }
-          return;
-        }
-
-        if (examingRes) {
-          window._hmt.push([
-            "_trackEvent",
-            "在线考试列表页面",
-            "断点续考",
-            "概览页返回",
-          ]);
-          this.$Spin.show({
-            render: () => {
-              return <div style="font-size: 24px">正在进入断点续考...</div>;
-            },
-          });
-          this.$router.push(
-            `/online-exam/exam/${examingRes.examId}/examRecordData/${
-              examingRes.examRecordDataId
-            }/order/1` +
-              (examingRes.faceVerifyMinute
-                ? `?faceVerifyMinute=${examingRes.faceVerifyMinute}`
-                : "")
-          );
-          setTimeout(() => this.$Spin.hide(), 1000);
-          return true;
-        }
-      } catch (error) {
-        this.$Message.error({
-          content: "获取断点续考信息异常",
-          duration: 15,
-          closable: true,
-        });
-        return true;
-      }
-    },
     async enterExam(course) {
       this.spinShow = true;
       this.processingMessage = "正在检测断点续考信息...";
+
       const alreadyInExam = await this.checkExamInProgress();
       if (alreadyInExam) {
         this.spinShow = false;
+        window._hmt.push([
+          "_trackEvent",
+          "在线考试列表页面",
+          "断点续考",
+          "进入",
+        ]);
         return;
       }
 

+ 0 - 1
src/features/OnlineExam/OnlineExamOverview.vue

@@ -81,7 +81,6 @@ export default {
           `/BEFORE_EXAM_REMARK`
       );
       this.beforeExamRemark = exam.data || "";
-      // await this.$http.get("/api/ecs_oe_student/examControl/endExam");
       const res = await this.$http.get(
         "/api/ecs_oe_student/examControl/startExam?examStudentId=" +
           this.$route.query.examStudentId

+ 4 - 74
src/features/OnlinePractice/OnlinePracticeList.vue

@@ -82,82 +82,12 @@ export default {
         moment(course.endTime)
       );
     },
-    async checkExamInProgress() {
-      try {
-        // 断点续考
-        const examingRes = (await this.$http.get(
-          "/api/ecs_oe_student/examControl/checkExamInProgress"
-        )).data;
-
-        if (examingRes.isExceed) {
-          // 超出断点续考次数的逻辑,仅此block
-          this.$Message.info({
-            content: `超出最大断点续考次数(${
-              examingRes.maxInterruptNum
-            }),正在自动交卷...`,
-            duration: 30,
-            closable: true,
-          });
-          this.$Spin.show({
-            render: () => {
-              return (
-                <div style="font-size: 24px">
-                  超出最大断点续考次数({examingRes.maxInterruptNum}
-                  ),正在自动交卷...
-                </div>
-              );
-            },
-          });
-          const res = await this.$http.get(
-            "/api/ecs_oe_student/examControl/endExam"
-          );
-          this.$Message.destroy();
-          if (res.status === 200) {
-            this.$router.replace({
-              path: `/online-exam/exam/${examingRes.examId}/examRecordData/${
-                examingRes.examRecordDataId
-              }/end`,
-            });
-            this.$Spin.hide();
-          } else {
-            this.$Message.error({
-              content: "交卷失败",
-              duration: 15,
-              closable: true,
-            });
-          }
-          return;
-        }
-
-        if (examingRes) {
-          this.$Spin.show({
-            render: () => {
-              return <div style="font-size: 24px">正在进入断点续考...</div>;
-            },
-          });
-          this.$router.push(
-            `/online-exam/exam/${examingRes.examId}/examRecordData/${
-              examingRes.examRecordDataId
-            }/order/1` +
-              (examingRes.faceVerifyMinute
-                ? `?faceVerifyMinute=${examingRes.faceVerifyMinute}`
-                : "")
-          );
-          setTimeout(() => this.$Spin.hide(), 1000);
-          return true;
-        }
-      } catch (error) {
-        this.$Message.error({
-          content: "获取断点续考信息异常",
-          duration: 15,
-          closable: true,
-        });
-        return true;
-      }
-    },
     async enterPractice(course) {
       const alreadyInExam = await this.checkExamInProgress();
-      if (alreadyInExam) return;
+      if (alreadyInExam) {
+        window._hmt.push(["_trackEvent", "在线练习页面", "断点续考", "进入"]);
+        return;
+      }
 
       window._hmt.push(["_trackEvent", "在线练习页面", "进入练习"]);
       this.$router.push(

+ 1 - 37
src/main.js

@@ -5,6 +5,7 @@ import store from "./store";
 import "./registerServiceWorker";
 
 import "./plugins/iview.js";
+import "./mixins/mixins.js";
 import "./styles/global.css";
 
 // import "./plugins/tracking.js";
@@ -75,43 +76,6 @@ if (process.env.NODE_ENV === "development") {
   })();
 }
 
-Vue.mixin({
-  beforeCreate() {
-    if (document.getElementById("app-placeholder"))
-      document.getElementById("app-placeholder").remove();
-  },
-  mounted() {
-    // console.log("mixin hook called");
-    [...document.getElementsByTagName("a")].forEach(e => {
-      e.addEventListener("click", function(event) {
-        event.preventDefault();
-      });
-    });
-  },
-  methods: {
-    async serverLog(level, logText) {
-      try {
-        await this.$http.post(
-          "/api/ecs_core/log/studentClient/" + level,
-          logText,
-          {
-            headers: {
-              "Content-Type": "text/plain",
-            },
-          }
-        );
-      } catch (error) {
-        console.log(error);
-      }
-    },
-    logout(cause = "") {
-      localStorage.removeItem("phoneVerified");
-      window._hmt.push(["_trackEvent", "退出", cause]);
-      this.$router.push("/login/" + localStorage.getItem("domain") + cause);
-    },
-  },
-});
-
 if (
   process.env.NODE_ENV === "development" &&
   process.env.VUE_APP_ENABLE_VUE_RENDER_LOGS === "true"

+ 110 - 0
src/mixins/mixins.js

@@ -0,0 +1,110 @@
+import Vue from "vue";
+
+Vue.mixin({
+  beforeCreate() {
+    if (document.getElementById("app-placeholder"))
+      document.getElementById("app-placeholder").remove();
+  },
+  mounted() {
+    // console.log("mixin hook called");
+    [...document.getElementsByTagName("a")].forEach(e => {
+      e.addEventListener("click", function(event) {
+        event.preventDefault();
+      });
+    });
+  },
+  methods: {
+    async serverLog(level, logText) {
+      try {
+        await this.$http.post(
+          "/api/ecs_core/log/studentClient/" + level,
+          logText,
+          {
+            headers: {
+              "Content-Type": "text/plain",
+            },
+          }
+        );
+      } catch (error) {
+        console.log(error);
+      }
+    },
+    async checkExamInProgress() {
+      try {
+        // 断点续考
+        const examingRes = (await this.$http.get(
+          "/api/ecs_oe_student/examControl/checkExamInProgress"
+        )).data;
+
+        if (examingRes.isExceed) {
+          // 超出断点续考次数的逻辑,仅此block
+          this.$Message.info({
+            content: `超出最大断点续考次数(${
+              examingRes.maxInterruptNum
+            }),正在自动交卷...`,
+            duration: 15,
+            closable: true,
+          });
+          this.$Spin.show({
+            render: () => {
+              return (
+                <div style="font-size: 24px">
+                  超出最大断点续考次数({examingRes.maxInterruptNum}
+                  ),正在自动交卷...
+                </div>
+              );
+            },
+          });
+          const res = await this.$http.get(
+            "/api/ecs_oe_student/examControl/endExam"
+          );
+          if (res.status === 200) {
+            this.$router.replace({
+              path: `/online-exam/exam/${examingRes.examId}/examRecordData/${
+                examingRes.examRecordDataId
+              }/end`,
+            });
+            this.$Spin.hide();
+          } else {
+            this.$Message.error({
+              content: "交卷失败",
+              duration: 15,
+              closable: true,
+            });
+          }
+          throw "交卷失败";
+        }
+
+        if (examingRes) {
+          this.$Spin.show({
+            render: () => {
+              return <div style="font-size: 24px">正在进入断点续考...</div>;
+            },
+          });
+          this.$router.push(
+            `/online-exam/exam/${examingRes.examId}/examRecordData/${
+              examingRes.examRecordDataId
+            }/order/1` +
+              (examingRes.faceVerifyMinute
+                ? `?faceVerifyMinute=${examingRes.faceVerifyMinute}`
+                : "")
+          );
+          setTimeout(() => this.$Spin.hide(), 1000);
+          return true;
+        }
+      } catch (error) {
+        this.$Message.error({
+          content: "获取断点续考信息异常",
+          duration: 15,
+          closable: true,
+        });
+        throw error;
+      }
+    },
+    logout(cause = "") {
+      localStorage.removeItem("phoneVerified");
+      window._hmt.push(["_trackEvent", "退出", cause]);
+      this.$router.push("/login/" + localStorage.getItem("domain") + cause);
+    },
+  },
+});