修复:完善测试数据模式回退

This commit is contained in:
焦龙言
2026-09-23 16:10:33 +08:00
parent 169d9889f3
commit 9ca95125b8
7 changed files with 171 additions and 37 deletions
+17 -10
View File
@@ -6,7 +6,11 @@ import { tansParams, blobValidate } from '@/utils/shierfeng'
import cache from '@/plugins/cache'
import { saveAs } from 'file-saver'
import useUserStore from '@/store/modules/user'
import { clearStoredTestBatchId, getStoredTestBatchId } from '@/utils/testBatchContext'
import {
applyTestBatchHeader,
getStoredTestBatchId,
recoverUnavailableTestBatch
} from '@/utils/testBatchContext'
import {
decryptTransportErrorResponse,
decryptTransportResponse,
@@ -22,11 +26,17 @@ export let isRelogin = { show: false };
async function rejectUnavailableTestBatch(data) {
const message = data.msg || '当前测试批次不可用,已切回正式数据模式'
clearStoredTestBatchId()
const { default: useTestBatchStore } = await import('@/store/modules/testBatch')
useTestBatchStore().leaveTestMode()
ElMessage({ message, type: 'error', duration: 5 * 1000 })
return Promise.reject(new Error(message))
await recoverUnavailableTestBatch({
loadStore: async () => {
const { default: useTestBatchStore } = await import('@/store/modules/testBatch')
return useTestBatchStore()
},
notify: recoveryMessage => ElMessage({ message: recoveryMessage, type: 'error', duration: 5 * 1000 }),
message
})
const error = new Error(message)
error.reason = data.reason
return Promise.reject(error)
}
function isTestBatchUnavailable(data) {
@@ -59,10 +69,7 @@ service.interceptors.request.use(async config => {
if (getToken() && !isToken) {
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
const testBatchId = getStoredTestBatchId()
if (getToken() && testBatchId) {
config.headers['X-Test-Batch-Id'] = String(testBatchId)
}
applyTestBatchHeader(config, getToken(), getStoredTestBatchId())
if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
const requestObj = {
url: config.url,
@@ -1,4 +1,5 @@
const TEST_BATCH_STORAGE_KEY = 'test-batch-id'
let unavailableBatchRecoveryPromise
export function getStoredTestBatchId() {
return sessionStorage.getItem(TEST_BATCH_STORAGE_KEY) || ''
@@ -11,3 +12,36 @@ export function setStoredTestBatchId(batchId) {
export function clearStoredTestBatchId() {
sessionStorage.removeItem(TEST_BATCH_STORAGE_KEY)
}
export function applyTestBatchHeader(config, token, batchId) {
if (token && batchId) {
config.headers = config.headers || {}
config.headers['X-Test-Batch-Id'] = String(batchId)
}
return config
}
export function clearLocalUserContext(state) {
state.admin = false
clearStoredTestBatchId()
}
export function logoutWithLocalCleanup(remoteLogout, state) {
return Promise.resolve().then(remoteLogout).finally(() => {
clearLocalUserContext(state)
})
}
export function recoverUnavailableTestBatch({ loadStore, notify, message }) {
if (!unavailableBatchRecoveryPromise) {
unavailableBatchRecoveryPromise = (async () => {
clearStoredTestBatchId()
const store = await loadStore()
store.leaveTestMode()
notify(message)
})().finally(() => {
unavailableBatchRecoveryPromise = undefined
})
}
return unavailableBatchRecoveryPromise
}