通过const app = getApp()实现在 page 页面获取 app.js 定义的属性globalData,即获取全局数据
阅读原文时间:2023年07月08日阅读:1

App.js是项目的入口文件,页面的 page.js 文件会覆盖 app.js文件,

App.js文件里面的一些方法:

onLaunch : function(){}:这个方法是当小程序加载完毕后就执行的方法

onLoad:function(options){}:页面初始化 options 为页面跳转所传递过来的参数

onReady:function(){}:页面渲染完成

onShow:function(){}:页面显示

onHide:function(){}:页面隐藏

onUnload:function(){}:页面关闭

还可以在 app.js的onLaunch里面定义一些全局的方法,如登录的方法和获取用户信息的方法(wx.getSetting),另外,在app.js里面,写上一些需要的东西,如globalData,在其他页面需要时,可以直接调用,无需一直写!

例子:

1、在app.js中App({ })里面写

//app.js
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)

// 登录  
wx.login({  
    success: res => {  
        // 发送 res.code 到后台换取 openId 验证平台账号是否登录绑定过  
        var that = this;  
        wx.request({  
            method: 'GET',  
            url: this.globalData.serverApi + "/mobileApi/user/checkBind?code="+res.code,  
            header: {  
                'content-type': 'application/json'  
            },  
            success (res) {  
                if(res.data.code == 301){  
                    //未登录  
                    var openId = res.data.openId;  
                    wx.reLaunch({  
                      url: '/pages/login/login?openId='+openId  
                    })  
                }else if(res.data.code == 1){  
                    //已登录  
                    that.globalData.userInfo = res.data.userInfo;  
                    that.globalData.token = res.data.token;  
                    var menuList = res.data.menuList;  
                    wx.setStorageSync('menuList', menuList);  
                }else if(res.data.code == 0){  
                    //获取openId失败  
                    wx.showToast({  
                      title: res.data.msg,  
                      icon: 'none',  
                      duration: 2000  
                    })  
                }

                // 由于 checkBind 是网络请求,可能会在 Page.onLoad 之后才返回  
                // 所以此处加入 callback 以防止这种情况  
                if (that.checkBindCallback) {  
                  that.checkBindCallback(res)  
                }  
            }  
        })  
  }  
})

// 获取用户信息  
/\* wx.getSetting({  
  success: res => {  
    if (res.authSetting\['scope.userInfo'\]) {  
      // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框  
      wx.getUserInfo({  
        success: res => {  
          // 可以将 res 发送给后台解码出 unionId  
          this.globalData.userInfo = res.userInfo

          // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回  
          // 所以此处加入 callback 以防止这种情况  
          if (this.userInfoReadyCallback) {  
            this.userInfoReadyCallback(res)  
          }  
        }  
      })  
    }  
  }  
}) \*/  

},
globalData: {
serverApi: "http://c15112b047.iok.la",
staticApi: "http://c15112b047.iok.la/uploadPath",
userInfo: null,
token: null
}
})

获取当前微信用户信息可以用在登陆页:判断是否存在登陆。登陆过就改变 app.js 里面的配置。例如这个用户是否登陆了,如果全局里面没有用户的信息,需要跳转到登陆的页面进行登陆,如果说有了用户的信息就不用跳转到登陆页面,当登陆完成之后,我们就可以设置全局的数据,然后返回到跳转的页面,直接读取用户的信息就可以。

2、在页面的index.js开头写上,然后就可以在page里面的onLoad()里调用你需要的东西!

//index.js
//获取应用实例
const app = getApp()
Page({
onLoad: function () {
if(app.globalData.token){
//代表从页面跳转过来
var menuList = wx.getStorageSync('menuList');
this.getMenuList(menuList);
this.animation = wx.createAnimation();
this.setData({hidden: false});
}else{
//代表第一次加载
wx.showLoading({
title: '加载中'
})
// 由于 checkBind 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.checkBindCallback = res => {
wx.hideLoading();
if(res.data.code == 1){
var menuList = wx.getStorageSync('menuList');
this.getMenuList(menuList);
this.animation = wx.createAnimation();
this.setData({hidden: false});
}
}
}
}
})