优化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; options?: UseDownloadBlobOptions;
} }
export type DeleteFileFunction = (options: downloadBlobOptions) => Promise<any>; export type DownloadBlobFunction = (options: downloadBlobOptions) => Promise<any>;
/** /**
* Blob * 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流文件 * 下载Blob流文件
*/ */
export default function useDownloadBlob() { export default function useDownloadBlob(returnType = "object") {
// loading状态 // loading状态
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@ -14,7 +14,7 @@ export default function useDownloadBlob() {
setLoading(true); setLoading(true);
return new Promise((resolve, reject) => { 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; 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]) => {
finalUrl.searchParams.append(key, value); finalUrl.searchParams.append(key, value);
@ -58,5 +58,7 @@ export default function useDownloadBlob() {
}); });
}; };
if (returnType === "array")
return [loading, downloadBlob]; 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"; import { useState } from "react";
/** /**
* 导入文件 * 导入文件
*/ */
export default function useImportFile() { export default function useImportFile(returnType = "object") {
// loading状态 // loading状态
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@ -13,7 +13,7 @@ export default function useImportFile() {
setLoading(true); setLoading(true);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const { files = [], params = {} } = options const { files = [], params = {} } = options;
const formData = new FormData(); const formData = new FormData();
// 将文件添加到formData中 // 将文件添加到formData中
@ -40,5 +40,7 @@ export default function useImportFile() {
}); });
}; };
if (returnType === "array")
return [loading, importFile]; return [loading, importFile];
return { loading, importFile };
} }