Home >WeChat Applet >Mini Program Development >WeChat applet-getUserInfo callback introduction

WeChat applet-getUserInfo callback introduction

不言
不言Original
2018-06-22 15:53:532458browse

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 in need can refer to it

WeChat Mini Program-Detailed Example of GetUserInfo Callback

Foreword:

I won’t brag about how powerful the WeChat Mini Program is, after all. I wrote it to talk directly 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 perform a series of subsequent 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 an 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 execute one of the methods randomly.

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

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use switchTab in WeChat applet development

How to use the UI framework in WeChat applet development

Usage of container view in WeChat applet development

The above is the detailed content of WeChat applet-getUserInfo callback introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn