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

不言
Release: 2019-02-16 14:21:45
forward
3473 people have browsed it

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>
Copy after login
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>
Copy after login

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

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!