Home  >  Article  >  WeChat Applet  >  Introduction to the method of mini program button to guide user authorization (code example)

Introduction to the method of mini program button to guide user authorization (code example)

不言
不言forward
2019-02-16 14:21:223488browse

This article brings you an introduction to the method of mini program button to guide user authorization (code example). It has certain reference value. Friends in need can refer to it. I hope it will help You helped.

wx.getUserInfo(OBJECT) Note: This interface has been adjusted. When using this interface, the authorization pop-up window will no longer appear. Please use

<button open-type="getUserInfo"></button>
to guide the user to actively perform authorization operations.
When the user If it is not authorized, calling this interface will directly report an error. When the user is authorized, you can use this interface to obtain user information

So we need to use the above button to request user authorization

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 {
      //用户按了拒绝按钮
    }
  }
})

Note: If the WeChat authorization pop-up window does not appear, it may be caused by the cache of previous authorization, because The pop-up window will only appear if you are not authorized, just clear the cache

Reference for this article: https://www.html.cn/study/20.html

The above is the detailed content of Introduction to the method of mini program button to guide user authorization (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete