From cf7eda004123f2ee07bfa7d05f51f71f9dbb4bcd Mon Sep 17 00:00:00 2001
From: wujingjing <gersonwu@qq.com>
Date: 星期四, 18 七月 2024 11:48:59 +0800
Subject: [PATCH] Merge branch 'master' of http://47.103.154.90:83/r/WI/Web.V1.0

---
 src/utils/request.ts |   97 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 70 insertions(+), 27 deletions(-)

diff --git a/src/utils/request.ts b/src/utils/request.ts
index 37b9574..2330610 100644
--- a/src/utils/request.ts
+++ b/src/utils/request.ts
@@ -4,32 +4,57 @@
 import emitter from './mitt';
 import { debounce } from './util';
 import { AUTH_URL, MAIN_URL, SECONDARY_URL } from '/@/constants';
-import { Local, Session } from '/@/utils/storage';
+import { Local, LoginInfo, Session } from '/@/utils/storage';
+import { LOGIN_URL } from '../api/ai/user';
+import { NO_AUTH_API_LIST } from '../api/ai/chat';
 // import JSONbig from 'json-bigint';
 
 //#region ====================== 鍚庣 res.err_code ======================
 export const enum ErrorCode {
 	/** @description 鏉冮檺楠岃瘉澶辫触 */
+	Message = 'MESSAGE',
+	/** @description 鍐呴儴閿欒 */
+	Exception = 'EXCEPTION',
+	/** @description 鏃犳潈浣跨敤 */
 	Auth = 'AUTH',
 }
 //#endregion
 
-const openLoginDlg = 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;
+};
+
+const handleNoAuth = debounce(() => {
+	emitNoAuth();
+});
 const initRequestInterceptor = (request: AxiosInstance) => {
 	// 娣诲姞璇锋眰鎷︽埅鍣�
 	request.interceptors.request.use(
 		(config) => {
 			// 鑾峰彇鏈湴鐨� token
 			const accessSession = Local.get(accessSessionKey);
-			if (accessSession) {
-				// 灏� token 娣诲姞鍒拌姹傛姤鏂囧ご涓�
-				config.headers['hswatersession'] = accessSession;
-			} else {
-				openLoginDlg();
+			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 '鏉冮檺楠岃瘉澶辫触';
+					}
+				}
 			}
+
 			return config;
 		},
 		(error) => {
@@ -63,27 +88,30 @@
 			if (!serveData.json_ok) {
 				switch (serveData?.err_code) {
 					case ErrorCode.Auth:
-						openLoginDlg();
-						throw '鏉冮檺楠岃瘉澶辫触';
+						if (res.config.url !== loginUrl) {
+							handleNoAuth();
+							throw '鏉冮檺楠岃瘉澶辫触';
+						}
+						break;
+					case ErrorCode.Exception:
+						ElMessage.error('鍐呴儴閿欒锛�');
+						throw '鍐呴儴閿欒';
 				}
-				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;
 		},
 		(error) => {
+			if (typeof error === 'string') {
+				// ElMessage.error(error);
+				return Promise.reject(error);
+			}
 			// 澶勭悊鍝嶅簲閿欒
 			if (error.response) {
 				if (error.response.status === 401) {
 					clearAccessTokens();
 				}
 			}
-
 			// 瀵瑰搷搴旈敊璇仛鐐逛粈涔�
 			if (error.message.indexOf('timeout') != -1) {
 				ElMessage.error('缃戠粶瓒呮椂');
@@ -132,19 +160,33 @@
  * @description 鍩熷悕鍓嶇紑
  * 闃叉绫讳技浜� http://sqi.beng35.com/airp 鍜� http://sqi.beng35.com/test 鍏敤鍚屼竴涓� token 鎴� userInfo
  */
-const subDomainName = window.location.pathname
-	.split('/')
-	.filter((item) => !!item)
-	.join('-');
-const domainPrefix = subDomainName ? `${subDomainName}-` : '';
+export const getDomainPrefix = (win: Window) => {
+	const subDomainName = win.location.pathname
+		.split('/')
+		.filter((item) => !!item)
+		.join('-');
+	const domainPrefix = subDomainName ? `${subDomainName}-` : '';
+	return domainPrefix;
+};
+
 // token 閿畾涔�
-export const accessSessionKey = domainPrefix + 'access-session';
-export const userNameKey = domainPrefix + 'userName';
+export const sessionName = 'access-session';
+export const userName = 'userName';
+
+export const getSessionKey = (win: Window) => {
+	return getDomainPrefix(win) + sessionName;
+};
+export const getUserNameKey = (win: Window) => {
+	return getDomainPrefix(win) + userName;
+};
+
+export const accessSessionKey = getSessionKey(window);
+export const userNameKey = getUserNameKey(window);
 
 export const refreshAccessTokenKey = `x-${accessSessionKey}`;
 
 // userInfo閿畾涔�
-export const userInfoKey = domainPrefix + 'userInfo';
+export const userInfoKey = getDomainPrefix(window) + 'userInfo';
 
 // 鑾峰彇 token
 export const getSession = () => {
@@ -153,7 +195,8 @@
 
 // 娓呴櫎 token
 export const clearAccessTokens = async () => {
-	Local.remove(accessSessionKey);
+	// Local.remove(accessSessionKey);
+	LoginInfo.remove();
 	// 娓呴櫎鐢ㄦ埛淇℃伅锛堟瘡娆″埛鏂伴兘闇�瑕佸埄鐢ㄧ敤鎴蜂俊鎭幓璇锋眰瀵瑰簲鏉冮檺鑿滃崟锛�
 	Local.remove(userInfoKey);
 	// 娓呴櫎鍏朵粬

--
Gitblit v1.9.3