From bb4170bb6bf4af9cd76f976b1460ea76d33b3bb9 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期四, 31 十月 2024 16:28:14 +0800 Subject: [PATCH] 对话日志 --- src/utils/request.ts | 23 +++++++++++++++-------- 1 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index fde5c8f..885b50e 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,15 +1,17 @@ -import type { AxiosInstance, AxiosRequestConfig } from 'axios'; +import type { AxiosInstance, AxiosRequestConfig, CancelToken } from 'axios'; import axios from 'axios'; import { ElLoadingService, ElMessage } from 'element-plus'; import router from '../router'; -import { debounce } from './util'; +import { debounce, decodeFormData } from './util'; import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants'; import { Local, Session } from '/@/utils/storage'; +import { Logger } from '../model/logger/Logger'; // import JSONbig from 'json-bigint'; export type ExtraConfig = { loading?: boolean; noAuth?: boolean; handleFail?: boolean; + cancelToken?: CancelToken | undefined; }; export const checkAuth = () => { const session = Local.get(accessSessionKey); @@ -37,11 +39,12 @@ clearAccessTokens(); window.location.reload(); }); +window.handleNoAuth = handleNoAuth; let requestNum = 0; let loadingInstance: ReturnType<typeof ElLoadingService>; const addLoading = () => { - // 澧炲姞loading 濡傛灉pending璇锋眰鏁伴噺绛変簬1锛屽脊鍑簂oading, 闃叉閲嶅寮瑰嚭 + // 澧炲姞loading 濡傛灉pending璇锋眰鏁伴噺绛変簬1锛屽脊鍑簂oading, 闃叉閲嶅寮瑰嚭F requestNum++; if (requestNum == 1) { loadingInstance = ElLoadingService({ @@ -61,7 +64,6 @@ // 娣诲姞璇锋眰鎷︽埅鍣� request.interceptors.request.use( (config) => { - console.log('馃殌 ~ config:', config); // 鑾峰彇鏈湴鐨� token const accessSession = Local.get(accessSessionKey); const { loading = true, noAuth = false, handleFail = true } = config as ExtraConfig; @@ -113,17 +115,22 @@ case ErrorCode.Auth: handleNoAuth(); throw '鏉冮檺楠岃瘉澶辫触'; - break; case ErrorCode.Exception: + const param = res.config.data ? `\n 璇锋眰鍙傛暟锛�${JSON.stringify(decodeFormData(res.config.data))}\n` : ''; ElMessage.error('鍐呴儴閿欒锛�'); - throw '鍐呴儴閿欒'; + Logger.error(`${res.config.url} 鍝嶅簲澶辫触${param}`, serveData?.json_msg && new Error(serveData?.json_msg)); + throw ''; + + return res.data; } // 闈� message error锛屼笖 handleFail 涓� true // message error 涓嶉渶瑕佸鐞� if (handleFail && serveData?.err_code !== ErrorCode.Message) { - const errorText = serveData?.json_msg || '鍝嶅簲澶辫触锛�'; + const errorText = serveData?.json_msg; ElMessage.error(errorText); - throw errorText; + const param = res.config.data ? `\n 璇锋眰鍙傛暟锛�${JSON.stringify(decodeFormData(res.config.data))}\n` : ''; + Logger.error(`${res.config.url} 鍝嶅簲澶辫触${param}`, errorText && new Error(errorText)); + throw ''; } } return res.data; -- Gitblit v1.9.3