sadais-piui 1.2.7-beta186 → 1.2.7-beta188

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.
@@ -1,12 +1,3 @@
1
- <!--
2
- * @Author: zhangzhenfei
3
- * @Date: 2021-08-13 11:31:57
4
- * @LastEditTime: 2025-04-18 16:28:01
5
- * @LastEditors: zhangzhenfei
6
- * @Description: 图片上传组件
7
- * @FilePath: \piui\components\pi-upload-img\index.vue
8
- -->
9
-
10
1
  <template>
11
2
  <view
12
3
  class="pi-upload-img"
@@ -236,6 +227,13 @@ export default {
236
227
  return uploadImg.imgField
237
228
  }
238
229
  },
230
+ // 文件预览名称字段,字符串形式(默认值:'name')
231
+ imgNameField: {
232
+ type: String,
233
+ default() {
234
+ return uploadImg.imgNameField
235
+ }
236
+ },
239
237
  // 显示的图片模式
240
238
  imageMode: {
241
239
  type: String,
@@ -367,6 +365,13 @@ export default {
367
365
  }
368
366
  }
369
367
  },
368
+ // 网络异常监听配置
369
+ networkConfig: {
370
+ type: Object,
371
+ default() {
372
+ return uploadImg.networkConfig
373
+ }
374
+ },
370
375
  // 删除之前的钩子
371
376
  beforeRemove: {
372
377
  type: [Object, Function],
@@ -729,7 +734,10 @@ export default {
729
734
  handlePreview(img) {
730
735
  if (this.disablePreview) return
731
736
  const file = this.imgField ? img[this.imgField] : img
732
- const customFileName = (this.imgField ? img?.name : '') || (file ? file.substring(file.lastIndexOf('/') + 1) : '')
737
+ const customFileName =
738
+ (this.imgField && this.imgNameField ? img?.[this.imgNameField] : '') ||
739
+ (this.imgField ? img?.name : '') ||
740
+ (file ? file.substring(file.lastIndexOf('/') + 1) : '')
733
741
 
734
742
  if (isImageFilename(file)) {
735
743
  this.handlePreviewImage(file)
@@ -841,6 +849,8 @@ export default {
841
849
  },
842
850
 
843
851
  async handleUpload(paths) {
852
+ let imageUploadDuration = 0
853
+ let imageUploadCount = 0
844
854
  const beforeUpload = this.beforeUpload[PI_DEFAULT_FN_FLAG]
845
855
  ? this.beforeUpload.fn
846
856
  : this.beforeUpload
@@ -878,13 +888,21 @@ export default {
878
888
  formData: this.formData,
879
889
  header: this.headers
880
890
  }
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
891
+ const uploadStartTime = Date.now()
892
+ try {
893
+ if (this.$pi.lang.isFunction(onProgress)) {
894
+ console.log(TAG, '使用自定义上传函数')
895
+ // 上传之前的钩子, 如果定义了则使用钩子上传文件
896
+ uploadResult = await onProgress(params, this.customUploadParams)
897
+ } else {
898
+ const [, uploadFileResult] = await uni.uploadFile(params)
899
+ uploadResult = uploadFileResult
900
+ }
901
+ } finally {
902
+ if (this.isNetworkMonitorImage(file)) {
903
+ imageUploadDuration += Date.now() - uploadStartTime
904
+ imageUploadCount++
905
+ }
888
906
  }
889
907
  console.log(TAG, '上传文件结果', uploadResult)
890
908
 
@@ -902,6 +920,28 @@ export default {
902
920
  } catch (e) {
903
921
  console.log(TAG, '上传文件失败', e)
904
922
  this.$emit('fail', e)
923
+ } finally {
924
+ this.handleNetworkUploadEnd(imageUploadDuration, imageUploadCount)
925
+ }
926
+ },
927
+ isNetworkMonitorImage(file) {
928
+ const filePath = file?.path || file?.tempFilePath || file || ''
929
+ return this.type === 'image' || isImageFilename(filePath)
930
+ },
931
+ handleNetworkUploadEnd(imageUploadDuration, imageUploadCount) {
932
+ const { enable, warningThreshold } = this.networkConfig || {}
933
+ const threshold = Number(warningThreshold)
934
+ if (
935
+ !enable ||
936
+ !imageUploadCount ||
937
+ !Number.isFinite(threshold) ||
938
+ threshold <= 0
939
+ ) {
940
+ return
941
+ }
942
+
943
+ if (imageUploadDuration / imageUploadCount > threshold * 1000) {
944
+ this.$emit('networkError')
905
945
  }
906
946
  },
907
947
  handleCrop(res) {
@@ -1119,10 +1159,6 @@ export default {
1119
1159
 
1120
1160
  const tempFilePaths = compressTempFiles.length > 0 ? compressTempFiles : tempFiles
1121
1161
 
1122
- console.log('this.getUploadMaxCount',this.getUploadMaxCount)
1123
- console.log('this.crop',this.crop)
1124
- console.log('tempFilePaths.length',tempFilePaths.length)
1125
-
1126
1162
  if (
1127
1163
  this.getUploadMaxCount === 1 &&
1128
1164
  this.crop &&
@@ -13,6 +13,7 @@ export default {
13
13
  customClass: '', // 自定义样式类,字符串形式('')
14
14
  customStyle: {}, // 自定义样式,对象形式(默认值:{})
15
15
  imgField: '', // 上传图片的字段名称,字符串形式(默认值:'')
16
+ imgNameField: 'name', // 文件预览名称字段,字符串形式(默认值:'name')
16
17
  action: '', // 上传接口地址,字符串形式(默认值:'')
17
18
  headers: {}, // 请求头,对象形式(默认值:{})
18
19
  formData: {}, // 上传表单额外数据,对象形式(默认值:{})
@@ -98,6 +99,10 @@ export default {
98
99
  }, // 压缩配置
99
100
  beforeUpload: null, // 上传之前的钩子
100
101
  onProgress: null, // 文件上传时的钩子
102
+ networkConfig: {
103
+ enable: false,
104
+ warningThreshold: 10 // 慢上传提醒阈值,单位秒
105
+ }, // 网络异常监听配置
101
106
  beforeRemove: null, // 删除之前的钩子
102
107
  customTakePhoto: false, // 是否自定义拍照(如果为true,则不调用相机,通过事件自己实现拍照上传)
103
108
  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-beta188",
4
4
  "description": "piui组件库",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,4 +25,4 @@
25
25
  "publishConfig": {
26
26
  "registry": "https://registry.npmjs.org/"
27
27
  }
28
- }
28
+ }