1 parent
d941074095
commit
9855a20906
5 files changed
+156
-2
No files matched your search
@@ -0,0 +1,18 @@
|
||||
const maximumErrorBytes = 1024 * 1024
|
||||
|
||||
// 下载接口的 JSON 业务错误也会被 Axios 包装成 Blob。
|
||||
// 仅解开小体积错误信封,真实 CSV、JSON 导出和未知内容保持原样。
|
||||
export async function decodeBlobAPIError(response) {
|
||||
const payload = response?.data
|
||||
if (!(payload instanceof Blob) || payload.size > maximumErrorBytes) return
|
||||
const contentType = payload.type || response.headers?.['content-type'] || ''
|
||||
if (!/(?:application|text)\/(?:[\w.-]+\+)?json\b/i.test(contentType)) return
|
||||
try {
|
||||
const body = JSON.parse(await payload.text())
|
||||
if (body && !Array.isArray(body) && typeof body.errorNo === 'number' && body.errorNo !== 0) {
|
||||
response.data = body
|
||||
}
|
||||
} catch {
|
||||
// 非 JSON 内容仍交给原有下载校验处理,不能掩盖原始 HTTP 错误。
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import axios from 'axios'
|
||||
import lang from '../i18n/i18n';
|
||||
import {router} from "@/router";
|
||||
import {createLoginRedirectHandler} from "@/utils/navigation.mjs";
|
||||
import { decodeBlobAPIError } from './apiResponse.mjs'
|
||||
|
||||
//创建axios的一个实例
|
||||
const http = axios.create({
|
||||
@@ -72,6 +73,7 @@ function isAuthenticationFailureError(error) {
|
||||
|
||||
http.interceptors.response.use(async (response) => {
|
||||
//响应成功
|
||||
await decodeBlobAPIError(response)
|
||||
if (response.data?.errorNo === 403
|
||||
&& response.data?.data?.code !== 'RECENT_AUTH_REQUIRED'
|
||||
&& !isSetupRequest(response.config)
|
||||
@@ -81,6 +83,7 @@ http.interceptors.response.use(async (response) => {
|
||||
return response.data;
|
||||
}, async (error) => {
|
||||
//响应错误
|
||||
await decodeBlobAPIError(error.response)
|
||||
if (error.response && error.response.status) {
|
||||
let message = ""
|
||||
switch (error.response.status) {
|
||||
|
||||
@@ -727,7 +727,9 @@ export function usePage(pageKey = 'base') {
|
||||
if (!canExportAudit.value || working.value) return
|
||||
working.value = format === 'csv' ? 'export-csv' : 'export'
|
||||
try {
|
||||
const response = await http.post('/api/v1/audits/config', { format, limit: 500 }, { responseType: 'blob' })
|
||||
const response = await requestWithRecentAuthentication(() => http.post(
|
||||
'/api/v1/audits/config', { format, limit: 500 }, { responseType: 'blob' }
|
||||
))
|
||||
if (!(response instanceof Blob)) {
|
||||
throw new Error(response?.errorMsg || '审计导出响应格式不正确')
|
||||
}
|
||||
@@ -747,7 +749,7 @@ export function usePage(pageKey = 'base') {
|
||||
URL.revokeObjectURL(url)
|
||||
ElMessage.success('审计记录已导出')
|
||||
} catch (error) {
|
||||
ElMessage.error(error.message || '审计导出失败')
|
||||
if (!isRecentAuthenticationCancelled(error)) ElMessage.error(error.message || '审计导出失败')
|
||||
} finally {
|
||||
working.value = ''
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user