@hzab/form-render-mobile 1.0.4 → 1.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/form-render-mobile",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "formily-form-render-mobile",
5
5
  "main": "lib",
6
6
  "scripts": {
@@ -1,6 +1,9 @@
1
1
  declare global {
2
2
  interface Window {
3
3
  resolveLocalFileSystemURL?: (url: string, successCallback: (entry: any) => void, errorCallback?: (error: any) => void) => void;
4
+ chooser?:{
5
+ getFile: any
6
+ }
4
7
  }
5
8
 
6
9
  interface Navigator {
@@ -11,6 +14,7 @@ declare global {
11
14
  }
12
15
  }
13
16
 
17
+
14
18
  interface IGalleryUopload {
15
19
  options?: Partial<{
16
20
  quality: number;
@@ -89,13 +93,9 @@ export function onGalleryUopload(opt?: IGalleryUopload) {
89
93
  if (reader.result) {
90
94
  const imgBlob: any = new Blob([reader.result], { type: "image/jpeg" });
91
95
 
92
- if (!imgBlob?.name) imgBlob.name = `${Date.now()}.${imgBlob.type?.replace("image/", "")}`;
93
-
94
- if (!imgBlob?.lastModifiedDate) imgBlob.lastModifiedDate = new Date();
95
-
96
- if (!imgBlob?.lastModified) imgBlob.lastModified = imgBlob.lastModifiedDate.getTime();
96
+ const _blob = addBlobAttributeUtil(imgBlob)
97
97
 
98
- onUploadSuccess?.(imgBlob);
98
+ onUploadSuccess?.(_blob);
99
99
  }
100
100
  };
101
101
  reader.readAsArrayBuffer(file);
@@ -122,3 +122,133 @@ export function onGalleryUopload(opt?: IGalleryUopload) {
122
122
  onUploadError?.(error);
123
123
  }
124
124
  }
125
+
126
+ interface ChooseUpload {
127
+ quality: number;
128
+ // 选择返回值的格式。通常有2个枚举:DATA_URL 0 或者 FILE_URI 1
129
+ destinationType: number;
130
+ // 图片来源类型: 0 照片库,1 相机, 2 已保存相册
131
+ sourceType: number;
132
+ encodingType: string;
133
+ mediaType: string;
134
+ allowEdit: boolean;
135
+ correctOrientation: boolean;
136
+ }
137
+
138
+ interface IChooserOptions {
139
+ mimeTypes?: string[];
140
+ // maxFileSize?: number
141
+ }
142
+
143
+ // 异常类型
144
+ export enum ErrorCallbackEnum {
145
+ // 文件超出指定大小
146
+ SIZE_EXCEED_ERROR = 1,
147
+
148
+ // 文件类型错误
149
+ FILE_TYPE_ERROR = 2
150
+ }
151
+
152
+ type ExclusiveOptions = ChooseUpload & IChooserOptions;
153
+
154
+ interface IUploadOptions {
155
+ additionalParams?: ExclusiveOptions;
156
+ errorCallback?: (type?: ErrorCallbackEnum) => void;
157
+ onSuccess?: (values: { blob: Blob, fileType: string }) => void;
158
+ onError?: (error: Error) => void;
159
+ }
160
+
161
+
162
+ interface IUploader {
163
+ upload(options: IUploadOptions): void;
164
+ }
165
+
166
+ /**
167
+ * 基础类
168
+ */
169
+ export default class UploadManager {
170
+ private uploader: IUploader;
171
+
172
+ defaultAccept: string[] = ["image/jpg", "image/jpeg", "image/png", "image/gif"];
173
+ // 单位MB
174
+ // maxFileSize: number = 1024000
175
+
176
+ constructor(uploader: IUploader) {
177
+ this.uploader = uploader;
178
+ }
179
+
180
+ addAccept(accepts: string[]) {
181
+ this.defaultAccept = [...this.defaultAccept, ...accepts]
182
+ }
183
+
184
+ // 切换插件
185
+ protected setUploader(uploader: IUploader) {
186
+ this.uploader = uploader;
187
+ }
188
+
189
+ uploadFile(options: IUploadOptions) {
190
+ this.uploader.upload(options);
191
+ }
192
+ }
193
+
194
+ const addBlobAttributeUtil = (blob: Blob | any) => {
195
+ const _blob = blob;
196
+ if (!blob?.name && blob.type.indexOf("image/") > 0) {
197
+ blob.name = `${Date.now()}.${blob.type?.replace("image/", "")}`;
198
+ }
199
+
200
+ if (!_blob?.lastModifiedDate) _blob.lastModifiedDate = new Date();
201
+
202
+ if (!_blob?.lastModified) _blob.lastModified = _blob.lastModifiedDate.getTime();
203
+
204
+ return _blob
205
+ }
206
+
207
+
208
+
209
+ /**
210
+ * 通过cordova-plugin-chooser上传文件
211
+ * 文档:https://github.com/MaximBelov/cordova-plugin-chooser
212
+ */
213
+ export class ChooserUploader extends UploadManager implements IUploader {
214
+
215
+ async upload(options: IUploadOptions) {
216
+ try {
217
+ const { mimeTypes} = options?.additionalParams || {}
218
+ const mergeAccepts = [...this.defaultAccept, ...(mimeTypes || [])]
219
+ // const maxFileSize = _maxFileSize || this.maxFileSize;
220
+
221
+ const fileObj: { path: string; mimeType: string; size: number; name: string } = await window.chooser.getFile({ mimeTypes: mergeAccepts.join(",") });
222
+ const { path, size, mimeType } = fileObj || {}
223
+
224
+ console.log("file", fileObj.mimeType, fileObj?.size)
225
+
226
+ // 不属于指定类型
227
+ if (!mergeAccepts.includes(fileObj.mimeType)) {
228
+ options?.errorCallback?.(ErrorCallbackEnum.FILE_TYPE_ERROR)
229
+ return
230
+ // 超出文件最大限制
231
+ }
232
+ // else if (size > maxFileSize) {
233
+ // options?.errorCallback?.(ErrorCallbackEnum.SIZE_EXCEED_ERROR)
234
+ // return
235
+ // }
236
+ else {
237
+ window.resolveLocalFileSystemURL(path, (fileEntry: any) => {
238
+ fileEntry.file((file: File) => {
239
+ const reader = new FileReader();
240
+ reader.onloadend = () => {
241
+ const blob: any = new Blob([reader.result as ArrayBuffer], { type: mimeType });
242
+ const _blob = addBlobAttributeUtil(blob)
243
+
244
+ options.onSuccess?.({ blob: _blob, fileType: mimeType });
245
+ };
246
+ reader.readAsArrayBuffer(file);
247
+ }, (err: any) => options.onError?.(err));
248
+ }, (err: any) => options.onError?.(err));
249
+ }
250
+ } catch (error) {
251
+ console.error("chooser:", error);
252
+ }
253
+ }
254
+ }
@@ -7,7 +7,7 @@ import ItemList from "./components/ItemList";
7
7
  import { getImage, getVideo, getAudio } from "./common/cordova-camera";
8
8
  import { handleMaxCount, handleInputFileList } from "./common/utils";
9
9
  import { handleOssUpload } from "./common/ossUpload";
10
- import { onGalleryUopload } from "../../common/location-utils";
10
+ import uploadManager, { ChooserUploader, onGalleryUopload } from "../../common/location-utils";
11
11
 
12
12
  import "./uploader.less";
13
13
 
@@ -39,12 +39,14 @@ function Uploader(props, ref) {
39
39
  onCountExceed,
40
40
  headers,
41
41
  uploadBtnNode,
42
- // 是否使用cordova-plugin-camera插件上传文件
43
- enableCordovaPluginCamera = true,
42
+ // cordova插件上传类型 cordova-plugin-chooser | cordova-plugin-camera
43
+ cordovaPlugin = 'cordova-plugin-chooser'
44
44
  } = props;
45
45
  const [loading, setLoading] = useState(false);
46
46
  const [fileList, setFileList] = useState(handleInputFileList(value, maxCount));
47
47
  const [actionSheetVisible, setActionSheetVisible] = useState(false);
48
+ const uploadManagerRef = useRef(new uploadManager(new ChooserUploader()));
49
+
48
50
  useEffect(() => {
49
51
  setFileList(handleInputFileList(value, maxCount));
50
52
  }, [value]);
@@ -136,7 +138,7 @@ function Uploader(props, ref) {
136
138
  function onPicker({ key }) {
137
139
  switch (key) {
138
140
  case "select":
139
- if (navigator?.camera && enableCordovaPluginCamera) {
141
+ if (navigator?.camera && cordovaPlugin === 'cordova-plugin-camera') {
140
142
  onGalleryUopload?.({
141
143
  onUploadSuccess: (res) => {
142
144
  onFileChange({
@@ -148,6 +150,26 @@ function Uploader(props, ref) {
148
150
  });
149
151
  },
150
152
  });
153
+ } else if (window?.chooser && cordovaPlugin === 'cordova-plugin-chooser') {
154
+ uploadManagerRef.current.uploadFile({
155
+ errorCallback(type) {
156
+ if (type === 2) {
157
+ Toast.show("文件类型错误,请重新选择");
158
+ }
159
+ },
160
+ onSuccess(values) {
161
+ onFileChange({
162
+ persist: () => {},
163
+ target: {
164
+ value: "",
165
+ files: [values.blob],
166
+ },
167
+ });
168
+ },
169
+ onError(error) {
170
+ console.log("文件选择失败:", error);
171
+ },
172
+ });
151
173
  } else {
152
174
  uploaderRef.current.click();
153
175
  }