fix: excel download json response error
This commit is contained in:
parent
4a09a203c4
commit
78c25497d6
@ -1,8 +1,9 @@
|
|||||||
import Request from '/admin/support/request'
|
import Request from '/admin/support/request'
|
||||||
import { ref, watch } from 'vue'
|
import { ref, watch } from 'vue'
|
||||||
|
import Message from '/admin/support/message'
|
||||||
|
|
||||||
const http = new Request()
|
|
||||||
export function useExcelDownload() {
|
export function useExcelDownload() {
|
||||||
|
const http = new Request()
|
||||||
const isSuccess = ref(false)
|
const isSuccess = ref(false)
|
||||||
const loading = ref<boolean>(false)
|
const loading = ref<boolean>(false)
|
||||||
const afterDownload = ref()
|
const afterDownload = ref()
|
||||||
@ -13,13 +14,25 @@ export function useExcelDownload() {
|
|||||||
.init()
|
.init()
|
||||||
.get(path + '/export', data)
|
.get(path + '/export', data)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
const downloadLink = document.createElement('a')
|
if (r.headers['content-type'] === 'application/json') {
|
||||||
const blob = new Blob([r.data], { type: r.headers['content-type'] })
|
const blob = new Blob([r.data], { type: r.headers['content-type'] })
|
||||||
downloadLink.href = URL.createObjectURL(blob)
|
const blobReader = new Response(blob).json()
|
||||||
downloadLink.download = r.headers.filename
|
blobReader.then(res => {
|
||||||
document.body.appendChild(downloadLink)
|
if (res.code === 1e4) {
|
||||||
downloadLink.click()
|
Message.success(res.message)
|
||||||
document.body.removeChild(downloadLink)
|
} else {
|
||||||
|
Message.error(res.message)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
const downloadLink = document.createElement('a')
|
||||||
|
const blob = new Blob([r.data], { type: r.headers['content-type'] })
|
||||||
|
downloadLink.href = URL.createObjectURL(blob)
|
||||||
|
downloadLink.download = r.headers.filename
|
||||||
|
document.body.appendChild(downloadLink)
|
||||||
|
downloadLink.click()
|
||||||
|
document.body.removeChild(downloadLink)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user