Home > Web Front-end > HTML Tutorial > Determine whether the login is invalid code

Determine whether the login is invalid code

php中世界最好的语言
Release: 2018-02-24 09:43:05
Original
2703 people have browsed it

This time I will bring you the code to determine whether the login is invalid and determine whether the login is invalid. What are the precautions?. Here is a practical case. Let’s take a look.

var that = this;
        new Promise((resolve, reject) => {
            // 检查wx.login有没有失效,失效的话重新wx.login
            wx.checkSession({
                success() {
                    console.log('code', wx.getStorageSync('code'));
                    console.log('globalData', wx.getStorageSync('globalData'));
             //如果小程序再次从后台进入前台的话,用storage保存的话是有值的
             //如果用this.globalData的话来保存数据,下次从后台进入前台的话是值还是空的 
                },
                fail() {
                    wx.login({
                        success(res) {
                            wx.getUserInfo({
                                success(re) {
                                    wx.setStorageSync('code', res.code)
                                    that.globalData.code = res.code;
                                    that.globalData.userInfo = re.userInfo;
                                    wx.setStorageSync('globalData', that.globalData);
                                }
                            })
                        },
                        fail: function(err) {
                            
                        }
                    })
                }
            })
            resolve();
        }).then((res) => {
            var code=wx.getStorageSync('code');
            var globalData=wx.getStorageSync('globalData')
           
        })
Copy after login

First of all, wx.checkSession is to determine whether the wx.login method login state is invalid.
Thinking
wx.checkSession({
Success(){ //Login at this time The status is not invalid, so you don’t need to go to wx.login
//But according to my needs, I need to connect to the interface next, and I need the login data, but how to get the data at this time
    1. If the applet enters the foreground from the background again, it will have a value if you use storage to save it
  2. If you use this.globalData to save the data, the next time you enter the foreground from the background, the value will still be empty} ,
fail(){ wx.login()
}
})

This is my implementation idea
The code is above

I believe after reading these You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

What is the importance of overflow scrolling

Commonly used input text box content is automatically vertically centered and defaults Click the prompt text to be empty

How to make the copyright symbol more beautiful in HTML

How to implement mouse hover in HTML Stop prompting the content of tag A

The above is the detailed content of Determine whether the login is invalid code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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