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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
