在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ 小程序怎么能拿到app.js里的信息

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

app.js里已經(jīng)存進(jìn)去了 ,但是在index頁(yè)面拿不到 上圖
App({

util: require('we7/resource/js/util.js'),
siteInfo: require('siteinfo.js'),
//啟動(dòng)
onLaunch: function () {


// 獲取用戶(hù)信息
this.getUserInfo();
this.getSys();
this.location();

},
location:function(){

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

},
//獲取用戶(hù)信息
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: '授權(quán)提示',
            content: '您取消了授權(quán),如果需要正常使用,請(qǐng)按確定并在授權(quán)管理中選中"用戶(hù)信息",或者在我的-授權(quán)管理選中"用戶(hù)信息",重新進(jìn)入小程序即可正常使用 ',
            success: res => {
              if (res.confirm) {
                console.log('用戶(hù)點(diǎn)擊確定')
                wx.openSetting({
                  success: res => {
                    console.log(res)
                  }
                })
              } else if (res.cancel) {
                console.log('用戶(hù)點(diǎn)擊取消')
              }
            }
          })
        }
      }
    })
  }
})

},
//獲取手機(jī)信息
getSys: function () {

var that = this;
//  這里要非常注意,微信的scroll-view必須要設(shè)置高度才能監(jiān)聽(tīng)滾動(dòng)事件,所以,需要在頁(yè)面的onLoad事件中給scroll-view的高度賦值
wx.getSystemInfo({
  success: function (res) {

    //設(shè)置變量值
    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
這是為什么

回答
編輯回答
有你在

const app = getApp();

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

2018年5月1日 12:14
編輯回答
凝雅

this.getUserInfo();
this.getSys();
this.location();
這些回調(diào)都是異步的,可能會(huì)在 Page.onLoad 之后才返回,所以為null。
可以加個(gè)判斷,如getUserInfo:

if (app.globalData.userInfo) {
    this.setData({
    userInfo: app.globalData.userInfo,
  })
}else{
    wx.getUserInfo(...)
}
2017年10月20日 11:06