# wx.getUserInfo(Object object)

with Promise style call: Not supported

User authorization: Need scope.userInfo。

Mini Program plugin: Support, need to Mini Program base library version no less than 2.3.1

When used in Mini Programs plug-ins, it needs to be called after obtaining user authorization in the user information feature page. Otherwise, return fail。 See for details User Information Function Page

Get user information.

# parameter

# Object object

attribute type Default values Required Introductions
withCredentials boolean no Whether to bring login information. when withCredentials for true Requires that it has previously been called wx.login And the login state has not expired, the returned data contains encryptedData, iv And other sensitive informationwhen withCredentials for false The logon state is not required, and the returned data does not contain encryptedData, iv Sensitive information.
long string in no Language for displaying user information
success function no Interface calls the successful callback function
fail function no Interface calls failed callback functions
complete function no Interface calls the end of the callback function (call success or failure will be executed)

object.long Legal value

value Introductions Minimum version
in English
zh_CN Simplified Chinese
zh_TW Traditional Chinese

# object.success callback

# parameter
# Object res
attribute type Introductions Minimum version
userInfo UserInfo User information object, does not contain openid And other sensitive information
rawData string Raw data string that does not include sensitive information for computing signatures
signature string Use sha1( rawData + sessionkey ) Gets a string used to verify user information, see Signature verification and encryption and decryption of user data
encryptedData string Encrypted data for complete user information, including sensitive data, see [Signature verification and encryption and decryption of user data]((signature#Encryption data decryption algorithm))
iv string Encryption algorithm initial vector, see [Signature verification and encryption and decryption of user data]((signature#Encryption data decryption algorithm))
cloudID string Cloud of sensitive data ID, OpenCloud DevelopmentThe Mini Program will return, can directly access open data through the cloud call, seeDirect access to open data for cloud calls 2.7.0

# Interface Adjustment Specification

The interface will be adjusted to optimize the user login experience, see User Information Interface Adjustment Specification

# sample code

// Must be called if the user has authorized it
wx.getUserInfo({
  success: function(res) {
    where userInfo = res.userInfo
    where nickName = userInfo.nickName
    where avatarUrl = userInfo.avatarUrl
    where gender = userInfo.gender //gender 0: Unknown, 1: Male, 2: Female 
    where province = userInfo.province
    where city = userInfo.city
    where country = userInfo.country
  }
})

Sensitive data can be obtained in two ways:

  1. Use Encryption data decryption algorithm
  2. Use Direct access to open data for cloud calls Get the open data as follows json Structure:
{
  "openId": "OPENID",
  "nickName": "NICKNAME",
  "gender": "GENDER",
  "city": "CITY",
  "province": "PROVINCE",
  "country": "COUNTRY",
  "avatarUrl": "AVATARURL",
  "unionId": "UNIONID",
  "watermark": {
    "appid":"APPID",
    "timestamp":"TIMESTAMP"
  }
}

# Mini Program user information component sample code

<!-- If you only present a user profile nickname, you can use the <open-data /> assembly -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>
<!-- Need to use button To authorize the login. -->
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">Authorized Login</button>
<view wx:else>Please upgrade the WeChat version</view>
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function() {
    // View Authorization
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // Authorized, you can directly call getUserInfo Get avatar nicknames
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo)
            }
          })
        }
      }
    })
  },
  bindGetUserInfo (and) {
    console.log(and.detail.userInfo)
  }
})