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

How to realize the authorized login in WeChat Mini Programs

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how to achieve authorization login in WeChat Mini Programs". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to achieve authorization login in WeChat Mini Programs" can help you solve your doubts. Let's follow the editor's ideas to slowly deepen, let's learn new knowledge.

Time sequence of WeChat Mini Programs login process

Description:

Mini Program calls wx.login () to obtain temporary login credentials code, which is passed back to the developer server.

The developer server uses code in exchange for the user's unique ID openid and session key session_key.

Temporary login credentials code can only be used once

What is openid? After the follower interacts with the official account, the official account can get the follower's OpenID (encrypted WeChat account, and each user's OpenID is unique to each official account. For different official accounts, the openid of the same user is different. -- Wechat public platform developer documentation

The identity of an ordinary user, unique to the current official account

Different official accounts, same user, different openid

You can simply interpret it as

Openid = hash (uid + app_id) what is unionid? If developers have multiple mobile applications, web apps, and public accounts (including Mini Program), they can distinguish the uniqueness of users through unionid, because as long as they are mobile apps, web apps and public accounts (including Mini Program) under the same Wechat open platform account, the user's unionid is unique. In other words, the same user, different applications under the same Wechat open platform, unionid is the same. Description of UnionID mechanism

If developers have a need for a unified user account among multiple mobile applications, website applications and public accounts, they need to go to the Wechat Open platform (open.weixin.qq.com) to bind the official account, then they can use the UnionID mechanism to meet the above needs.

There can be multiple mobile apps, website apps, public accounts and Mini Program under one Wechat open platform account.

Users' unionid is unique as long as they are mobile apps, website apps and public accounts (including Mini Program) under the same Wechat open platform account.

Unique identifier of the user on the open platform

You can simply interpret it as:

Unionid = hash (uid + open platform id)

To sum up

Wechat has a unique openId for different users under different applications, but to determine whether users are the same user, we need to rely on unionid to distinguish. Generally speaking, your own backend will have its own user table, and each user has a different userid. That is to say, the applications of the same subject of the same user under the same Wechat open platform correspond to the same userid,unionid and different openid. So when the user logs in, we can only rely on the unionid returned to us by Wechat to determine whether it is the same user. After correlating our user table, we can get the corresponding userid.

2. How does WeChat Mini Programs get unionid?

Mini Program, which is bound to a developer's account, can obtain UnionID in the following three ways.

Call the API wx.getUserInfo to obtain UnionID from the decrypted data. Note that user authorization is required for this API. Developers should properly handle the situation after the user refuses to authorize it.

If there is an official account with the same subject under the developer account, and the user has followed the official account. Developers can obtain the user's UnionID directly through wx.login without the user's re-authorization.

If an official account or mobile application with the same subject exists under the developer's account, and the user has been authorized to log in to the official account or mobile application. Developers can also obtain the user's UnionID directly through wx.login without the user's re-authorization.

When a user meets conditions 2 and 3, the developer can obtain the user's unionid directly through wx.login, otherwise the API wx.getUserInfo must be called. The extra thing to pay attention to is to properly handle the user's refusal of authorization.

Best practices for logging in

Call wx.login to get the code.

Use wx.getSetting to obtain the authorization of a user

If the user has been authorized, call API wx.getUserInfo directly to get the latest information of the user

The user is not authorized, a button is displayed in the interface to prompt the user to log in, and when the user clicks and authorizes, he or she will get the latest information about the user.

Transfer the acquired user data to the backend together with the code returned by wx.login

Encapsulate ajax ()

In the real business scenario, we hope that when users enter Mini Program, they can browse products normally without logging in, and have a basic understanding of Mini Program. Do not directly pop up boxes to ask for user authorization, otherwise it will interfere with users and lead to the loss of new users. When users need to use some advanced features and scenarios, ask for user authorization at this time, so the chances of user authorization will be greatly improved.

Encapsulate the logic of login into ajax

Process:

The significance of encapsulation

No longer care about whether the current interface needs to log in, whether the user is authorized, all requests directly call ajax (), and complete all login and authorization processes if necessary. Mini Program portal page increases, when business development, you just need to focus on business implementation.

After reading this, the article "how to achieve authorized login in WeChat Mini Programs" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, 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