search
HomeWeChat AppletMini Program DevelopmentBasics of WeChat Mini Program Development - app.js (3)

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
  }
})

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor