Home > Article > Web Front-end > Detailed explanation of WeChat applet getUserInfo callback
This article mainly introduces the relevant information about the detailed explanation of the WeChat applet-getUserInfo callback example. I hope this article can help everyone understand and master this part of the content. Friends who need it can refer to it. I hope it can help everyone.
WeChat applet-getUserInfo callback example detailed explanation
Foreword:
I won’t brag about WeChat applet here How powerful the program is, after all, it was not written by me. Let me just talk about the strange bugs I encountered while developing small programs.
getUserInfo is called multiple times:
Many developers should have used wx.getUserInfo, which is used to obtain basic user information. The premise is that user authorization is required. If the user If there was no authorization before, a prompt box will pop up as follows:
After clicking Allow, we can get the user information and then perform a series of operations. This all looks so good, but during a certain run, after clicking Allow Authorization, the program did not execute as we expected. Finally, I found that the wx.getUserInfo method was also called in other places, and only one of the callbacks for successful authorization was executed. Assume the following code:
Conclusion:
1. If the user is authorized: A B C will be printed.
2. The user is not authorized: After clicking Allow, one of the three numbers A, B, and C will be printed randomly.
Of course it is best not to call the wx.getUserInfo method in multiple places at the same time, otherwise you will have a headache when encountering this problem. My view on this issue is that calling the getUserInfo method multiple times will only generate one instance of the pop-up window, so when you click Allow, it is normal to only execute the callback once, but it is strange to randomly execute one of the methods.
Solution:
1. Do not call the wx.getUserInfo method at the same time. There are very few situations where it is necessary to call it at the same time.
2. Encapsulate a getUserInfo method, store the incoming callback method into an array, and clear the array after traversing the calls in the authorization success/failure callback.
The following is the test environment:
WeChat developer tool version: v1.01.170925
Basic library version: 1.3.0
Related recommendations:
javascript - decryption of encryptData in the mini program wx.getUserInfo
How to implement the WeChat mini program to return multi-level pages
Detailed explanation of WeChat mini program template
The above is the detailed content of Detailed explanation of WeChat applet getUserInfo callback. For more information, please follow other related articles on the PHP Chinese website!