Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process for Wechat users to visit Mini Program and log in?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of what Wechat users visit Mini Program login process, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that after reading this Wechat user login process of Mini Program users will have something to gain, let's take a look.

Overview

After you have developed a Mini Program and deployed it online, the first time a Wechat user visits the Mini Program, an authorization interface pops up where the user can choose whether or not to log in using Wechat, and if so, go directly to Mini Program. When you enter the Mini Program for the second time, you will find that the authorization interface will not pop up and go directly to Mini Program.

Wechat provides Mini Program login flowchart.

We can find this diagram from the timing of the login process. To fully understand a picture, you need to know a lot of things. Let's first take a look at some of the interfaces provided by Wechat.

Introduction of Mini Program API

Wx.login ()

This method is initiated by Mini Program. If you use the wepy of Mini Program component development framework launched by Tencent team, the pseudo code is as follows:

Wepy.login () .then (res = > {const code = res.code})

In this way, you can obtain a code from the Wechat platform. This code is a temporary login credential to obtain the openid.

Wx.request ()

This is also initiated by Mini Program and is used to request the interface on the developer server (that is, our application server). The pseudo code is called as follows:

Wepy.request ({url: `xxurl`, data: {/ / input parameter},}) .then (res = > {/ / get the data returned by the application server from res})

Get the interface of openid

When a developer develops a Mini Program, when a user visits the Mini Program, the Wechat platform assigns an openid to that user. The following APIs are used:

Https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

This Wechat interface is usually initiated and called by our application server, not Mini Program.

Wx.getUserInfo

GetUserInfo is used to obtain user information on Wechat platform. Note that if Wechat users visit Mini Program for the first time, an authorization interface pops up. When the user is authorized, getUserInfo can call and return the user data.

All four interfaces are used when using Wechat to access Mini Program.

Persistent session and login authentication based on token

Although Mini Program does not support the mechanism of cookie, it supports setting token in header.

This token is generated by the application server.

Header: {'xxxxtoken': token,}

When Mini Program calls the application server interface, it must bring the token, and the application server parses and authenticates the token. Of course, if you visit Mini Program for the first time, you can only token from the application server. The author intends to use pseudo-code to express the whole login process after using token, because it is difficult to express with words or pictures.

Const code = wx.login (); if (code) {/ / code exists / / get the local totenconst token = wx.getStorageSync ('xxxxxtoken') if (token) {/ / the local existence of token in Mini Program without the need for pop-up authorization interface / / directly pass into the code field. Call the application server's method to verify token. If the verification is successful, you need to return user information. Const userinfo = wx.request (http://xxxxxValidateToken(code); if (userinfo) {/ / indicates that you have logged in successfully and go directly to the main interface of Mini Program. }} else {/ / indicates that Mini Program does not have token locally. At this time, you need to pop up the authorization interface and let Wechat users decide whether to access Mini Program or not, if the user chooses yes. Const weixinuserinfo = wx.getUserInfo (); / / the authorization interface will pop up. If (weixinuserinfo) {/ / generate or verify token const userinfo = wx.request (http://xxxxxValidateToken(code) provided by Wechat; const token = userinfo.getToken (); / / store token in Mini Program local wx.setStorageSync ('xxxxxtoken', token)}

In the pseudo code above, the application server (our application server) will be called

Http://xxxxxValidateToken(code)

Method. The implementation logic of this method is roughly as follows:

1. To verify whether the Wechat user exists, you can call the jscode2session method provided by Wechat, which will return an openid. We must save this openid to the database in the business code and associate it with userid.

2. Determine whether it is a new user, and if so, generate a token and generate a new user information to store in the database. If it is not a new user, verify token.

After this process, Mini Program can use the wx.request method with token to access the business method of the application server and obtain business data.

Generation and Verification of token

I have seen some companies directly send userid and openid back to Mini Program after encryption. Also seen to return to Mini Program after encryption with userid and password. If you choose the second way, the logic for verifying token is roughly as follows:

First decrypt, obtain userid and password, and obtain the user password from the database according to userid, and compare it with the password decrypted from token. If equal, the verification passes.

This is the end of the article on "what is the process for Wechat users to visit Mini Program and log in". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the process of Wechat users visiting Mini Program login". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report