前端:增加测试数据模式切换
This commit is contained in:
@@ -6,6 +6,7 @@ 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 {
|
||||
decryptTransportErrorResponse,
|
||||
decryptTransportResponse,
|
||||
@@ -19,6 +20,19 @@ let downloadLoadingInstance;
|
||||
// 是否显示重新登录
|
||||
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))
|
||||
}
|
||||
|
||||
function isTestBatchUnavailable(data) {
|
||||
return data?.reason === 'TEST_BATCH_UNAVAILABLE'
|
||||
}
|
||||
|
||||
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
@@ -45,6 +59,10 @@ 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)
|
||||
}
|
||||
if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
|
||||
const requestObj = {
|
||||
url: config.url,
|
||||
@@ -98,6 +116,9 @@ service.interceptors.request.use(async config => {
|
||||
service.interceptors.response.use(async res => {
|
||||
// 响应若命中了传输层加密,这里先还原为原始业务 JSON。
|
||||
res = await decryptTransportResponse(res)
|
||||
if (isTestBatchUnavailable(res.data)) {
|
||||
return rejectUnavailableTestBatch(res.data)
|
||||
}
|
||||
// 未设置状态码则默认成功状态
|
||||
const code = res.data.code || 200;
|
||||
// 获取错误信息
|
||||
@@ -135,6 +156,9 @@ service.interceptors.response.use(async res => {
|
||||
async error => {
|
||||
// 错误响应也可能是加密信封,先尝试解密再进入统一错误提示流程。
|
||||
error = await decryptTransportErrorResponse(error)
|
||||
if (isTestBatchUnavailable(error.response?.data)) {
|
||||
return rejectUnavailableTestBatch(error.response.data)
|
||||
}
|
||||
// 若后端提示密钥失效,则清空本地公钥缓存并基于原始请求重试一次。
|
||||
if (shouldRetryTransportWithFreshKey(error) && error.config && !error.config.__transportRetried) {
|
||||
invalidateTransportKeyMeta()
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
const TEST_BATCH_STORAGE_KEY = 'test-batch-id'
|
||||
|
||||
export function getStoredTestBatchId() {
|
||||
return sessionStorage.getItem(TEST_BATCH_STORAGE_KEY) || ''
|
||||
}
|
||||
|
||||
export function setStoredTestBatchId(batchId) {
|
||||
sessionStorage.setItem(TEST_BATCH_STORAGE_KEY, String(batchId))
|
||||
}
|
||||
|
||||
export function clearStoredTestBatchId() {
|
||||
sessionStorage.removeItem(TEST_BATCH_STORAGE_KEY)
|
||||
}
|
||||
Reference in New Issue
Block a user