Browse Source

查看文件

zhaoxiaohai 3 years ago
parent
commit
6755d6f03d
1 changed files with 53 additions and 0 deletions
  1. 53 0
      utils/checkFile.js

+ 53 - 0
utils/checkFile.js

@@ -0,0 +1,53 @@
+module.exports.checkFile = (item) => {
+    if (['image', 'video'].includes(item.fileType)) {
+        preViewMedia(item)
+    } else if (['word', 'excel', 'ppt', 'pdf'].includes(item.fileType)) {
+        openDocument(item)
+    } else {
+        wx.setClipboardData({
+            data: item.url,
+            success: function () {
+                wx.showToast({
+                    title: '当前文件类型不支持在线浏览,已将文件下载地址复制到剪切板,您可在浏览器中打开链接下载到本地浏览',
+                    icon: "none",
+                    duration: 5000
+                });
+            }
+        })
+    }
+}
+
+function preViewMedia(item) {
+    wx.previewMedia({
+        sources: [{
+            url: item.url,
+            type: item.fileType,
+        }],
+        current: 0,
+        showmenu: true
+    })
+}
+
+function openDocument(item) {
+    wx.downloadFile({
+        url: item.url,
+        success: function (res) {
+            console.log(res.tempFilePath)
+            wx.openDocument({
+                filePath: res.tempFilePath,
+                fileType: item.postfix,
+                showMenu: true,
+                success: function (res) {
+                    console.log(res)
+                    console.log('打开文档成功')
+                },
+                fail: (err) => {
+                    wx.showToast({
+                        title: '打开失败',
+                        icon: "none"
+                    })
+                }
+            })
+        }
+    })
+}