小程序批量下载压缩包(zip文件)

# wxml

>

<view class="search_btn" catchtap="batchDownload">批量下载</view>
 
```js  
//  批量下载
batchDownload(e){
    const {multipleCheckId,orderList}=this.data;
    const ids=multipleCheckId.map(item=>item.id)
    const Token = wx.getStorageSync("token");
    const first_token = wx.getStorageSync("first_token");
    const second_token = wx.getStorageSync("second_token");
    const that=this;
    if(!ids.length||ids.length>20){
    wx.showToast({
        title: ids.length>20?"选择的环境样本不能超过20个":"请选择可下载报告的环境样本",
        icon: "none",
        duration: 2000,
      });
      return;
    }
     wx.showLoading({
      title: "下载中",
    });
  
  if(ids.length===1){      // 单个下载   pdf文件
  wx.request({
    url: API_BASE_URL + `environment/samples/report`,
    method: "POST",
    header: {
      "Content-Type": "application/json",
      token: `${Token}`,
      "first-token": first_token,
      "second-token":second_token,
    },
    data: {ids:ids},
    responseType: "arraybuffer",
    success(res) {
      wx.hideLoading();
      var fileManager = wx.getFileSystemManager();
      var FilePath =
      wx.env.USER_DATA_PATH + "/" +"环境"+
      "-" +
      Math.floor(Math.random() * 1000) +
      "批量下载报告.pdf";
      fileManager.writeFile({
        data: res.data,
        filePath: FilePath,
        encoding: "binary",
        success(res) {
          // 编码格式
          wx.openDocument({
            //成功之后直接打开
            filePath: FilePath,
            showMenu: true,
            fileType: "pdf",
            success: (result) => {
              // 打开文档成功
              wx.hideLoading({
                success: (res) => {},
              });
              const newList = orderList.map(item=>{
                const newItem = {...item};
                if(newItem.checked==true){
                  newItem.checked=false;
                }
                return newItem
              })
              that.setData({orderList:newList,multipleCheckId:[]})
            },
            fail: (err) => {
              //   打开文档失败
              wx.hideLoading({
                success: (res) => {},
              });
            },
          });
        },
        fail: (res) => {
          wx.hideLoading({
            success: (res) => {},
          });
          wx.showToast({
            title: "下载失败!",
            icon: "none",
            duration: 2000, //停留时间
          });
        },
      });
    },
  });
}else{       // 批量下载压缩包文件
  const fileType=that.data.fileType
  const now = new Date();
    let year = now.getFullYear();
    let month =
    (now.getMonth() + 1) < 10 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1;
    let day = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let hour = now.getHours()< 10 ? "0" + now.getHours() : now.getHours(); // 时
    let minutes = now.getMinutes()< 10 ? "0" + now.getMinutes() : now.getMinutes(); // 分
    let seconds = now.getSeconds()< 10 ? "0" + now.getSeconds() : now.getSeconds() //秒
    let today =`${year}${month}${day}${hour}${minutes}${seconds}`;
      wx.request({
        url: API_BASE_URL + `environment/samples/report`,
        method: "POST",
        header: {
          "Content-Type": "application/json",
          token: `${Token}`,
          "first-token": first_token,
          "second-token":second_token,
        },
        data: {ids:ids},
        responseType: "arraybuffer",
        success(res) {
          var fileManager = wx.getFileSystemManager();
          var FilePath =
          wx.env.USER_DATA_PATH + "/" +"环境"+
          "-" +"批量下载报告"+"-"+today+
          ".zip";
          fileManager.writeFile({
            data: res.data,
            filePath: FilePath,
            encoding: "binary",
            success(res) {
              wx.hideLoading();
              that.setData({
                FilePathUrl:FilePath
              })
              // that.uuzip(FilePath, fileType) // 解压
              // 编码格式
              wx.openDocument({
                //成功之后直接打开
                filePath: FilePath,
                showMenu: true,
                fileType: "zip",
                success: (result) => {
                  const newList = orderList.map(item=>{
                    const newItem = {...item};
                    if(newItem.checked==true){
                      newItem.checked=false;
                    }
                    return newItem
                  })
                  that.setData({orderList:newList,multipleCheckId:[]})
                  // 打开文档成功
                  wx.hideLoading({
                    success: (res) => {},
                  });
                },
                fail: (err) => {
                  //   打开文档失败
                  wx.hideLoading({
                    success: (res) => {},
                  });
                },
              });
            },
            fail: (res) => {
              wx.hideLoading({
                success: (res) => {},
              });
              wx.showToast({
                title: "导出失败!",
                icon: "none",
                duration: 2000, //停留时间
              });
            },
          });
        },
      });
    }
},

posted on 2023-01-31 09:36  imi小码娜  阅读(240)  评论(0编辑  收藏  举报

导航