Quickly develop one of WeChat applet login authentication

1. Background

I remember that in 2011 or 2012, everyone started to do client Android and IOS development in droves. I only started doing Andoird development in 2014 and worked for more than two years. Then around 2018, WeChat mini programs became popular, and I also did two There are two small programs, one is to migrate the functions of the original milk public account to the small program, and the user experience has been greatly improved. The other is a simple small program for selling honey made for XX. At that time, 3 people were invested and two weeks Get it done left and right.

WeChat mini program development is actually relatively simple. The mini program code is similar to the Vue overall architecture. The more complicated parts are login and payment, because these two parts involve three-party interaction (mini program, developer server, WeChat server), today I will talk about how the login is interactive (this was done in 18 years, some interfaces of WeChat may have been adjusted now, but the change will not be too big)

2. Some noun concepts

1、openid

Ordinary user tags are unique to the current developer account. One openid corresponds to a specific application. Different official accounts and different mini programs will assign different openids to the same WeChat user.

2、Unions

WeChat users are marked uniformly. The unionid of the same WeChat user under an open platform account is unique, and the official account/mini program needs to be bound to the WeChat open platform.

Case description: The Milk application started by developing a public account - a service account. At this time, when the user registered in our table, there would be an openid_1. Then the mini program became popular. We decided to develop the mini program, but the mini program would be reassigned to the same user. An openid_2, this is not good. You can't let a user see his own order on the official account, and then run to the mini program and can't find the order, right? What's the deal? WeChat also has an open platform. You apply for another email address to register for the open platform, and then bind the official account and mini program to it, so that it will assign a unified unionid to you.

3、session_key

It is used for users to decrypt WeChat user data. The sensitive data returned by the interface is ciphertext, such as unionId, mobile phone number, etc., and session_key needs to be used as the key to decrypt.

3. WeChat provides several main interfaces related to login

Mini program call

1、wx.login()

Obtain the temporary login credential CODE, which is valid for 5 minutes and will expire immediately after use.

2、wx.authorize()

Initiate a user authorization request. If you want to obtain user information, geographical location, save photo albums, cameras, etc., you need to call this interface to obtain the user's authorization.

3、wx.getUserInfo()

Get the encrypted user data, return the encryptedData data, the encryption vector value IV, and pass the data to the server. The server completes the decryption of user information registration, etc. The above two methods must be called before calling this interface.

Developer server call

1. jscode2session: Get openid, unionid, sessiooo_key through CODE.

4. Call the interaction diagram

Guess you like

Origin blog.csdn.net/2301_76787421/article/details/133500494