瀏覽代碼

考试过程中服务器计时

Michael Wang 4 年之前
父節點
當前提交
e749eacdb2
共有 4 個文件被更改,包括 49 次插入23 次删除
  1. 4 3
      src/App.vue
  2. 27 7
      src/features/OnlineExam/Examing/RemainTime.vue
  3. 16 12
      src/utils/axiosRetry.js
  4. 2 1
      src/utils/monitors.js

+ 4 - 3
src/App.vue

@@ -54,9 +54,10 @@ export default {
             "学生端不是置顶状态",
           ]);
           if (this.$route.name === "OnlineExamingHome") {
-            this.$http.post("/api/ecs_oe_student/examControl/switchScreen", {
-              examRecordDataId: this.$route.params.examRecordDataId,
-            });
+            this.$http.post(
+              "/api/ecs_oe_student/examControl/switchScreen?examRecordDataId=" +
+                this.$route.params.examRecordDataId
+            );
           }
           return;
 

+ 27 - 7
src/features/OnlineExam/Examing/RemainTime.vue

@@ -14,6 +14,7 @@
 <script>
 import moment from "moment";
 import { createNamespacedHelpers } from "vuex";
+import { createLog } from "@/utils/logger";
 const { mapMutations } = createNamespacedHelpers("examingHomeModule");
 
 export default {
@@ -41,7 +42,22 @@ export default {
   },
   async mounted() {
     this.heartbeatErrorNum = 0;
-    this.getRemainTimeFromServer();
+
+    if (this.$route.name === "OnlineExamingHome") {
+      try {
+        await this.$httpPostRetry(
+          "/api/ecs_oe_student/examControl/startAnswer?examRecordDataId=" +
+            this.$route.params.examRecordDataId
+        );
+      } catch (error) {
+        const msg = "/startAnswer 出错";
+        this.logout("?LogoutReason=" + msg);
+        createLog({ error: msg });
+        return;
+      }
+    }
+
+    await this.getRemainTimeFromServer();
     this.heartbeatInterval = setInterval(() => {
       this.getRemainTimeFromServer();
     }, 60 * 1000);
@@ -78,12 +94,16 @@ export default {
           if (res.data <= 0) {
             this.setShouldSubmitPaper();
           }
-          if (this.remainTime === null) {
-            // 仅当剩余考试时间没有被初始化才使用服务器时间。否则使用本机时间。
-            this.remainTime = res.data;
-            console.log("初始化剩余时间:", res.data);
-            this.logger({ action: "初始化剩余时间:", response: res.data });
-          }
+          // if (this.remainTime === null) {
+          // 仅当剩余考试时间没有被初始化才使用服务器时间。否则使用本机时间。
+          this.remainTime = res.data;
+          console.log("重置剩余时间:", res.data);
+          this.logger({
+            action: "重置剩余时间:",
+            response: res.data,
+            diff: this.remainTime - res.data,
+          });
+          // }
           this.heartbeatErrorNum = 0;
         } else {
           this.$Message.error({

+ 16 - 12
src/utils/axiosRetry.js

@@ -1,37 +1,41 @@
 import Vue from "vue";
 
 Vue.prototype.$httpGetRetry = async function (...args) {
+  let success = false;
+  let error;
   for (let i = 0; i < 5; i++) {
     try {
       const res = await Vue.prototype.$http.get.apply(Vue, args);
-
+      success = true;
       return res;
-    } catch (error) {
-      console.log(error);
-      if (!error.response) {
+    } catch (_error) {
+      console.log(_error);
+      error = _error;
+      if (!_error.response) {
         await new Promise((resolve) => setTimeout(resolve, 1000));
         continue; // 网络不通
-      } else {
-        return; // 其他错误不管
       }
     }
   }
+  if (!success) throw error;
 };
 
 Vue.prototype.$httpPostRetry = async function (...args) {
+  let success = false;
+  let error;
   for (let i = 0; i < 5; i++) {
     try {
       const res = await Vue.prototype.$http.post.apply(Vue, args);
-
+      success = true;
       return res;
-    } catch (error) {
-      console.log(error);
-      if (!error.response) {
+    } catch (_error) {
+      console.log(_error);
+      error = _error;
+      if (!_error.response) {
         await new Promise((resolve) => setTimeout(resolve, 1000));
         continue; // 网络不通
-      } else {
-        return; // 其他错误不管
       }
     }
   }
+  if (!success) throw error;
 };

+ 2 - 1
src/utils/monitors.js

@@ -14,6 +14,7 @@ Vue.config.errorHandler = (error) => {
   window._hmt.push(["_trackEvent", "Vue组件错误"]);
   createLog({
     action: "Vue组件错误",
+    path: window.location,
     message: error.message,
     stack: error.stack,
     error: JSON.stringify(error, (key, value) =>
@@ -55,7 +56,7 @@ window.addEventListener("error", function (event) {
   ]);
   createLog({
     action: "全局JS错误",
-    page: window.location.pathname,
+    page: window.location,
     message: event.message,
     stack: event.error.stack,
     error: JSON.stringify(event.error, (key, value) =>