From d7ae67486dc0793fe0596140ca6f13a1b48f848c Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期一, 06 一月 2025 16:37:45 +0800 Subject: [PATCH] Merge branch 'test' of http://47.103.154.90:83/r/WI/Web.V1.0 into test --- src/utils/request.ts | 102 +++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index f487bbf..0747b4a 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,10 +1,14 @@ -import type { AxiosInstance, AxiosRequestConfig } from 'axios'; +import type { AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults } 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 { Logger } from '../model/logger/Logger'; 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 { isSharePage } from '../stores/chatRoom'; // import JSONbig from 'json-bigint'; //#region ====================== 鍚庣 res.err_code ====================== @@ -18,28 +22,40 @@ } //#endregion -const handleNoAuth = debounce(() => { +const emitNoAuth = () => { emitter.emit('logout'); emitter.emit('openLoginDlg'); +}; + +export const handleNormalAuth = () => { + const accessSession = Local.get(accessSessionKey); + if (!accessSession) { + emitter.emit('logout'); + emitter.emit('openLoginDlg'); + } + return !!accessSession; +}; + +export const handleNoAuth = debounce(() => { + emitNoAuth(); }); -const loginUrl = '/login'; const initRequestInterceptor = (request: AxiosInstance) => { // 娣诲姞璇锋眰鎷︽埅鍣� request.interceptors.request.use( (config) => { // 鑾峰彇鏈湴鐨� token const accessSession = Local.get(accessSessionKey); - if (accessSession) { // 灏� token 娣诲姞鍒拌姹傛姤鏂囧ご涓� config.headers['hswatersession'] = accessSession; - } else { - if (config.url !== loginUrl) { + } + if (!NO_AUTH_API_LIST.includes(config.url) && !isSharePage.value) { + if (!accessSession && config.url !== LOGIN_URL && config.url !== TEL_LOGIN_URL) { handleNoAuth(config.url); - throw '鏉冮檺楠岃瘉澶辫触'; } } + return config; }, (error) => { @@ -73,20 +89,25 @@ 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 && !isSharePage.value) { 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)); + return res.data; } - } return res.data; }, (error) => { + if (typeof error === 'string') { + // ElMessage.error(error); + return Promise.reject(error); + } // 澶勭悊鍝嶅簲閿欒 if (error.response) { if (error.response.status === 401) { @@ -108,18 +129,68 @@ ); }; // 閰嶇疆鏂板缓涓�涓� axios 瀹炰緥 -const createAxiosInstance = () => { +const createAxiosInstance = (option: Partial<CreateAxiosDefaults<any>> = {}) => { return axios.create({ baseURL: MAIN_URL, - timeout: 50000, - headers: { 'Content-Type': 'application/json;charset=utf-8 ' }, + timeout: 1200000, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + ...option, }); }; -const service = createAxiosInstance(); +const service = createAxiosInstance(); export const mainRequest = service; +//#region ====================== 娴佸搷搴旀暟鎹� ====================== +const streamInstance = createAxiosInstance({ + adapter: 'fetch', + responseType: 'stream', +}); +const decoder = new TextDecoder(); +const readStream = async (stream: ReadableStream, cb: (value) => void): Promise<any> => { + const reader = stream.getReader(); + let lastValue = ''; + const p = new Promise(async (resolve, reject) => { + let fullValue = ''; + while (1) { + const { done, value } = await reader.read(); + if (done) { + break; + } + // const txt = decoder.decode(Uint8Array.from([...lastValue, ...value])); + const txt = decoder.decode(value); + const txtArr = txt.split('\n'); + txtArr[0] = lastValue + txtArr[0]; + txtArr.forEach((value, index, array) => { + // 涓�鑸笉浼氬嚭鐜拌繛缁崲琛岋紝鍙彲鑳芥渶鍚庝竴涓槸鎹㈣ + if (index === array.length - 1) { + lastValue = value; + } else { + const decodeValue = decodeURIComponent(value); + fullValue += decodeValue; + cb(decodeValue); + } + }); + } + resolve(fullValue); + }); + return p; +}; + +export const streamReq = async (config: AxiosRequestConfig<any>, callback: (value) => void) => { + const response = await streamInstance(config); + const stream = response as unknown as ReadableStream; + return readStream(stream, (value) => { + const jsonValue = JSON.parse(value); + callback(jsonValue); + }); +}; +//#endregion + initRequestInterceptor(service); +initRequestInterceptor(streamInstance); export function secondaryRequest(config: AxiosRequestConfig<any>) { return service({ @@ -168,7 +239,6 @@ // userInfo閿畾涔� export const userInfoKey = getDomainPrefix(window) + 'userInfo'; - // 鑾峰彇 token export const getSession = () => { return Local.get(accessSessionKey); -- Gitblit v1.9.3