@hzab/form-render-mobile 1.2.0 → 1.2.1-beta1
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/CHANGELOG.md +251 -243
- package/package.json +71 -71
- package/src/components/Uploader/common/ossUpload.js +69 -61
- package/src/components/Uploader/common/utils.js +147 -142
- package/src/components/Uploader/components/Image/index.tsx +45 -45
- package/src/components/Uploader/components/ItemList/index.tsx +53 -49
- package/src/components/Uploader/uploader.jsx +295 -287
|
@@ -1,287 +1,295 @@
|
|
|
1
|
-
import { useState, useRef, useEffect, useMemo, useImperativeHandle, forwardRef } from "react";
|
|
2
|
-
import { Button, Dialog, ActionSheet, Toast } from "antd-mobile";
|
|
3
|
-
import { nanoid } from "nanoid";
|
|
4
|
-
|
|
5
|
-
import ItemList from "./components/ItemList";
|
|
6
|
-
|
|
7
|
-
import { getImage, getVideo, getAudio } from "./common/cordova-camera";
|
|
8
|
-
import { handleMaxCount, handleInputFileList } from "./common/utils";
|
|
9
|
-
import { handleOssUpload } from "./common/ossUpload";
|
|
10
|
-
import uploadManager, { ChooserUploader, onGalleryUopload } from "../../common/location-utils";
|
|
11
|
-
|
|
12
|
-
import "./uploader.less";
|
|
13
|
-
|
|
14
|
-
const _ = require("lodash");
|
|
15
|
-
|
|
16
|
-
function Uploader(props, ref) {
|
|
17
|
-
const {
|
|
18
|
-
name,
|
|
19
|
-
multiple,
|
|
20
|
-
// 模式字符串: all | select | image | video 或 数组 select | image | video 组合:如:['select', 'image']
|
|
21
|
-
mode = "select",
|
|
22
|
-
onChange,
|
|
23
|
-
accept,
|
|
24
|
-
disabled,
|
|
25
|
-
readOnly,
|
|
26
|
-
value,
|
|
27
|
-
baseUrl = "",
|
|
28
|
-
// 文件大小限制
|
|
29
|
-
maxSize,
|
|
30
|
-
maxCount = 1,
|
|
31
|
-
// 是否使用 oss 上传文件
|
|
32
|
-
isOssUpload,
|
|
33
|
-
// 是否使用字符串结果
|
|
34
|
-
isStrRes,
|
|
35
|
-
// maxCount === 1 时,结果是否自动去除数组嵌套
|
|
36
|
-
isResRemoveArr = true,
|
|
37
|
-
ossUrl,
|
|
38
|
-
ossOpt,
|
|
39
|
-
onCountExceed,
|
|
40
|
-
headers,
|
|
41
|
-
uploadBtnNode,
|
|
42
|
-
// cordova插件上传类型 cordova-plugin-chooser | cordova-plugin-camera
|
|
43
|
-
cordovaPlugin = "cordova-plugin-camera",
|
|
44
|
-
uploaderProps
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const [
|
|
48
|
-
const [
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
setLoading(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
_files = _files
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
//
|
|
218
|
-
//
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
{
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
1
|
+
import { useState, useRef, useEffect, useMemo, useImperativeHandle, forwardRef } from "react";
|
|
2
|
+
import { Button, Dialog, ActionSheet, Toast } from "antd-mobile";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
|
|
5
|
+
import ItemList from "./components/ItemList";
|
|
6
|
+
|
|
7
|
+
import { getImage, getVideo, getAudio } from "./common/cordova-camera";
|
|
8
|
+
import { handleMaxCount, handleInputFileList } from "./common/utils";
|
|
9
|
+
import { handleOssUpload } from "./common/ossUpload";
|
|
10
|
+
import uploadManager, { ChooserUploader, onGalleryUopload } from "../../common/location-utils";
|
|
11
|
+
|
|
12
|
+
import "./uploader.less";
|
|
13
|
+
|
|
14
|
+
const _ = require("lodash");
|
|
15
|
+
|
|
16
|
+
function Uploader(props, ref) {
|
|
17
|
+
const {
|
|
18
|
+
name,
|
|
19
|
+
multiple,
|
|
20
|
+
// 模式字符串: all | select | image | video 或 数组 select | image | video 组合:如:['select', 'image']
|
|
21
|
+
mode = "select",
|
|
22
|
+
onChange,
|
|
23
|
+
accept,
|
|
24
|
+
disabled,
|
|
25
|
+
readOnly,
|
|
26
|
+
value,
|
|
27
|
+
baseUrl = "",
|
|
28
|
+
// 文件大小限制
|
|
29
|
+
maxSize,
|
|
30
|
+
maxCount = 1,
|
|
31
|
+
// 是否使用 oss 上传文件
|
|
32
|
+
isOssUpload,
|
|
33
|
+
// 是否使用字符串结果
|
|
34
|
+
isStrRes,
|
|
35
|
+
// maxCount === 1 时,结果是否自动去除数组嵌套
|
|
36
|
+
isResRemoveArr = true,
|
|
37
|
+
ossUrl,
|
|
38
|
+
ossOpt,
|
|
39
|
+
onCountExceed,
|
|
40
|
+
headers,
|
|
41
|
+
uploadBtnNode,
|
|
42
|
+
// cordova插件上传类型 cordova-plugin-chooser | cordova-plugin-camera
|
|
43
|
+
cordovaPlugin = "cordova-plugin-camera",
|
|
44
|
+
uploaderProps,
|
|
45
|
+
thumbnailParams = "x-oss-process=image/resize,w_100"
|
|
46
|
+
} = props;
|
|
47
|
+
const [loading, setLoading] = useState(false);
|
|
48
|
+
const [fileList, setFileList] = useState(handleInputFileList(value, maxCount, {
|
|
49
|
+
thumbnailParams,
|
|
50
|
+
ossOpt
|
|
51
|
+
}));
|
|
52
|
+
const [actionSheetVisible, setActionSheetVisible] = useState(false);
|
|
53
|
+
const uploadManagerRef = useRef(new uploadManager(new ChooserUploader()));
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
setFileList(handleInputFileList(value, maxCount, {
|
|
57
|
+
thumbnailParams,
|
|
58
|
+
ossOpt
|
|
59
|
+
}));
|
|
60
|
+
}, [value, thumbnailParams, ossOpt]);
|
|
61
|
+
|
|
62
|
+
useImperativeHandle(ref, () => ({ onPicker, setActionSheetVisible }));
|
|
63
|
+
|
|
64
|
+
async function onFileChange(e) {
|
|
65
|
+
e.persist();
|
|
66
|
+
const { files: rawFiles } = e.target;
|
|
67
|
+
let files = [...fileList, ...[].slice.call(rawFiles)];
|
|
68
|
+
// 超出个数限制只保留前面的 maxCount 个
|
|
69
|
+
if (maxCount > 0 && files.length > maxCount) {
|
|
70
|
+
onCountExceed && onCountExceed(files.length - maxCount);
|
|
71
|
+
files.length = maxCount;
|
|
72
|
+
Toast.show({
|
|
73
|
+
icon: "fail",
|
|
74
|
+
content: "文件个数超出限制",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
files = files.filter((it) => {
|
|
78
|
+
if (!it.uid) {
|
|
79
|
+
it.uid = nanoid();
|
|
80
|
+
}
|
|
81
|
+
if (maxSize && it.size > maxSize) {
|
|
82
|
+
Toast.show({
|
|
83
|
+
icon: "fail",
|
|
84
|
+
content: "文件大小超出限制:" + it.size,
|
|
85
|
+
});
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
return it;
|
|
89
|
+
});
|
|
90
|
+
// 处理 oss 逻辑
|
|
91
|
+
if (isOssUpload) {
|
|
92
|
+
setLoading(true);
|
|
93
|
+
files = await handleOssUpload(files, {
|
|
94
|
+
axios: props.axios,
|
|
95
|
+
axiosConf: props?.axiosConf,
|
|
96
|
+
...ossOpt,
|
|
97
|
+
ossUrl,
|
|
98
|
+
thumbnailParams
|
|
99
|
+
});
|
|
100
|
+
setLoading(false);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
e.target.value = ""; // HACK: fix the same file doesn't trigger onChange
|
|
104
|
+
handleChange(files);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function handleChange(files, optType) {
|
|
108
|
+
let _files = files;
|
|
109
|
+
if (optType !== "del" && maxCount > 1) {
|
|
110
|
+
_files = [...fileList, ..._files];
|
|
111
|
+
}
|
|
112
|
+
_files = handleMaxCount(files, maxCount);
|
|
113
|
+
setFileList(_files);
|
|
114
|
+
if (isOssUpload && isStrRes) {
|
|
115
|
+
_files = _files?.map((file) => {
|
|
116
|
+
return file.url || file.ossUrl;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (!_files || _files.length <= 0) {
|
|
120
|
+
_files = undefined;
|
|
121
|
+
} else if (isResRemoveArr && maxCount === 1) {
|
|
122
|
+
// maxCount 为1的时候返回结果去除数组
|
|
123
|
+
_files = _files[0];
|
|
124
|
+
}
|
|
125
|
+
onChange && onChange(_files);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const uploaderRef = useRef();
|
|
129
|
+
const audioUploaderRef = useRef();
|
|
130
|
+
|
|
131
|
+
const actions = useMemo(() => {
|
|
132
|
+
const allEnum = {
|
|
133
|
+
select: { text: "选择文件", key: "select" },
|
|
134
|
+
image: { text: "拍照", key: "captureImage" },
|
|
135
|
+
video: { text: "录像", key: "captureVideo" },
|
|
136
|
+
audio: { text: "录音", key: "captureAudio" },
|
|
137
|
+
};
|
|
138
|
+
if (Array.isArray(mode)) {
|
|
139
|
+
return mode.filter((it) => !!allEnum[it]).map((it) => allEnum[it]);
|
|
140
|
+
}
|
|
141
|
+
if (mode === "all") {
|
|
142
|
+
return Object.keys(allEnum).map((key) => allEnum[key]);
|
|
143
|
+
}
|
|
144
|
+
return allEnum[mode] ? [allEnum[mode]] : [allEnum.select];
|
|
145
|
+
}, [mode]);
|
|
146
|
+
|
|
147
|
+
function onPicker({ key }) {
|
|
148
|
+
switch (key) {
|
|
149
|
+
case "select":
|
|
150
|
+
if (navigator?.camera && cordovaPlugin === "cordova-plugin-camera") {
|
|
151
|
+
onGalleryUopload?.({
|
|
152
|
+
onUploadSuccess: (res) => {
|
|
153
|
+
onFileChange({
|
|
154
|
+
persist: () => { },
|
|
155
|
+
target: {
|
|
156
|
+
value: "",
|
|
157
|
+
files: [res],
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
options: {
|
|
162
|
+
cameraDirection: uploaderProps?.CameraConf?.frontCamera ? Camera.Direction.FRONT : null,
|
|
163
|
+
...uploaderProps?.CameraConf
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
} else if (window?.chooser && cordovaPlugin === "cordova-plugin-chooser") {
|
|
167
|
+
uploadManagerRef.current.uploadFile({
|
|
168
|
+
errorCallback(type) {
|
|
169
|
+
if (type === 2) {
|
|
170
|
+
Toast.show("文件类型错误,请重新选择");
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
onSuccess(values) {
|
|
174
|
+
onFileChange({
|
|
175
|
+
persist: () => { },
|
|
176
|
+
target: {
|
|
177
|
+
value: "",
|
|
178
|
+
files: [values.blob],
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
onError(error) {
|
|
183
|
+
console.log("文件选择失败:", error);
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
} else {
|
|
187
|
+
uploaderRef.current.click();
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
|
|
191
|
+
case "captureImage":
|
|
192
|
+
getImage(props.imgPluginOpt, props.getImgOpt).then((res) => {
|
|
193
|
+
onFileChange({
|
|
194
|
+
persist: () => { },
|
|
195
|
+
target: {
|
|
196
|
+
value: "",
|
|
197
|
+
files: [res],
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
break;
|
|
202
|
+
|
|
203
|
+
case "captureVideo":
|
|
204
|
+
getVideo().then((res) => {
|
|
205
|
+
onFileChange({
|
|
206
|
+
persist: () => { },
|
|
207
|
+
target: {
|
|
208
|
+
value: "",
|
|
209
|
+
files: [res],
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
break;
|
|
214
|
+
|
|
215
|
+
case "captureAudio":
|
|
216
|
+
audioUploaderRef.current.click();
|
|
217
|
+
// TODO: reader 报错
|
|
218
|
+
// getAudio().then((res) => {
|
|
219
|
+
// onFileChange({
|
|
220
|
+
// persist: () => {},
|
|
221
|
+
// target: {
|
|
222
|
+
// value: "",
|
|
223
|
+
// files: [res],
|
|
224
|
+
// },
|
|
225
|
+
// });
|
|
226
|
+
// });
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
setActionSheetVisible(false);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function onItemDel(idx) {
|
|
233
|
+
Dialog.confirm({
|
|
234
|
+
content: "确认删除?",
|
|
235
|
+
onConfirm: () => {
|
|
236
|
+
const middleValue = _.cloneDeep(fileList);
|
|
237
|
+
middleValue.splice(idx, 1);
|
|
238
|
+
handleChange(middleValue, "del");
|
|
239
|
+
setFileList(middleValue);
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const inputProps = {
|
|
245
|
+
className: "aria-hidden",
|
|
246
|
+
style: { display: "none" },
|
|
247
|
+
id: name,
|
|
248
|
+
onChange: onFileChange,
|
|
249
|
+
type: "file",
|
|
250
|
+
name: name,
|
|
251
|
+
multiple: multiple,
|
|
252
|
+
accept: accept,
|
|
253
|
+
capture: props.capture,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
return (
|
|
257
|
+
<div className="uploader">
|
|
258
|
+
<input {...inputProps} ref={uploaderRef}></input>
|
|
259
|
+
<input {...inputProps} ref={audioUploaderRef} id={name + "-audio"} accept="audio/*"></input>
|
|
260
|
+
<ItemList
|
|
261
|
+
fileList={fileList}
|
|
262
|
+
baseUrl={isOssUpload ? baseUrl : ""}
|
|
263
|
+
disabled={disabled}
|
|
264
|
+
readOnly={readOnly}
|
|
265
|
+
onItemDel={onItemDel}
|
|
266
|
+
headers={headers}
|
|
267
|
+
/>
|
|
268
|
+
{disabled || readOnly || (maxCount > 0 && fileList.length >= maxCount) ? null : (
|
|
269
|
+
<>
|
|
270
|
+
<div
|
|
271
|
+
className={`uploader-add-btn ${loading ? "uploader-add-btn-loading" : ""}`}
|
|
272
|
+
onClick={() => {
|
|
273
|
+
if (loading) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
setActionSheetVisible(true);
|
|
277
|
+
}}
|
|
278
|
+
loading={loading}
|
|
279
|
+
>
|
|
280
|
+
{loading ? "上传中..." : uploadBtnNode ? uploadBtnNode : "+"}
|
|
281
|
+
</div>
|
|
282
|
+
<ActionSheet
|
|
283
|
+
cancelText="取消"
|
|
284
|
+
visible={actionSheetVisible}
|
|
285
|
+
actions={actions}
|
|
286
|
+
onAction={onPicker}
|
|
287
|
+
onClose={() => setActionSheetVisible(false)}
|
|
288
|
+
/>
|
|
289
|
+
</>
|
|
290
|
+
)}
|
|
291
|
+
</div>
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export default forwardRef(Uploader);
|