Explorar el Código

fix useFaceCompare被重复调用,会导致snapId不是全局唯一的问题

Michael Wang hace 3 años
padre
commit
4387834ac0
Se han modificado 1 ficheros con 20 adiciones y 8 borrados
  1. 20 8
      src/features/OnlineExam/Examing/setups/useFaceCompare.ts

+ 20 - 8
src/features/OnlineExam/Examing/setups/useFaceCompare.ts

@@ -2,26 +2,36 @@ import { httpApp } from "@/plugins/axiosApp";
 import { useTimers } from "@/setups/useTimers";
 import { store } from "@/store/store";
 import { showLogout } from "@/utils/utils";
-import { onUnmounted, watch } from "vue";
+import { onUnmounted, Ref, ref, watch } from "vue";
 
 // 不管被请求多少次,interval只需要初始化一次
-let snapIntervalLock = false;
+// useFaceCompare被重复调用,会导致snapId不是全局唯一的问题
+
+type Ret = {
+  snapId: Ref<number>;
+  doSnap: () => void;
+  showSnapResult: (
+    fileName: string,
+    examRecordDataId: string | number
+  ) => Promise<void>;
+};
+let singleton: Ret;
 /** 人脸后台Face++比对 */
-export function useFaceCompare() {
+export function useFaceCompare(): Ret {
+  if (singleton) return singleton;
+
   const { addInterval, addTimeout } = useTimers();
 
   let initSnapInterval: number;
   let initSnapshotTrialTimes = 0;
 
-  let snapId = $ref(0);
+  const snapId = ref(0);
 
   watch(
     () => store.exam.faceCheckEnabled,
     () => {
       if (!store.exam.faceCheckEnabled) return;
 
-      if (snapIntervalLock) return;
-      snapIntervalLock = true;
       initSnapInterval = addInterval(() => {
         logger({
           cnl: ["server"],
@@ -86,7 +96,7 @@ export function useFaceCompare() {
   );
 
   function doSnap() {
-    snapId = Date.now();
+    snapId.value = Date.now();
   }
 
   // 离开此页面时,可能还有心跳请求未返回
@@ -136,5 +146,7 @@ export function useFaceCompare() {
     }
   }
 
-  return { snapId: $$(snapId), doSnap, showSnapResult };
+  singleton = { snapId, doSnap, showSnapResult };
+
+  return singleton;
 }