Get User Information - WeChat Mini Program

open-type in the button component

The open-type in <button open-type=""></button> is used to provide WeChat open capability. open-type has the following legal values:

contact, open the customer service session, if the user returns to the applet after clicking the message card in the session, the specific information can be obtained from the bindcontact callback.
share, which triggers users to retweet.
getPhoneNumber, to obtain the user's mobile phone number, user information can be obtained from the bindgetphonenumber callback.
getUserInfo, to obtain user information, user information can be obtained from the bindgetuserinfo callback.
launchApp, open the APP.
openSetting, open the authorization setting page.
feedback to open the Feedback page.
 

<!--index.wxml-->
<button open-type="contact" bindcontact="contact">contact </button>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">getPhoneNumber</button>
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">getUserInfo</button>
// index.js

Page({
  contact:function(e){
    console.log(e.detail);
  },
  getPhoneNumber:function(e){
    console.log(e.detail);
  },
  getUserInfo:function(e){
    console.log(e.detail.userInfo);
  }
})

 Use the click button to prompt the user for authorization, and the user information can only be obtained after the user authorizes and agrees.

<!--index.wxml-->
<view class="container">
  <button wx:if="{
   
   {!hasUserInfo}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">获取头像昵称</button>
  <view class="userinfo" wx:else>
    <image src="{
   
   {userInfo.avatarUrl}}"></image>
    <text>{
   
   {userInfo.nickName}}</text>
  </view>
</view>
// index.js
Page({
  data:{
    hasUserInfo:false,
    userInfo:{}
  },
  getUserInfo:function(e){
    console.log(e);
    console.log(e.detail.userInfo);
    if(e.detail.userInfo){
      this.setData({
        userInfo:e.detail.userInfo,
        hasUserInfo:true
      })
    }
  }
})

  getUserInfoLittle user information is given. This is because from April 13, 2021, getUserInfopop-up windows will no longer pop up, and anonymous user personal information will be returned directly,

WeChat offers getUserProfile()to replacegetUserInfo()

Interface getUserProfile

Interface getUserProfile

The interface getUserProfileis used to replace getUserInfoto obtain user information.
It can only be called after a click event is generated on the page. An authorization window will pop up for each request, and the user will return after agreeing userInfo. See the example below for details.

<!--index.wxml-->
<view class="container">
  <button wx:if="{
   
   {!hasUserInfo}}" bindtap="getUserProfile">获取头像昵称</button>
  <view class="userinfo">
    <image src="{
   
   {userInfo.avatarUrl}}"></image>
    <view>{
   
   {userInfo.nickName}}</view>
  </view>
</view>
// index.js
Page({
  data:{
    hasUserInfo:false,
    userInfo:{}
  },
  getUserProfile:function(){
    wx.getUserProfile({
      desc: '获取用户信息',
      success:(res) => {
        console.log(res.userInfo);
        this.setData({
          hasUserInfo:true,
          userInfo:res.userInfo
        })
      }
    })
  }
})

 

The parameter res of the success callback function of getUserProfile() contains the following attributes,

errMsg, error message.
userInfo, user information object, does not contain sensitive information such as openid.
rawData, the raw data string that does not contain sensitive information, is used to calculate the signature.
signature, use sha1(rawData+session_key) to get a string, which is used to verify user information.
encryptedData, encrypted data of complete user information including sensitive data.
iv, the initial vector of the encryption algorithm.

If the foreground of the applet needs to send user information to the background of the applet, it can be wx.request()achieved by


View authorization results 

wx.getSetting Gets the user's current settings. Only the permissions that the applet has requested from the user will appear in the return value. Look at a small example.

<!--index.wxml-->
<button bindtap="getSetting">查看授权结果</button>
// index.js
Page({
  getSetting:function(){
    wx.getSetting({
      success:(res) => {
        console.log(res.authSetting);
        if(res.authSetting["scope.userInfo"]){
          wx.getUserInfo({
            success: (res) => {
              console.log(res.userInfo);
            }
          })
        }
      }
    })
  }
})

res.authSettingcontains

  • scope.address,mailing address
  • scope.invoice,bill
  • scope.invoiceTitle,Invoice
  • scope.userInfo,User Info

The component  open-data is used to display WeChat open data. The so-called "open" data refers to data that can be displayed without user authorization, such as the user's avatar, nickname, gender, etc.

The type of open data has the following legal values,
groupName
userNickName, user nickname
userAvatarUrl, user avatar
userGender, user gender
userCity, user city
userProvince, user province
userCountry, user country
userLanguage, user language
 

开放数据的类型,有如下合法值,
groupName
userNickName,用户昵称
userAvatarUrl,用户头像
userGender,用户性别
userCity,用户所在城市
userProvince,用户所在省份
userCountry,用户所在国家
userLanguage,用户的语言

Guess you like

Origin blog.csdn.net/m0_46965984/article/details/124320405