不想从新写页面引导用户受权,由于官方说明小程序
scope 为 "scope.userInfo" 时,没法弹出受权窗口,请使用<button open-type="getUserInfo"></button>
理一下思路:
这一步目的是为了获取用户的openid微信
//1.登录获取用户openid wx.login({ success: function(resdata) { if (resdata.code) { wx.request({ url: appConfig.config.getOpenId, data: { //somedata }, success: function(res) { //do something } }) } else { that.openAlert(); } }, fail: function(res) { that.openAlert(); } });
//2. 查看是否受权 wx.getSetting({ success: function(res) { if (res.authSetting['scope.userInfo']) { //受权了 } else { //未受权 } } })
//3. 已经受权,获取用户信息 wx.getUserInfo({ success: function(res) { that.globalData.userInfo = res.userInfo console.log(res.userInfo) } })
//4.未受权,引导用户受权 wx.showModal({ title: '用户未受权', content: '如需正常使用小程序功能,请进行用户受权。', showCancel: true, success: function(res) { if (res.confirm) { console.log("用户确认受权") } else { console.log("用户取消受权") } } })
//5.受权成功 if (wx.openSetting) { wx.openSetting({ success: function(res) { //从新登录 } }) }
//6.因外部缘由受权失败 else { wx.showModal({ title: '受权提示', content: '小程序须要您的微信受权才能使用哦~ 错过受权页面的处理方法:删除小程序->从新搜索进入->点击受权按钮' }) }