zy-react-library 1.0.28 → 1.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,9 +12,10 @@ export interface downloadBlobOptions {
12
12
  options?: UseDownloadBlobOptions;
13
13
  }
14
14
 
15
- export type DeleteFileFunction = (options: downloadBlobOptions) => Promise<any>;
15
+ export type DownloadBlobFunction = (options: downloadBlobOptions) => Promise<any>;
16
16
 
17
17
  /**
18
18
  * 下载Blob流文件
19
19
  */
20
- export default function useDownloadBlob(): [boolean, DeleteFileFunction];
20
+ export default function useImportFile(returnType: "array"): [boolean, DownloadBlobFunction];
21
+ export default function useImportFile(returnType?: "object"): { loading: boolean; importFile: DownloadBlobFunction };
@@ -5,16 +5,18 @@ import { useState } from "react";
5
5
  /**
6
6
  * 下载Blob流文件
7
7
  */
8
- export default function useDownloadBlob() {
8
+ export default function useDownloadBlob(returnType = "object") {
9
9
  // loading状态
10
10
  const [loading, setLoading] = useState(false);
11
11
 
12
12
  // 下载Blob流文件
13
- const downloadBlob = (url, options = { name: "", type: "", params: {} }) => {
13
+ const downloadBlob = (url, options) => {
14
14
  setLoading(true);
15
+
15
16
  return new Promise((resolve, reject) => {
17
+ const { name = "", type = "", params = {} } = options;
16
18
  const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url;
17
- Object.entries(options.params).forEach(([key, value]) => {
19
+ Object.entries(params).forEach(([key, value]) => {
18
20
  finalUrl.searchParams.append(key, value);
19
21
  });
20
22
  fetch(finalUrl, {
@@ -32,14 +34,14 @@ export default function useDownloadBlob() {
32
34
  })
33
35
  .then((blob) => {
34
36
  const finalBlob = new Blob([blob], {
35
- type: options.type || "application/vnd.ms-excel",
37
+ type: type || "application/vnd.ms-excel",
36
38
  });
37
39
  const downloadElement = document.createElement("a");
38
40
  const href = window.URL.createObjectURL(finalBlob);
39
41
  downloadElement.style.display = "none";
40
42
  downloadElement.href = href;
41
43
  downloadElement.download
42
- = options.name || dayjs().format("YYYY-MM-DD HH:mm:ss");
44
+ = name || dayjs().format("YYYY-MM-DD HH:mm:ss");
43
45
  document.body.appendChild(downloadElement);
44
46
  downloadElement.click();
45
47
  document.body.removeChild(downloadElement);
@@ -56,5 +58,7 @@ export default function useDownloadBlob() {
56
58
  });
57
59
  };
58
60
 
59
- return [loading, downloadBlob];
61
+ if (returnType === "array")
62
+ return [loading, downloadBlob];
63
+ return { loading, downloadBlob };
60
64
  }
@@ -0,0 +1,25 @@
1
+ export interface UploadFile {
2
+ /** 原始文件对象 */
3
+ originFileObj?: File;
4
+ [key: string]: any;
5
+ }
6
+
7
+ interface UseImportFileOptions {
8
+ /** 要上传的文件数组 */
9
+ files: UploadFile[];
10
+ /** 额外携带的参数对象 */
11
+ params?: Record<string, any>;
12
+ }
13
+
14
+ export interface ImportFileOptions {
15
+ url: string;
16
+ options: UseImportFileOptions;
17
+ }
18
+
19
+ export type ImportFileFunction = (url: string, options: UseImportFileOptions) => Promise<any>;
20
+
21
+ /**
22
+ * 导入文件
23
+ */
24
+ export default function useImportFile(returnType: "array"): [boolean, ImportFileFunction];
25
+ export default function useImportFile(returnType?: "object"): { loading: boolean; importFile: ImportFileFunction };
@@ -0,0 +1,46 @@
1
+ import { request } from "@cqsjjb/jjb-common-lib/http";
2
+ import { useState } from "react";
3
+
4
+ /**
5
+ * 导入文件
6
+ */
7
+ export default function useImportFile(returnType = "object") {
8
+ // loading状态
9
+ const [loading, setLoading] = useState(false);
10
+
11
+ // 导入文件
12
+ const importFile = (url, options) => {
13
+ setLoading(true);
14
+
15
+ return new Promise((resolve, reject) => {
16
+ const { files = [], params = {} } = options;
17
+ const formData = new FormData();
18
+
19
+ // 将文件添加到formData中
20
+ files.forEach((f) => {
21
+ f.originFileObj && formData.append("file", f.originFileObj);
22
+ });
23
+
24
+ // 将额外携带的参数添加到formData中
25
+ Object.keys(params).forEach((key) => {
26
+ formData.append(key, params[key]);
27
+ });
28
+
29
+ // 发送请求
30
+ request(url, "post", formData, { "Content-Type": "multipart/form-data" })
31
+ .then((res) => {
32
+ resolve(res);
33
+ })
34
+ .catch((err) => {
35
+ reject(err);
36
+ })
37
+ .finally(() => {
38
+ setLoading(false);
39
+ });
40
+ });
41
+ };
42
+
43
+ if (returnType === "array")
44
+ return [loading, importFile];
45
+ return { loading, importFile };
46
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "zy-react-library",
3
3
  "private": false,
4
- "version": "1.0.28",
4
+ "version": "1.0.30",
5
5
  "type": "module",
6
6
  "description": "",
7
7
  "author": "LiuJiaNan",