From c54bd2eb68d5140e06f940554479eeb0ae53eb28 Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Thu, 30 Oct 2025 17:38:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EuseImportFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/useDownloadBlob/index.js | 10 ++++--- hooks/useImportFile/index.d.ts | 24 ++++++++++++++++ hooks/useImportFile/index.js | 44 +++++++++++++++++++++++++++++ todo/hooks/useUploadFile/index.d.ts | 2 +- 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 hooks/useImportFile/index.d.ts create mode 100644 hooks/useImportFile/index.js diff --git a/hooks/useDownloadBlob/index.js b/hooks/useDownloadBlob/index.js index c1b0232..1841b45 100644 --- a/hooks/useDownloadBlob/index.js +++ b/hooks/useDownloadBlob/index.js @@ -10,11 +10,13 @@ export default function useDownloadBlob() { const [loading, setLoading] = useState(false); // 下载Blob流文件 - const downloadBlob = (url, options = { name: "", type: "", params: {} }) => { + const downloadBlob = (url, options) => { setLoading(true); + return new Promise((resolve, reject) => { + const { name= "", type= "", params= {} } = options const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url; - Object.entries(options.params).forEach(([key, value]) => { + Object.entries(params).forEach(([key, value]) => { finalUrl.searchParams.append(key, value); }); fetch(finalUrl, { @@ -32,14 +34,14 @@ export default function useDownloadBlob() { }) .then((blob) => { const finalBlob = new Blob([blob], { - type: options.type || "application/vnd.ms-excel", + type: type || "application/vnd.ms-excel", }); const downloadElement = document.createElement("a"); const href = window.URL.createObjectURL(finalBlob); downloadElement.style.display = "none"; downloadElement.href = href; downloadElement.download - = options.name || dayjs().format("YYYY-MM-DD HH:mm:ss"); + = name || dayjs().format("YYYY-MM-DD HH:mm:ss"); document.body.appendChild(downloadElement); downloadElement.click(); document.body.removeChild(downloadElement); diff --git a/hooks/useImportFile/index.d.ts b/hooks/useImportFile/index.d.ts new file mode 100644 index 0000000..7ac23e4 --- /dev/null +++ b/hooks/useImportFile/index.d.ts @@ -0,0 +1,24 @@ +export interface UploadFile { + /** 原始文件对象 */ + originFileObj?: File; + [key: string]: any; +} + +interface UseImportFileOptions { + /** 要上传的文件数组 */ + files: UploadFile[]; + /** 额外携带的参数对象 */ + params?: Record; +} + +export interface ImportFileOptions { + url: string; + options: UseImportFileOptions; +} + +export type ImportFileFunction = (url: string, options: UseImportFileOptions) => Promise; + +/** + * 导入文件 + */ +export default function useImportFile(): [boolean, ImportFileFunction]; diff --git a/hooks/useImportFile/index.js b/hooks/useImportFile/index.js new file mode 100644 index 0000000..6c58ec4 --- /dev/null +++ b/hooks/useImportFile/index.js @@ -0,0 +1,44 @@ +import {request} from "@cqsjjb/jjb-common-lib/http"; +import { useState } from "react"; + +/** + * 导入文件 + */ +export default function useImportFile() { + // loading状态 + const [loading, setLoading] = useState(false); + + // 导入文件 + const importFile = (url, options) => { + setLoading(true); + + return new Promise((resolve, reject) => { + const { files = [], params = {} } = options + const formData = new FormData(); + + // 将文件添加到formData中 + files.forEach((f) => { + f.originFileObj && formData.append("file", f.originFileObj); + }); + + // 将额外携带的参数添加到formData中 + Object.keys(params).forEach((key) => { + formData.append(key, params[key]); + }); + + // 发送请求 + request(url, "post", formData, { "Content-Type": "multipart/form-data" }) + .then((res) => { + resolve(res); + }) + .catch((err) => { + reject(err); + }) + .finally(() => { + setLoading(false); + }); + }); + }; + + return [loading, importFile]; +} diff --git a/todo/hooks/useUploadFile/index.d.ts b/todo/hooks/useUploadFile/index.d.ts index 7a0b207..6537158 100644 --- a/todo/hooks/useUploadFile/index.d.ts +++ b/todo/hooks/useUploadFile/index.d.ts @@ -6,7 +6,7 @@ export interface UploadFile { export interface SingleUploadFileOptions { /** 要上传的文件 */ - files: UploadFile; + files: UploadFile[]; /** 是否单文件上传 */ single?: true; /** 上传的参数 */