关于微信小程序 保存图片、查看权限状态、查看是否加入我的小程序等API

wx.checkIsAddedToMyMiniProgram({ 
          success: (res) => {
            console.log('res',res) 
            if(!res.added){ 
              currentPage.setData({
                showNotice:true
              })
              setTimeout(()=>{
                currentPage.setData({
                  showNotice:false
                })
              },7000)
            }
          },
          fail:(error)=>{
            console.log('error',error)
          }

wx.checkIsAddedToMyMiniProgram 查看该小程序是否已经加入我的小程序、success表示查看成功,res.added的布尔值表示是否加入我的小程序

微信小程序的APP与页面的生成速度也不同,在具体使用的时候可以通过打印控制台了解先后生成的顺序。必要的话可以使用settimeout来进行延时

getcurrentpages() 获取当前页面栈,数组中第一个元素为首页,最后一个元素为当前页面

wx.downloadFile  只支持保存本地路径

 wx.downloadFile({
      url: this.data.codeImg, //仅为示例,并非真实的资源
      success: (res) => {
        // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          wx.createInnerAudioContext({
            filePath: res.tempFilePath
          })
          this.setData({
            shareImg: res.tempFilePath
          })
          console.log('res', res.tempFilePath)
          //判断用户确认权限结果
          wx.getSetting({
            success: (res) => {
              console.log(res.authSetting)
              if (res.authSetting['scope.writePhotosAlbum'] === true) {
                console.log('用户请求权限成功', 'res', res.tempFilePath, this.data.shareImg)
                this.getimage()
              } else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
                wx.authorize({
                  scope: 'scope.writePhotosAlbum',
                  success: (res) => {
                    wx.saveImageToPhotosAlbum({
                      filePath: this.data.shareImg,
                      success: function (t) {
                        wx.showToast({
                          title: "保存成功",
                          icon: "none"
                        });
                      },
                      fail: function (t) {
                        wx.showToast({
                          title: "保存失败,请重试",
                          icon: "none"
                        });
                        console.log('filePath', t)
                      }
                    })
                  },
                  fail() {
                    wx.showToast({
                      title: '您没有授权,无法保存到相册',
                      icon: 'none'
                    })
                  }
                })
              } else {
                wx.openSetting({
                  success:(res)=>{
                    if (res.authSetting['scope.writePhotosAlbum']) {
                      this.getimage()
                    } else {
                      wx.showToast({
                        title: '您没有授权,无法保存到相册',
                        icon: 'none'
                      })  
                    }
                  }
                })
              }
            }
          })
           
        }
      }
    }) 

可以先用wx.createInnerAudioContext  将线上图片地址转换为临时路径

wx.authorize 在调用保存图片等权限时,只能调起弹窗一次,也就是说如果用户拒绝了,那就默认他无法再进行保存图片等操作、因此我们在点击事件时需要判断用户的权限状态。

使用 wx.getSetting 判断用户是否给权限了

1.给权限了(调起权限弹窗), 同意了,直接进行后续操作

2.给权限了(调起权限弹窗),没同意,但是需要再次打开权限,使用wx.openSetting

3.没给权限(没调起权限弹窗) res.authSetting['scope.writePhotosAlbum'] === undefined 需要调起权限 wx.authorize

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值