微信小程序服务器文件下载及解压

对方法做个备份

方法一 下载及解压代码

第一个参数为下载地址,将下载地址以数组的方式传入,可以下载多个文件。

第二个参数为想要将下载并解压好的文件保存到的本地路径。

第三个参数为是不是微信云端路径,是的话为true;

const wxP = require('../utils/util.js');


Download_zip: function (load_path = [], target_path = '', if_cloud = false) {
      const fs = wx.getFileSystemManager();
      let length = load_path.length;
      const unzip_API = function (res_path, target_path) {
        wxP.saveFile({
          tempFilePath: res_path,
        }).then(res => {
          let FilePath = res.savedFilePath
          fs.unzip({
            zipFilePath: FilePath,
            targetPath: target_path,
            success(res) {
              console.log('fs.unzipe sucess')
              if(length == 1) {
                toast.sucess();
              }
              else {
                length = length - 1;
                wx.showToast({
                  title: '资源正在下载',
                  icon: 'loading',
                  mask: true,
                  duration: 60000
                })
              }

          },
            fail(err) {
            }
        })
      }).catch(err => {
        console.log('解压函数 unzip_API 失败',err);
      })
    }
      let load_API = function (path) {
        return new Promise((resolve, error) => {
          if(if_cloud){
            toast.loading();
            wxP.clouddownloadFile({
              fileID: path,
            }).then(res => {
              resolve(res);
            }).catch(err => {
              toast.hide().then(() => {toast.fail();})
                error('Loadfn cloud 失败',err)
            })}
          else{
            toast.loading();
            wxP.downloadFile({
              url: path,
            }).then(res => {
              resolve(res);
            }).catch(err => {
              toast.hide().then(() => {toast.fail();})
                error('Loadfn 失败',err)
            })}
        })
      }
      const toast = {
        loading: function () {
          wx.showToast({
            title: '正在下载中...',
            icon: 'loading',
            mask: true,
            duration: 1000,
          })
        },
        hide: function () {
          return new Promise ((resolve,error) => {
            wx.hideToast({
              success: (res) => {resolve(res)},
              fail: (err) => {error(err)}
            })
          })
        },
        sucess: function () {
          wx.showToast({
            title: '下载完成',
            icon: 'success',
            duration: 2000
          })
        },
        fail: function () {
          wx.showToast({
            title: '请检查您的网络',
            icon: 'error',
            duration: 2000
          })
        }
      }
      const createfiles = function (target_path) {
        fs.access({
          path: target_path,
          success(res) {

          },
          fail(err) {
            fs.mkdir({
              dirPath: target_path,
              recursive: true,//很关键
              success(res) {
              },
              fail(err) {
              }
            })
          }
        })
      }
      const clearfile = function () {
        wx.getSavedFileList({  // 获取文件列表
        success (res) {
          res.fileList.forEach((val, key) => { // 遍历文件列表里的数据
            // 删除存储的垃圾数据
            wx.removeSavedFile({
                filePath: val.filePath
            });
          })
        }
       })
      }
      let load_number = -1;
      const Loading = function () {
        return new Promise((resolve, error) => {
          load_number++;
          if(load_path[load_number]) {
            resolve();
          }
          else {
            error();
          }
        }).then(() => {
            return load_API(load_path[load_number]);
        }).then((res) => {
          unzip_API(res.tempFilePath, target_path)
          return Loading();
        }).catch((err) => {
          wx.hideToast();
          fs.readdir({
            dirPath: target_path,
            success(res) {
              // if(if_cloud)
              //   App.globalData.cloud_ifload = true;
              // else
              //   App.globalData.Gif_ifload = true;
            },
            fail(err) {
            }
        })
        })
        

      }
      const if_haddown = function () {
        return new Promise((resolve, error) => {
           fs.readdir({
            dirPath: target_path,
            recursive: true,
            success: (res) => {if(res.files.length > 0) resolve(true); else error(false);},
            fail: (err) => {error(false);}
        })
        })
        
      }
      
      
      clearfile();
      createfiles(target_path);
      if_haddown().then((res) => {console.log(target_path + '存在')}).catch((err) => {Loading()});
    }

wxP是指微信API promise化

function wxcloudPromise(functionName, params) {
  return new Promise(function(resolve, reject) {
      wx.cloud[functionName]({
          ...params,
          success: resolve,
          fail: reject
      })
  })
}

function wxPromise(functionName, params) {
  return new Promise(function(resolve, reject) {
      wx[functionName]({
          ...params,
          success: resolve,
          fail: reject
      })
  })
}

function clouddownloadFile(params = {}) {
  return wxcloudPromise('downloadFile', params);
}

function downloadFile(params = {}) {
  return wxPromise('downloadFile', params);
}

function saveFile(params = {}) {
  return wxPromise('saveFile', params);
}

function getFileSystemManager(params = {}) {
  return wxPromise('getFileSystemManager', params);
}

function getStorage(params = {}) {
  return wxPromise('getStorage', params);
}

// function (params = {}) {
//   return wxPromise('', params);
// }


module.exports = {
  clouddownloadFile,
  downloadFile,
  saveFile,
  getFileSystemManager,
  getStorage,
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红叶落水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值