From 5e440a7dc434c43eb828fa62cf9c12b0078b8565 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期一, 20 一月 2025 11:36:18 +0800 Subject: [PATCH] !173 发布 5.3.0-BETA 公测版本 Merge pull request !173 from 疯狂的狮子Li/dev --- src/utils/request.ts | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index ed67713..9682b81 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -8,9 +8,11 @@ import { LoadingInstance } from 'element-plus/es/components/loading/src/loading'; import FileSaver from 'file-saver'; import { getLanguage } from '@/lang'; -import { encryptBase64, encryptWithAes, generateAesKey } from '@/utils/crypto'; -import { encrypt } from '@/utils/jsencrypt'; +import { encryptBase64, encryptWithAes, generateAesKey, decryptWithAes, decryptBase64 } from '@/utils/crypto'; +import { encrypt, decrypt } from '@/utils/jsencrypt'; +import router from '@/router'; +const encryptHeader = 'encrypt-key'; let downloadLoadingInstance: LoadingInstance; // 鏄惁鏄剧ず閲嶆柊鐧诲綍 export const isRelogin = { show: false }; @@ -35,11 +37,12 @@ // 瀵瑰簲鍥介檯鍖栬祫婧愭枃浠跺悗缂� config.headers['Content-Language'] = getLanguage(); - const isToken = (config.headers || {}).isToken === false; + const isToken = config.headers?.isToken === false; // 鏄惁闇�瑕侀槻姝㈡暟鎹噸澶嶆彁浜� - const isRepeatSubmit = (config.headers || {}).repeatSubmit === false; + const isRepeatSubmit = config.headers?.repeatSubmit === false; // 鏄惁闇�瑕佸姞瀵� - const isEncrypt = (config.headers || {}).isEncrypt === 'true'; + const isEncrypt = config.headers?.isEncrypt === 'true'; + if (getToken() && !isToken) { config.headers['Authorization'] = 'Bearer ' + getToken(); // 璁╂瘡涓姹傛惡甯﹁嚜瀹氫箟token 璇锋牴鎹疄闄呮儏鍐佃嚜琛屼慨鏀� } @@ -74,12 +77,14 @@ } } } - // 褰撳紑鍚弬鏁板姞瀵� - if (isEncrypt && (config.method === 'post' || config.method === 'put')) { - // 鐢熸垚涓�涓� AES 瀵嗛挜 - const aesKey = generateAesKey(); - config.headers['encrypt-key'] = encrypt(encryptBase64(aesKey)); - config.data = typeof config.data === 'object' ? encryptWithAes(JSON.stringify(config.data), aesKey) : encryptWithAes(config.data, aesKey); + if (import.meta.env.VITE_APP_ENCRYPT === 'true') { + // 褰撳紑鍚弬鏁板姞瀵� + if (isEncrypt && (config.method === 'post' || config.method === 'put')) { + // 鐢熸垚涓�涓� AES 瀵嗛挜 + const aesKey = generateAesKey(); + config.headers[encryptHeader] = encrypt(encryptBase64(aesKey)); + config.data = typeof config.data === 'object' ? encryptWithAes(JSON.stringify(config.data), aesKey) : encryptWithAes(config.data, aesKey); + } } // FormData鏁版嵁鍘昏姹傚ごContent-Type if (config.data instanceof FormData) { @@ -88,7 +93,6 @@ return config; }, (error: any) => { - console.log(error); return Promise.reject(error); } ); @@ -96,6 +100,22 @@ // 鍝嶅簲鎷︽埅鍣� service.interceptors.response.use( (res: AxiosResponse) => { + if (import.meta.env.VITE_APP_ENCRYPT === 'true') { + // 鍔犲瘑鍚庣殑 AES 绉橀挜 + const keyStr = res.headers[encryptHeader]; + // 鍔犲瘑 + if (keyStr != null && keyStr != '') { + const data = res.data; + // 璇锋眰浣� AES 瑙e瘑 + const base64Str = decrypt(keyStr); + // base64 瑙g爜 寰楀埌璇锋眰澶寸殑 AES 绉橀挜 + const aesKey = decryptBase64(base64Str.toString()); + // aesKey 瑙g爜 data + const decryptData = decryptWithAes(data, aesKey); + // 灏嗙粨鏋� (寰楀埌鐨勬槸 JSON 瀛楃涓�) 杞负 JSON + res.data = JSON.parse(decryptData); + } + } // 鏈缃姸鎬佺爜鍒欓粯璁ゆ垚鍔熺姸鎬� const code = res.data.code || HttpStatus.SUCCESS; // 鑾峰彇閿欒淇℃伅 @@ -115,15 +135,19 @@ }).then(() => { isRelogin.show = false; useUserStore().logout().then(() => { - location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index'; - }); + router.replace({ + path: '/login', + query: { + redirect: encodeURIComponent(router.currentRoute.value.fullPath || '/') + } + }) + }); }).catch(() => { isRelogin.show = false; }); } return Promise.reject('鏃犳晥鐨勪細璇濓紝鎴栬�呬細璇濆凡杩囨湡锛岃閲嶆柊鐧诲綍銆�'); } else if (code === HttpStatus.SERVER_ERROR) { - console.log(msg); ElMessage({ message: msg, type: 'error' }); return Promise.reject(new Error(msg)); } else if (code === HttpStatus.WARN) { -- Gitblit v1.9.3