From d160c3a61cc38359de4794b208884bba430f0a1c Mon Sep 17 00:00:00 2001 From: LiuHao <liuhaoai545@gmail.com> Date: 星期一, 10 七月 2023 22:56:42 +0800 Subject: [PATCH] merge --- src/utils/request.ts | 37 +++++++++++++++++++++++++++---------- 1 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index 5fe8f11..74f0d19 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,4 +1,4 @@ -import axios, { InternalAxiosRequestConfig } from 'axios'; +import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios'; import { useUserStore } from '@/store/modules/user'; import { getToken } from '@/utils/auth'; import { tansParams, blobValidate } from '@/utils/ruoyi'; @@ -7,14 +7,15 @@ import { errorCode } from '@/utils/errorCode'; 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'; let downloadLoadingInstance: LoadingInstance; // 鏄惁鏄剧ず閲嶆柊鐧诲綍 export const isRelogin = { show: false }; axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'; -// 瀵瑰簲鍥介檯鍖栬祫婧愭枃浠跺悗缂� -axios.defaults.headers['Content-Language'] = 'zh_CN'; // 鍒涘缓 axios 瀹炰緥 const service = axios.create({ baseURL: import.meta.env.VITE_APP_BASE_API, @@ -24,9 +25,14 @@ // 璇锋眰鎷︽埅鍣� service.interceptors.request.use( (config: InternalAxiosRequestConfig) => { + // 瀵瑰簲鍥介檯鍖栬祫婧愭枃浠跺悗缂� + config.headers['Content-Language'] = getLanguage(); + const isToken = (config.headers || {}).isToken === false; // 鏄惁闇�瑕侀槻姝㈡暟鎹噸澶嶆彁浜� - const isRepeatSubmit = !(config.headers || {}).repeatSubmit; + const isRepeatSubmit = (config.headers || {}).repeatSubmit === false; + // 鏄惁闇�瑕佸姞瀵� + const isEncrypt = (config.headers || {}).isEncrypt === 'true'; if (getToken() && !isToken) { config.headers['Authorization'] = 'Bearer ' + getToken(); // 璁╂瘡涓姹傛惡甯﹁嚜瀹氫箟token 璇锋牴鎹疄闄呮儏鍐佃嚜琛屼慨鏀� } @@ -61,6 +67,17 @@ } } } + // 褰撳紑鍚弬鏁板姞瀵� + 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); + } + // FormData鏁版嵁鍘昏姹傚ごContent-Type + if (config.data instanceof FormData) { + delete config.headers['Content-Type']; + } return config; }, (error: any) => { @@ -71,7 +88,7 @@ // 鍝嶅簲鎷︽埅鍣� service.interceptors.response.use( - (res) => { + (res: AxiosResponse) => { // 鏈缃姸鎬佺爜鍒欓粯璁ゆ垚鍔熺姸鎬� const code = res.data.code || HttpStatus.SUCCESS; // 鑾峰彇閿欒淇℃伅 @@ -112,7 +129,7 @@ return Promise.resolve(res.data); } }, - (error) => { + (error: any) => { let { message } = error; if (message == 'Network Error') { message = '鍚庣鎺ュ彛杩炴帴寮傚父'; @@ -131,16 +148,16 @@ // prettier-ignore return service.post(url, params, { transformRequest: [ - (params) => { + (params: any) => { return tansParams(params); } ], headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, responseType: 'blob' - }).then(async (resp) => { + }).then(async (resp: any) => { const isLogin = blobValidate(resp); if (isLogin) { - const blob = new Blob([resp as any]); + const blob = new Blob([resp]); FileSaver.saveAs(blob, fileName); } else { const resText = await resp.data.text(); @@ -149,7 +166,7 @@ ElMessage.error(errMsg); } downloadLoadingInstance.close(); - }).catch((r) => { + }).catch((r: any) => { console.error(r); ElMessage.error('涓嬭浇鏂囦欢鍑虹幇閿欒锛岃鑱旂郴绠$悊鍛橈紒'); downloadLoadingInstance.close(); -- Gitblit v1.9.3