优化useImportFile和useDownloadBlob

master
LiuJiaNan 2025-10-31 08:48:31 +08:00
parent f68c5a9aea
commit f317b13358
4 changed files with 16 additions and 10 deletions

View File

@ -12,9 +12,10 @@ export interface downloadBlobOptions {
options?: UseDownloadBlobOptions;
}
export type DeleteFileFunction = (options: downloadBlobOptions) => Promise<any>;
export type DownloadBlobFunction = (options: downloadBlobOptions) => Promise<any>;
/**
* Blob
*/
export default function useDownloadBlob(): [boolean, DeleteFileFunction];
export default function useImportFile(returnType: "array"): [boolean, DownloadBlobFunction];
export default function useImportFile(returnType?: "object"): { loading: boolean; importFile: DownloadBlobFunction };

View File

@ -5,7 +5,7 @@ import { useState } from "react";
/**
* 下载Blob流文件
*/
export default function useDownloadBlob() {
export default function useDownloadBlob(returnType = "object") {
// loading状态
const [loading, setLoading] = useState(false);
@ -14,7 +14,7 @@ export default function useDownloadBlob() {
setLoading(true);
return new Promise((resolve, reject) => {
const { name= "", type= "", params= {} } = options
const { name = "", type = "", params = {} } = options;
const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url;
Object.entries(params).forEach(([key, value]) => {
finalUrl.searchParams.append(key, value);
@ -58,5 +58,7 @@ export default function useDownloadBlob() {
});
};
return [loading, downloadBlob];
if (returnType === "array")
return [loading, downloadBlob];
return { loading, downloadBlob };
}

View File

@ -21,4 +21,5 @@ export type ImportFileFunction = (url: string, options: UseImportFileOptions) =>
/**
*
*/
export default function useImportFile(): [boolean, ImportFileFunction];
export default function useImportFile(returnType: "array"): [boolean, ImportFileFunction];
export default function useImportFile(returnType?: "object"): { loading: boolean; importFile: ImportFileFunction };

View File

@ -1,10 +1,10 @@
import {request} from "@cqsjjb/jjb-common-lib/http";
import { request } from "@cqsjjb/jjb-common-lib/http";
import { useState } from "react";
/**
* 导入文件
*/
export default function useImportFile() {
export default function useImportFile(returnType = "object") {
// loading状态
const [loading, setLoading] = useState(false);
@ -13,7 +13,7 @@ export default function useImportFile() {
setLoading(true);
return new Promise((resolve, reject) => {
const { files = [], params = {} } = options
const { files = [], params = {} } = options;
const formData = new FormData();
// 将文件添加到formData中
@ -40,5 +40,7 @@ export default function useImportFile() {
});
};
return [loading, importFile];
if (returnType === "array")
return [loading, importFile];
return { loading, importFile };
}