收藏
回答

代码如下,getApp().globalData.userInfo 接收不到回传数据,请大神们解析?

// app.js

App({

  onLaunch() {

    // 展示本地存储能力

    var logs = wx.getStorageSync('logs'|| []

    logs.unshift(Date.now())

    wx.setStorageSync('logs', logs)

    // 登录

    wx.login({

      success:res => {

        // 发送res.code 到后台换取openId,sessionKey,unionId

      }

    })

    // 获取用户信息

    wx.getSetting({

      success:res =>{

        if(res.authSetting['scope.userInfo']){

          //已经授权,可直接调用getUserInfo获取用户名或昵称

          wx.getUserInfo({

            success:res =>{

              this.globalData.userInfo = res.userInfo

              if(this.userInfoReadCallback){

                this.userInfoReadCallback(res)

              }

            }

          })

        }

      }

    })

  },

  globalData:{

    user:{},

    userInfo:{}

  },

})






// pages/load/load.js

Page({


    /**

     * 页面的初始数据

     */

    data: {

        canIUse: wx.canIUse('button.open-type.getUserInfo')

    },


    next:function(e){

      console.log('userInfo',getApp().globalData.userInfo)

     wx.redirectTo({

        url: '/pages/login/login'

      })

    },


    /**

     * 生命周期函数--监听页面加载

     */

    onLoad: function (options) {

    var that = this

    wx.showLoading({

      title: '加载中',

    })

    wx.login({

        success (res) {

          if (res.code{

            //发起网络请求

             wx.request({

               url: 'https://127.0.0.1:3000/onLogin',

               data: {

                 code: res.code

                }

             })


            // 查看是否授权

            wx.getSetting({

                success (res){

                    if (res.authSetting['scope.userInfo']) {

                        // 已经授权,可以直接调用 getUserInfo 获取头像昵称

                        wx.getUserInfo({

                            success: function(res) {

                                getApp().globalData.userInfo = res.userInfo

                                //that.next();

                            }

                        })

                    }

                }

             })  

          } else {

            console.log('登录失败!' + res.errMsg)

          }

        }

      })

     

      setTimeout(function(){

          wx.hideLoading()

          },2000)

    },

    bindGetUserInfo (e) {

      console.log(e.detail.userInfo)

      getApp().globalData.userInfo = e.userInfo

      this.next();

    },


    /**

     * 生命周期函数--监听页面初次渲染完成

     */

    onReady: function () {


    },


    /**

     * 生命周期函数--监听页面显示

     */

    onShow: function () {


    },


    /**

     * 生命周期函数--监听页面隐藏

     */

    onHide: function () {


    },


    /**

     * 生命周期函数--监听页面卸载

     */

    onUnload: function () {


    },


    /**

     * 页面相关事件处理函数--监听用户下拉动作

     */

    onPullDownRefresh: function () {


    },


    /**

     * 页面上拉触底事件的处理函数

     */

    onReachBottom: function () {


    },


    /**

     * 用户点击右上角分享

     */

    onShareAppMessage: function () {


    }

})


回答关注问题邀请回答
收藏

3 个回答

登录 后发表内容