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

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
@@ -82,7 +82,9 @@ onMounted(async () => {
} catch (error) {
testBatchState.options = []
hasLoadError.value = true
ElMessage.error('测试批次选项加载失败')
if (error?.reason !== 'TEST_BATCH_UNAVAILABLE') {
ElMessage.error('测试批次选项加载失败')
}
} finally {
isLoading.value = false
}
@@ -173,13 +175,22 @@ async function handleBatchSelect(batchId) {
}
@media screen and (max-width: 767px) {
.mode-selector,
.batch-selector {
display: none;
.mode-tag {
max-width: 48px;
}
.mode-tag {
max-width: 116px;
.mode-selector {
gap: 0;
}
.batch-selector {
width: 72px;
flex-basis: 72px;
}
:deep(.mode-selector .el-radio-button__inner) {
padding-right: 2px;
padding-left: 2px;
}
}
</style>
@@ -294,6 +294,10 @@ async function toggleTheme(event) {
@media screen and (max-width: 767px) {
.navbar .right-menu {
.test-batch-mode {
padding: 0;
}
.avatar-container .avatar-wrapper .user-nickname {
display: none;
}
@@ -1,8 +1,10 @@
import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import { listAvailableTestBatches } from '@/api/system/testBatch'
import {
clearStoredTestBatchId,
getStoredTestBatchId,
recoverUnavailableTestBatch,
setStoredTestBatchId
} from '@/utils/testBatchContext'
@@ -22,7 +24,11 @@ const useTestBatchStore = defineStore('testBatch', {
this.options = Array.isArray(response.data) ? response.data : []
const current = this.options.find(item => String(item.batchId) === String(this.currentBatchId))
if (this.currentBatchId && (!current || current.status !== 'ACTIVE')) {
this.leaveTestMode()
await recoverUnavailableTestBatch({
loadStore: async () => this,
notify: message => ElMessage({ message, type: 'warning', duration: 5 * 1000 }),
message: '当前测试批次已不可用,已切回正式数据模式'
})
} else {
this.currentBatchName = current?.batchName || ''
}
@@ -4,7 +4,7 @@ import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { isHttp, isEmpty } from "@/utils/validate"
import defAva from '@/assets/images/profile.jpg'
import { clearStoredTestBatchId } from '@/utils/testBatchContext'
import { logoutWithLocalCleanup } from '@/utils/testBatchContext'
const useUserStore = defineStore(
'user',
@@ -76,18 +76,11 @@ const useUserStore = defineStore(
},
// 退出系统
logOut() {
return new Promise((resolve, reject) => {
logout(this.token).then(() => {
this.token = ''
this.roles = []
this.permissions = []
this.admin = false
removeToken()
clearStoredTestBatchId()
resolve()
}).catch(error => {
reject(error)
})
return logoutWithLocalCleanup(() => logout(this.token), this).finally(() => {
this.token = ''
this.roles = []
this.permissions = []
removeToken()
})
}
}
+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
}