feat:新增excel 下载 hook
This commit is contained in:
parent
bb4422e36b
commit
759aa3fcdf
37
resources/admin/composables/curd/useExcelDownload.ts
Normal file
37
resources/admin/composables/curd/useExcelDownload.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import Request from '/admin/support/request'
|
||||||
|
import { ref, watch } from 'vue'
|
||||||
|
|
||||||
|
const http = new Request()
|
||||||
|
export function useExcelDownload() {
|
||||||
|
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 => {
|
||||||
|
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(() => {
|
||||||
|
loading.value = false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const success = (func: Function) => {
|
||||||
|
watch(isSuccess, function () {
|
||||||
|
isSuccess.value = false
|
||||||
|
func()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return { download, success, loading, afterDownload }
|
||||||
|
}
|
0
resources/admin/support/request.ts
Normal file
0
resources/admin/support/request.ts
Normal file
Loading…
x
Reference in New Issue
Block a user