From 2c9729f0cecffe6dbb7f8f99ebad72f3e85a7ded Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期四, 17 十月 2024 12:06:24 +0800 Subject: [PATCH] 传所有 filter;并更新 filter value --- src/utils/request.ts | 43 +++++++++++++++++++++++-------------------- 1 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index 822ca60..c8756bf 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,17 +1,22 @@ import type { AxiosInstance, AxiosRequestConfig } from 'axios'; import axios from 'axios'; import { ElMessage } from 'element-plus'; +import { NO_AUTH_API_LIST } from '../api/ai/chat'; +import { LOGIN_URL, TEL_LOGIN_URL } from '../api/ai/user'; import emitter from './mitt'; -import { debounce } from './util'; +import { debounce, decodeFormData } from './util'; import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants'; import { Local, LoginInfo, Session } from '/@/utils/storage'; -import { LOGIN_URL } from '../api/ai/user'; -import { NO_AUTH_API_LIST } from '../api/ai/chat'; +import { Logger } from '../model/logger/Logger'; // import JSONbig from 'json-bigint'; //#region ====================== 鍚庣 res.err_code ====================== export const enum ErrorCode { /** @description 鏉冮檺楠岃瘉澶辫触 */ + Message = 'MESSAGE', + /** @description 鍐呴儴閿欒 */ + Exception = 'EXCEPTION', + /** @description 鏃犳潈浣跨敤 */ Auth = 'AUTH', } //#endregion @@ -30,7 +35,7 @@ return !!accessSession; }; -const handleNoAuth = debounce(() => { +export const handleNoAuth = debounce(() => { emitNoAuth(); }); const initRequestInterceptor = (request: AxiosInstance) => { @@ -39,15 +44,14 @@ (config) => { // 鑾峰彇鏈湴鐨� token const accessSession = Local.get(accessSessionKey); + if (accessSession) { + // 灏� token 娣诲姞鍒拌姹傛姤鏂囧ご涓� + config.headers['hswatersession'] = accessSession; + } if (!NO_AUTH_API_LIST.includes(config.url)) { - if (accessSession) { - // 灏� token 娣诲姞鍒拌姹傛姤鏂囧ご涓� - config.headers['hswatersession'] = accessSession; - } else { - if (config.url !== LOGIN_URL) { - handleNoAuth(config.url); - throw '鏉冮檺楠岃瘉澶辫触'; - } + if (!accessSession && config.url !== LOGIN_URL && config.url !== TEL_LOGIN_URL) { + handleNoAuth(config.url); + throw '鏉冮檺楠岃瘉澶辫触'; } } @@ -84,18 +88,17 @@ if (!serveData.json_ok) { switch (serveData?.err_code) { case ErrorCode.Auth: - if (res.config.url !== loginUrl) { + if (res.config.url !== LOGIN_URL && res.config.url !== TEL_LOGIN_URL) { handleNoAuth(); throw '鏉冮檺楠岃瘉澶辫触'; } + break; + case ErrorCode.Exception: + const param = res.config.data ? `\n 璇锋眰鍙傛暟锛�${JSON.stringify(decodeFormData(res.config.data))}\n` : ''; + ElMessage.error('鍐呴儴閿欒锛�'); + Logger.error(`${res.config.url} 鍝嶅簲澶辫触${param}`, serveData?.json_msg && new Error(serveData?.json_msg)); + return res.data; } - // const msg = serveData.json_msg ?? ''; - - // const error = serveData?.err_code ? `${msg ? `銆�${serveData.err_code}銆慲 : serveData.err_code}` : ''; - // const tip = error + msg || '璇锋眰澶辫触'; - // ElMessage.error(tip); - // const url = res.request.responseURL; - // throw new Error(url + '\n' + tip); } return res.data; }, -- Gitblit v1.9.3