// pages/auth/auth.js const app = getApp(); var Api = require('../../utils/api.js'); Page({ /** * 页面的初始数据 */ data: { canIUse: wx.canIUse('button.open-type.getUserInfo') }, onAuth() { wx.getSetting({ success: (res) => { if (res.authSetting['scope.userInfo']) { // app.globalData.userInfo = res.userInfo wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo } }) } } }) //登录 wx.login({ success: res => { // console.log(res) // 发送 res.code 到后台换取 openId, sessionKey, unionId if (res.code) { //发起网络请求 wx.request({ url: Api.postgetOpenId(), data: { codeId: res.code }, success (res) { // console.log(res) if(res.statusCode==200){ // app.globalData.openid=res.data.data.openid wx.setStorageSync('openid', res.data.data.openid) var pages = getCurrentPages(); // 当前页面 var beforePage = pages[pages.length - 2] wx.navigateBack({ success: function() { beforePage.onLoad(); // 执行前一个页面的onLoad方法 } }) }else{ } } }) } else { console.log('登录失败!' + res.errMsg) } } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { wx.hideHomeButton({ success: function() { }, }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })