From 9b4692b625b97ecfa2e25262e826721114df51b7 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期一, 27 十一月 2023 10:59:00 +0800
Subject: [PATCH] !59 响应加密功能 Merge pull request !59 from MichelleChung/dev

---
 src/utils/request.ts |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/utils/request.ts b/src/utils/request.ts
index ed67713..a183ca6 100644
--- a/src/utils/request.ts
+++ b/src/utils/request.ts
@@ -8,9 +8,10 @@
 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 };
@@ -78,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
@@ -96,6 +97,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;
     // 鑾峰彇閿欒淇℃伅

--
Gitblit v1.9.3