小程序button引导用户授权

栏目: IOS · Android · 发布时间: 6年前

内容简介:wx.getUserInfo(OBJECT) 注意:此接口有调整,使用该接口将不再出现授权弹窗,请使用引导用户主动进行授权操作1.index.wxml

wx.getUserInfo(OBJECT) 注意:此接口有调整,使用该接口将不再出现授权弹窗,请使用

<button open-type="getUserInfo"></button>

引导用户主动进行授权操作

所以我们要使用上述button来请求用户授权

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...

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Designing Web Navigation

Designing Web Navigation

James Kalbach / O'Reilly Media / 2007-8-15 / USD 49.99

Thoroughly rewritten for today's web environment, this bestselling book offers a fresh look at a fundamental topic of web site development: navigation design. Amid all the changes to the Web in the pa......一起来看看 《Designing Web Navigation》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试