worth mentioning

worth mentioning

I. Overview

In the project, the function of WeChat login is realized, which is described as follows:

  1. Open the homepage of the website, click the login button, http://XXXX/index
  2. Click to log in, a QR code pops up
  3. After scanning the code on WeChat, jump to the public platform
    • If the user is already following, log in directly
    • If the user does not follow, prompt the user to follow, after paying attention, log in directly

After scanning the code, what ulterior secrets have happened between the front desk-backstage-WeChat? This series of articles uses the PHP-Laravel framework to take you step by step to realize the function of WeChat login.

Use Laravel to implement server-side Use workerman to implement websocket server

2. Principle

  1. The client clicks to log in, initiates a request to the background, and obtains the ticket and scene_id (randomly generated integer)
  2. The client uses the ticket to initiate a request to the WeChat server, obtain the QR code https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=abc and initiate a websocket connection to the background websocket server, and push the scene_id, the background Add scene_id to the connection array
  3. The user uses WeChat to scan the code, clicks to follow, and the WeChat official account pushes the user's attention to the PHP server
  4. The PHP server uses openid to request the WeChat server, get the user's detailed information, save it to the data table, and then push openid and scene_id to the websocket server
  5. The websocket server receives the openid and scene_id pushed by the php server, uses scene_id to determine which client it is, and then pushes the openid to it
  6. The client receives the push of the websocket, carries the openid to the php server to request the login login interface, and the php server writes the openid into the session, indicating that the user has logged in
Reference: https://cloud.tencent.com/developer/article/1480137 Third Party Login-Cloud + Community-Tencent Cloud