优化useDownloadFile
parent
4c650b93e8
commit
5d4e086c52
|
|
@ -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]) => {
|
||||||
|
|
|
||||||
|
|
@ -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 };
|
||||||
|
|
|
||||||
|
|
@ -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") {
|
||||||
if (!url)
|
// loading状态
|
||||||
throw new Error("没有下载地址");
|
const [loading, setLoading] = useState(false);
|
||||||
Modal.confirm({ title: "提示", content: "确定要下载此文件吗?", onOk: () => {
|
|
||||||
const fileUrl = getFileUrl();
|
// 下载文件
|
||||||
if (name) {
|
const downloadFile = (options) => {
|
||||||
if (!getFileSuffix(url))
|
setLoading(true);
|
||||||
name = name + getFileSuffix(url);
|
|
||||||
}
|
const { url, name: fileName } = options;
|
||||||
else {
|
if (!url)
|
||||||
name = getFileName(url);
|
throw new Error("请传入 url");
|
||||||
}
|
|
||||||
fetch(!url.includes(fileUrl) ? fileUrl + url : url)
|
Modal.confirm({
|
||||||
.then(res => res.blob())
|
title: "提示",
|
||||||
.then((blob) => {
|
content: "确定要下载此文件吗?",
|
||||||
const a = document.createElement("a");
|
onOk: () => {
|
||||||
document.body.appendChild(a);
|
const fileUrl = getFileUrl();
|
||||||
a.style.display = "none";
|
let name = fileName;
|
||||||
const url = window.URL.createObjectURL(blob);
|
|
||||||
a.href = url;
|
if (name) {
|
||||||
a.download = `${name}`;
|
if (!getFileSuffix(name) && getFileSuffix(url)) {
|
||||||
a.click();
|
name = `${name}.${getFileSuffix(url)}`;
|
||||||
document.body.removeChild(a);
|
}
|
||||||
window.URL.revokeObjectURL(url);
|
}
|
||||||
})
|
else {
|
||||||
.catch(() => {
|
name = getFileName(url);
|
||||||
message.error("下载失败");
|
}
|
||||||
});
|
|
||||||
} });
|
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 };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 || {};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue