From f0389d2ab6fcfd190f9a3dd2b7f93eac7cd1aa86 Mon Sep 17 00:00:00 2001 From: fengchenchen Date: Tue, 7 Sep 2021 20:28:20 +0800 Subject: [PATCH] =?UTF-8?q?[update]=20=E4=B8=8B=E8=BD=BDexcel=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD=E7=9A=84=E7=BB=9F=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/manage.js | 78 ++++++++++++++++++++++++++++--------- src/mixins/JeroListMixin.js | 58 ++++++--------------------- 2 files changed, 71 insertions(+), 65 deletions(-) diff --git a/src/api/manage.js b/src/api/manage.js index fe756e1..aa2ed46 100644 --- a/src/api/manage.js +++ b/src/api/manage.js @@ -112,9 +112,48 @@ export function downFile(url,parameter){ responseType: 'blob' }) } +/** + * 通过A标签下载文件的通用方法 + * @param data--接口返回的数据 + * @param fileName --下载文件的名称 + */ +export function downloadFileByA(data, fileName) { + // 当没有返回数据或者返回错误信息的时候,对数据进行解析后提示 + if (!data || data.type === 'application/json') { + const reader = new FileReader() + reader.readAsText(data) + // 处理load事件。该事件在读取操作完成时触发 + reader.onload = e => { + // 解析返回的值 + const res = JSON.parse(e.target.result) + // 异常信息抛出 + if (!res.success) { + Vue.prototype['$message'].warning(res.message || '文件下载失败') + } + } + return + } + // if (!data || data.size === 0) { + // Vue.prototype['$message'].warning('文件下载失败') + // return + // } + if (typeof window.navigator.msSaveBlob !== 'undefined') { + window.navigator.msSaveBlob(new Blob([data]), fileName) + } else { + let url = window.URL.createObjectURL(new Blob([data])) + let link = document.createElement('a') + link.style.display = 'none' + link.href = url + link.setAttribute('download', fileName) + document.body.appendChild(link) + link.click() + document.body.removeChild(link) //下载完成移除元素 + window.URL.revokeObjectURL(url) //释放掉blob对象 + } +} /** - * 下载文件 + * get请求下载文件 * @param url 文件路径 * @param fileName 文件名 * @param parameter @@ -122,23 +161,26 @@ export function downFile(url,parameter){ */ export function downloadFile(url, fileName, parameter) { return downFile(url, parameter).then((data) => { - if (!data || data.size === 0) { - Vue.prototype['$message'].warning('文件下载失败') - return - } - if (typeof window.navigator.msSaveBlob !== 'undefined') { - window.navigator.msSaveBlob(new Blob([data]), fileName) - } else { - let url = window.URL.createObjectURL(new Blob([data])) - let link = document.createElement('a') - link.style.display = 'none' - link.href = url - link.setAttribute('download', fileName) - document.body.appendChild(link) - link.click() - document.body.removeChild(link) //下载完成移除元素 - window.URL.revokeObjectURL(url) //释放掉blob对象 - } + // 调用a标签下载文件的方法 + downloadFileByA(data, fileName) + }) +} +/** + * post下载文件 + * @param url 文件路径 + * @param fileName 文件名 + * @param parameter + * @returns {*} + */ +export function downloadPostFile(url, fileName, parameter) { + return axios({ + url: url, + data: parameter, + method: 'post', + responseType: 'blob' + }).then((data) => { + // 调用a标签下载文件的方法 + downloadFileByA(data, fileName) }) } diff --git a/src/mixins/JeroListMixin.js b/src/mixins/JeroListMixin.js index 1c5f372..d44ef64 100644 --- a/src/mixins/JeroListMixin.js +++ b/src/mixins/JeroListMixin.js @@ -4,7 +4,7 @@ * data中url定义 list为查询列表 delete为删除单条记录 deleteBatch为批量删除 */ import {filterObj} from '@/utils/util' -import {deleteAction, getAction, downFile, getFileAccessHttpUrl} from '@/api/manage' +import {getAction, downloadFile, getFileAccessHttpUrl} from '@/api/manage' import Vue from 'vue' import {ACCESS_TOKEN, TENANT_ID} from '@/store/mutation-types' import store from '@/store' @@ -188,7 +188,7 @@ export const JeroListMixin = { that.$message.success(res.message) // 判断当前删除的数据是否是最后一页的全部数据,如果是的话页码减一 if (that.ipagination.current > 1 && (that.ipagination.current === Math.ceil(that.ipagination.total / that.ipagination.pageSize)) && - that.selectedRowKeys.length === that.dataSource.length + that.selectedRowKeys.length === that.dataSource.length ) { that.ipagination.current -= 1 } @@ -282,25 +282,7 @@ export const JeroListMixin = { param['selections'] = this.selectedRowKeys.join(',') } console.log('导出参数', param) - downFile(this.url.exportXlsUrl, param).then((data) => { - if (!data) { - this.$message.warning('文件下载失败') - return - } - if (typeof window.navigator.msSaveBlob !== 'undefined') { - window.navigator.msSaveBlob(new Blob([data], {type: 'application/vnd.ms-excel'}), fileName + '.xls') - } else { - let url = window.URL.createObjectURL(new Blob([data], {type: 'application/vnd.ms-excel'})) - let link = document.createElement('a') - link.style.display = 'none' - link.href = url - link.setAttribute('download', fileName + '.xls') - document.body.appendChild(link) - link.click() - document.body.removeChild(link) //下载完成移除元素 - window.URL.revokeObjectURL(url) //释放掉blob对象 - } - }) + downloadFile(this.url.exportXlsUrl, fileName + '.xls', param) }, /* * 下载模板 @@ -309,25 +291,7 @@ export const JeroListMixin = { if (!fileName || typeof fileName != 'string') { fileName = '模板文件' } - downFile(this.url.downTemplateUrl).then((data) => { - if (!data) { - this.$message.warning('文件下载失败') - return - } - if (typeof window.navigator.msSaveBlob !== 'undefined') { - window.navigator.msSaveBlob(new Blob([data], {type: 'application/vnd.ms-excel'}), fileName + '.xls') - } else { - let url = window.URL.createObjectURL(new Blob([data], {type: 'application/vnd.ms-excel'})) - let link = document.createElement('a') - link.style.display = 'none' - link.href = url - link.setAttribute('download', fileName + '.xls') - document.body.appendChild(link) - link.click() - document.body.removeChild(link) //下载完成移除元素 - window.URL.revokeObjectURL(url) //释放掉blob对象 - } - }) + downloadFile(this.url.downTemplateUrl, fileName + '.xls') }, /* 导入 */ handleImportExcel(info) { @@ -343,12 +307,12 @@ export const JeroListMixin = { this.$warning({ title: message, content: (
- {msg} -
- 具体详情请 + {msg} +
+ 具体详情请 点击下载 -
+ ) }) } else { @@ -359,7 +323,7 @@ export const JeroListMixin = { const messageArr = info.file.response.message.split('
') let htmlDom = [] messageArr.map(tt => { - if(tt) { + if (tt) { htmlDom.push((
{tt}
)) } }) @@ -367,7 +331,7 @@ export const JeroListMixin = { title: '文件导入错误', content: (h) =>
{htmlDom} -
+ }) // this.$message.error(`${info.file.name} ${info.file.response.message}.`); } @@ -397,7 +361,7 @@ export const JeroListMixin = { /* * 打开项目详情模态框 * */ - openDetailModal(record){ + openDetailModal(record) { this.$refs.projectDetail.open(record) }, /* 图片预览 */