zy-react-library 1.0.29 → 1.0.31
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.
- package/hooks/useDownloadBlob/index.d.ts +3 -2
- package/hooks/useDownloadBlob/index.js +8 -3
- package/hooks/useDownloadFile/index.d.ts +11 -1
- package/hooks/useDownloadFile/index.js +55 -29
- package/hooks/useImportFile/index.d.ts +2 -1
- package/hooks/useImportFile/index.js +9 -4
- package/hooks/useTable/index.js +3 -0
- package/package.json +1 -1
|
@@ -12,9 +12,10 @@ export interface downloadBlobOptions {
|
|
|
12
12
|
options?: UseDownloadBlobOptions;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export type
|
|
15
|
+
export type DownloadBlobFunction = (options: downloadBlobOptions) => Promise<any>;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* 下载Blob流文件
|
|
19
19
|
*/
|
|
20
|
-
export default function
|
|
20
|
+
export default function useImportFile(returnType: "array"): [boolean, DownloadBlobFunction];
|
|
21
|
+
export default function useImportFile(returnType?: "object"): { loading: boolean; importFile: DownloadBlobFunction };
|
|
@@ -5,7 +5,7 @@ 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
|
|
|
@@ -14,7 +14,10 @@ export default function useDownloadBlob() {
|
|
|
14
14
|
setLoading(true);
|
|
15
15
|
|
|
16
16
|
return new Promise((resolve, reject) => {
|
|
17
|
-
|
|
17
|
+
if (!url)
|
|
18
|
+
return reject(new Error("请传入 url"));
|
|
19
|
+
|
|
20
|
+
const { name = "", type = "", params = {} } = options;
|
|
18
21
|
const finalUrl = (process.env.app.API_HOST || window.__JJB_ENVIRONMENT__.API_HOST) + url;
|
|
19
22
|
Object.entries(params).forEach(([key, value]) => {
|
|
20
23
|
finalUrl.searchParams.append(key, value);
|
|
@@ -58,5 +61,7 @@ export default function useDownloadBlob() {
|
|
|
58
61
|
});
|
|
59
62
|
};
|
|
60
63
|
|
|
61
|
-
|
|
64
|
+
if (returnType === "array")
|
|
65
|
+
return [loading, downloadBlob];
|
|
66
|
+
return { loading, downloadBlob };
|
|
62
67
|
}
|
|
@@ -1,4 +1,14 @@
|
|
|
1
|
+
export interface DownloadFileOptions {
|
|
2
|
+
/** 下载文件的URL */
|
|
3
|
+
url: string;
|
|
4
|
+
/** 下载文件的自定义文件名 */
|
|
5
|
+
name?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type DownloadFileFunction = (options: DownloadFileOptions) => void;
|
|
9
|
+
|
|
1
10
|
/**
|
|
2
11
|
* 下载文件
|
|
3
12
|
*/
|
|
4
|
-
export default function useDownloadFile(
|
|
13
|
+
export default function useDownloadFile(returnType: "array"): [boolean, DownloadFileFunction];
|
|
14
|
+
export default function useDownloadFile(returnType?: "object"): { loading: boolean; downloadFile: DownloadFileFunction };
|
|
@@ -1,36 +1,62 @@
|
|
|
1
1
|
import { message, Modal } from "antd";
|
|
2
2
|
import { getFileName, getFileSuffix, getFileUrl } from "../../utils/index.js";
|
|
3
|
+
import { useState } from "react";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* 下载文件
|
|
6
7
|
*/
|
|
7
|
-
export default function useDownloadFile(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
8
|
+
export default function useDownloadFile(returnType = "object") {
|
|
9
|
+
// loading状态
|
|
10
|
+
const [loading, setLoading] = useState(false);
|
|
11
|
+
|
|
12
|
+
// 下载文件
|
|
13
|
+
const downloadFile = (options) => {
|
|
14
|
+
setLoading(true);
|
|
15
|
+
|
|
16
|
+
const { url, name: fileName } = options;
|
|
17
|
+
if (!url)
|
|
18
|
+
throw new Error("请传入 url");
|
|
19
|
+
|
|
20
|
+
Modal.confirm({
|
|
21
|
+
title: "提示",
|
|
22
|
+
content: "确定要下载此文件吗?",
|
|
23
|
+
onOk: () => {
|
|
24
|
+
const fileUrl = getFileUrl();
|
|
25
|
+
let name = fileName;
|
|
26
|
+
|
|
27
|
+
if (name) {
|
|
28
|
+
if (!getFileSuffix(name) && getFileSuffix(url)) {
|
|
29
|
+
name = `${name}.${getFileSuffix(url)}`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
name = getFileName(url);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fetch(!url.includes(fileUrl) ? fileUrl + url : url)
|
|
37
|
+
.then(res => res.blob())
|
|
38
|
+
.then((blob) => {
|
|
39
|
+
const a = document.createElement("a");
|
|
40
|
+
document.body.appendChild(a);
|
|
41
|
+
a.style.display = "none";
|
|
42
|
+
const urlObject = window.URL.createObjectURL(blob);
|
|
43
|
+
a.href = urlObject;
|
|
44
|
+
a.download = `${name}`;
|
|
45
|
+
a.click();
|
|
46
|
+
document.body.removeChild(a);
|
|
47
|
+
window.URL.revokeObjectURL(urlObject);
|
|
48
|
+
})
|
|
49
|
+
.catch(() => {
|
|
50
|
+
message.error("下载失败");
|
|
51
|
+
})
|
|
52
|
+
.finally(() => {
|
|
53
|
+
setLoading(false);
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
if (returnType === "array")
|
|
60
|
+
return [loading, downloadFile];
|
|
61
|
+
return { loading, downloadFile };
|
|
36
62
|
}
|
|
@@ -21,4 +21,5 @@ export type ImportFileFunction = (url: string, options: UseImportFileOptions) =>
|
|
|
21
21
|
/**
|
|
22
22
|
* 导入文件
|
|
23
23
|
*/
|
|
24
|
-
export default function useImportFile(): [boolean, ImportFileFunction];
|
|
24
|
+
export default function useImportFile(returnType: "array"): [boolean, ImportFileFunction];
|
|
25
|
+
export default function useImportFile(returnType?: "object"): { loading: boolean; importFile: ImportFileFunction };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {request} from "@cqsjjb/jjb-common-lib/http";
|
|
1
|
+
import { request } from "@cqsjjb/jjb-common-lib/http";
|
|
2
2
|
import { useState } from "react";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 导入文件
|
|
6
6
|
*/
|
|
7
|
-
export default function useImportFile() {
|
|
7
|
+
export default function useImportFile(returnType = "object") {
|
|
8
8
|
// loading状态
|
|
9
9
|
const [loading, setLoading] = useState(false);
|
|
10
10
|
|
|
@@ -13,7 +13,10 @@ export default function useImportFile() {
|
|
|
13
13
|
setLoading(true);
|
|
14
14
|
|
|
15
15
|
return new Promise((resolve, reject) => {
|
|
16
|
-
|
|
16
|
+
if (!url)
|
|
17
|
+
return reject(new Error("请传入 url"));
|
|
18
|
+
|
|
19
|
+
const { files = [], params = {} } = options;
|
|
17
20
|
const formData = new FormData();
|
|
18
21
|
|
|
19
22
|
// 将文件添加到formData中
|
|
@@ -40,5 +43,7 @@ export default function useImportFile() {
|
|
|
40
43
|
});
|
|
41
44
|
};
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
if (returnType === "array")
|
|
47
|
+
return [loading, importFile];
|
|
48
|
+
return { loading, importFile };
|
|
44
49
|
}
|
package/hooks/useTable/index.js
CHANGED
|
@@ -101,6 +101,9 @@ function getQuery(keysStr, valuesStr) {
|
|
|
101
101
|
* 自定义 useTable,继承 ahooks 的 useAntdTable,根据需求进行扩展
|
|
102
102
|
*/
|
|
103
103
|
function useTable(service, options) {
|
|
104
|
+
if (!service)
|
|
105
|
+
throw new Error("请传入 service");
|
|
106
|
+
|
|
104
107
|
// 获取额外参数和转换函数
|
|
105
108
|
const { params: extraParams, transform, ...restOptions } = options || {};
|
|
106
109
|
|