sadais-piui 1.2.7-beta191 → 1.2.7-beta193

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.
@@ -512,7 +512,7 @@ export default {
512
512
  {
513
513
  id: 2,
514
514
  text: '选择微信聊天文件'
515
- },
515
+ }
516
516
  ]
517
517
  },
518
518
  currentSourceType() {
@@ -672,7 +672,19 @@ export default {
672
672
  : [])
673
673
  ]
674
674
  }
675
- return extension?.filter(ext => !!ext)
675
+ return [
676
+ ...new Set(
677
+ extension
678
+ .filter(ext => !!ext)
679
+ .map(ext =>
680
+ String(ext)
681
+ .trim()
682
+ .replace(/^\./, '')
683
+ .toLowerCase()
684
+ )
685
+ .filter(ext => !!ext)
686
+ )
687
+ ]
676
688
  }
677
689
  },
678
690
  methods: {
@@ -688,39 +700,27 @@ export default {
688
700
  return ext.toLocaleUpperCase()
689
701
  },
690
702
  isRightType(selectFile) {
691
- if(this.getExtension.length === 0) return true
692
- const isRightType = this.getExtension.some(ext =>
693
- selectFile.name.endsWith(ext)
694
- )
695
- console.log(
696
- TAG,
697
- '当前文件扩展名配置为:',
698
- this.getExtension,
699
- '当前选择文件:',
700
- selectFile.name,
701
- '是否符合:',
702
- isRightType
703
- )
704
- if (!isRightType) {
705
- uni.showModal({
706
- title: '文件类型错误',
707
- content: `请选择${this.getExtension.join('、')}类型的文件上传`,
708
- showCancel: false,
709
- confirmText: '好的',
710
- success: res => {
711
- if (res.confirm) {
712
- this.handleChoose()
713
- }
714
- }
715
- })
716
- }
717
- return isRightType
703
+ if (this.getExtension.length === 0) return true
704
+ const filePath =
705
+ typeof selectFile === 'string'
706
+ ? selectFile
707
+ : selectFile?.path || selectFile?.tempFilePath || ''
708
+ // 优先使用原始文件名,缺失时从临时路径提取。
709
+ const fileName =
710
+ selectFile?.name ||
711
+ filePath
712
+ .split(/[?#]/)[0]
713
+ .split(/[\\/]/)
714
+ .pop()
715
+ const extension = fileName.match(/\.([^.]+)$/)?.[1]?.toLowerCase()
716
+ return this.getExtension.includes(extension)
718
717
  },
719
718
  async handleDelete(index) {
720
719
  let val = null
721
- const beforeRemove = this.beforeRemove && this.beforeRemove[PI_DEFAULT_FN_FLAG]
722
- ? this.beforeRemove.fn
723
- : this.beforeRemove
720
+ const beforeRemove =
721
+ this.beforeRemove && this.beforeRemove[PI_DEFAULT_FN_FLAG]
722
+ ? this.beforeRemove.fn
723
+ : this.beforeRemove
724
724
  if (this.$pi.lang.isFunction(beforeRemove)) {
725
725
  // 删除之前的钩子, 如果定义了则使用钩子删除文件
726
726
  val = await beforeRemove(index, this.val)
@@ -849,6 +849,25 @@ export default {
849
849
  },
850
850
 
851
851
  async handleUpload(paths) {
852
+ // 所有入口统一校验,先于业务钩子和实际上传执行。
853
+ const files = paths.filter(file => this.isRightType(file))
854
+ if (files.length !== paths.length) {
855
+ uni.showModal({
856
+ title: '文件类型错误',
857
+ content: `已跳过不支持的文件,请选择${this.getExtension.join(
858
+ '、'
859
+ )}类型的文件上传`,
860
+ showCancel: false,
861
+ confirmText: '好的',
862
+ success: res => {
863
+ if (res.confirm && files.length === 0) {
864
+ this.handleChoose()
865
+ }
866
+ }
867
+ })
868
+ }
869
+ if (files.length === 0) return
870
+
852
871
  let imageUploadDuration = 0
853
872
  let imageUploadCount = 0
854
873
  const beforeUpload = this.beforeUpload[PI_DEFAULT_FN_FLAG]
@@ -861,7 +880,7 @@ export default {
861
880
  ? this.parseResultFn.fn
862
881
  : this.parseResultFn
863
882
  try {
864
- for await (const filePath of paths) {
883
+ for await (const filePath of files) {
865
884
  // ---------- 上传之前的钩子 ----------
866
885
  const file = filePath
867
886
 
@@ -1095,12 +1114,6 @@ export default {
1095
1114
  if (errMsg !== 'chooseMessageFile:ok') {
1096
1115
  throw new Error(errMsg)
1097
1116
  }
1098
- // chooseMessageFile 没有扩展名限制,这里自己做过滤
1099
- tempFiles = tempFiles.filter(file => {
1100
- return this.isRightType(file)
1101
- })
1102
- console.log(TAG, '过滤后的文件列表:', tempFiles)
1103
-
1104
1117
  // 检查文件大小限制
1105
1118
  const sizeCheckResult = this.checkFileSize(tempFiles)
1106
1119
  if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
@@ -1151,13 +1164,14 @@ export default {
1151
1164
  compressTempFiles.push({
1152
1165
  path: filePaths[0],
1153
1166
  name: tempFile?.name || '',
1154
- size: tempFile?.size || undefined,
1167
+ size: tempFile?.size || undefined
1155
1168
  })
1156
1169
  }
1157
1170
  }
1158
1171
  // #endif
1159
1172
 
1160
- const tempFilePaths = compressTempFiles.length > 0 ? compressTempFiles : tempFiles
1173
+ const tempFilePaths =
1174
+ compressTempFiles.length > 0 ? compressTempFiles : tempFiles
1161
1175
 
1162
1176
  if (
1163
1177
  this.getUploadMaxCount === 1 &&
@@ -1195,15 +1209,20 @@ export default {
1195
1209
  return
1196
1210
  }
1197
1211
 
1198
- let { errMsg, tempFilePath, tempFile } = result
1212
+ const { errMsg, tempFilePath, size } = result
1199
1213
  if (errMsg !== 'chooseVideo:ok') {
1200
1214
  throw new Error(errMsg)
1201
1215
  }
1202
- tempFile.path = tempFilePath
1216
+ let file = { path: tempFilePath, size }
1217
+ // #ifdef H5
1218
+ // H5 保留原始 File 对象,使用其文件名校验 blob 临时文件。
1219
+ file = result.tempFile || file
1220
+ file.path = tempFilePath
1221
+ // #endif
1203
1222
  console.log(TAG, '上传视频结果', result)
1204
1223
 
1205
1224
  // 检查文件大小限制
1206
- const sizeCheckResult = this.checkFileSize([tempFile])
1225
+ const sizeCheckResult = this.checkFileSize([file])
1207
1226
  if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
1208
1227
  this.handleUpload(sizeCheckResult.files)
1209
1228
  }
@@ -1219,39 +1238,58 @@ export default {
1219
1238
  const hasVideo = this.fileTypes.includes('video')
1220
1239
  const sourceType = this.getChooseFileOpts.sourceType
1221
1240
  if (hasImage && hasVideo) {
1222
- this.handleUniChooseMedia(['mix'], sourceType)
1241
+ this.handleUniChooseMedia({ mediaType: ['mix'], sourceType })
1223
1242
  } else if (hasImage) {
1224
- this.handleUniChooseMedia(['image'], sourceType)
1243
+ this.handleUniChooseMedia({ mediaType: ['image'], sourceType })
1225
1244
  } else if (hasVideo) {
1226
- this.handleUniChooseMedia(['video'], sourceType)
1245
+ this.handleUniChooseMedia({ mediaType: ['video'], sourceType })
1227
1246
  }
1228
1247
  } else {
1229
1248
  // 选择视频
1230
- this.handleUniChooseMedia(['video'], this.getChooseVideoOpts.sourceType)
1249
+ this.handleUniChooseMedia({
1250
+ mediaType: ['video'],
1251
+ sourceType: this.getChooseVideoOpts.sourceType
1252
+ })
1231
1253
  }
1232
1254
  },
1233
- async handleUniChooseMedia(mediaType, sourceType) {
1255
+ async handleUniChooseMedia({ mediaType, sourceType }) {
1234
1256
  const { maxDuration, camera } =
1235
1257
  this.type === 'video' ? this.getChooseVideoOpts : this.getChooseFileOpts
1236
1258
 
1237
1259
  try {
1238
- const [cancel, result] = await uni.chooseMedia({
1260
+ const chooseMediaOptions = {
1239
1261
  count: this.getUploadMaxCount,
1240
1262
  mediaType,
1241
1263
  extension: this.getExtension,
1242
1264
  sourceType,
1243
1265
  maxDuration,
1244
1266
  camera
1245
- })
1267
+ }
1268
+ // #ifdef MP-WEIXIN
1269
+ // iOS 相册视频先取原文件,避免选择器压缩失败后拿不到文件路径。
1270
+ // 选择成功后仍沿用组件压缩及失败回退流程。
1271
+ const isIosVideoAlbum =
1272
+ this.$pi.system.systemInfo.platform === 'ios' &&
1273
+ sourceType.includes('album') &&
1274
+ mediaType.some(type => ['video', 'mix'].includes(type))
1275
+ if (isIosVideoAlbum) chooseMediaOptions.sizeType = ['original']
1276
+ // #endif
1277
+ const [cancel, result] = await uni.chooseMedia(chooseMediaOptions)
1246
1278
 
1247
- if (cancel && cancel?.errMsg === 'chooseMedia:fail cancel') {
1248
- console.log(TAG, '用户取消选择媒体')
1249
- return
1279
+ if (cancel) {
1280
+ if (cancel.errMsg === 'chooseMedia:fail cancel') {
1281
+ console.log(TAG, '用户取消选择媒体')
1282
+ return
1283
+ }
1284
+ throw cancel
1250
1285
  }
1251
1286
 
1252
- let { errMsg, tempFiles } = result
1253
- if (errMsg !== 'chooseMedia:ok') {
1254
- throw new Error(errMsg)
1287
+ if (result?.errMsg !== 'chooseMedia:ok') {
1288
+ throw result || new Error('选择媒体失败,未返回有效结果')
1289
+ }
1290
+ const { tempFiles } = result
1291
+ if (!Array.isArray(tempFiles) || tempFiles.length === 0) {
1292
+ throw new Error('未获取到可上传的图片或视频,请重新选择')
1255
1293
  }
1256
1294
  console.log(TAG, '选择媒体结果', result)
1257
1295
 
@@ -1278,18 +1316,27 @@ export default {
1278
1316
  compressTempFiles.push({
1279
1317
  path: filePath,
1280
1318
  name: tempFile?.name || '',
1281
- size: tempFile?.size || undefined,
1319
+ size: tempFile?.size || undefined
1282
1320
  })
1283
1321
  }
1284
1322
  }
1285
1323
  // #endif
1286
1324
 
1287
- const tempFilePaths = compressTempFiles.length > 0 ? compressTempFiles : tempFiles
1325
+ const tempFilePaths =
1326
+ compressTempFiles.length > 0 ? compressTempFiles : tempFiles
1288
1327
 
1289
1328
  // 检查文件大小限制
1290
- this.handleCheckFileSize(tempFilePaths)
1329
+ await this.handleCheckFileSize(tempFilePaths)
1291
1330
  } catch (error) {
1292
- console.log(TAG, '上传视频失败', error)
1331
+ console.error(TAG, '选择或处理媒体失败', error)
1332
+ const errorMessage =
1333
+ error?.errMsg || error?.message || '未返回具体错误原因'
1334
+ uni.showModal({
1335
+ title: '选择或处理文件失败',
1336
+ content: `未能获取或处理所选图片、视频,请重新选择后重试。\n错误信息:${errorMessage}`,
1337
+ showCancel: false,
1338
+ confirmText: '我知道了'
1339
+ })
1293
1340
  this.$emit('fail', error)
1294
1341
  }
1295
1342
  },
@@ -1308,14 +1355,26 @@ export default {
1308
1355
  uni.showLoading({
1309
1356
  title: '视频压缩中...'
1310
1357
  })
1311
- const [, result] = await uni.compressVideo({
1312
- src: tempUrl,
1313
- quality: 'high' // 压缩质量 支持low, medium, high
1314
- })
1315
- uni.hideLoading()
1316
- const { errMsg, tempFilePath } = result
1317
- if (errMsg == 'compressVideo:ok') {
1318
- return tempFilePath
1358
+ try {
1359
+ const [error, result] = await uni.compressVideo({
1360
+ src: tempUrl,
1361
+ quality: 'high' // 压缩质量 支持low, medium, high
1362
+ })
1363
+ if (
1364
+ !error &&
1365
+ result?.errMsg === 'compressVideo:ok' &&
1366
+ result?.tempFilePath
1367
+ ) {
1368
+ return result.tempFilePath
1369
+ }
1370
+ console.error(TAG, '视频压缩失败,使用选择的视频继续上传', {
1371
+ error,
1372
+ result
1373
+ })
1374
+ } catch (error) {
1375
+ console.error(TAG, '视频压缩异常,使用选择的视频继续上传', error)
1376
+ } finally {
1377
+ uni.hideLoading()
1319
1378
  }
1320
1379
  return tempUrl
1321
1380
  },
@@ -1344,7 +1403,10 @@ export default {
1344
1403
  // #endif
1345
1404
 
1346
1405
  // #ifdef MP-WEIXIN
1347
- this.handleUniChooseMedia(['video'], ['camera'])
1406
+ this.handleUniChooseMedia({
1407
+ mediaType: ['video'],
1408
+ sourceType: ['camera']
1409
+ })
1348
1410
  // #endif
1349
1411
  return
1350
1412
  }
@@ -1359,7 +1421,10 @@ export default {
1359
1421
  // #endif
1360
1422
 
1361
1423
  // #ifdef MP-WEIXIN
1362
- this.handleUniChooseMedia(['video'], ['album'])
1424
+ this.handleUniChooseMedia({
1425
+ mediaType: ['video'],
1426
+ sourceType: ['album']
1427
+ })
1363
1428
  // #endif
1364
1429
  return
1365
1430
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sadais-piui",
3
- "version": "1.2.7-beta191",
3
+ "version": "1.2.7-beta193",
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
+ }