Home > Article > Backend Development > WeChat PC login and mobile login logic sharing
PC-side login requires application on the WeChat open platform, while mobile-side authorized login only requires a certified service account. Then if you use the same WeChat user to log in to the same website platform, you will find that the WeChat user openid obtained on the PC side is different from that on the mobile side. How to solve this problem?
1. Official approach
We need to bind a WeChat service account to the WeChat open platform. At this time, a strange thing will happen. If the WeChat service account does not If you apply for a WeChat mini program, the WeChat open platform prompts you to open the mini program first.
Then after binding the WeChat service account to the WeChat open platform, we can obtain a unified UnionID. This UnionID is unified whether it is a PC WeChat login or a mobile WeChat login.
Let’s take a look at the official explanation:
Description of the role of the UnionID mechanism: If the developer has multiple mobile applications, website applications and public accounts, he can obtain the user’s basic information by The unionid is used to distinguish the uniqueness of users, because the same user has the same unionid for different applications (mobile applications, website applications and public accounts) under the same WeChat open platform.
2. A different approach
Scenario 1.
1. Mobile phone number registration-》Successful.
2. When the user logs in using WeChat on the PC for the first time, he will reach the binding mobile phone number page. If the mobile phone number has been registered, the data table field weixin_open_id will be updated.
3. The user logs in using PC WeChat for the second time and updates the last login time.
Scenario 2,
1. Mobile phone number registration-》Successful.
2. When the user logs in using the mobile WeChat for the first time, he will reach the binding mobile phone number page. If the mobile phone number has been registered, the data table field weixin_open_id_m will be updated.
3. The user logs in using mobile WeChat for the second time and updates the last login time.
Scenario 3,
1. The user logs in using WeChat on the PC for the first time, and then reaches the binding mobile phone number page and binds the mobile phone number. Insert record.
2. The user logs in using PC WeChat for the second time and updates the last login time.
Scenario 4,
1. The user logs in using the mobile WeChat for the first time, and then reaches the binding mobile phone number page and binds the mobile phone number. Insert record.
2. The user logs in using the mobile WeChat for the second time and updates the last login time.
In short, no matter whether the user registers with a mobile phone number for the first time or logs in with WeChat on PC or WeChat on mobile, as long as it is the first time to log in, we will get the user’s mobile phone number, and then the mobile phone number will be used as the user’s mobile phone number. certificate.
3. Need to pay attention
We can use the following code to determine whether it is PC WeChat login or mobile WeChat login:
if(request()->isMobile()){ //移动端 }else{ //PC端 }
PC WeChat login and mobile WeChat login needs to be handled separately.
Finally
We still recommend that you use the official method. If you encounter certain situations, you can use the second method. In addition, for large websites, the PC and mobile terminals are separated separately, which is not suitable for adaptive website development.
The above is the detailed content of WeChat PC login and mobile login logic sharing. For more information, please follow other related articles on the PHP Chinese website!