0 Preface
Small micro-channel program in order to optimize the user experience, immediately canceled the authorization window appears when entering the applet. Initiative requires the user to click the button to trigger the authorization window.
Well, in my practice, there have been the following problems.
1 . Not eject the authorization window 2 . Hope in case the user has been authorized, non-display button
1. realization
app.js the OnLaunch () function, add snippet obtain personal information. Implemented in case the user has authorization (the second time to open the applet, for example), the user automatically obtains personal information without the user's authorization.
// Get user information wx.getSetting ({ Success: RES => { IF (res.authSetting [ ' scope.userInfo ' ]) { the console.log ( " App: " + " the user has authorization " ) // has been authorized, getUserInfo head can directly call the nickname acquired, can not play the block wx.getUserInfo ({ Success: res => { // may be sent back to the decoding res unionid the this .globalData.userInfo = res.userInfo the console.log ( the this.globalData.userInfo) the this .globalData.hasUserInfo = to true // Since getUserInfo network request, may not return until Page.onLoad // so here is added to prevent this callback IF ( the this .userInfoReadyCallback) { the this . userInfoReadyCallback (RES) } }, fail: (RES) => { the console.log ( " App: " + " get user information failed " ) } }) } the else{ The console.log ( " App: " + " users unauthorized temporarily " ) } } })
me.wxml add the Authorize button (according to the specific page you actual situation).
<block wx:if="{{!hasUserInfo}}"> <image class="userAvatar" src='../../images/icon/wechat.png'></image> <button open-type="getUserInfo" bindgetuserinfo="getUserInfo">微信授权登录</button> </block>
The following variables and methods me.js added, before the user without authorization, the initiative requires the user to click the button.
data: { userInfo: null, hasUserInfo: false }, getUserInfo: function(e) { console.log("me: " + "用户点击授权") if(e.detail.userInfo){ this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) } }
2. Authorization not pop window
Here we must note
Authorization window will only appear when the user first authorization , that is, only once! !
In the micro-channel applet development tool where we need to clear all caches
3. The case has been authorized, non-display button
Since the user has authorized the time, app.js will obtain users' personal information (but not in), but the process is asynchronous