sadais-piui 1.2.7-beta182 → 1.2.7-beta184

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.
@@ -107,7 +107,7 @@
107
107
  <pi-img-cropper
108
108
  :custom-style="getCropOption.customStyle"
109
109
  :custom-class="getCropOption.customClass"
110
- :src="cropPic"
110
+ :src="cropPic.path"
111
111
  :file-type="getCropOption.fileType"
112
112
  :quality="getCropOption.quality"
113
113
  :crop-size="getCropOption.cropSize"
@@ -175,9 +175,8 @@ import {
175
175
  parseFileSize,
176
176
  formatFileSize,
177
177
  getFileInfoForPaths,
178
- getImageInfo,
179
- calculateCompressDimensions,
180
- handleCompressImgs
178
+ handleCompressImgs,
179
+ downloadFileAndPreview
181
180
  } from '../../utils/file-utils'
182
181
 
183
182
  const TAG = 'PiUploadImg'
@@ -327,7 +326,9 @@ export default {
327
326
  crop: {
328
327
  type: Boolean,
329
328
  // `false`
330
- default: uploadImg.crop
329
+ default() {
330
+ return uploadImg.crop
331
+ }
331
332
  },
332
333
  // 裁剪参数
333
334
  cropOption: {
@@ -464,7 +465,10 @@ export default {
464
465
  data() {
465
466
  return {
466
467
  showCrop: false, // 是否显示裁剪工具
467
- cropPic: '', // 需要剪裁的图片
468
+ cropPic: {
469
+ path: '',
470
+ name: ''
471
+ }, // 需要剪裁的图片
468
472
  cropedPic: '', // 剪裁后的图片
469
473
  showActionSheet: false, // 是否显示上传视频操作面板
470
474
  showCustomTakePhoto: false // 是否显示自定义拍照操作面板
@@ -725,6 +729,8 @@ export default {
725
729
  handlePreview(img) {
726
730
  if (this.disablePreview) return
727
731
  const file = this.imgField ? img[this.imgField] : img
732
+ const customFileName = (this.imgField ? img?.name : '') || (file ? file.substring(file.lastIndexOf('/') + 1) : '')
733
+
728
734
  if (isImageFilename(file)) {
729
735
  this.handlePreviewImage(file)
730
736
  return
@@ -744,21 +750,7 @@ export default {
744
750
  form.submit()
745
751
  return
746
752
  }
747
- uni.downloadFile({
748
- url: file,
749
- success: res => {
750
- const filePath = res.tempFilePath
751
- // fileType 文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx,支付宝小程序仅支持pdf
752
- // 支付宝小程序必填,其他平台非必填
753
- uni.openDocument({
754
- filePath,
755
- showMenu: true, // 右上角是否有可以转发分享的功能
756
- complete: res => {
757
- console.log('文件预览', res)
758
- }
759
- })
760
- }
761
- })
753
+ downloadFileAndPreview(file, customFileName)
762
754
  },
763
755
  // 预览图片
764
756
  handlePreviewImage(img) {
@@ -921,21 +913,17 @@ export default {
921
913
  console.log(TAG, '取消剪裁')
922
914
  this.$emit('cancelCrop')
923
915
  this.showCrop = false
924
- this.cropPic = ''
916
+ this.cropPic = {
917
+ path: '',
918
+ name: ''
919
+ }
925
920
  },
926
921
  async handleCompelteCrop() {
927
922
  console.log(TAG, '完成剪裁')
928
923
  this.$emit('croped', [this.cropedPic])
929
924
  this.showCrop = false
930
925
 
931
- // 剪裁完成后也要检查文件大小
932
- const tempFilesWithSize = await getFileInfoForPaths([this.cropedPic])
933
- const sizeCheckResult = this.checkFileSize(tempFilesWithSize)
934
-
935
- if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
936
- const validPaths = sizeCheckResult.files.map(file => file.path || file)
937
- this.handleUpload(validPaths)
938
- }
926
+ this.handleCheckFileSize([this.cropPic])
939
927
  },
940
928
 
941
929
  handleChoose() {
@@ -1024,7 +1012,7 @@ export default {
1024
1012
  }
1025
1013
 
1026
1014
  let { errMsg, tempFiles } = result
1027
- console.log(TAG, '上传文件结果', result)
1015
+ console.log(TAG, '选择文件结果', result)
1028
1016
  if (errMsg !== 'chooseFile:ok') {
1029
1017
  throw new Error(errMsg)
1030
1018
  }
@@ -1102,19 +1090,39 @@ export default {
1102
1090
  return
1103
1091
  }
1104
1092
 
1105
- let { errMsg, tempFilePaths } = result
1093
+ let { errMsg, tempFiles } = result
1106
1094
  if (errMsg !== 'chooseImage:ok') {
1107
1095
  throw new Error(errMsg)
1108
1096
  }
1097
+
1098
+ console.log(TAG, '选择图片结果', tempFiles)
1099
+
1100
+ const compressTempFiles = []
1101
+
1109
1102
  // #ifndef H5
1110
- // 压缩图片
1111
- if (this.compressOption) {
1112
- tempFilePaths = await handleCompressImgs(
1113
- tempFilePaths,
1114
- this.compressOption
1115
- )
1103
+ // 非H5端/开发者工具,执行压缩图片操作
1104
+ if (this.$pi.system.systemInfo.platform !== 'devtools') {
1105
+ for await (const tempFile of tempFiles) {
1106
+ const filePaths = await handleCompressImgs(
1107
+ [tempFile.path],
1108
+ this.compressOption
1109
+ )
1110
+
1111
+ compressTempFiles.push({
1112
+ path: filePaths[0],
1113
+ name: tempFile?.name || '',
1114
+ size: tempFile?.size || undefined,
1115
+ })
1116
+ }
1116
1117
  }
1117
1118
  // #endif
1119
+
1120
+ const tempFilePaths = compressTempFiles.length > 0 ? compressTempFiles : tempFiles
1121
+
1122
+ console.log('this.getUploadMaxCount',this.getUploadMaxCount)
1123
+ console.log('this.crop',this.crop)
1124
+ console.log('tempFilePaths.length',tempFilePaths.length)
1125
+
1118
1126
  if (
1119
1127
  this.getUploadMaxCount === 1 &&
1120
1128
  this.crop &&
@@ -1126,18 +1134,7 @@ export default {
1126
1134
  return
1127
1135
  }
1128
1136
 
1129
- // 对于选择的图片路径,我们需要构造合适的文件对象进行大小检查
1130
- // uni-app 的 chooseImage 返回的 tempFilePaths 不包含文件大小信息
1131
- const tempFilesWithSize = await getFileInfoForPaths(tempFilePaths)
1132
- const sizeCheckResult = this.checkFileSize(tempFilesWithSize)
1133
-
1134
- if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
1135
- // 恢复为路径数组格式传递给 handleUpload
1136
- const validPaths = sizeCheckResult.files.map(
1137
- file => file.path || file
1138
- )
1139
- this.handleUpload(validPaths)
1140
- }
1137
+ this.handleCheckFileSize(tempFilePaths)
1141
1138
  } catch (error) {
1142
1139
  console.log(TAG, '上传文件失败', error)
1143
1140
  this.$emit('fail', error)
@@ -1220,12 +1217,13 @@ export default {
1220
1217
  if (errMsg !== 'chooseMedia:ok') {
1221
1218
  throw new Error(errMsg)
1222
1219
  }
1223
- console.log(TAG, '上传媒体结果', result)
1220
+ console.log(TAG, '选择媒体结果', result)
1221
+
1222
+ const compressTempFiles = []
1224
1223
 
1225
1224
  // #ifndef H5
1226
1225
  // 非H5端/开发者工具,上传视频,执行压缩视频操作
1227
1226
  if (this.$pi.system.systemInfo.platform !== 'devtools') {
1228
- const compressTempFiles = []
1229
1227
  for await (const tempFile of tempFiles) {
1230
1228
  const tempFilePath = tempFile.tempFilePath
1231
1229
  let filePath = ''
@@ -1241,25 +1239,33 @@ export default {
1241
1239
  filePath = tempFile.tempFilePath
1242
1240
  }
1243
1241
 
1244
- compressTempFiles.push(filePath)
1242
+ compressTempFiles.push({
1243
+ path: filePath,
1244
+ name: tempFile?.name || '',
1245
+ size: tempFile?.size || undefined,
1246
+ })
1245
1247
  }
1246
- this.handleUpload(compressTempFiles)
1247
- return
1248
1248
  }
1249
1249
  // #endif
1250
1250
 
1251
+ const tempFilePaths = compressTempFiles.length > 0 ? compressTempFiles : tempFiles
1252
+
1251
1253
  // 检查文件大小限制
1252
- const sizeCheckResult = this.checkFileSize(tempFiles)
1253
- if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
1254
- this.handleUpload(
1255
- sizeCheckResult.files.map(file => file.tempFilePath || file)
1256
- )
1257
- }
1254
+ this.handleCheckFileSize(tempFilePaths)
1258
1255
  } catch (error) {
1259
1256
  console.log(TAG, '上传视频失败', error)
1260
1257
  this.$emit('fail', error)
1261
1258
  }
1262
1259
  },
1260
+ // 检查文件大小限制
1261
+ async handleCheckFileSize(tempFilePaths) {
1262
+ const tempFilesWithSize = await getFileInfoForPaths(tempFilePaths)
1263
+ const sizeCheckResult = this.checkFileSize(tempFilesWithSize)
1264
+ console.log(TAG, '检查文件大小限制结果', sizeCheckResult)
1265
+ if (sizeCheckResult.valid && sizeCheckResult.files.length > 0) {
1266
+ this.handleUpload(sizeCheckResult.files)
1267
+ }
1268
+ },
1263
1269
  // 压缩视频接口
1264
1270
  async handleCompressVideo(tempUrl) {
1265
1271
  console.log('视频压缩中')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sadais-piui",
3
- "version": "1.2.7-beta182",
3
+ "version": "1.2.7-beta184",
4
4
  "description": "piui组件库",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -84,25 +84,45 @@ export function formatFileSize(bytes) {
84
84
  export async function getFileInfoForPaths(filePaths) {
85
85
  const fileInfos = []
86
86
 
87
- for (const filePath of filePaths) {
87
+ for (const file of filePaths) {
88
+ const filePath = file.path || file.tempFilePath || file
88
89
  try {
90
+
91
+ if (file.size) {
92
+ fileInfos.push({
93
+ path: filePath,
94
+ name: file?.name || '',
95
+ size: file.size
96
+ })
97
+ continue
98
+ }
99
+
89
100
  const [, fileInfo] = await uni.getFileInfo({
90
- filePath: filePath
101
+ filePath
91
102
  })
92
103
 
93
104
  if (fileInfo && fileInfo.size !== undefined) {
94
105
  fileInfos.push({
95
106
  path: filePath,
107
+ name: file?.name || '',
96
108
  size: fileInfo.size
97
109
  })
98
110
  } else {
99
111
  // 如果无法获取文件信息,保持原路径(跳过大小检查)
100
- fileInfos.push(filePath)
112
+ fileInfos.push({
113
+ path: filePath,
114
+ name: file?.name || '',
115
+ size: undefined
116
+ })
101
117
  }
102
118
  } catch (error) {
103
119
  console.log('获取文件信息失败', error)
104
120
  // 如果获取文件信息失败,保持原路径(跳过大小检查)
105
- fileInfos.push(filePath)
121
+ fileInfos.push({
122
+ path: filePath,
123
+ name: file?.name || '',
124
+ size: undefined
125
+ })
106
126
  }
107
127
  }
108
128
 
@@ -271,3 +291,93 @@ export async function handleCompressImgs(tempFilePaths, compressOption = {}) {
271
291
  }
272
292
  return compressUrls
273
293
  }
294
+
295
+ /**
296
+ * 清空用户目录下的临时文件
297
+ */
298
+ export function clearTempFiles() {
299
+ return new Promise(resolve => {
300
+ const fileSystemManager = uni.getFileSystemManager()
301
+ const basePath = wx.env.USER_DATA_PATH // 用户数据路径
302
+ // 读取目录内文件列表
303
+ fileSystemManager.readdir({
304
+ dirPath: basePath,
305
+ success: res => {
306
+ console.log('目录内文件列表:', res)
307
+
308
+ // 清空临时文件
309
+ res.files?.forEach(item => {
310
+ fileSystemManager.unlink({
311
+ filePath: `${basePath}/${item}`,
312
+ success: () => console.log('删除临时文件成功:', item),
313
+ fail: err => console.log('删除临时文件失败:', err)
314
+ })
315
+ })
316
+
317
+ resolve({
318
+ success: true,
319
+ message: '清空临时文件成功'
320
+ })
321
+ },
322
+ fail: err => {
323
+ console.log('读取目录内文件列表失败:', err)
324
+ resolve({
325
+ success: false,
326
+ message: err
327
+ })
328
+ }
329
+ })
330
+ })
331
+ }
332
+
333
+ /**
334
+ * 下载文件并保存到本地预览
335
+ * @param {String} fileUrl 文件url
336
+ * @param {String} customFileName 自定义文件路径和名称
337
+ * @param {Object} previewOptions 预览选项
338
+ */
339
+ export function downloadFileAndPreview(fileUrl, customFileName = '', previewOptions = {}) {
340
+ return new Promise(async (resolve, reject) => {
341
+ const downloadOptions = { url: fileUrl }
342
+ const { showMenu = true } = previewOptions || {}
343
+
344
+ // #ifdef MP-WEIXIN
345
+ if (customFileName) {
346
+ const { success, message } = await clearTempFiles()
347
+ if (!success) {
348
+ reject(message)
349
+ return
350
+ }
351
+
352
+ // 替换斜杠和横杠为下划线,防止预览时路径有问题
353
+ const customFileNameStr = customFileName.replace(/[-/]/g, '_')
354
+ downloadOptions.filePath = `${wx.env.USER_DATA_PATH}/${customFileNameStr}`
355
+ }
356
+ // #endif
357
+
358
+ uni.downloadFile({
359
+ ...downloadOptions,
360
+ success: res => {
361
+ if (res.statusCode === 200) {
362
+ uni.openDocument({
363
+ filePath: res.tempFilePath || res.filePath,
364
+ showMenu,
365
+ success: () => {
366
+ resolve()
367
+ },
368
+ fail: err => {
369
+ console.log('文件预览失败', err)
370
+ reject(`文件预览失败: ${err?.errMsg || '未知错误'}`)
371
+ }
372
+ })
373
+ } else {
374
+ reject(`文件预览失败: ${res?.errMsg || '未知错误'}`)
375
+ }
376
+ },
377
+ fail: error => {
378
+ console.log('文件下载失败', error)
379
+ reject(`文件下载失败: ${error?.errMsg || '未知错误'}`)
380
+ }
381
+ })
382
+ })
383
+ }