From 8d14ba6b55c768c8789aa017c85d64e15578e5c8 Mon Sep 17 00:00:00 2001
From: liyuetong
Date: Thu, 9 Dec 2021 15:40:15 +0800
Subject: [PATCH] =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E5=AF=86=E9=92=A5=E5=8A=A0?=
=?UTF-8?q?=E5=AF=86=E8=A7=A3=E5=AF=86=E9=9C=80=E6=B1=82=EF=BC=8C=E5=8C=BA?=
=?UTF-8?q?=E5=88=86v3=E9=9A=8F=E6=9C=BA=E5=AF=86=E9=92=A5=E5=8A=A0?=
=?UTF-8?q?=E5=AF=86=EF=BC=8Cv2=E5=9B=BA=E5=AE=9A=E5=AF=86=E9=92=A5?=
=?UTF-8?q?=E5=8A=A0=E5=AF=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/assets/js/utils/request.js | 85 +++++++++++++++++++---------------
1 file changed, 47 insertions(+), 38 deletions(-)
diff --git a/src/assets/js/utils/request.js b/src/assets/js/utils/request.js
index a5faa862a..ed487fcea 100644
--- a/src/assets/js/utils/request.js
+++ b/src/assets/js/utils/request.js
@@ -155,13 +155,15 @@ service.interceptors.request.use(
message: '加载中……'
})
}
- // 生成本次请求随机密钥
let masterKey
- if (!window.localStorage.getItem('masterKey')) {
- masterKey = randomString();
- window.localStorage.setItem('masterKey',masterKey)
- } else {
- masterKey = window.localStorage.getItem('masterKey')
+ if(configApp.API_VERSION == 'v3'){
+ // 生成本次请求随机密钥
+ if (!window.localStorage.getItem('masterKey')) {
+ masterKey = randomString();
+ window.localStorage.setItem('masterKey',masterKey)
+ } else {
+ masterKey = window.localStorage.getItem('masterKey')
+ }
}
/**
* 请求拦截处理(待添加 判断走统一网关处理)
@@ -172,25 +174,28 @@ service.interceptors.request.use(
config.data = {}
}
if (!!config.data && config.data != null) {
- // let encrypt = AESTools.AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD)
- console.log('随机密钥:' + masterKey);
- // RSA加密随机密钥
- let ak = RSAEncrypt(masterKey, publicKey);
- console.log('加密后的masterKey:' + ak);
- // 随机密钥加密data
- let data = AESEncrypt(JSON.stringify(config.data), masterKey);
- console.log('MD5值:' + MD5(AESEncrypt(data, masterKey)).toString());
- // 验证数据完整性MD5
- // 数据完整性MD5需要进行二次AES加密
- config.headers['signature'] = MD5(AESEncrypt(data, masterKey)).toString();
- config.data = { ak, data };
- console.log(config.data);
- if(AESDecrypt(config.data.data, masterKey)){
- console.log(JSON.parse(AESDecrypt(config.data.data, masterKey)));
- }else{
- console.log(AESDecrypt(config.data.data, masterKey));
+ if(configApp.API_VERSION == 'v3'){
+ console.log('随机密钥:' + masterKey);
+ // RSA加密随机密钥
+ let ak = RSAEncrypt(masterKey, publicKey);
+ console.log('加密后的masterKey:' + ak);
+ // 随机密钥加密data
+ let data = AESEncrypt(JSON.stringify(config.data), masterKey);
+ console.log('MD5值:' + MD5(AESEncrypt(data, masterKey)).toString());
+ // 验证数据完整性MD5
+ // 数据完整性MD5需要进行二次AES加密
+ config.headers['signature'] = MD5(AESEncrypt(data, masterKey)).toString();
+ config.data = { ak, data };
+ console.log(config.data);
+ if(AESDecrypt(config.data.data, masterKey)){
+ console.log(JSON.parse(AESDecrypt(config.data.data, masterKey)));
+ }else{
+ console.log(AESDecrypt(config.data.data, masterKey));
+ }
+ }else if(configApp.API_VERSION == 'v2'){
+ let encrypt = AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD)
+ config.data = { data: encrypt }
}
- // config.data = { data: encrypt }
}
}
@@ -241,20 +246,24 @@ service.interceptors.response.use(
/api\/$/.test(response.config.url.split(configApp.API_VERSION)[0])
) {
if (res.response) {
- // 正常情況返回必有response 节点
- // 返回结果的随机密钥
- let masterKey;
- if (!window.localStorage.getItem('masterKey')) {
- return Promise.reject('密钥失效')
- } else {
- masterKey = window.localStorage.getItem('masterKey');
- }
- console.log('请求结果response' + response)
- console.log('请求结果masterKey:' + masterKey)
- console.log(JSON.parse(AESDecrypt(response.data.response, masterKey)))
- // 使用随机密钥解密返回结果data
- res = JSON.parse(AESDecrypt(res.response, masterKey))
- // res = JSON.parse(AESTools.AESDecrypt(res.response, configApp.REQ_PWD))
+ if(configApp.API_VERSION == 'v3'){
+ // 正常情況返回必有response 节点
+ // 返回结果的随机密钥
+ let masterKey;
+ if (!window.localStorage.getItem('masterKey')) {
+ return Promise.reject('密钥失效')
+ } else {
+ masterKey = window.localStorage.getItem('masterKey');
+ }
+ console.log('请求结果response' + response)
+ console.log('请求结果masterKey:' + masterKey)
+ console.log(JSON.parse(AESDecrypt(response.data.response, masterKey)))
+ // 使用随机密钥解密返回结果data
+ res = JSON.parse(AESDecrypt(res.response, masterKey))
+ }else if(configApp.API_VERSION == 'v2'){
+ // 使用固定密钥解密返回结果data
+ res = JSON.parse(AESDecrypt(res.response, configApp.REQ_PWD))
+ }
}
}