catchAdmin/resources/admin/composables/curd/useExcelDownload.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-07-05 17:17:31 +08:00
import Request from '/admin/support/request'
import { ref, watch } from 'vue'
import Message from '/admin/support/message'
2023-07-05 17:17:31 +08:00
export function useExcelDownload() {
const http = new Request()
2023-07-05 17:17:31 +08:00
const isSuccess = ref(false)
const loading = ref<boolean>(false)
const afterDownload = ref()
function download(path: string, data: object = {}) {
loading.value = true
http
.setResponseType('blob')
.init()
.get(path + '/export', data)
.then(r => {
if (r.headers['content-type'] === 'application/json') {
const blob = new Blob([r.data], { type: r.headers['content-type'] })
const blobReader = new Response(blob).json()
blobReader.then(res => {
if (res.code === 1e4) {
Message.success(res.message)
} 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)
}
2023-07-05 17:17:31 +08:00
})
.finally(() => {
loading.value = false
})
}
const success = (func: Function) => {
watch(isSuccess, function () {
isSuccess.value = false
func()
})
}
return { download, success, loading, afterDownload }
}