下载封装

This commit is contained in:
NiSen
2023-06-29 09:57:29 +08:00
parent 994546ca68
commit 06ab0866be
3 changed files with 49 additions and 21 deletions

View File

@@ -91,12 +91,12 @@
<div class="imptHeader">
<span>导入</span>
<div class="defeat">
<div class="detext" @click="downloadEeeorData(1)">
<div class="detext" @click="downloadTemplate(1)">
下载导入模版
</div>
</div>
<div class="defeat">
<div class="detext" @click="downloadEeeorData(2)">
<div class="detext" @click="downloadTemplate(2)">
下载最新案例模版
</div>
</div>
@@ -142,6 +142,7 @@ import { reactive, ref, computed, nextTick } from 'vue';
import { Form, message } from "ant-design-vue";
import { isTopList, downloadErrorRecords } from '@/api/case'
import CommonRecommend from "@/components/CaseManage/CommonRecommend";
import useDownload from '@/hooks/useDownload'
defineProps({
visible: {
type: Boolean,
@@ -196,6 +197,23 @@ const state = reactive({
}
]
})
const downloadTemplate = (type) => {
let downInfo = {
1: {
url: '/xboe/m/boe/cases/recommend/download-template',
name: '导入模版'
},
2: {
url: '',
name: '最新案例模版'
}
}
try {
useDownload(downInfo[type].url, {}, downInfo[type].name)
} catch (error) {
console.log(error);
}
}
//删除选中
const remove = (id) => {
state.selectedRowKeys = state.selectedRowKeys.filter(item => item !== id)

View File

@@ -1,21 +1,33 @@
import http from "@/api/configSys";
/**
*
* @param {String} url [请求的url地址]
* @param {blob} data 文件流Blob对象
* @param {String} fileType [导出文件类型] 默认值 xls
* @param {String} fileName [导出文件名称] 默认值 导出文件
* @param {string} mimeType [到处文件类型]
*/
const useDownload = (data, fileName = '导出文件', fileType = 'xls', mimeType = 'application/vnd.ms-excel;charset=UTF-8') => {
const link = document.createElement('a');// 创建a标签
let blob = new Blob([data], { type: mimeType }); // 设置文件类型
link.style.display = "none";
link.href = URL.createObjectURL(blob); // 创建URL
link.setAttribute("download", `${fileName}.${fileType}`);
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href);
document.body.removeChild(link);
const useDownload = (url, params = {}, fileName = '导出文件', fileType = 'xls', mimeType = 'application/vnd.ms-excel;charset=UTF-8') => {
return new Promise((resolve, reject) => {
http.post(url, params, { responseType: 'blob' })
.then(res => {
resolve(res.data);
if (!res.data) {
return
}
const link = document.createElement('a');// 创建a标签
let blob = new Blob([res.data], { type: mimeType }); // 设置文件类型
link.style.display = "none";
link.href = URL.createObjectURL(blob); // 创建URL
link.setAttribute("download", `${fileName}.${fileType}`);
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href);
document.body.removeChild(link);
})
.catch(err => {
reject(err.data);
})
});
}
export default useDownload

View File

@@ -246,13 +246,11 @@ function handleOper(record, type) {
}
// 信息下载
const downloadInfo = (record) => {
caseInfoDownload({ casesRecommendId: record.id })
.then((res) => {
useDownload(res.data, '案例信息')
})
.catch(() => {
message.info("信息下载失败");
});
try {
useDownload('/xboe/m/boe/cases/recommend/info_download', { casesRecommendId: record.id }, '案例信息')
} catch (error) {
console.log(error);
}
};
const handelChangePage = (page, pageSize) => {