From e10f112fa075441b1799fb6c0b61f95ece70d9c7 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期二, 15 八月 2023 13:53:34 +0800
Subject: [PATCH] fix 修复 使用 localStorage 返回值为null问题

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

diff --git a/src/utils/request.ts b/src/utils/request.ts
index d5fac4e..ed67713 100644
--- a/src/utils/request.ts
+++ b/src/utils/request.ts
@@ -8,12 +8,21 @@
 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 };
+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;
 // 鍒涘缓 axios 瀹炰緥
 const service = axios.create({
   baseURL: import.meta.env.VITE_APP_BASE_API,
@@ -29,6 +38,8 @@
     const isToken = (config.headers || {}).isToken === false;
     // 鏄惁闇�瑕侀槻姝㈡暟鎹噸澶嶆彁浜�
     const isRepeatSubmit = (config.headers || {}).repeatSubmit === false;
+    // 鏄惁闇�瑕佸姞瀵�
+    const isEncrypt = (config.headers || {}).isEncrypt === 'true';
     if (getToken() && !isToken) {
       config.headers['Authorization'] = 'Bearer ' + getToken(); // 璁╂瘡涓姹傛惡甯﹁嚜瀹氫箟token 璇锋牴鎹疄闄呮儏鍐佃嚜琛屼慨鏀�
     }
@@ -63,6 +74,13 @@
         }
       }
     }
+    // 褰撳紑鍚弬鏁板姞瀵�
+    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'];

--
Gitblit v1.9.3