diff --git a/src/utils/request.js b/src/utils/request.js index 6ae46169..42269568 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -228,27 +228,49 @@ export function downloadJson(url, params, filename, config) { // 通用下载方法 export function downloadZip(url, params, filename, config) { - downloadLoadingInstance = Loading.service({ - text: '正在下载数据,请稍候', - spinner: 'el-icon-loading', - background: 'rgba(0, 0, 0, 0.7)', - }) - return service - .post(url, params, { - transformRequest: [ - (params) => { - return params - }, - ], - headers: { 'Content-Type': 'application/json',encryptResponse: false }, - responseType: 'blob', - ...config, + const backgroundMode = config && config.background === true + const showLoading = !(config && config.showLoading === false) && !backgroundMode + const finalAxiosConfig = { + transformRequest: [ + (params) => { + return params + }, + ], + headers: { 'Content-Type': 'application/json', encryptResponse: false }, + responseType: 'blob', + // if background mode and no explicit timeout, extend to 10 minutes + timeout: backgroundMode && !(config && typeof config.timeout === 'number') ? 600000 : undefined, + ...config, + } + + if (showLoading) { + downloadLoadingInstance = Loading.service({ + text: '正在下载数据,请稍候', + spinner: 'el-icon-loading', + background: 'rgba(0, 0, 0, 0.7)', }) + } else if (backgroundMode) { + Notification.info({ + title: '已开始生成', + message: '批量导出任务已在后台执行,准备就绪后将自动下载。', + duration: 4000, + }) + } + + return service + .post(url, params, finalAxiosConfig) .then(async (data) => { const isBlob = blobValidate(data) if (isBlob) { const blob = new Blob([data]) saveAs(blob, filename) + if (backgroundMode) { + Notification.success({ + title: '导出完成', + message: 'ZIP 文件已准备好,正在开始下载。', + duration: 3000, + }) + } } else { const resText = await data.text() const rspObj = JSON.parse(resText) @@ -256,12 +278,20 @@ export function downloadZip(url, params, filename, config) { errorCode[rspObj.code] || rspObj.msg || errorCode['default'] Message.error(errMsg) } - downloadLoadingInstance.close() + if (showLoading && downloadLoadingInstance) { + downloadLoadingInstance.close() + } }) .catch((r) => { console.error(r) - Message.error('下载文件出现错误,请联系管理员!') - downloadLoadingInstance.close() + if (r && r.message && r.message.includes('timeout')) { + Message.error('导出超时,请减少选择数量或稍后重试') + } else { + Message.error('下载文件出现错误,请联系管理员!') + } + if (showLoading && downloadLoadingInstance) { + downloadLoadingInstance.close() + } }) } export default service diff --git a/src/views/material/cost/component/applyDetail.vue b/src/views/material/cost/component/applyDetail.vue index f668107e..9aa050ee 100644 --- a/src/views/material/cost/component/applyDetail.vue +++ b/src/views/material/cost/component/applyDetail.vue @@ -145,10 +145,10 @@