This is the second day of my participation in the More text Challenge. For more details, see more text Challenge

background

When we use wechat small program, we developers have some functions need to show the user’s personal information, but we can not be in the user’s knowledge to secretly steal the user’s information, so it will give some criminals to create an opportunity. This requires us developers to notify users, to request users’ personal information abovementively, and to use users’ personal information with their authorization.

methods

The first small program to get user information is by calling wx.getUserInfo(). It can only be implemented by binding to a button.

<button opentype ="getUserInfo" bindgetUserInfo =" bindgetUserInfo "> </button> //js file Page({data: {canIUse: wx.canIUse('button.open-type.getUserInfo') }, onLoad: Function () {wx.getSetting({success (res){if (res.authSetting[' scope.userinfo '])){if (res.authsetting [' scope.userinfo ']); Wx. getUserInfo({success: function(res) { console.log(res.userInfo) } }) } } }) }, bindGetUserInfo (e) { console.log(e.detail.userInfo) } })Copy the code

And the above method in April 28 24 hours after the release of the new version of the small program, developers call wX. getUserInfo or will no longer pop up the popup window, directly return anonymous user personal information. So how do we get user information. The official wechat mini program of course gives the solution – using wX.getUserProfile ().

The getUserProfile interface is added

If you need to obtain a user’s personal information (profile picture, nickname, gender, and region), you can do so through the WX.getUserProfile interface, which is supported since version 2.10.4 of the base library. This interface only returns the user’s personal information and does not include the user’s id. The desc attribute in this interface (which states the purpose of obtaining the user’s personal information) will be displayed in the popover later, please be careful to fill in. Each time the developer obtains the user’s personal information through this interface, the user needs to confirm. The developer should properly keep the profile picture and nickname filled in quickly by the user to avoid repeated pop-ups.

Page({ data: { userInfo: {}, hasUserInfo: false, canIUseGetUserProfile: false, }, onLoad() { if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: True})}}, getUserProfile(e) {// It is recommended to use wx.getUserProfile to obtain user information. Wx.getuserprofile ({desc: 'to improve member profile ', // declare the purpose of obtaining user's personal information, will be displayed later in the popup, please fill in success carefully: (res) => { this.setData({ userInfo: res.userInfo, hasUserInfo: True})}})}, getUserInfo(e) {// getUserInfo is not recommended to use getUserInfo to obtain user information. This.setdata ({userInfo: e.datail.userinfo, hasUserInfo: true})},})Copy the code

From the above example we can get the user profile picture and other information very well. However, developers need to be careful when they call the interface to avoid annoying users with too many pop-ups.