微信小程序引导用户授权获取用户信息
分类:小程序技术文档
时间:2021-03-20
浏览:381
微信官方公告:[https://developers.weixin.qq.com/community/develop/doc/0000a26e1aca6012e896a517556c01]
现在微信小程序只能通过点击按钮的方式显示获取用户信息的弹框,根据官方文档中的方法,我们可以在按钮上设置open-type,从而获取用户信息
但是用户存在拒绝授权的情况,这个时候就只能引导用户打开设置页,如果用户没有打开授权开关,则继续引导用户打开设置页
上代码
wxml
<view class='login'> <button open-type="getUserInfo" bindgetuserinfo="getUserInfo">微信授权登录</button> </view>
js
const app = getApp() Page({ /** * 页面的初始数据 */ data: { }, onLoad() { }, getUserInfo: function(e) { console.log(e) if (e.detail.userInfo) { // 用户同意授权 // 将获取到的用户信息传给后端 } else { // 用户拒绝授权 this.showModal() } }, showModal: function() { // 消息提示弹框,引导用户开启授权 let _this = this wx.showModal({ title: '小程序申请获得以下权限', content: '获得你的公开信息(昵称、头像、地区及性别)', success (res) { if (res.confirm) { wx.openSetting({ // 打开设置页 success (res) { console.log(res.authSetting) if (!res.authSetting['scope.userInfo']) { // 用户授权失败 _this.showModal() } } }) } else if (res.cancel) { _this.showModal() } } }) } })
本站文章如未注明出处则为原创,转载请注明出处,如有侵权请邮件联系站长