优化useDownloadFile

master
LiuJiaNan 2025-10-31 14:26:46 +08:00
parent 4c650b93e8
commit 5d4e086c52
5 changed files with 75 additions and 30 deletions

View File

@ -14,6 +14,9 @@ export default function useDownloadBlob(returnType = "object") {
setLoading(true); setLoading(true);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!url)
return reject(new Error("请传入 url"));
const { name = "", type = "", params = {} } = options; const { name = "", type = "", params = {} } = options;
const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url; const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url;
Object.entries(params).forEach(([key, value]) => { Object.entries(params).forEach(([key, value]) => {

View File

@ -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 };

View File

@ -1,36 +1,62 @@
import { message, Modal } from "antd"; import { message, Modal } from "antd";
import { getFileName, getFileSuffix, getFileUrl } from "../../utils/index.js"; import { getFileName, getFileSuffix, getFileUrl } from "../../utils/index.js";
import { useState } from "react";
/** /**
* 下载文件 * 下载文件
*/ */
export default function useDownloadFile(url, name) { export default function useDownloadFile(returnType = "object") {
// loading状态
const [loading, setLoading] = useState(false);
// 下载文件
const downloadFile = (options) => {
setLoading(true);
const { url, name: fileName } = options;
if (!url) if (!url)
throw new Error("没有下载地址"); throw new Error("请传入 url");
Modal.confirm({ title: "提示", content: "确定要下载此文件吗?", onOk: () => {
Modal.confirm({
title: "提示",
content: "确定要下载此文件吗?",
onOk: () => {
const fileUrl = getFileUrl(); const fileUrl = getFileUrl();
let name = fileName;
if (name) { if (name) {
if (!getFileSuffix(url)) if (!getFileSuffix(name) && getFileSuffix(url)) {
name = name + getFileSuffix(url); name = `${name}.${getFileSuffix(url)}`;
}
} }
else { else {
name = getFileName(url); name = getFileName(url);
} }
fetch(!url.includes(fileUrl) ? fileUrl + url : url) fetch(!url.includes(fileUrl) ? fileUrl + url : url)
.then(res => res.blob()) .then(res => res.blob())
.then((blob) => { .then((blob) => {
const a = document.createElement("a"); const a = document.createElement("a");
document.body.appendChild(a); document.body.appendChild(a);
a.style.display = "none"; a.style.display = "none";
const url = window.URL.createObjectURL(blob); const urlObject = window.URL.createObjectURL(blob);
a.href = url; a.href = urlObject;
a.download = `${name}`; a.download = `${name}`;
a.click(); a.click();
document.body.removeChild(a); document.body.removeChild(a);
window.URL.revokeObjectURL(url); window.URL.revokeObjectURL(urlObject);
}) })
.catch(() => { .catch(() => {
message.error("下载失败"); message.error("下载失败");
})
.finally(() => {
setLoading(false);
}); });
} }); },
});
};
if (returnType === "array")
return [loading, downloadFile];
return { loading, downloadFile };
} }

View File

@ -13,6 +13,9 @@ export default function useImportFile(returnType = "object") {
setLoading(true); setLoading(true);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!url)
return reject(new Error("请传入 url"));
const { files = [], params = {} } = options; const { files = [], params = {} } = options;
const formData = new FormData(); const formData = new FormData();

View File

@ -101,6 +101,9 @@ function getQuery(keysStr, valuesStr) {
* 自定义 useTable继承 ahooks useAntdTable根据需求进行扩展 * 自定义 useTable继承 ahooks useAntdTable根据需求进行扩展
*/ */
function useTable(service, options) { function useTable(service, options) {
if (!service)
throw new Error("请传入 service");
// 获取额外参数和转换函数 // 获取额外参数和转换函数
const { params: extraParams, transform, ...restOptions } = options || {}; const { params: extraParams, transform, ...restOptions } = options || {};