小程式button引導使用者授權
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...