WeChat-Applet-Export in Zip, Excel, Word, PNG auf das Mobiltelefon

1、需要使用的方法:
	wx.request(); //发请求调用接口
	wx.showModal();//打开弹框进行展示
	wx.setClipboardData();//复制文本的数据
	wx.downloadFile();//文件下载的方法
	wx.getFileSystemManager().saveFile();//保存数据
	wx.saveImageToPhotosAlbum();//保存数据到相册
2、在微信小程序中,用户使用的手机分苹果手机和安卓手机;安卓手机保存文件我们是可以找到在手机磁盘中找到文件的,苹果手机是找不到指定文件的;那么苹果手机则是返回文件的路径提供可复制功能在浏览器进行打开下载;
3、另外目前在保存zip、word、excel目前采用的是直接已图片的形式保存在手机相册中,然后通过修改文件的后缀来实现的(图片资源可以直接导出到手机相册);
4、多的话就不说了,直接上在实际开发中的源码:
  // 导出zip数据
  exportProject(){
    
    
    var strCode = ''
    var arr = JSON.parse(JSON.stringify(this.data.dataList))
    arr.length>0 && arr.forEach((item,index) =>{
    
    
      if(item.checked) {
    
    
        strCode += `${
      
      item.id},`
      }
    })
    // 有勾选导出勾选 没有勾选导出所有的数据
    if(strCode) {
    
    
      strCode = strCode.substr(0,strCode.length-1)
    } else {
    
    
      wx.showToast({
    
    
        title: '请先勾选在进行导出!',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
    
    
      title: '保存中...'
    })
    wx.request({
    
    
      url: `http://localhost:6666/upload/upload`, //下载文件的接口
      method: 'GET',
      header: {
    
    
        'Content-Type': 'application/json', // 默认值
        'accessToken': accessToken
      },
      success(resUrl){
    
    
      	//首先判断手机的类型是ios还是Android 判断的方法微信小程序有api进行调用
        if(wx.getStorageSync('flagIosOrAndroid')  == 'ios') {
    
     //苹果手机直接弹出地址进行复制操作
          wx.hideLoading()
          wx.showModal({
    
    
            title: '下载地址',
            content: resUrl,
            showCancel: false,
            confirmText: '复制' ,
            success (res) {
    
    
              if (res.confirm) {
    
    
                wx.setClipboardData({
    
    //复制文本
                  data: resUrl.data,
                  success: function (res) {
    
    
                    wx.showToast({
    
    
                      title: '复制成功',
                      icon:"none",
                      mask:"true"//是否设置点击蒙版,防止点击穿透
                    })
                  }
               })
              }
            }
          })
        } else {
    
     //安卓手机将文件保存为png 导出之后改后缀
          wx.downloadFile({
    
    
            url: `${
      
      resUrl}`, //仅为示例,并非真实的资源
            success (resCode) {
    
    
              wx.hideLoading()
              var savePath = wx.env.USER_DATA_PATH + `/Zip.png`;
              wx.getFileSystemManager().saveFile({
    
    
                tempFilePath: resCode.tempFilePath||resCode.filePath,
                filePath:savePath,
                success: function (res){
    
    
                  //保存图片
                  wx.saveImageToPhotosAlbum({
    
    
                    filePath:res.savedFilePath,
                    success(res) {
    
    
                      //这里可以出个弹窗,给用户提醒,让他去文件管理器里面找到后重命名
                      wx.showModal({
    
    
                        title: '文件保存成功',
                        content: `请在文件管理器找到Zip.png修改后缀为zip`,
                        showCancel: false,
                        confirmText: '确定' ,
                        success (res) {
    
    
                        }
                      })
                    }
                  })
                },
              })
            },
            fail(err){
    
    
              wx.hideLoading()
              wx.showToast({
    
    
                title: '文件下载失败!',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      },
      fail(err) {
    
    
        wx.hideLoading()
        wx.showToast({
    
    
          title: '文件获取失败!',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
总结:目前采用的是这种方法来进行导出到手机的 ,示例为导出zip的例子;导出excel和word方法都类似,导出图片的也是类似;有其他的好的方法也可以给我推荐推荐,我们一起学习,互相交流。觉得有用记得点赞加关注哦,方便下次用的时候不迷路。

Guess you like

Origin blog.csdn.net/qqjuanqq/article/details/128019036