From 5412daaca05811d4c249590217aa549333ee1dff Mon Sep 17 00:00:00 2001 From: hongchao <3228015117@qq.com> Date: Tue, 21 Oct 2025 17:39:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AA=E7=BB=93=E7=AE=97=E4=B8=80=E9=94=AE?= =?UTF-8?q?=E5=AF=BC=E5=87=BAzip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 3 +- src/utils/request.js | 39 +++++++++++++++++++ .../material/cost/component/unreportHome.vue | 36 ++++++++++++++++- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index c4cb2a68..06e19fd6 100644 --- a/src/main.js +++ b/src/main.js @@ -12,7 +12,7 @@ import store from './store' import router from './router' import directive from './directive' // directive import plugins from './plugins' // plugins -import { download,downloadJson } from '@/utils/request' +import { download,downloadJson,downloadZip } from '@/utils/request' import './assets/icons' // icon import './permission' // permission control @@ -49,6 +49,7 @@ Vue.prototype.selectDictLabel = selectDictLabel Vue.prototype.selectDictLabels = selectDictLabels Vue.prototype.download = download Vue.prototype.downloadJson = downloadJson +Vue.prototype.downloadZip = downloadZip Vue.prototype.handleTree = handleTree Vue.prototype.indexContinuation = indexContinuation Vue.prototype.globalUrl = global_ diff --git a/src/utils/request.js b/src/utils/request.js index b2c48f6f..6ae46169 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -225,4 +225,43 @@ 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, + }) + .then(async (data) => { + const isBlob = blobValidate(data) + if (isBlob) { + const blob = new Blob([data]) + saveAs(blob, filename) + } else { + const resText = await data.text() + const rspObj = JSON.parse(resText) + const errMsg = + errorCode[rspObj.code] || rspObj.msg || errorCode['default'] + Message.error(errMsg) + } + downloadLoadingInstance.close() + }) + .catch((r) => { + console.error(r) + Message.error('下载文件出现错误,请联系管理员!') + downloadLoadingInstance.close() + }) +} export default service diff --git a/src/views/material/cost/component/unreportHome.vue b/src/views/material/cost/component/unreportHome.vue index d4289af2..78074234 100644 --- a/src/views/material/cost/component/unreportHome.vue +++ b/src/views/material/cost/component/unreportHome.vue @@ -26,10 +26,15 @@ 查询 重置 导出Excel + 批量导出ZIP - + +