How to get the unionid of a mini program

WJ
Release: 2020-06-10 11:41:55
Original
8131 people have browsed it

How to get the unionid of a mini program

How to get the unionid of the mini program?

Problem background: I made the WeChat applet and everything was normal. However, when the trial version was finally released, I found that many users could not bind users and the background returned: illegal parameters. After multiple investigations, it was found that the data returned by the code request obtained by WeChat did not contain UnionId, that is, the parameters returned by the interface did not contain the UnionId parameter.

1. UnionID mechanism description

In short, WeChat has a unique openId for different users in different applications, but if you want to determine whether the user is the same user, You need to rely on unionid to distinguish. Usually, your own backend will have its own user table, and each user has a different userid. That is to say, the same user's applications of the same subject under the same WeChat open platform correspond to the same userid, unionid and different openid. So when a user logs in, we can only rely on the unionid returned to us by WeChat to determine whether it is the same user, and then associate it with our user table to get the corresponding user_id.

Note: The following sentence describes: The user has followed the official account, or has logged into the App or official account. When the user opens the mini program, the developer can directly obtain the user's UnionID through wx.login. . That is: if the user has not followed the official account, or has not logged into the App, the user's UnionID cannot be obtained through wx.login, and the UnionId can only be obtained through wx.getUserInfo. It has been verified that the mini program users whose UnionId does not exist in the system are all users who have not followed the official account or have not used WeChat authorization in the App

2. Problem background

General In this case (that is, before logging in to the mini program, the user has followed the official account or logged in to the official account or logged in to the app using WeChat login), the user can successfully log in to the website through the following two steps: (1) wx.login Get the code; (2) Request the background through the code, and forward the WeChat api in the background in exchange for information such as openid and session_key. This can get Unionid

Another situation (that is, before logging in to the mini program, you have not followed it Official account, I have never logged into the official account, nor have I logged into the app using WeChat login method), the code obtained through wx.login cannot be exchanged for information such as unionid and openid.

3. Solution:

To obtain the UnionId of the mini program, the UnionId of wx.getUserInfo should be the main one. wx.getUserInfo requires user authorization. In terms of products, the processing flow for users to refuse authorization needs to be considered.

 For the method of decrypting the data after wx.getUserInfo obtains it, please see the official documentation.

Solution: Get the user's encrypted data encryptedData and the initial vector iv of the encryption algorithm through wx.getUserInfo with login status, and then pass the encryptdata, iv and code to the backend, and the backend will receive it through The obtained encryptedData, iv, code and previous session_key are used to decrypt the user's openid, unionid, etc. Encrypted data decryption algorithm

Related recommendations:小program tutorial

The above is the detailed content of How to get the unionid of a mini program. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!