From 5d4e086c520ceef50b65912963eda0650e864710 Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Fri, 31 Oct 2025 14:26:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96useDownloadFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/useDownloadBlob/index.js | 3 ++ hooks/useDownloadFile/index.d.ts | 12 ++++- hooks/useDownloadFile/index.js | 84 +++++++++++++++++++++----------- hooks/useImportFile/index.js | 3 ++ hooks/useTable/index.js | 3 ++ 5 files changed, 75 insertions(+), 30 deletions(-) diff --git a/hooks/useDownloadBlob/index.js b/hooks/useDownloadBlob/index.js index c37481d..e66af53 100644 --- a/hooks/useDownloadBlob/index.js +++ b/hooks/useDownloadBlob/index.js @@ -14,6 +14,9 @@ export default function useDownloadBlob(returnType = "object") { setLoading(true); return new Promise((resolve, reject) => { + if (!url) + return reject(new Error("请传入 url")); + const { name = "", type = "", params = {} } = options; const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url; Object.entries(params).forEach(([key, value]) => { diff --git a/hooks/useDownloadFile/index.d.ts b/hooks/useDownloadFile/index.d.ts index b685e7a..c1822ea 100644 --- a/hooks/useDownloadFile/index.d.ts +++ b/hooks/useDownloadFile/index.d.ts @@ -1,4 +1,14 @@ +export interface DownloadFileOptions { + /** 下载文件的URL */ + url: string; + /** 下载文件的自定义文件名 */ + name?: string; +} + +export type DownloadFileFunction = (options: DownloadFileOptions) => void; + /** * 下载文件 */ -export default function useDownloadFile(url: string, name?: string): void; +export default function useDownloadFile(returnType: "array"): [boolean, DownloadFileFunction]; +export default function useDownloadFile(returnType?: "object"): { loading: boolean; downloadFile: DownloadFileFunction }; diff --git a/hooks/useDownloadFile/index.js b/hooks/useDownloadFile/index.js index 7f5b1b9..5907ed0 100644 --- a/hooks/useDownloadFile/index.js +++ b/hooks/useDownloadFile/index.js @@ -1,36 +1,62 @@ import { message, Modal } from "antd"; import { getFileName, getFileSuffix, getFileUrl } from "../../utils/index.js"; +import { useState } from "react"; /** * 下载文件 */ -export default function useDownloadFile(url, name) { - if (!url) - throw new Error("没有下载地址"); - Modal.confirm({ title: "提示", content: "确定要下载此文件吗?", onOk: () => { - const fileUrl = getFileUrl(); - if (name) { - if (!getFileSuffix(url)) - name = name + getFileSuffix(url); - } - else { - name = getFileName(url); - } - fetch(!url.includes(fileUrl) ? fileUrl + url : url) - .then(res => res.blob()) - .then((blob) => { - const a = document.createElement("a"); - document.body.appendChild(a); - a.style.display = "none"; - const url = window.URL.createObjectURL(blob); - a.href = url; - a.download = `${name}`; - a.click(); - document.body.removeChild(a); - window.URL.revokeObjectURL(url); - }) - .catch(() => { - message.error("下载失败"); - }); - } }); +export default function useDownloadFile(returnType = "object") { + // loading状态 + const [loading, setLoading] = useState(false); + + // 下载文件 + const downloadFile = (options) => { + setLoading(true); + + const { url, name: fileName } = options; + if (!url) + throw new Error("请传入 url"); + + Modal.confirm({ + title: "提示", + content: "确定要下载此文件吗?", + onOk: () => { + const fileUrl = getFileUrl(); + let name = fileName; + + if (name) { + if (!getFileSuffix(name) && getFileSuffix(url)) { + name = `${name}.${getFileSuffix(url)}`; + } + } + else { + name = getFileName(url); + } + + fetch(!url.includes(fileUrl) ? fileUrl + url : url) + .then(res => res.blob()) + .then((blob) => { + const a = document.createElement("a"); + document.body.appendChild(a); + a.style.display = "none"; + const urlObject = window.URL.createObjectURL(blob); + a.href = urlObject; + a.download = `${name}`; + a.click(); + document.body.removeChild(a); + window.URL.revokeObjectURL(urlObject); + }) + .catch(() => { + message.error("下载失败"); + }) + .finally(() => { + setLoading(false); + }); + }, + }); + }; + + if (returnType === "array") + return [loading, downloadFile]; + return { loading, downloadFile }; } diff --git a/hooks/useImportFile/index.js b/hooks/useImportFile/index.js index e3a823e..6059235 100644 --- a/hooks/useImportFile/index.js +++ b/hooks/useImportFile/index.js @@ -13,6 +13,9 @@ export default function useImportFile(returnType = "object") { setLoading(true); return new Promise((resolve, reject) => { + if (!url) + return reject(new Error("请传入 url")); + const { files = [], params = {} } = options; const formData = new FormData(); diff --git a/hooks/useTable/index.js b/hooks/useTable/index.js index 8c3fa75..0783bb5 100644 --- a/hooks/useTable/index.js +++ b/hooks/useTable/index.js @@ -101,6 +101,9 @@ function getQuery(keysStr, valuesStr) { * 自定义 useTable,继承 ahooks 的 useAntdTable,根据需求进行扩展 */ function useTable(service, options) { + if (!service) + throw new Error("请传入 service"); + // 获取额外参数和转换函数 const { params: extraParams, transform, ...restOptions } = options || {};