sadais-piui 1.2.7-beta182 → 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'
|
|
@@ -725,6 +726,8 @@ export default {
|
|
|
725
726
|
handlePreview(img) {
|
|
726
727
|
if (this.disablePreview) return
|
|
727
728
|
const file = this.imgField ? img[this.imgField] : img
|
|
729
|
+
const customFileName = (this.imgField ? img?.name : '') || (file ? file.substring(file.lastIndexOf('/') + 1) : '')
|
|
730
|
+
|
|
728
731
|
if (isImageFilename(file)) {
|
|
729
732
|
this.handlePreviewImage(file)
|
|
730
733
|
return
|
|
@@ -744,21 +747,7 @@ export default {
|
|
|
744
747
|
form.submit()
|
|
745
748
|
return
|
|
746
749
|
}
|
|
747
|
-
|
|
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
|
-
})
|
|
750
|
+
downloadFileAndPreview(file, customFileName)
|
|
762
751
|
},
|
|
763
752
|
// 预览图片
|
|
764
753
|
handlePreviewImage(img) {
|
package/package.json
CHANGED
package/utils/file-utils.js
CHANGED
|
@@ -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
|
+
}
|