Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to obtain user Information by WeChat Mini Programs developer

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of WeChat Mini Programs development how to obtain user information, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that everyone will gain something after reading this WeChat Mini Programs development article on how to obtain user information. Let's take a look.

In the first, direct authorization acquisition (on the same page):

First of all, write the button to get user information in the WXML file of a page of Wechat program.

Log in to {{userinfor.nickName}}

Then write the logical structure of the specific click event on the js page.

Data: {userinfor: {}, / / the user stores the obtained user information}, getUserInfor (e) {wx.getUserProfile ({desc:'to improve the user's personal information' / / the displayed message success: (res) = > {console.log (res.userInfo) / / print the user information to the console this.setData ({userinfor:res.userInfo}) / / Save the information to the defined container}})}

The second way is to obtain user information from different pages:

The main idea in the second is to get the user information first, then store it in the cache, and then jump to other pages to get the data in the cache.

First, we need to go from page A to page B.

A page WXML code

Enter the login page {{userinfor.nickName}}

A page JS page code

/ / data: {userinfor: {},}, goto () {wx.navigateTo ({url:'.. / text/text',})}

B page WXML page code

Log in

B page JS page code

Data: {}, getUserInfor (e) {wx.getUserProfile ({desc: 'used to obtain user information', success: (res) = > {const userinfor = res.userInfo / / write user data to the cache wx.setStorageSync ('UserInfo', userinfor) / / Jump back to the previous page wx.navigateBack ({delta:1})}})}

When we click the B page login button and allow access to user information, we can find that the console cache page will pop up a key value with a key value of UserInfo, which means that we have stored the user's information in the cache, and then there is how to take the information out of the cache. Next, we go to page A to write a method to get the data in the cache.

Add the onShow method to the A page JS page, or add the code from the following methods to the original onShow method

Data: {userinfor: {},}, onShow () {const userinfor = wx.getStorageSync ('UserInfo') this.setData ({userinfor})}

At this point, the user data is successfully written to the cache to read data from other pages.

This is the end of the article on "how to get user Information by WeChat Mini Programs developer". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "WeChat Mini Programs development how to obtain user information". If you still want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report