小程序授权

2019-12-24  本文已影响0人  关耳木南

小程序授权涉及3个接口
-wx.getSettings获取当前用户的授权状态
-wx.openSettings打开设置界面
-wx.authorize发起授权请求
wx.authorize发起的授权请求都可以通过wx.openSettings打开设置界面看到请求授权了哪些。


微信图片_20191224174136.png 微信图片_20191224174143.png 微信图片_20191224174147.png
<!--测试1.wxml-->
<view wx:if="{{isShow}}">
  <button hidden="{{!flag}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">点击授权</button>
  <button hidden="{{flag}}" open-type="openSetting">打开授权页面</button>
</view>
<view wx:else>ad</view>
// pages/login2/login2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    flag: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.getSetting({
      success(res) {
        console.log(res.authSetting)
/*
  打印结果{scope.record: false, scope.address: false, scope.userLocation: false, scope.userInfo: true}
true为授权成功
*/
      }
    })      
  },
  getUserInfo: function(e) {
    var that = this;
    console.log(e.detail.userInfo);
    wx.getSetting({
      success(res) {
        console.log(res.authSetting['scope.userInfo']);
        if (!res.authSetting['scope.userInfo']) {//是否授权userInfo接口
          console.log(11)
          wx.authorize({//提前向用户发起授权请求。调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能
            scope: 'scope.userInfo',
            success(res) {//点击授权弹窗中的允许按钮
              // wx.startRecord()
              console.log(res);
              console.log("点击授权")
            },
            fail(err){//点击授权弹窗中的拒绝按钮
              console.log("点击拒绝")
              that.setData({
                flag: false
              })
            }
          })
        }else{
          that.setData({
            isShow: false
          })
        }
      }
    })
  }
})
上一篇 下一篇

猜你喜欢

热点阅读