前端代码:
onShow: function () { // 登录 var token = wx.getStorageSync('token') var code if (token) { wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (app.userInfoReadyCallback) { app.userInfoReadyCallback(res) } }, fail: () => { //跳转授权失败页面 wx.reLaunch({ url: loginFailedUrl, }) } }) } else { wx.login({ success: res => { code = res.code wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (app.userInfoReadyCallback) { app.userInfoReadyCallback(res) } // 发送 res.code 到后台换取 openId, sessionKey, unionId util.get("login", { code: code, encryptedData: res.encryptedData, iv: res.iv, }, this.doLoginSucc) }, fail: () => { wx.showModal({ title: '授权失败', content: '登录需允许授权,是否现在进行授权', success: res => { if (res.confirm) { wx.openSetting({ success: res => { if (res.authSetting["scope.userInfo"] == true) { wx.getUserInfo({ withCredentials: false, success: res => { app.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (app.userInfoReadyCallback) { app.userInfoReadyCallback(res) } // 发送 res.code 到后台换取 openId, sessionKey, unionId util.get("login", { code: code, encryptedData: res.encryptedData, iv: res.iv, }, this.doLoginSucc) }, fail: () => { //跳转授权失败页面 wx.reLaunch({ url: loginFailedUrl, }) } }) } } }) } else if (res.cancel) { //跳转授权失败页面 wx.reLaunch({ url: loginFailedUrl, }) } } }) } }) } }) } }, onShow: function () { // 登录 var token = wx.g