sadais-piui 1.2.7-beta181 → 1.2.7-beta183

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.
@@ -177,7 +177,8 @@ import {
177
177
  getFileInfoForPaths,
178
178
  getImageInfo,
179
179
  calculateCompressDimensions,
180
- handleCompressImgs
180
+ handleCompressImgs,
181
+ downloadFileAndPreview
181
182
  } from '../../utils/file-utils'
182
183
 
183
184
  const TAG = 'PiUploadImg'
@@ -480,10 +481,6 @@ export default {
480
481
  // #endif
481
482
  return isQYWX
482
483
  },
483
- // 是否pc端
484
- isPcMiniProgram() {
485
- return this.$pi.system.systemInfo.platform === 'windows'
486
- },
487
484
  getDisabled() {
488
485
  const parent = this.$pi.parent(this, 'PiFormItem')
489
486
  return this.disabled || (parent && parent.getDisabled)
@@ -508,12 +505,7 @@ export default {
508
505
  id: 2,
509
506
  text: '选择微信聊天文件'
510
507
  },
511
- {
512
- id: 3,
513
- text: '选择文件',
514
- hidden: !this.isPcMiniProgram
515
- }
516
- ].filter(item => !item.hidden)
508
+ ]
517
509
  },
518
510
  currentSourceType() {
519
511
  if (this.type === 'video') {
@@ -734,6 +726,8 @@ export default {
734
726
  handlePreview(img) {
735
727
  if (this.disablePreview) return
736
728
  const file = this.imgField ? img[this.imgField] : img
729
+ const customFileName = (this.imgField ? img?.name : '') || (file ? file.substring(file.lastIndexOf('/') + 1) : '')
730
+
737
731
  if (isImageFilename(file)) {
738
732
  this.handlePreviewImage(file)
739
733
  return
@@ -753,21 +747,7 @@ export default {
753
747
  form.submit()
754
748
  return
755
749
  }
756
- uni.downloadFile({
757
- url: file,
758
- success: res => {
759
- const filePath = res.tempFilePath
760
- // fileType 文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx,支付宝小程序仅支持pdf
761
- // 支付宝小程序必填,其他平台非必填
762
- uni.openDocument({
763
- filePath,
764
- showMenu: true, // 右上角是否有可以转发分享的功能
765
- complete: res => {
766
- console.log('文件预览', res)
767
- }
768
- })
769
- }
770
- })
750
+ downloadFileAndPreview(file, customFileName)
771
751
  },
772
752
  // 预览图片
773
753
  handlePreviewImage(img) {
@@ -1289,10 +1269,8 @@ export default {
1289
1269
  handleActionSheetSelect(type) {
1290
1270
  if (type.id === 1) {
1291
1271
  this.handleChooseMedia()
1292
- } else if (type.id === 2) {
1272
+ } else {
1293
1273
  this.handleSelectMessageFile()
1294
- } else if (type.id === 3) {
1295
- this.handleChooseFile()
1296
1274
  }
1297
1275
  },
1298
1276
  handleEmitTakePhoto(takeType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sadais-piui",
3
- "version": "1.2.7-beta181",
3
+ "version": "1.2.7-beta183",
4
4
  "description": "piui组件库",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -271,3 +271,93 @@ export async function handleCompressImgs(tempFilePaths, compressOption = {}) {
271
271
  }
272
272
  return compressUrls
273
273
  }
274
+
275
+ /**
276
+ * 清空用户目录下的临时文件
277
+ */
278
+ export function clearTempFiles() {
279
+ return new Promise(resolve => {
280
+ const fileSystemManager = uni.getFileSystemManager()
281
+ const basePath = wx.env.USER_DATA_PATH // 用户数据路径
282
+ // 读取目录内文件列表
283
+ fileSystemManager.readdir({
284
+ dirPath: basePath,
285
+ success: res => {
286
+ console.log('目录内文件列表:', res)
287
+
288
+ // 清空临时文件
289
+ res.files?.forEach(item => {
290
+ fileSystemManager.unlink({
291
+ filePath: `${basePath}/${item}`,
292
+ success: () => console.log('删除临时文件成功:', item),
293
+ fail: err => console.log('删除临时文件失败:', err)
294
+ })
295
+ })
296
+
297
+ resolve({
298
+ success: true,
299
+ message: '清空临时文件成功'
300
+ })
301
+ },
302
+ fail: err => {
303
+ console.log('读取目录内文件列表失败:', err)
304
+ resolve({
305
+ success: false,
306
+ message: err
307
+ })
308
+ }
309
+ })
310
+ })
311
+ }
312
+
313
+ /**
314
+ * 下载文件并保存到本地预览
315
+ * @param {String} fileUrl 文件url
316
+ * @param {String} customFileName 自定义文件路径和名称
317
+ * @param {Object} previewOptions 预览选项
318
+ */
319
+ export function downloadFileAndPreview(fileUrl, customFileName = '', previewOptions = {}) {
320
+ return new Promise(async (resolve, reject) => {
321
+ const downloadOptions = { url: fileUrl }
322
+ const { showMenu = true } = previewOptions || {}
323
+
324
+ // #ifdef MP-WEIXIN
325
+ if (customFileName) {
326
+ const { success, message } = await clearTempFiles()
327
+ if (!success) {
328
+ reject(message)
329
+ return
330
+ }
331
+
332
+ // 替换斜杠和横杠为下划线,防止预览时路径有问题
333
+ const customFileNameStr = customFileName.replace(/[-/]/g, '_')
334
+ downloadOptions.filePath = `${wx.env.USER_DATA_PATH}/${customFileNameStr}`
335
+ }
336
+ // #endif
337
+
338
+ uni.downloadFile({
339
+ ...downloadOptions,
340
+ success: res => {
341
+ if (res.statusCode === 200) {
342
+ uni.openDocument({
343
+ filePath: res.tempFilePath || res.filePath,
344
+ showMenu,
345
+ success: () => {
346
+ resolve()
347
+ },
348
+ fail: err => {
349
+ console.log('文件预览失败', err)
350
+ reject(`文件预览失败: ${err?.errMsg || '未知错误'}`)
351
+ }
352
+ })
353
+ } else {
354
+ reject(`文件预览失败: ${res?.errMsg || '未知错误'}`)
355
+ }
356
+ },
357
+ fail: error => {
358
+ console.log('文件下载失败', error)
359
+ reject(`文件下载失败: ${error?.errMsg || '未知错误'}`)
360
+ }
361
+ })
362
+ })
363
+ }