优化useImportFile和useDownloadBlob
							parent
							
								
									f68c5a9aea
								
							
						
					
					
						commit
						f317b13358
					
				|  | @ -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 }; | ||||
|  |  | |||
|  | @ -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 }; | ||||
| } | ||||
|  |  | |||
|  | @ -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 }; | ||||
|  |  | |||
|  | @ -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 }; | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue