| | |
| | | |
| | | // 响应拦截 - 文件流直接返回 blob |
| | | reportRequest.interceptors.response.use( |
| | | response => { |
| | | // 对于 blob 类型的响应,直接返回 data |
| | | async response => { |
| | | // 对于 blob 类型的响应,需要判断是否是错误信息 |
| | | if (response.config.responseType === 'blob') { |
| | | return response.data |
| | | const blob = response.data |
| | | |
| | | // 检查 blob 的 type,如果是 application/json,说明返回的是错误信息 |
| | | if (blob.type === 'application/json') { |
| | | try { |
| | | // 将 blob 转换为文本 |
| | | const text = await blob.text() |
| | | const errorData = JSON.parse(text) |
| | | |
| | | // 抛出错误信息 |
| | | return Promise.reject({ |
| | | message: errorData.errorMsg || errorData.msg || '操作失败', |
| | | data: errorData |
| | | }) |
| | | } catch (e) { |
| | | return Promise.reject({ message: '解析错误信息失败' }) |
| | | } |
| | | } |
| | | |
| | | // 正常的文件流,直接返回 |
| | | return blob |
| | | } |
| | | return response.data |
| | | }, |