Browse Source

1.文件夹下载进入全部文件列表然后全部下载并保存相册

FailedToRead 2 years ago
parent
commit
b4e13c215b

+ 61 - 13
miniprogram/component/documentItem/documentItem.ts

@@ -1,4 +1,5 @@
 // component/documentItem/documentItem.ts
+import { httpUtil } from "../../utils/restful"
 Component({
     /**
      * 组件的属性列表
@@ -17,7 +18,8 @@ Component({
     data: {
         type: 1,
         hasDownLoad: false,
-        downLoadProgress: ''
+        downLoadProgress: '',
+        progressType: 0,
     },
     lifetimes: {
         attached: function () {
@@ -42,34 +44,80 @@ Component({
         showActionWindow: function () {
             this.triggerEvent("showActionWindow", { item: this.properties.itemData, itemIndex: this.properties.itemIndex })
         },
-        setDownLoadProgress: function (data: any) {
-            console.log("设置下载百分比:", data.detail.progress)
-            this.setData({
-                downLoadProgress: '下载:' + data.detail.progress + '%',
-                hasDownLoad: true
+
+        downLoadItem: function (progressType, fileType) {
+            console.log("fileType:", fileType)
+            let that = this;
+            that.setData({
+                progressType: progressType,
+                type: fileType
             })
+            //文件下载
+            httpUtil.wxDownLoadFile(that.data.itemData.url, function (res: any) {
+                // console.log("下载没===", res)
+                that.setDownLoadProgress(res)
+            }).then((res) => {
+                // console.log("下载成功:", res)
+                that.downLoadComplete(res)
+
+
+            }).catch((res) => {
+                // console.log("下载失败")
+
+            })
+
+        },
+
+        setDownLoadProgress: function (data: any) {
+
+            if (this.data.progressType == 0) {
+                console.log("设置下载百分比:", data.detail.progress)
+                this.setData({
+                    downLoadProgress: '下载:' + data.detail.progress + '%',
+                    hasDownLoad: true
+                })
+            } else {
+                this.setData({
+                    downLoadProgress: '下载:' + data.progress + '%',
+                    hasDownLoad: true
+                })
+            }
         },
         downLoadComplete: function (data: any) {
-            // console.log("下载成功:", data)
+            console.log("下载成功:", data)
             // console.log("data.detail.data.tempFilePath:", data.detail.data.tempFilePath)
+            console.log("downLoadComplete:", this.data.type)
+            let path = ''
+            if (this.data.progressType == 0) {
+                path = data.detail.data.tempFilePath
+            } else if (this.data.progressType == 1) {
+                path = data.tempFilePath
+            }
+
             if (this.data.type == 0) {
                 //0是图片,图片保存到相册
                 wx.saveImageToPhotosAlbum({
-                    filePath: data.detail.data.tempFilePath,
+                    filePath: path,
                     success(res) {
-                        wx.showModal({
-                            content: '保存相册成功'
+                        wx.showToast({
+                            title: '保存相册成功',
+                            icon: 'succes',
+                            duration: 1000,
+                            mask: true
                         })
                     }
                 })
             } else if (this.data.type == 1) {
                 //1是视频,视频保存到相册
                 wx.saveVideoToPhotosAlbum({
-                    filePath: data.detail.data.tempFilePath,
+                    filePath: path,
                     success(res) {
                         // console.log(res.errMsg)
-                        wx.showModal({
-                            content: '保存相册成功'
+                        wx.showToast({
+                            title: '保存相册成功',
+                            icon: 'succes',
+                            duration: 1000,
+                            mask: true
                         })
                     }
                 })

+ 3 - 0
miniprogram/component/fileBottomPop/fileBottomPop.ts

@@ -136,6 +136,9 @@ Component({
             let that = this;
             if (that.data.windowType == 0) {
                 //文件夹下载
+                wx.navigateTo({
+                    url: '../../../pages/document/document?lessId=' + that.data.itemData.id + '&downLoadAll=true'
+                })
             } else if (that.data.windowType == 1) {
                 //文件下载
                 httpUtil.wxDownLoadFile(that.data.itemData.url, function (res: any) {

+ 17 - 3
miniprogram/pages/document/document.ts

@@ -8,17 +8,25 @@ Page({
     data: {
         lessId: '',
         itemList: null,
-        showItemIndex: 0
+        showItemIndex: 0,
+        downloadAll: false
     },
 
     /**
      * 生命周期函数--监听页面加载
      */
     onLoad(options) {
+        let bool = false;
+        if (options.downLoadAll) {
+            bool = true
+        }
         this.setData({
-            lessId: options.lessId
+            lessId: options.lessId,
+            downloadAll: bool
         })
         this.getDocumentAll();
+
+
         console.log("this.data.itemList:", this.data.itemList)
     },
 
@@ -49,7 +57,13 @@ Page({
             this.setData({
                 itemList: res.data.data.list
             })
-
+            if (this.data.downloadAll) {
+                //下载全部
+                console.log("this.data.downloadAll:", this.data.downloadAll)
+                this.data.itemList.forEach((item, index) => {
+                    this.selectComponent('#documentItem' + index).downLoadItem(1, item.type);
+                });
+            }
         }).catch((res) => {
             console.log("获取所有文件失败:", res)
         })