小程序button引導用戶受權

wx.getUserInfo(OBJECT) 注意:此接口有調整,使用該接口將再也不出現受權彈窗,請使用
<button open-type="getUserInfo"></button>
引導用戶主動進行受權操做
當用戶未受權過,調用該接口將直接報錯 當用戶受權過,能夠使用該接口獲取用戶信息

因此咱們要使用上述button來請求用戶受權html


1.index.wxml小程序

<button 
    wx:if="{{canIUse}}" 
    open-type="getUserInfo" 
    bindgetuserinfo="bindGetUserInfo"
>受權登陸</button>
<view wx:else>請升級微信版本</view>

2.index.js緩存

Page({
  data: {
    //判斷小程序的API,回調,參數,組件等是否在當前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function () {
    // 查看是否受權
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
              //用戶已經受權過
            }
          })
        }
      }
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      //用戶按了容許受權按鈕
    } else {
      //用戶按了拒絕按鈕
    }
  }
})

注:若是未出現微信受權的彈窗,則多是由於以前受權的緩存致使的,由於只有未受權纔會出現彈窗,清除緩存便可
參考:https://blog.csdn.net/weixin_...
https://blog.csdn.net/weidong...
https://www.cnblogs.com/legen...微信

相關文章
相關標籤/搜索