sadais-piui 1.2.7-beta186 → 1.2.7-beta187

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.
@@ -367,6 +367,13 @@ export default {
367
367
  }
368
368
  }
369
369
  },
370
+ // 网络异常监听配置
371
+ networkConfig: {
372
+ type: Object,
373
+ default() {
374
+ return uploadImg.networkConfig
375
+ }
376
+ },
370
377
  // 删除之前的钩子
371
378
  beforeRemove: {
372
379
  type: [Object, Function],
@@ -841,6 +848,8 @@ export default {
841
848
  },
842
849
 
843
850
  async handleUpload(paths) {
851
+ let imageUploadDuration = 0
852
+ let imageUploadCount = 0
844
853
  const beforeUpload = this.beforeUpload[PI_DEFAULT_FN_FLAG]
845
854
  ? this.beforeUpload.fn
846
855
  : this.beforeUpload
@@ -878,13 +887,21 @@ export default {
878
887
  formData: this.formData,
879
888
  header: this.headers
880
889
  }
881
- if (this.$pi.lang.isFunction(onProgress)) {
882
- console.log(TAG, '使用自定义上传函数')
883
- // 上传之前的钩子, 如果定义了则使用钩子上传文件
884
- uploadResult = await onProgress(params, this.customUploadParams)
885
- } else {
886
- const [, uploadFileResult] = await uni.uploadFile(params)
887
- uploadResult = uploadFileResult
890
+ const uploadStartTime = Date.now()
891
+ try {
892
+ if (this.$pi.lang.isFunction(onProgress)) {
893
+ console.log(TAG, '使用自定义上传函数')
894
+ // 上传之前的钩子, 如果定义了则使用钩子上传文件
895
+ uploadResult = await onProgress(params, this.customUploadParams)
896
+ } else {
897
+ const [, uploadFileResult] = await uni.uploadFile(params)
898
+ uploadResult = uploadFileResult
899
+ }
900
+ } finally {
901
+ if (this.isNetworkMonitorImage(file)) {
902
+ imageUploadDuration += Date.now() - uploadStartTime
903
+ imageUploadCount++
904
+ }
888
905
  }
889
906
  console.log(TAG, '上传文件结果', uploadResult)
890
907
 
@@ -902,6 +919,28 @@ export default {
902
919
  } catch (e) {
903
920
  console.log(TAG, '上传文件失败', e)
904
921
  this.$emit('fail', e)
922
+ } finally {
923
+ this.handleNetworkUploadEnd(imageUploadDuration, imageUploadCount)
924
+ }
925
+ },
926
+ isNetworkMonitorImage(file) {
927
+ const filePath = file?.path || file?.tempFilePath || file || ''
928
+ return this.type === 'image' || isImageFilename(filePath)
929
+ },
930
+ handleNetworkUploadEnd(imageUploadDuration, imageUploadCount) {
931
+ const { enable, warningThreshold } = this.networkConfig || {}
932
+ const threshold = Number(warningThreshold)
933
+ if (
934
+ !enable ||
935
+ !imageUploadCount ||
936
+ !Number.isFinite(threshold) ||
937
+ threshold <= 0
938
+ ) {
939
+ return
940
+ }
941
+
942
+ if (imageUploadDuration / imageUploadCount > threshold * 1000) {
943
+ this.$emit('networkError')
905
944
  }
906
945
  },
907
946
  handleCrop(res) {
@@ -98,6 +98,10 @@ export default {
98
98
  }, // 压缩配置
99
99
  beforeUpload: null, // 上传之前的钩子
100
100
  onProgress: null, // 文件上传时的钩子
101
+ networkConfig: {
102
+ enable: false,
103
+ warningThreshold: 10 // 慢上传提醒阈值,单位秒
104
+ }, // 网络异常监听配置
101
105
  beforeRemove: null, // 删除之前的钩子
102
106
  customTakePhoto: false, // 是否自定义拍照(如果为true,则不调用相机,通过事件自己实现拍照上传)
103
107
  fileIconMap: {}, // 文件类型和图标映射关系,对象形式({PDF: 'https://xxx'})
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sadais-piui",
3
- "version": "1.2.7-beta186",
3
+ "version": "1.2.7-beta187",
4
4
  "description": "piui组件库",
5
5
  "main": "index.js",
6
6
  "scripts": {