小程序下载图片(文件)

发布时间 2023-10-20 11:34:21作者: shuihanxiao
 this.getPicSave()

  getPicSave(){
    let _this=this
    wx.getSetting({
      scope: 'scope.writePhotosAlbum',
      success(res) {
          if (res.authSetting['scope.writePhotosAlbum']) {
            _this.photoAuthorization();
          } else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
              wx.authorize({
                  scope: 'scope.writePhotosAlbum',
                  success() {
                    _this.photoAuthorization();
                  },
                  fail(){
                      wx.showToast({
                          title: '您没有授权,无法保存到相册',
                          icon: 'none'
                      })
                  }
              })
          }else {
            wx.showModal({
              title: '是否授权保存到相册',
              content: '请确认授权,否则无法保存到相册',
              success: res => {
                if (res.confirm) {
                  wx.openSetting({     
                    success(res) {
                        if (res.authSetting['scope.writePhotosAlbum']) {
                          _this.photoAuthorization();
                        }else{
                            wx.showToast({
                                title:'您没有授权,无法保存到相册',
                                icon:'none',
                                duration: 2000, //toast 显示时长,单位为ms,默认值1500
                                mask: true //是否显示透明蒙层,防止触摸穿透,默认为false
                            })
                            _this.setData({ isSaving: false });                                
                        }
                    }
                })
                }
              }
            })
             
          }
      }
  })
  },

  async photoAuthorization() {
    let { sellerId, schoolId } = this.data.SellerInfo
    let requstUrl = GET_SELLERQRCODE //替换自己项目的接口和入参
    let data = {
      schoolId,
      sellerId,
    }
    try {
      let result = await Util.request(requstUrl, data, true)
      const code = result.code
      if (code == 200) {
        wx.downloadFile({
          url: result.data.qrCodeUrl, //返回文件url(图片url)
          success: (res) => {
            wx.showShareImageMenu({
              path: res.tempFilePath,
              success: (res) => {
                console.log(402, res);
                if (res.errMsg == 'showShareImageMenu:ok') {
                  wx.showToast({
                    title: '操作成功',
                    icon: 'success',
                    duration: 1300,
                    mask: true
                  })
                  return
                }
              },
              fail: (res) => {
              }
            })
          }
        })
        return
      } else {
        wx.showToast({
          title: '请重新登录',
          icon: 'none',
          duration: 1300,
          mask: true
        })
        return
      }
    } catch (err) {
      console.log(err);
    }



  },