Micro letter applet authorized to log on best practices

This article is reproduced in: Ape 2048 Website ➧ https://www.mk2048.com/blog/blog.php?id=h21aac011j

Business development company, recently completed the development of a micro-channel applet. The scene is this: have the same body of APP and micro-channel applets at the same WeChat open platform to accomplish the same business, the user enters the app or unionid must obtain the user when the micro-channel small program to confirm the current user identity and complete the registration. Applet "to obtain user information" api (getUserInfo) is called by and large has been compared to the previous update, implement user authorization and login is very important and elegant, the following are authorized to log in to achieve my micro letter applet related processes in development time ideas and summary, the following share.

First, the micro-channel timing applet login process

Login process timing

Description:

  1. Applet calls wx.login () to obtain temporary registration certificate code, and back to the developer server
  2. Developers exchange server code that uniquely identifies the user and session key openid session_key.
  3. Temporary registration certificate code can only be used once

What is openid?

After the number of followers and generate public message exchange, available to the public number of followers OpenID (micro signal encrypted, each user is unique to each public OpenID numbers for different public numbers, different users of the same openid ). - micro-channel public platform developer documentation

  • Ordinary user's identity, the current number of unique public
  • Different public number,? The same user, different openid

You can simply understood as

openid = hash(uid + app_id)

What is unionid?

If developers have multiple mobile applications, web applications, and the public accounts (including applets), can be used to distinguish users by unionid unique, because as long as the mobile applications in the same WeChat open platform accounts, web applications, and the public accounts (including applets), users unionid is unique. In other words, the same user, the same for different applications in a micro-channel open platform, unionid same. UnionID mechanism Description

If a developer? Demand unified user accounts across multiple mobile applications, web applications, and the public accounts, need to go to the WeChat open platform (open.weixin.qq.com) binding public number, you can use UnionID mechanism meet these needs.

  • You can have multiple mobile applications, web applications next WeChat open platform account? Public accounts and applets
  • As long as under the same account WeChat open platform for mobile applications, web applications and public accounts (including applets), users unionid is unique.

The user's unique identifier in an open platform

You can simply understood as:

unionid = hash(uid + 开放平台id)

Under summarize micro-channel for different user has only one openId in different applications, however, to determine whether the user is not the same user, you need to rely on unionid to distinguish. Their general background of a user will have their own table, each user has a different userid. That is a user in the same application with the same subject at a micro-channel open platform corresponds to the same userid, unionid and different openid. So when the user logged in, we can only rely on micro letter returned to us to judge unionid is not the same user, to associate our users in the table, get the corresponding userid.

Second, the micro-channel applet? How to get unionid?

Bound applet developer account, you can obtain the following three ways UnionID.

  1. Call Interface wx.getUserInfo , get UnionID from the decrypted data. Note that this interface requires a user license, please developer to properly handle the situation after the user denying authorization.
  2. If there are public body under the same numbers developer account, and the user has the attention of the public number. Developers can get directly to the user by wx.login UnionID, again without user authorization.
  3. If there is the same number or a public body under the mobile application developer account, and the user has logged in the public authorization number or mobile applications. Developers can also get directly to the user by wx.login UnionID, again without user authorization.

When you meet the conditions 2 and 3, developers can get directly through wx.login? To the user's unionid, or you must call interface wx.getUserInfo, extra attention needs to do is to properly handle the user to refuse authorization.

Third, log on best practices

  1. Call wx.login get code.
  2. Use wx.getSetting obtain authorization of users
    • If the user has authorization, directly call API wx.getUserInfo obtain the latest information of the user;
    • The user is not authorized to display a button that prompts the user to log in, access to the latest information of the user when the user clicks on and after authorization in the interface.
  3. The acquired user data along with the pass code wx.login return rear end

Package Ajax ()

In a real business scenario, we hope that, when the user enters a small program, without logging in normal circumstances can browse the merchandise, have a basic knowledge of the applet, do not direct the pop-up box asking for user authorization, otherwise it will interfere with the user, resulting in new the loss of customers, when the user needs to use some of the advanced features and scene, this time again requires user authorization, the probability of such authorized users will be greatly enhanced. The login logic flow ajax package:

Meaning the package is no longer concerned that the interface need to be logged, whether the user is authorized to direct all requests calls ajax(), complete all registration and authorization process when necessary. Applet entry page increases, business development, you only need to focus on the business implementation.

Reference material

Guess you like

Origin www.cnblogs.com/htmlandcss/p/11782681.html