Basics of WeChat Mini Program Development - app.js (3)

Y2J
Release: 2017-04-25 09:18:35
Original
3827 people have browsed it

WeChat Mini Program Development Tutorial (Basics) 1-First Introduction to WeChat Mini Programs
WeChat Mini Program Development Tutorial (Basics) 2-WeChat Mini Program Structure Overview

Written in the previous tutorial Yes, the development tool will generate a default program framework, in which the main process code of the program is included in app.js. In the default implementation, this part of the function is relatively simple, but it is still valuable for learning and researching small program development.

Since there are not many lines of code, I will post it all at once and explain it later

//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData:{
    userInfo:null
  }
})
Copy after login

The code defines an App object, which contains two methods, onLaunch and getUserInfo, and the globalData member variable
The onLaunch method will be called by the framework when the program starts. In this method, the program obtains a logs variable through the local storage interface provided by WeChat. The first time it is obtained, it will return empty. At this time, the variable is initialized to an empty array. Then add a formatted time string at the head of the array, and then store the array variable locally.

The WeChat applet framework provides a series of APIs to help us store local data. The above code uses two APIs, wx.getStorageSync and wx.setStorageSync. For more related APIs, please refer here

The getUserInfo method is easy to understand and will perform the function of obtaining user information. However, this method is different from onLaunch. It is not a function preset by the framework, so it will not automatically trigger the call. This method is called in the onload method of index.js (the index.js part will be explained in subsequent tutorials). This method requires the caller to pass a parameter named cb. First, it determines whether the user information has been obtained. If it has been obtained and cb is a function, the cb function will be called and the globalData member variable will be passed in. Otherwise, the wx.login interface will be called. to obtain user information.

Students who are not familiar with js or have little contact with scripting languages ​​may be confused by the syntax of the above code. These lines of code involve concepts such as function callbacks, anonymous functions, closures, etc. I will focus on this in the future. Write a separate tutorial for each part.

The code finally defines the globalData member variable, which contains the userInfo variable to save user information.

The above is the detailed content of Basics of WeChat Mini Program Development - app.js (3). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template