瀏覽代碼

限流:去除网络超时引起的限流统计日志不准

Michael Wang 4 年之前
父節點
當前提交
f5eff27515
共有 3 個文件被更改,包括 14 次插入4 次删除
  1. 5 1
      src/features/Login/Login.vue
  2. 5 1
      src/features/OnlineExam/OnlineExamList.vue
  3. 4 2
      src/utils/tryLimit.js

+ 5 - 1
src/features/Login/Login.vue

@@ -492,7 +492,10 @@ export default {
       }
       this.loginBtnLoading = true;
 
-      const limitResult = await tryLimit({ action: "login", limit: 500 });
+      const { limitResult, serverOk } = await tryLimit({
+        action: "login",
+        limit: 500,
+      });
       this.logger({
         action: "登录页面--login clicked",
         logId: "登录限流API call",
@@ -501,6 +504,7 @@ export default {
         createLog({
           currentPage: "登录页面--login clicked",
           action: "登录被限流",
+          serverOk,
         });
         this.$Modal.warning({
           title: "提示",

+ 5 - 1
src/features/OnlineExam/OnlineExamList.vue

@@ -180,7 +180,10 @@ export default {
       );
 
       this.enterButtonClicked = true;
-      const limitResult = await tryLimit({ action: "startExam", limit: 100 });
+      const { limitResult, serverOk } = await tryLimit({
+        action: "startExam",
+        limit: 100,
+      });
       this.logger({
         action: "在线考试列表页面",
         logId: "开考限流API call",
@@ -208,6 +211,7 @@ export default {
           action: "在线考试列表页面",
           logId: "开考被限流",
           detail: "限流-" + minutesAfterCourseStart + "分被限流",
+          serverOk,
         });
         this.$Modal.warning({
           title: "提示",

+ 4 - 2
src/utils/tryLimit.js

@@ -13,6 +13,7 @@ export async function tryLimit({ action, limit = 100 }) {
 
     if (!serverPass) {
       // 休眠 1 ~ 5 秒 再试
+      res = null; // 供外部判断
 
       const sleepTime = Math.random() * 4 + 1;
       console.log({ sleepTime, now: Date.now() });
@@ -25,13 +26,14 @@ export async function tryLimit({ action, limit = 100 }) {
       serverPass = res.data.pass;
       createLog({
         action: "限流后自动重试",
+        limitAction: action,
         detail: serverPass ? "进入" : "依然限流",
       });
     }
   } catch (error) {
     console.log(error);
-    window._hmt.push(["_trackEvent", "在线考试列表页面", "获取rateLimit失败"]);
+    window._hmt.push(["_trackEvent", action, "获取rateLimit失败"]);
   }
 
-  return serverPass;
+  return { limitResult: serverPass, serverOk: !!res };
 }