美文网首页
微信小程序个人信息授权、微信运动授权、相册授权

微信小程序个人信息授权、微信运动授权、相册授权

作者: 海豚先生的博客 | 来源:发表于2020-04-23 21:09 被阅读0次
  // 获取相册授权
  getAlbumAuth () {
    let that = this
    wx.getSetting({
      success(res) {
        console.log('获取权限列表',res)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          // 弹窗询问授权
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              console.log('用户同意使用相册')
              that.savaImageToPhoto();
            },
            fail () {
              console.log('用户拒绝了相册授权,显示弹窗再次申请授权')
              that.setData({
                isshowAuthAlbum: true
              })

            }
          })
        } else {
          console.log('已经获取相册权限了')
          that.savaImageToPhoto();
        }
      }
    })
  },
  // 保存图片至相册
  savaImageToPhoto: function(){
    let that = this;
    wx.showLoading({
      title: '努力生成中...'
    })
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: that.data.winWidth,
      height: that.data.winHeight,
      destWidth: that.data.winWidth*4,
      // destWidth: 375 * 750 / wx.getSystemInfoSync().windowWidth,
      // destHeight: 654 * 750 / wx.getSystemInfoSync().windowWidth,
      destHeight: (that.data.winHeight)*4,
      canvasId: 'canvasImg',
      success: function (res) {
        wx.hideLoading()
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            utils.showToast('保存成功')
          },
          fail () {
            console.log('调用相册失败了')
          }
        })
      },
      fail: function (res) {
        console.log(res)
        console.log('保存到相册出错了---------')
      }
    })
  },
  // 微信运动授权
  wxRunGiveAuth () {
    let _this = this
    // 打开微信运动授权弹窗
    wx.getWeRunData({
      success: function(res) {
        console.log(res)
        console.log('获取微信运动成功')
        _this.setData({
          authWerunFlag: true
        })
        _this.uploadWxSteps(res)
      },
      fail: function() {
        console.log('获取微信运动失败,显示弹窗再次申请授权')
        // 授权失败弹窗提示
        _this.setData({
          isShowWerunPop: true
        })
      }
    })
  },
    // 获取用户授权信息,一直点击一直有弹窗
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log('用户已经授权个人信息啦')
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        } else {
          // 用户未授权个人信息,弹窗进行授权
          console.log('用户没有授权个人信息')
          this.globalData.userInfo = null
        }
      }
    })

相关文章

网友评论

      本文标题:微信小程序个人信息授权、微信运动授权、相册授权

      本文链接:https://www.haomeiwen.com/subject/trmkbctx.html