From 464723fb4351541205e976c4a939cd34dcf0452c Mon Sep 17 00:00:00 2001
From: zhengyiming <540361168@qq.com>
Date: 星期日, 28 九月 2025 15:50:36 +0800
Subject: [PATCH] fix: 新增日志

---
 src/utils/request/index.ts |   45 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/src/utils/request/index.ts b/src/utils/request/index.ts
index eaabecb..d5c39ef 100644
--- a/src/utils/request/index.ts
+++ b/src/utils/request/index.ts
@@ -7,6 +7,7 @@
 import { ElLoading as Loading, ElNotification } from 'element-plus';
 import { router } from '@/router';
 import { Message, tokenIsExpired } from '@/utils';
+import { httpLoggerRecord } from '../LoggerRecord';
 
 // 鍔犺浇鐜鍙橀噺 VITE_PROXY_DOMAIN锛堝紑鍙戠幆澧冿級  VITE_PROXY_DOMAIN_REAL锛堟墦鍖呭悗鐨勭嚎涓婄幆澧冿級
 const { VITE_PROXY_DOMAIN, VITE_PROXY_DOMAIN_REAL, DEV } = loadEnv();
@@ -67,6 +68,7 @@
    */
   msg?: string;
   showType?: ErrorShowType;
+  traceId?: string;
 }
 
 interface ErrorResponse {
@@ -107,6 +109,19 @@
     // 閿欒鎺ユ敹鍙婂鐞�
     errorHandler: (error, opts) => {
       console.log('error: ', error);
+
+      const url = opts.url ?? '';
+      httpLoggerRecord.error({
+        message: `[${url}] 璇锋眰閿欒`,
+        httpParams: {
+          url: url,
+          //@ts-ignore
+          traceId: error?.info?.traceId,
+          stackTrace: error.stack,
+        },
+        args: [{ data: opts.data, params: opts.params, headers: opts.headers }],
+      });
+
       if (opts?.skipErrorHandler) throw error;
 
       if (opts?.customErrorHandler) {
@@ -120,6 +135,9 @@
         const errorInfo: ErrorInfo | undefined = (error as any).info;
         if (errorInfo) {
           const { errorMessage, errorCode } = errorInfo;
+          if (Number(errorCode) === 401) {
+            handleLogout();
+          }
           switch (errorInfo.showType) {
             case ErrorShowType.SILENT:
               // do nothing
@@ -145,7 +163,7 @@
       } else if ((error as AxiosError<ResponseStructure, IRequestOptions>).response) {
         // Axios 鐨勯敊璇�
         // 璇锋眰鎴愬姛鍙戝嚭涓旀湇鍔″櫒涔熷搷搴斾簡鐘舵�佺爜锛屼絾鐘舵�佷唬鐮佽秴鍑轰簡 2xx 鐨勮寖鍥�
-        handleAxiosResponseError(error as AxiosError<ErrorResponse, IRequestOptions>);
+        handleAxiosResponseError(error as AxiosError<ResponseStructure, IRequestOptions>);
         // Message.errorMessage(`Response status:${(error as AxiosError).response.status}`);
       } else if ((error as AxiosError).request) {
         // 璇锋眰宸茬粡鎴愬姛鍙戣捣锛屼絾娌℃湁鏀跺埌鍝嶅簲
@@ -178,6 +196,14 @@
         if (config.needNProcess) {
           NProgress.start();
         }
+
+        httpLoggerRecord.info({
+          message: `[${$config.url}] 璇锋眰寮�濮媊,
+          httpParams: {
+            url: $config.url,
+          },
+          args: [{ data: $config.data, params: $config.params, headers: $config.headers }],
+        });
 
         const userStore = useUserStoreHook();
 
@@ -220,6 +246,15 @@
     [
       (response) => {
         const $config = response.config as IRequestOptions;
+
+        httpLoggerRecord.info({
+          message: `[${$config.url}] 璇锋眰缁撴潫`,
+          httpParams: {
+            url: $config.url,
+            traceId: response.data?.traceId,
+          },
+          args: [{ data: $config.data, params: $config.params, headers: $config.headers }],
+        });
 
         const { needNProcess, getResponse = false } = $config;
 
@@ -264,18 +299,18 @@
   [505]: 'HTTP鐗堟湰涓嶅彈鏀寔',
 };
 
-function handleAxiosResponseError(error: AxiosError<ErrorResponse, IRequestOptions>) {
+function handleAxiosResponseError(error: AxiosError<ResponseStructure, IRequestOptions>) {
   if (error.response.config.url.toLowerCase().includes(RefreshTokenUrl.toLowerCase())) {
     handleLogout();
     return;
   }
   if (error && error.response) {
     let message = ErrorMessageMap[error.response?.status] ?? '璇锋眰閿欒';
-    if (error.response.data?.error?.message) {
-      message = error.response.data?.error?.message;
+    if (error.response.data?.msg) {
+      message = error.response.data?.msg;
     }
 
-    if (error.response?.status === 401) {
+    if (error.response?.status === 401 || error.response.data.code === 401) {
       handleLogout();
     }
     Message.errorMessage(message);

--
Gitblit v1.9.1