下载接口的调用
封装与导出下载方法
export function download (url, filename) {
getBlob(url, function (blob) {
saveAs(blob, filename)
})
}
export function getBlob (url, cb) {
const instant = axios.create({
withCredentials: true,
timeout: 600000,
headers:{
'token':store.getters.token
}
})
instant.get(url, {
responseType: 'blob'
}).then(response => {
cb(response.data)
})
}
export function saveAs (blob, filename) {
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename)
} else {
var link = document.createElement('a')
var body = document.querySelector('body')
link.href = window.URL.createObjectURL(blob)
link.href = xss(link.href,{
whiteList: [],
stripIgnoreTag: true,
stripIgnoreTagBody: ["script"]
})
link.download = filename
link.style.display = 'none'
body.appendChild(link)
link.click()
body.removeChild(link)
window.URL.revokeObjectURL(link.href)
}
}
export function getAllItem (id, listTree) {
if (!id) {
return ''
} else {
const firstId = id.split(',')[0]
const currentList = listTree.filter(item => item.value == firstId)
if (currentList.length > 0) {
const parentList = listTree.filter(item => item.value == currentList[0].parentId)
if (parentList.length > 0) {
const currentId = parentList[0].value + ',' + id
return getAllItem(currentId, listTree)
} else {
return id
}
} else {
return id
}
}
}
使用与调用
先导入
import { 方法名} from "方法封装地址";
import Qs from "qs";
在调用
点击事件名() {
if (!this.isDown) {
this.showMsg("无数据信息!", "error");
return;
}
if (this.total > 20000) {
this.showMsg("每次下载数据不能超过2万条!", "error");
return;
}
const url =
process.env.VUE_APP_API_URL +
"/statistic/trl/perform/download?" +
Qs.stringify(this.formItem);
download(url, "个人业绩统计列表.xlsx");
},