diff --git a/src/api/construction-person/entry-and-exit-manage/person-entry.js b/src/api/construction-person/entry-and-exit-manage/person-entry.js index 2e800b2..44d86d0 100644 --- a/src/api/construction-person/entry-and-exit-manage/person-entry.js +++ b/src/api/construction-person/entry-and-exit-manage/person-entry.js @@ -146,5 +146,6 @@ export const downloadTaskAPI = (url) => { return request({ url: `/bmw/zipDownload/downloadFile${url}`, method: 'GET', + responseType: 'blob', }) } diff --git a/src/views/construction-person/entry-and-exit-manage/person-entry/index.vue b/src/views/construction-person/entry-and-exit-manage/person-entry/index.vue index 7af169e..acaf88a 100644 --- a/src/views/construction-person/entry-and-exit-manage/person-entry/index.vue +++ b/src/views/construction-person/entry-and-exit-manage/person-entry/index.vue @@ -270,6 +270,8 @@ export default { subId: '', teamId: '', }, + // 开启一个下载任务定时器 + downloadTaskTimer: null, } }, methods: { @@ -500,12 +502,38 @@ export default { return } + if (this.downloadTaskTimer) { + this.$modal.msgError('已有下载任务,请稍后再试') + return + } + try { const res = await getDownloadTaskAPI(this.queryDownloadTask) if (res.taskId) { - const result = await getDownloadTaskLinkAPI(res.taskId) - const result1 = await downloadTaskAPI(result.downloadUrl) - console.log('result1', result1) + this.downloadTaskTimer = setInterval(async () => { + const result = await getDownloadTaskLinkAPI(res.taskId) + + if (result.status === 'completed') { + clearInterval(this.downloadTaskTimer) + this.downloadTaskTimer = null + const result1 = await downloadTaskAPI( + result.downloadUrl, + ) + + // 创建 Blob 对象 + const blob = new Blob([result1], { + type: 'application/zip', + }) + // 3。创建临时下载链接 + const link = document.createElement('a') + link.href = URL.createObjectURL(blob) + link.download = '下载文件.zip' + document.body.appendChild(link) + link.click() + document.body.removeChild(link) + URL.revokeObjectURL(link.href) + } + }, 5000) } } catch (error) { console.error('下载文件失败', error)