Pārlūkot izejas kodu

保存文件地址与下载视频分开

xiaohaizhao 1 gadu atpakaļ
vecāks
revīzija
d445419004

+ 74 - 2
pages/tabbar/smartStore/modules/viewPage.js

@@ -74,11 +74,41 @@ Component({
                 this.closeShow();
             })
         },
+        mySaveVideo() {
+            const that = this,
+                fileSelected = this.data.fileSelected;
+            const downloadTask = wx.downloadFile({
+                url: fileSelected.url,
+                filePath: wx.env.USER_DATA_PATH + '/' + new Date().valueOf() + '.mp4',
+                timeout: 600000,
+                success(res) {
+                    console.log("下载", res)
+                    that.saveVideo(res.filePath)
+                },
+                fail({
+                    errMsg
+                }) {
+                    if (errMsg == 'downloadFile:fail exceed max file size') wx.showToast({
+                        title: '视频体积超大,无法保存!请尝试打开视频长按保存下载',
+                        icon: "none"
+                    })
+                }
+            })
+            downloadTask.onProgressUpdate((res) => {
+                wx.showLoading({
+                    title: `保存进度` + res.progress + `%`,
+                    mask: true
+                })
+                if (res.progress == 100) wx.hideLoading()
+            })
+            that.closeShow();
+        },
         /* 文件下载 */
         dowmLoad() {
-            const that = this;
+            const that = this,
+                fileSelected = this.data.fileSelected;
             wx.setClipboardData({
-                data: this.data.fileSelected.url,
+                data: fileSelected.url,
                 success: function () {
                     wx.showToast({
                         title: '复制成功,将链接放置到浏览器中便可下载文件',
@@ -89,6 +119,48 @@ Component({
                 }
             })
         },
+        saveVideo(filePath) {
+            const that = this;
+            wx.saveVideoToPhotosAlbum({
+                filePath,
+                success(res) {
+                    console.log("保存", res)
+                    wx.showModal({
+                        title: '提示',
+                        content: '视频已保存到系统相册',
+                        showCancel: false
+                    })
+                    wx.hideLoading()
+                },
+                fail(err) {
+                    console.log("保存失败", err)
+                    if (err.errno == 103) {
+                        wx.showModal({
+                            title: '提示',
+                            content: '未获取保存相册权限,无法保存!',
+                            confirmText: "前去授权",
+                            success({
+                                confirm
+                            }) {
+                                if (confirm) wx.openSetting({
+                                    success(res) {
+                                        console.log(res.authSetting["scope.writePhotosAlbum"])
+                                        if (res.authSetting["scope.writePhotosAlbum"]) {
+                                            that.saveVideo(filePath)
+                                        } else {
+                                            wx.showToast({
+                                                title: '未获得授权',
+                                                icon: "none"
+                                            })
+                                        }
+                                    }
+                                })
+                            }
+                        })
+                    }
+                }
+            })
+        },
         /* 获得列表 */
         getList(init = false) {
             if (init.detail != undefined) init = init.detail;

+ 5 - 2
pages/tabbar/smartStore/modules/viewPage.wxml

@@ -10,7 +10,7 @@
     <view class="unread">总共{{inTotal}}个</view>
     <My_files list="{{list}}" checkedId='{{fileSelected.attachmentid}}' bindchangeId='changeId' />
     <My_empty wx:if="{{list.length==0}}" />
-    <view style="height: {{padBot?'200rpx;':'50rpx'}}" />
+    <view style="height: {{padBot?'230rpx;':'50rpx'}}" />
 </My_listBox>
 <van-action-sheet show="{{ show }}" bind:click-overlay='closeShow'>
     <view class="sheet-header">
@@ -25,8 +25,11 @@
             <van-icon class="icon star" name="star" />取消收藏
         </block>
     </navigator>
+    <navigator wx:if="{{fileSelected.postfix=='mp4'}}" bindtap="mySaveVideo" url='#' class="option">
+        <text class="iconfont icon-a-tonggaoshujuxiazailiang icon" />保存到相册
+    </navigator>
     <navigator wx:if="{{fileSelected.postfix!='folder'}}" bindtap="dowmLoad" url='#' class="option">
-        <text class="iconfont icon-a-tonggaoshujuxiazailiang icon" />复制下载地址
+        <text class="iconfont icon-a-tonggaoshujuxiazailiang icon" />复制下载地址(请在电脑浏览器中打开)
     </navigator>
     <view wx:if="{{padBot}}" style="height: 115rpx;" />
 </van-action-sheet>