How to Implement WeChat Public Account QR Code Login with Phone Binding
This guide walks you through the complete backend workflow, from preparing a verified WeChat service account and configuring server settings to generating parameterized QR codes, polling user status, handling follow and scan events, and binding phone numbers for seamless automatic login.
1. Preparation
To enable automatic login after a user scans a QR code and follows the public account, you must first register a WeChat service account (not a subscription account), purchase verification, obtain the AppID and AppSecret, and deploy a publicly accessible server.
Register a WeChat service account.
Purchase annual verification (≈300 CNY).
Enable the developer password to get appId and secret.
Deploy the server code on a public host.
Configure the server settings in the public‑account backend (URL, token, AES key, encryption mode).
After configuring the server, existing auto‑reply and custom menu settings become invalid; they must be set via the WeChat API and handled in the event‑receiving code.
2. Scan‑Follow Auto Login Process
2.1 Client Flow
User requests a QR code and identifier from the backend.
Client polls the status endpoint using the identifier.
If the user is already registered, the returned token is used to log in.
If the user is not registered, a phone‑binding dialog is shown; after binding, the returned token logs the user in.
2.2 Server Flow
The server provides three APIs and listens for scan events to obtain the user’s OpenID and determine registration status.
Generate a parameterized QR code using the “Create QR code with parameters” API; the subsequent follow event is captured.
Polling API returns one of three states: continue polling, not registered (prompt phone binding), or registered (return token).
When a follow or scan event is received, the server checks the OpenID in the database and sets the polling state accordingly, generating a token for registered users.
Phone‑binding API is independent of WeChat events; on success it returns a login token.
2.3 User Scan Flow
If the user scans before following, only a follow event is sent after the user clicks “Follow”.
If the user has already followed, a scan event is sent directly.
3. Code Samples
3.1 Generate QR Code
@PostMapping("userQrcodeCreate")
private Result<WeixinQrcodeResponseVO> userQrcodeCreate(@RequestBody @Validated WeixinMPRequestVO req) {
// implementation omitted for brevity
}3.2 Polling Status
public enum UserSanLoopStatusEnum {
EXPIRED(1), LOOP(2), REG(3), NOT_REG(4);
}
public WeixinUserStatusResponseVO userStatus(WeixinMPRequestVO req) {
// implementation omitted for brevity
}3.3 Event Handlers
@Component
public class SubscribeHandler extends AbstractHandler {
// handle follow event
}
@Component
public class ScanHandler extends AbstractHandler {
// handle scan event
}Summary
The article details the complete workflow and code required to achieve automatic login in a Java SpringBoot backend by leveraging WeChat public‑account QR codes, polling, and phone‑binding, enabling a seamless user experience for both new and existing users.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
