diff --git a/src/assets/js/utils/request1.js b/src/assets/js/utils/request1.js index bcf097131..7a42bc9fe 100644 --- a/src/assets/js/utils/request1.js +++ b/src/assets/js/utils/request1.js @@ -2,8 +2,17 @@ import axios from 'axios' import { Dialog, Toast } from 'vant' import CacheUtils from '@/assets/js/utils/cacheUtils' import configApp from '@/config' -import AESTools from '@/assets/js/utils/cryptoJsUtil' -import MD5 from 'js-md5' +import { + MD5, + RSAEncrypt, + AESEncryptV2, + AESDecryptV2, + AESEncrypt, + AESDecrypt, + randomString, + publicKey, +} from '@/assets/js/utils/cryptoJsUtil' +// import MD5 from 'js-md5' let sale = ['/sale/order/orderDetail'] //在线投保 // 卡单 @@ -28,23 +37,58 @@ service.interceptors.request.use( message: '加载中……' }) } + let masterKey + if(configApp.API_VERSION == 'v3'){ + // 生成本次请求随机密钥 + if (!window.localStorage.getItem('masterKey')) { + masterKey = randomString(); + window.localStorage.setItem('masterKey',masterKey) + } else { + masterKey = window.localStorage.getItem('masterKey') + } + } /** * 请求拦截处理(待添加 判断走统一网关处理) */ - if (config.url && /api\/$/.test(config.url.split(configApp.API_VERSION)[0]) && configApp.API_VERSION == 'v2') { + if (config.url && /api\/$/.test(config.url.split(configApp.API_VERSION)[0]) && (configApp.API_VERSION == 'v2' || configApp.API_VERSION == 'v3')) { if (!config.data || config.data == null) { config.data = {} } if (!!config.data && config.data != null) { - let encrypt = AESTools.AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD) - config.data = { data: encrypt } + 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(); + if(config.method == 'get'){ + config.headers['ak'] = ak + config.headers['data'] = data + } + 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'){ + config.headers['signature'] = MD5(timeStr + CacheUtils.getLocItem('token')) + let encrypt = AESEncryptV2(JSON.stringify(config.data), configApp.REQ_PWD) + config.data = { data: encrypt } + } } } config.headers['token'] = CacheUtils.getLocItem('token') // 添加请时间戳 let timeStr = new Date().getTime() + '' config.headers['timeStr'] = timeStr - config.headers['signature'] = MD5(timeStr + CacheUtils.getLocItem('token')) + // config.headers['signature'] = MD5(timeStr + CacheUtils.getLocItem('token')) return config }, error => { @@ -58,16 +102,33 @@ service.interceptors.response.use( response => { let res = response.data if ( - configApp.API_VERSION == 'v2' && - response.config.url && - response.headers['content-type'].match(/application\/json/) && - /api\/$/.test(response.config.url.split(configApp.API_VERSION)[0]) - ) { - if (res.response) { - // 正常情況返回必有response 节点 - res = JSON.parse(AESTools.AESDecrypt(res.response, configApp.REQ_PWD)) - } - } + (configApp.API_VERSION == 'v2' || configApp.API_VERSION == 'v3') && + response.config.url && + response.headers['content-type'].match(/application\/json/) && + /api\/$/.test(response.config.url.split(configApp.API_VERSION)[0]) + ) { + if (res.response) { + 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(AESDecryptV2(res.response, configApp.REQ_PWD)) + console.log(res,'JSON.parse(AESDecrypt(res.response, configApp.REQ_PWD))') + } + } + } Toast.clear() if (res.code != 0) { if (res.code == 10001 || res.code == 10002) {