添加环境配置项,控制启用/不启用加密功能。

This commit is contained in:
wu.jifen
2025-09-02 10:33:15 +08:00
parent eb70d12e55
commit 0c4c2ee084
4 changed files with 53 additions and 40 deletions

3
.env
View File

@@ -3,5 +3,6 @@ NODE_ENV = 'dev' // 如果是生产环境请记得切换为production
# flag # flag
VUE_APP_FLAG='dev' 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_DOCS='http://39.104.123.254:7546/'
VUE_APP_USE_ENCRYPT='false'

View File

@@ -3,7 +3,8 @@ NODE_ENV = 'dev' // 如果是生产环境请记得切换为production
# flag # flag
VUE_APP_FLAG='dev' 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_STATIC='http://39.104.123.254:7536/'
VUE_APP_USE_ENCRYPT='false'

View File

@@ -14,12 +14,15 @@ NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/login', '/authentication', '/404'] // no redirect whitelist const whiteList = ['/login', '/authentication', '/404'] // no redirect whitelist
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
let envInfo = process.env
if (envInfo.VUE_APP_USE_ENCRYPT === 'true') {
if (getPrivateKey() === '' || getPrivateKey() === null) { if (getPrivateKey() === '' || getPrivateKey() === null) {
await fetchPrivateKey() await fetchPrivateKey()
} }
if (getPublicKey() === '' || getPublicKey() === null) { if (getPublicKey() === '' || getPublicKey() === null) {
await fetchPublicKey() await fetchPublicKey()
} }
}
routerEach(to, from, next) routerEach(to, from, next)
}) })
function routerEach(to, from, next) { function routerEach(to, from, next) {

View File

@@ -31,6 +31,8 @@ function endLoading() {
} }
} }
let envInfo = process.env
// request interceptor // request interceptor
service.interceptors.request.use( service.interceptors.request.use(
config => { config => {
@@ -47,6 +49,7 @@ service.interceptors.request.use(
if (deviceId) { if (deviceId) {
config.headers['deviceId'] = localStorage.getItem('deviceId') config.headers['deviceId'] = localStorage.getItem('deviceId')
} }
if (envInfo.VUE_APP_USE_ENCRYPT === 'true') {
// 3. 判断是否需要加密:跳过非 JSON 请求、OPTIONS 请求、自定义排除项 // 3. 判断是否需要加密:跳过非 JSON 请求、OPTIONS 请求、自定义排除项
const shouldSkipEncryption = () => { const shouldSkipEncryption = () => {
// 跳过 Content-Type 不是 application/json 的请求 // 跳过 Content-Type 不是 application/json 的请求
@@ -57,7 +60,9 @@ service.interceptors.request.use(
} }
// // 跳过自定义标记不加密的请求 (在encrypt.js中已写这部分逻辑) // // 跳过自定义标记不加密的请求 (在encrypt.js中已写这部分逻辑)
const url = config.url.substring(config.url.lastIndexOf(':')).substring(6) const url = config.url
.substring(config.url.lastIndexOf(':'))
.substring(6)
return !shouldEncrypt(url) return !shouldEncrypt(url)
} }
@@ -74,6 +79,7 @@ service.interceptors.request.use(
config.data = { content: encrypt(config.data, config.url) } config.data = { content: encrypt(config.data, config.url) }
} }
} }
}
// config.type 可以从api的接口地址定义 可以不触发loading // config.type 可以从api的接口地址定义 可以不触发loading
if (config.noLoading !== true) { if (config.noLoading !== true) {
@@ -89,6 +95,7 @@ service.interceptors.request.use(
service.interceptors.response.use( service.interceptors.response.use(
response => { response => {
let res = response.data let res = response.data
if (envInfo.VUE_APP_USE_ENCRYPT === 'true') {
// 解密处理 // 解密处理
if ( if (
response.config.headers['X-Encrypted'] === 'true' && response.config.headers['X-Encrypted'] === 'true' &&
@@ -103,6 +110,7 @@ service.interceptors.response.use(
if (res !== '' && typeof res === 'string') { if (res !== '' && typeof res === 'string') {
res = JSON.parse(res) res = JSON.parse(res)
} }
}
endLoading() endLoading()
if (response.config.back) { if (response.config.back) {
return res return res