- axios的封装
export function getExcel (url, params) {
return new Promise((resolve, reject) => {
axios
.get(url, {
params: params,
timeout: 1500000,
responseType: 'blob'
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
- 具体的数据调用 1:
loads_excel (isSearch) {
console.log(isSearch)
this.download_ing = true
let params = {
search: this.search,
}
if (isSearch) {
params.is_search = isSearch
}
this.getExcel('/project/excel', params)
.then((response) => {
const blob = new Blob([response])
if (response.type === 'application/json') {
// 如果后端传的json数据则将blob转换成json(比如返回错误信息)
var reader = new FileReader()
reader.readAsText(blob, 'utf-8')
reader.onload = function (e) {
var result = JSON.parse(reader.result)
if (result.res === 0) {
alert(result.msg)
}
}
} else {
// 如果不是json数据就读取文件
let date = `[${formatDate(new Date(), 'yyyy-MM-dd')}]`
let fileName = `项目财务报表${date}.xls`
if (isSearch) {
fileName = `研发项目信息表${date}.xls`
}
if ('download' in document.createElement('a')) { // 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
this.download_ing = false
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName)
this.download_ing = false
}
}
})
.catch((error) => {
this.download_ing = false
alert(error)
})
},
2:
// 导出下级出差报表
exportFilter () {
var userId = this.$store.state.userId
var token = this.$store.state.token
axios.get(Glob.PORT + '/trip/lower_trip_msg', {
headers: {
'id': userId,
"token": token
},
responseType: 'blob'
}).then(
(response) => {
console.log(response)
const blob = new Blob([response.data])
if (response.data.type === "application/json") {
//如果后端传的json数据则将blob转换成json(比如返回错误信息)
var reader = new FileReader();
reader.readAsText(blob, 'utf-8');
reader.onload = function (e) {
var result = JSON.parse(reader.result);
if (result.res === 7 || result.res === 8 || result.res === 9) {
window.localStorage.setItem("userId", "")
window.location.reload()
} else {
if (result.res === 0) {
alert(result.msg)
}
}
}
} else {
//如果不是json数据就读取文件
const fileName = `出差报表.xlsx`
if ('download' in document.createElement('a')) { // 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName)
}
}
}
)
}