From 8514062951192813aafb3c0861322bfed180f499 Mon Sep 17 00:00:00 2001 From: u2nyakim Date: Fri, 29 Aug 2025 16:22:33 +0800 Subject: [PATCH] =?UTF-8?q?up.=20=E5=8F=8C=E5=90=91=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- z_ele/src/config/setting.ts | 5 ++++- z_ele/src/utils/request.ts | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/z_ele/src/config/setting.ts b/z_ele/src/config/setting.ts index f4a78c4..abf64b0 100644 --- a/z_ele/src/config/setting.ts +++ b/z_ele/src/config/setting.ts @@ -34,5 +34,8 @@ export const MAP_KEY = '006d995d433058322319fa797f2876f5'; /** EleAdminPlus授权码 */ export const LICENSE_CODE = import.meta.env.VITE_LICENSE; - +/** 数据加密key */ export const DATA_SECRET_KEY = "mySecretKey123!" + +/** 请求加密开关 */ +export const REQUEST_ENCRYPTION = import.meta.env.REQUEST_ENCRYPTION; diff --git a/z_ele/src/utils/request.ts b/z_ele/src/utils/request.ts index 88a7aee..8da7547 100644 --- a/z_ele/src/utils/request.ts +++ b/z_ele/src/utils/request.ts @@ -4,7 +4,7 @@ import axios from 'axios'; import type { AxiosResponse, InternalAxiosRequestConfig, Method } from 'axios'; import { unref } from 'vue'; -import { API_BASE_URL, LAYOUT_PATH, DATA_SECRET_KEY } from '@/config/setting'; +import { API_BASE_URL, LAYOUT_PATH, DATA_SECRET_KEY, REQUEST_ENCRYPTION } from '@/config/setting'; import type { ApiResult } from '@/api'; import router from '@/router'; import { isWhiteList } from '@/router/routes'; @@ -44,11 +44,7 @@ export function decryptData(ciphertext: string): any { */ export function requestInterceptor(config: SecureRequestConfig) { const method = config.method?.toLowerCase() as Method; - // 是否加密 - const shouldEncrypt = ( - !config.skipEncryption && - config.data && - ['post', 'put', 'delete', 'patch'].includes(method)); + // 添加客户端信息到header const client = useClientStore(); @@ -64,15 +60,22 @@ export function requestInterceptor(config: SecureRequestConfig) { config.headers['Authorization'] = token; } // get请求处理数组和对象类型参数 - if (config.method === 'get' && config.params) { + if (method === 'get' && config.params) { config.url = toURLSearch(config.params, config.url); config.params = {}; } - if (shouldEncrypt && !config.headers?.['X-Encrypted']) { - config.data = { - encryptedData: encryptData(config.data) - }; - config.headers['X-Encrypted'] = true; + if (REQUEST_ENCRYPTION) { + // 是否加密 + const shouldEncrypt = ( + !config.skipEncryption && + config.data && + ['post', 'put', 'delete', 'patch'].includes(method)); + if (shouldEncrypt && !config.headers?.['X-Encrypted']) { + config.data = { + encryptedData: encryptData(config.data) + }; + config.headers['X-Encrypted'] = true; + } } }