瀏覽代碼

日志记录优化

hongrunxia 3 天之前
父節點
當前提交
d0c17a5942
共有 2 個文件被更改,包括 45 次插入40 次删除
  1. 1 1
      src/router/guard/permissionGuard.ts
  2. 44 39
      src/router/helper/menuHelper.ts

+ 1 - 1
src/router/guard/permissionGuard.ts

@@ -278,6 +278,6 @@ export function createPermissionGuard(router: Router) {
   });
 
   router.afterEach(async (to, from) => {
-    await addBrowseLog(to, from);
+    await addBrowseLog(to, from, [...whitePathList, PAGE_NOT_FOUND_ROUTE]);
   });
 }

+ 44 - 39
src/router/helper/menuHelper.ts

@@ -6,6 +6,7 @@ import { isUrl } from '/@/utils/is';
 import { RouteParams } from 'vue-router';
 import { toRaw } from 'vue';
 import { defHttp } from '/@/utils/http/axios';
+import { useUserStoreWithOut } from '/@/store/modules/user';
 
 let currentRouter = '';
 export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
@@ -98,57 +99,61 @@ export function configureDynamicParamsMenu(menu: Menu, params: RouteParams) {
   menu.children?.forEach((item) => configureDynamicParamsMenu(item, params));
 }
 
-export async function addBrowseLog(to, from) {
-  let currentBrowseId = '';
-  if (to.path !== '/sys/log/addBrowseLog') {
-    const url = '/sys/log/addBrowseLog';
+export async function addBrowseLog(to, from, whitePathList) {
+  const userStore = useUserStoreWithOut();
+  const token = userStore.getToken;
+  if (token) {
+    let currentBrowseId = '';
+    if (to.path !== '/sys/log/addBrowseLog') {
+      const url = '/sys/log/addBrowseLog';
 
-    // 生成时间戳函数
-    const formatTimestamp = () => {
-      const date = new Date();
-      return [
-        date.getFullYear(),
-        String(date.getMonth() + 1).padStart(2, '0'),
-        String(date.getDate()).padStart(2, '0'),
-        String(date.getHours()).padStart(2, '0'),
-        String(date.getMinutes()).padStart(2, '0'),
-        String(date.getSeconds()).padStart(2, '0'),
-        String(date.getMilliseconds()).padStart(3, '0'),
-      ].join('');
-    };
-    // 2. 记录新页面进入日志
-    currentBrowseId = formatTimestamp();
-    if (!currentRouter) {
-      currentRouter = to.fullPath;
-      try {
-        await defHttp.post({
-          url,
-          params: {
-            browseId: currentBrowseId,
-            isEnd: false,
-            method: to.fullPath,
-          },
-        });
-        console.log('进入页面日志记录成功');
-      } catch (e) {
-        console.error('进入页面日志记录失败:', e);
-      }
-    } else {
-      if (from.fullPath === currentRouter) {
+      // 生成时间戳函数
+      const formatTimestamp = () => {
+        const date = new Date();
+        return [
+          date.getFullYear(),
+          String(date.getMonth() + 1).padStart(2, '0'),
+          String(date.getDate()).padStart(2, '0'),
+          String(date.getHours()).padStart(2, '0'),
+          String(date.getMinutes()).padStart(2, '0'),
+          String(date.getSeconds()).padStart(2, '0'),
+          String(date.getMilliseconds()).padStart(3, '0'),
+        ].join('');
+      };
+      // 2. 记录新页面进入日志
+      currentBrowseId = formatTimestamp();
+      if (!currentRouter && !whitePathList.includes(to.path)) {
+        currentRouter = to.fullPath;
         try {
-          currentRouter = '';
           await defHttp.post({
             url,
             params: {
               browseId: currentBrowseId,
-              isEnd: true,
-              method: from.fullPath,
+              isEnd: false,
+              method: to.fullPath,
             },
           });
           console.log('进入页面日志记录成功');
         } catch (e) {
           console.error('进入页面日志记录失败:', e);
         }
+      } else {
+        if (from.fullPath === currentRouter) {
+          try {
+            currentRouter = '';
+            await defHttp.post({
+              url,
+              params: {
+                browseId: currentBrowseId,
+                isEnd: true,
+                method: from.fullPath,
+              },
+            });
+            console.log('进入页面日志记录成功');
+          } catch (e) {
+            console.error('进入页面日志记录失败:', e);
+          }
+        }
       }
     }
   }