Browse Source

时长统计

15591641157 2 months ago
parent
commit
f5ca354fde
1 changed files with 21 additions and 24 deletions
  1. 21 24
      packages/app/src/composables/analysis.ts

+ 21 - 24
packages/app/src/composables/analysis.ts

@@ -18,30 +18,29 @@ export const useAnalysis = (automatic: boolean, isApp = false) => {
   const { userInfo, isLogined } = storeToRefs(userStore)
   const viewDuration = ref(0)
   const viewStartAt = ref<Date | null>(null);
-  const totalUsageTime = ref(0);
   const option = ref<Record<string, any>>({})
 
-  const report = async (type) => {
-    if (!isLogined.value) return
-    const duration = automatic
-      ? (viewDuration.value = dayjs().diff(viewStartAt.value, 'second'))
-      : 0
+  const report = async (type: AnalysisEventType, duration: number) => {
+    if (!isLogined.value) return;
 
-    await createBrowseRecord({
-      stylistId: userInfo.value.userId,
-      bizType: type,
-      duration,
-      ...option.value,
-    })
-    viewDuration.value = 0
-    viewStartAt.value = undefined
-    option.value = {}
-  }
+    try {
+      await createBrowseRecord({
+        stylistId: userInfo.value.userId,
+        bizType: type,
+        duration,
+        ...option.value,
+      });
+    } catch (error) {
+      console.error('Failed to report:', error);
+    } finally {
+      option.value = {};
+    }
+  };
 
   onLaunch(async () => {
     if (automatic) {
       if (isApp) {
-        await report(AnalysisEventType.LaunchApp)
+        await report(AnalysisEventType.LaunchApp,0)
       }
     }
   })
@@ -59,14 +58,12 @@ export const useAnalysis = (automatic: boolean, isApp = false) => {
   })
   onHide(async () => {
     if (automatic && viewStartAt.value) {
-      const usageTime = dayjs().diff(viewStartAt.value, 'second'); // 本次使用时长(秒)
-      totalUsageTime.value += usageTime; // 累计到总时长
-      console.log(`本次使用时长:${usageTime} 秒`);
-      console.log(`累计使用时长:${totalUsageTime.value} 秒`);
-
-      await report(AnalysisEventType.ViewPage); // 上报页面浏览记录
+      const duration = dayjs().diff(viewStartAt.value, 'second');
+      console.log(`本次使用时长:${duration} 秒`);
+      await report(AnalysisEventType.ViewPage, duration);
     }
-  })
+  });
+
   onUnload(async () => {
     // if (automatic) {
     //   await report(AnalysisEventType.ViewPage)