小程序怎么能拿到app.js里的信息

app.js里已经存进去了 ,但是在index页面拿不到 上图
App({

util: require('we7/resource/js/util.js'),
siteInfo: require('siteinfo.js'),
//启动
onLaunch: function () {


// 获取用户信息
this.getUserInfo();
this.getSys();
this.location();

},
location:function(){

var that = this
wx.getLocation({
  success: function (res) {
    that.globalData.location = res
  },
})

},
//获取用户信息
getUserInfo: function (cb) {

var that = this
wx.login({
  success: function (res) {
    var code = res.code
    wx.getUserInfo({
      success: function (res) {
        that.globalData.userInfo = res.userInfo
        if (res == undefined) {
          wx: wx.showModal({
            title: '21',
            content: '',
            showCancel: true,
            cancelText: '',
            cancelColor: '',
            confirmText: '',
            confirmColor: '',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
        typeof cb == "function" && cb(that.globalData.userInfo)
        var img = res.userInfo.avatarUrl
        var name = res.userInfo.nickName
        that.util.request({
          url: 'entry/wxapp/openid',
          data: { code: code },
          success: res => {
            var openid = res.data.openid
            that.util.request({
              'url': 'entry/wxapp/login',
              'cachetime': '0',
              data: { openid: openid, img: img, name: name },
              success: res => {
                that.globalData.user = res.data;
                wx.setStorageSync('userinfo', res.data)
              },
            })
          }
        })
      },
      fail: res => {
        console.log(res)
        if (res.errMsg = "getUserInfo:fail auth deny") {
          wx.showModal({
            title: '授权提示',
            content: '您取消了授权,如果需要正常使用,请按确定并在授权管理中选中"用户信息",或者在我的-授权管理选中"用户信息",重新进入小程序即可正常使用 ',
            success: res => {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.openSetting({
                  success: res => {
                    console.log(res)
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  }
})

},
//获取手机信息
getSys: function () {

var that = this;
//  这里要非常注意,微信的scroll-view必须要设置高度才能监听滚动事件,所以,需要在页面的onLoad事件中给scroll-view的高度赋值
wx.getSystemInfo({
  success: function (res) {

    //设置变量值
    that.globalData.sysInfo = res;
    that.globalData.windowW = res.windowWidth;
    that.globalData.windowH = res.windowHeight;
  }
})

},
ormatDate: function (dateNum) {

var date = new Date(dateNum * 1000);
return date.getFullYear() + "-" + fixZero(date.getMonth() + 1, 2) + "-" + fixZero(date.getDate(), 2) + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2);
function fixZero(num, length) {
  var str = "" + num;
  var len = str.length;
  var s = "";
  for (var i = length; i-- > len;) {
    s += "0";
  }
  return s + str;
}

},
//全局变量
globalData: {

user: null,
userInfo: null,
sysInfo: null,
windowW: null,
windowH: null,
location: null

},
})

clipboard.png
这是为什么

阅读 4.7k
2 个回答

const app = getApp();

index.js中引入 然后app.globalData.userInfo

this.getUserInfo();
this.getSys();
this.location();
这些回调都是异步的,可能会在 Page.onLoad 之后才返回,所以为null。
可以加个判断,如getUserInfo:

if (app.globalData.userInfo) {
    this.setData({
    userInfo: app.globalData.userInfo,
  })
}else{
    wx.getUserInfo(...)
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题