From 0c4c2ee084c1caf377d9c9e3ccc7abd9e409c7a1 Mon Sep 17 00:00:00 2001 From: "wu.jifen" Date: Tue, 2 Sep 2025 10:33:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=8E=AF=E5=A2=83=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9=EF=BC=8C=E6=8E=A7=E5=88=B6=E5=90=AF=E7=94=A8?= =?UTF-8?q?/=E4=B8=8D=E5=90=AF=E7=94=A8=E5=8A=A0=E5=AF=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 3 +- .env.dev | 3 +- src/assets/js/utils/permission.js | 13 +++--- src/assets/js/utils/request.js | 74 +++++++++++++++++-------------- 4 files changed, 53 insertions(+), 40 deletions(-) diff --git a/.env b/.env index d9f4a15..993a1cb 100644 --- a/.env +++ b/.env @@ -3,5 +3,6 @@ NODE_ENV = 'dev' // 如果是生产环境,请记得切换为production # flag VUE_APP_FLAG='dev' -VUE_APP_ADMIN='http://39.104.123.254:7196' +VUE_APP_ADMIN='http://39.104.123.254:7197' VUE_APP_DOCS='http://39.104.123.254:7546/' +VUE_APP_USE_ENCRYPT='false' diff --git a/.env.dev b/.env.dev index 5cc4482..f0284b0 100644 --- a/.env.dev +++ b/.env.dev @@ -3,7 +3,8 @@ NODE_ENV = 'dev' // 如果是生产环境,请记得切换为production # flag VUE_APP_FLAG='dev' -VUE_APP_ADMIN='http://39.104.123.254:7196' +VUE_APP_ADMIN='http://39.104.123.254:7197' VUE_APP_STATIC='http://39.104.123.254:7536/' +VUE_APP_USE_ENCRYPT='false' diff --git a/src/assets/js/utils/permission.js b/src/assets/js/utils/permission.js index 10f409a..7131b4a 100644 --- a/src/assets/js/utils/permission.js +++ b/src/assets/js/utils/permission.js @@ -14,11 +14,14 @@ NProgress.configure({ showSpinner: false }) // NProgress Configuration const whiteList = ['/login', '/authentication', '/404'] // no redirect whitelist router.beforeEach(async (to, from, next) => { - if (getPrivateKey() === '' || getPrivateKey() === null) { - await fetchPrivateKey() - } - if (getPublicKey() === '' || getPublicKey() === null) { - await fetchPublicKey() + let envInfo = process.env + if (envInfo.VUE_APP_USE_ENCRYPT === 'true') { + if (getPrivateKey() === '' || getPrivateKey() === null) { + await fetchPrivateKey() + } + if (getPublicKey() === '' || getPublicKey() === null) { + await fetchPublicKey() + } } routerEach(to, from, next) }) diff --git a/src/assets/js/utils/request.js b/src/assets/js/utils/request.js index 348f617..6fbd8cc 100644 --- a/src/assets/js/utils/request.js +++ b/src/assets/js/utils/request.js @@ -31,6 +31,8 @@ function endLoading() { } } +let envInfo = process.env + // request interceptor service.interceptors.request.use( config => { @@ -47,31 +49,35 @@ service.interceptors.request.use( if (deviceId) { config.headers['deviceId'] = localStorage.getItem('deviceId') } - // 3. 判断是否需要加密:跳过非 JSON 请求、OPTIONS 请求、自定义排除项 - const shouldSkipEncryption = () => { - // 跳过 Content-Type 不是 application/json 的请求 - const contentType = - config.headers['Content-Type'] || config.headers['content-type'] - if (contentType && !contentType.includes('application/json')) { - return true + if (envInfo.VUE_APP_USE_ENCRYPT === 'true') { + // 3. 判断是否需要加密:跳过非 JSON 请求、OPTIONS 请求、自定义排除项 + const shouldSkipEncryption = () => { + // 跳过 Content-Type 不是 application/json 的请求 + const contentType = + config.headers['Content-Type'] || config.headers['content-type'] + if (contentType && !contentType.includes('application/json')) { + return true + } + + // // 跳过自定义标记不加密的请求 (在encrypt.js中已写这部分逻辑) + const url = config.url + .substring(config.url.lastIndexOf(':')) + .substring(6) + return !shouldEncrypt(url) } - // // 跳过自定义标记不加密的请求 (在encrypt.js中已写这部分逻辑) - const url = config.url.substring(config.url.lastIndexOf(':')).substring(6) - return !shouldEncrypt(url) - } + // 4. 执行加密逻辑 + if (!shouldSkipEncryption()) { + // 添加加密标识(便于调试) + config.headers['X-Encrypted'] = 'true' - // 4. 执行加密逻辑 - if (!shouldSkipEncryption()) { - // 添加加密标识(便于调试) - config.headers['X-Encrypted'] = 'true' + if (config.data) { + // 保存原始数据用于调试 + config.originalData = config.data - if (config.data) { - // 保存原始数据用于调试 - config.originalData = config.data - - // 加密数据,包装成 { content: "encryptedString" } - config.data = { content: encrypt(config.data, config.url) } + // 加密数据,包装成 { content: "encryptedString" } + config.data = { content: encrypt(config.data, config.url) } + } } } @@ -89,19 +95,21 @@ service.interceptors.request.use( service.interceptors.response.use( response => { let res = response.data - // 解密处理 - if ( - response.config.headers['X-Encrypted'] === 'true' && - typeof res === 'string' - ) { - try { - res = decryptWithPrivateKey(res) - } catch (e) { - logger.error('解密响应失败', e) + if (envInfo.VUE_APP_USE_ENCRYPT === 'true') { + // 解密处理 + if ( + response.config.headers['X-Encrypted'] === 'true' && + typeof res === 'string' + ) { + try { + res = decryptWithPrivateKey(res) + } catch (e) { + logger.error('解密响应失败', e) + } + } + if (res !== '' && typeof res === 'string') { + res = JSON.parse(res) } - } - if (res !== '' && typeof res === 'string') { - res = JSON.parse(res) } endLoading() if (response.config.back) {