瀏覽代碼

SLS日志记录的某种方式会cancel request

Michael Wang 5 年之前
父節點
當前提交
43b169e329
共有 5 個文件被更改,包括 21 次插入13 次删除
  1. 1 0
      .env.development
  2. 3 3
      src/features/Login/Login.vue
  3. 7 7
      src/features/OnlineExam/Examing/ws.js
  4. 2 2
      src/mixins/mixins.js
  5. 8 1
      src/utils/loghub-tracking.js

+ 1 - 0
.env.development

@@ -13,6 +13,7 @@ VUE_APP_TK_SERVER_API_URL=http://ecs-test.qmth.com.cn
 VUE_APP_CORE_HOST_URL=https://ecs-test.qmth.com.cn:8878
 VUE_APP_OE_ADMIN_HOST_URL=https://ecs-test.qmth.com.cn:8878
 VUE_APP_OE_STUDENT_HOST_URL=https://ecs-test.qmth.com.cn:8878
+VUE_APP_SLS_STORE_NAME=student-client-test
 
 # VUE_APP_CONFIG_FILE_SEVER_URL=https://ecs-test-static.qmth.com.cn
 # VUE_APP_TK_SERVER_HTML_URL=http://ecs-dev.qmth.com.cn

+ 3 - 3
src/features/Login/Login.vue

@@ -131,7 +131,7 @@ import {
   VUE_APP_CONFIG_FILE_SEVER_URL,
 } from "@/constants/constants";
 import UA, { chromeUA } from "@/utils/ua.js";
-import { createLog } from "@/utils/logger";
+import { createLog, createUserLog } from "@/utils/logger";
 
 // 检测devtools.  仅在chrome 72+ 有效。
 let element = new Image();
@@ -559,6 +559,8 @@ export default {
             this.$route.query.LogoutReason,
           ]);
           const alreadyInExam = await this.checkExamInProgress();
+          window._hmt.push(["_trackEvent", "登录页面", "登录成功"]);
+          createUserLog({ action: "登录成功" });
           if (alreadyInExam) {
             this.logger({ action: "断点续考", detail: "登录页面" });
             window._hmt.push([
@@ -569,8 +571,6 @@ export default {
             ]);
             return;
           }
-          window._hmt.push(["_trackEvent", "登录页面", "登录成功"]);
-          this.logger({ action: "登录成功" });
           this.$router.push("/online-exam");
           let userIds = JSON.parse(localStorage.getItem("userIds"));
           userIds = [...new Set(userIds).add(user.id)];

+ 7 - 7
src/features/OnlineExam/Examing/ws.js

@@ -1,7 +1,7 @@
 import store from "@/store";
 import { Message } from "iview";
 import { VUE_APP_WK_SERVER_SOCKET_FOR_AUDIO } from "@/constants/constants";
-import { createUserLog } from "@/utils/logger";
+import { createLog } from "@/utils/logger";
 
 let ws;
 let shouldReconnect = true;
@@ -13,7 +13,7 @@ let reconnectNumber = 0;
 export function openWS({ examRecordDataId }) {
   window._hmt.push(["_trackEvent", "websocket", "准备连接"]);
   console.log("in openWS", examRecordDataId);
-  createUserLog({ type: "websocket", action: "准备连接", examRecordDataId });
+  createLog({ type: "websocket", action: "准备连接", examRecordDataId });
   ws = new WebSocket(
     VUE_APP_WK_SERVER_SOCKET_FOR_AUDIO +
       `?key=${store.state.user.key}&token=${store.state.user.token}`
@@ -27,7 +27,7 @@ export function openWS({ examRecordDataId }) {
 
     ws.onclose = () => {
       console.log("ws close by server");
-      createUserLog({ type: "websocket", action: "ws close by server" });
+      createLog({ type: "websocket", action: "ws close by server" });
       for (const heartbeatId of heartbeatIds) {
         clearInterval(heartbeatId);
       }
@@ -55,7 +55,7 @@ export function openWS({ examRecordDataId }) {
             "websocket",
             "连接被关闭后-准备连接",
           ]);
-          createUserLog({
+          createLog({
             type: "websocket",
             action: "连接被关闭后-准备连接",
             detail: "onclose",
@@ -88,7 +88,7 @@ export function openWS({ examRecordDataId }) {
       }
       if (location.href.includes("/order/")) {
         window._hmt.push(["_trackEvent", "websocket", "连接错误后-重新连接"]);
-        createUserLog({
+        createLog({
           type: "websocket",
           action: "连接被关闭后-准备连接",
           detail: "onerror",
@@ -118,13 +118,13 @@ function heartbeat() {
 }
 
 export function closeWsWithoutReconnect() {
-  createUserLog({ type: "websocket", action: "客户端准备关闭ws。" });
+  createLog({ type: "websocket", action: "客户端准备关闭ws。" });
   shouldReconnect = false;
   try {
     if (ws && ws.readyState === 1) ws.close();
   } catch (e) {
     console.log("关闭ws异常。");
-    createUserLog({ type: "websocket", action: "关闭ws异常。", detail: e });
+    createLog({ type: "websocket", action: "关闭ws异常。", detail: e });
   }
 }
 

+ 2 - 2
src/mixins/mixins.js

@@ -1,5 +1,5 @@
 import Vue from "vue";
-import { createUserLog } from "@/utils/logger";
+import { createLog } from "@/utils/logger";
 
 Vue.mixin({
   beforeCreate() {
@@ -16,7 +16,7 @@ Vue.mixin({
   },
   methods: {
     logger(logs) {
-      createUserLog(logs);
+      createLog(logs);
     },
     async serverLog(level, logText) {
       try {

+ 8 - 1
src/utils/loghub-tracking.js

@@ -40,7 +40,14 @@
         ++k;
       }
       try {
-        this.httpRequest_.open("GET", url, true);
+        /**
+         * 为了不阻塞页面加载,脚本会异步发送HTTP请求,如果页面加载过程中需要多次发送数据,后面的请求会覆盖前面的HTTP请求,
+         * 看到的现象是浏览器中会显示Web Tracking请求退出。使用同步发送可以避免该问题,将脚本中的内容进行如下替换,可实现同步发送。
+         *
+         * https://help.aliyun.com/document_detail/31752.html
+         */
+
+        this.httpRequest_.open("GET", url, false);
         this.httpRequest_.send(null);
       } catch (ex) {
         if (