From 78c25497d609444dbd495c744780886c8c11bcb4 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 10 Jul 2023 18:32:20 +0800 Subject: [PATCH] fix: excel download json response error --- .../composables/curd/useExcelDownload.ts | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/resources/admin/composables/curd/useExcelDownload.ts b/resources/admin/composables/curd/useExcelDownload.ts index 2662084..11a1433 100644 --- a/resources/admin/composables/curd/useExcelDownload.ts +++ b/resources/admin/composables/curd/useExcelDownload.ts @@ -1,8 +1,9 @@ import Request from '/admin/support/request' import { ref, watch } from 'vue' +import Message from '/admin/support/message' -const http = new Request() export function useExcelDownload() { + const http = new Request() const isSuccess = ref(false) const loading = ref(false) const afterDownload = ref() @@ -13,13 +14,25 @@ export function useExcelDownload() { .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) + 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) + } }) .finally(() => { loading.value = false