From 759aa3fcdf461d5400b4dbc570f47ce7dbe12162 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Wed, 5 Jul 2023 17:17:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9Eexcel=20=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../composables/curd/useExcelDownload.ts | 37 +++++++++++++++++++ resources/admin/support/request.ts | 0 2 files changed, 37 insertions(+) create mode 100644 resources/admin/composables/curd/useExcelDownload.ts create mode 100644 resources/admin/support/request.ts diff --git a/resources/admin/composables/curd/useExcelDownload.ts b/resources/admin/composables/curd/useExcelDownload.ts new file mode 100644 index 0000000..2662084 --- /dev/null +++ b/resources/admin/composables/curd/useExcelDownload.ts @@ -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(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 } +} diff --git a/resources/admin/support/request.ts b/resources/admin/support/request.ts new file mode 100644 index 0000000..e69de29