fix: 报告下载 token过期下载文件错误

This commit is contained in:
@changcode
2022-05-10 18:20:44 +08:00
parent da0eb999e7
commit d8e79b97f7

View File

@@ -188,49 +188,53 @@ export default {
} else if (!localStorage.getItem(storageKey.token) && n === 1) { } else if (!localStorage.getItem(storageKey.token) && n === 1) {
return window.location.replace('/') return window.location.replace('/')
} }
axios.get(url, { responseType: 'blob', params: params }).then(res => { get(api.reportTemp).then(res => {
if (window.navigator.msSaveOrOpenBlob) { if (res.code === 200) {
// 兼容ie11 axios.get(url, { responseType: 'blob', params: params }).then(res => {
const blobObject = new Blob([res.data]) if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blobObject, fileName) // 兼容ie11
} else { const blobObject = new Blob([res.data])
const url = URL.createObjectURL(new Blob([res.data])) window.navigator.msSaveOrOpenBlob(blobObject, fileName)
const a = document.createElement('a') } else {
document.body.appendChild(a) // 此处增加了将创建的添加到body当中 const url = URL.createObjectURL(new Blob([res.data]))
a.href = url const a = document.createElement('a')
a.download = fileName document.body.appendChild(a) // 此处增加了将创建的添加到body当中
a.target = '_blank' a.href = url
a.click() a.download = fileName
a.remove() // 将a标签移除 a.target = '_blank'
} a.click()
if (n === 2) { // 批量下载 a.remove() // 将a标签移除
this.$refs.dataTable.loading = false }
} else if (n === 1) { // 列表单个下载 if (n === 2) { // 批量下载
this.$refs.dataTable.loadingTableId = !u.id this.$refs.dataTable.loading = false
} } else if (n === 1) { // 列表单个下载
}, error => { this.$refs.dataTable.loadingTableId = !u.id
const $self = this }
const reader = new FileReader() }, error => {
reader.onload = function (event) { const $self = this
const responseText = reader.result const reader = new FileReader()
const exception = JSON.parse(responseText) reader.onload = function (event) {
if (exception.message) { const responseText = reader.result
$self.$message.error(exception.message) const exception = JSON.parse(responseText)
} else { if (exception.message) {
console.error(error) $self.$message.error(exception.message)
} } else {
} console.error(error)
reader.readAsText(error.response.data) }
if (n === 2) { // 批量下载 }
this.$refs.dataTable.loading = false reader.readAsText(error.response.data)
} else if (n === 1) { // 列表单个下载 if (n === 2) { // 批量下载
this.$refs.dataTable.loadingTableId = !u.id this.$refs.dataTable.loading = false
} } else if (n === 1) { // 列表单个下载
}).catch(() => { this.$refs.dataTable.loadingTableId = !u.id
if (n === 2) { // 批量下载 }
this.$refs.dataTable.loading = false }).catch(() => {
} else if (n === 1) { // 列表单个下载 if (n === 2) { // 批量下载
this.$refs.dataTable.loadingTableId = !u.id this.$refs.dataTable.loading = false
} else if (n === 1) { // 列表单个下载
this.$refs.dataTable.loadingTableId = !u.id
}
})
} }
}) })
}, },