From b06f6a316b6a8293bb8aba6bfff33336b66c032a Mon Sep 17 00:00:00 2001
From: ahaos <8406649+lhailgl@user.noreply.gitee.com>
Date: 星期三, 13 十二月 2023 09:01:52 +0800
Subject: [PATCH] !64 版本升级 * Merge branch 'dev' of gitee.com:JavaLionLi/plus-ui into ts * 升级依赖 * !61 fix: 删除重复环境变量ElUploadInstance * fix: 删除重复环境变量ElUploadInstance

---
 src/utils/request.ts |   33 ++++++++++++++++++++++++---------
 1 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/utils/request.ts b/src/utils/request.ts
index 5b8480b..6135d69 100644
--- a/src/utils/request.ts
+++ b/src/utils/request.ts
@@ -8,16 +8,19 @@
 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';
 
+const encryptHeader = 'encrypt-key';
 let downloadLoadingInstance: LoadingInstance;
 // 鏄惁鏄剧ず閲嶆柊鐧诲綍
 export const isRelogin = { show: false };
-export const globalHeaders = {
-  Authorization: "Bearer " + getToken(),
-  clientid: import.meta.env.VITE_APP_CLIENT_ID
-}
+export const globalHeaders = () => {
+  return {
+    Authorization: 'Bearer ' + getToken(),
+    clientid: import.meta.env.VITE_APP_CLIENT_ID
+  };
+};
 
 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
 axios.defaults.headers['clientid'] = import.meta.env.VITE_APP_CLIENT_ID;
@@ -76,7 +79,7 @@
     if (isEncrypt && (config.method === 'post' || config.method === 'put')) {
       // 鐢熸垚涓�涓� AES 瀵嗛挜
       const aesKey = generateAesKey();
-      config.headers['encrypt-key'] = encrypt(encryptBase64(aesKey));
+      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
@@ -86,7 +89,6 @@
     return config;
   },
   (error: any) => {
-    console.log(error);
     return Promise.reject(error);
   }
 );
@@ -94,6 +96,20 @@
 // 鍝嶅簲鎷︽埅鍣�
 service.interceptors.response.use(
   (res: AxiosResponse) => {
+    // 鍔犲瘑鍚庣殑 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;
     // 鑾峰彇閿欒淇℃伅
@@ -121,7 +137,6 @@
       }
       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