From 0867fa8807ecd4fb8accc21b3596ca13f0f6d809 Mon Sep 17 00:00:00 2001 From: Ky10 <7703482+ky10_code@user.noreply.gitee.com> Date: Thu, 1 Aug 2024 17:35:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=9F=E6=88=90=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=8B=E8=BD=BDzip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 下载zip的前端代码部分 --- src/api/generator.ts | 20 ++++++++++++++++++++ src/views/generator/index.vue | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/src/api/generator.ts b/src/api/generator.ts index 31e19a2b..a40d218b 100644 --- a/src/api/generator.ts +++ b/src/api/generator.ts @@ -38,6 +38,26 @@ class GeneratorAPI { method: "get", }); } + /** + * 下载 ZIP 文件 + * @param url + * @param fileName + */ + static downloadZip(tableName: string, fileName?: string) { + return request({ + url: `${GENERATOR_BASE_URL}/${tableName}/downloadZip`, + method: "get", + responseType: "blob", + }).then((res) => { + const blob = new Blob([res.data], { type: "application/zip" }); + const a = document.createElement("a"); + const url = window.URL.createObjectURL(blob); + a.href = url; + a.download = fileName || "下载文件.zip"; + a.click(); + window.URL.revokeObjectURL(url); + }); + } } export default GeneratorAPI; diff --git a/src/views/generator/index.vue b/src/views/generator/index.vue index 9987514a..b611ef3e 100644 --- a/src/views/generator/index.vue +++ b/src/views/generator/index.vue @@ -465,6 +465,14 @@ function handleNextClick() { if (active.value++ >= 2) { active.value = 2; } + if (active.value === 2) { + const tableName = formData.value.tableName; + if (!tableName) { + ElMessage.error("表名不能为空"); + return; + } + GeneratorAPI.downloadZip(tableName, "youlai-admin-code.zip"); + } } }