mirror of
http://112.124.100.131/ebiz-ai/ebiz-ai-knowledge-manage.git
synced 2025-12-15 05:46:50 +08:00
添加环境配置项,控制启用/不启用加密功能。
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user