From 680c00bd06ed535a3e172b48cc8913184f50777e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 27 Mar 2026 16:13:20 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8E=E7=AB=AF=E5=BC=82=E5=B8=B8=E6=8A=A5?= =?UTF-8?q?=E9=94=99=20=E8=A2=AB=E8=BE=9E=E6=8E=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/download.ts | 8 +++-- src/utils/request.ts | 70 +++++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/plugins/download.ts b/src/plugins/download.ts index 3c86e5a..4f7d48d 100644 --- a/src/plugins/download.ts +++ b/src/plugins/download.ts @@ -1,7 +1,7 @@ import axios from 'axios'; import errorCode from '@/utils/errorCode'; import { blobValidate } from '@/utils/ruoyi'; -import { globalHeaders } from '@/utils/request'; +import { extractErrorMessage, globalHeaders } from '@/utils/request'; import { saveBlob } from '@/utils/save'; import type { LoadingInstance } from 'element-plus'; @@ -28,7 +28,8 @@ export default { downloadLoadingInstance?.close(); } catch (r) { console.error(r); - ElMessage.error('下载文件出现错误,请联系管理员!'); + const errMsg = await extractErrorMessage(r); + ElMessage.error(errMsg || '下载文件出现错误,请联系管理员!'); downloadLoadingInstance?.close(); } }, @@ -52,7 +53,8 @@ export default { downloadLoadingInstance?.close(); } catch (r) { console.error(r); - ElMessage.error('下载文件出现错误,请联系管理员!'); + const errMsg = await extractErrorMessage(r); + ElMessage.error(errMsg || '下载文件出现错误,请联系管理员!'); downloadLoadingInstance?.close(); } }, diff --git a/src/utils/request.ts b/src/utils/request.ts index fd343af..41f85c5 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -16,6 +16,64 @@ const encryptHeader = 'encrypt-key'; let downloadLoadingInstance: LoadingInstance | undefined; // 是否显示重新登录 export const isRelogin = { show: false }; + +function normalizeErrorMessage(message?: string) { + if (!message) { + return undefined; + } + if (message === 'Network Error') { + return '后端接口连接异常'; + } + if (message.includes('timeout')) { + return '系统接口请求超时'; + } + if (message.includes('Request failed with status code')) { + return '系统接口' + message.slice(-3) + '异常'; + } + return message; +} + +async function parseResponseErrorData(data: unknown): Promise { + if (!data) { + return undefined; + } + + if (data instanceof Blob) { + return parseResponseErrorData(await data.text()); + } + + if (data instanceof ArrayBuffer) { + return parseResponseErrorData(new TextDecoder().decode(data)); + } + + if (typeof data === 'string') { + const text = data.trim(); + if (!text) { + return undefined; + } + try { + return parseResponseErrorData(JSON.parse(text)); + } catch { + return text; + } + } + + if (typeof data === 'object') { + const payload = data as Record; + return errorCode[payload.code] || payload.msg || payload.message || errorCode['default']; + } + + return undefined; +} + +export async function extractErrorMessage(error: any): Promise { + const responseMessage = await parseResponseErrorData(error?.response?.data); + if (responseMessage) { + return responseMessage; + } + return normalizeErrorMessage(error?.message); +} + export const globalHeaders = () => { return { Authorization: 'Bearer ' + getToken(), @@ -164,15 +222,8 @@ service.interceptors.response.use( return Promise.resolve(res.data); } }, - (error: any) => { - let { message } = error; - if (message == 'Network Error') { - message = '后端接口连接异常'; - } else if (message.includes('timeout')) { - message = '系统接口请求超时'; - } else if (message.includes('Request failed with status code')) { - message = '系统接口' + message.substr(message.length - 3) + '异常'; - } + async (error: any) => { + const message = await extractErrorMessage(error) || errorCode['default']; ElMessage({ message: message, type: 'error', duration: 5 * 1000 }); return Promise.reject(error); } @@ -204,7 +255,6 @@ export function download(url: string, params: any, fileName: string) { downloadLoadingInstance?.close(); }).catch((r: any) => { console.error(r); - ElMessage.error('下载文件出现错误,请联系管理员!'); downloadLoadingInstance?.close(); }); }