# wx.getUserInfo(Object object)
User Authorization is required for
scope.userInfo
before this API is called.
Gets user information.
# Parameters
# Object object
Attribute | Type | Default | Required | Description |
---|---|---|---|---|
withCredentials | boolean | No | Indicates whether to include login status information. When withCredentials is true, wx.login must be called previously and the login status must be effective. In this case, sensitive information such as encryptedData and iv is returned. When withCredentials is false, the login status is not required, and sensitive information such as encryptedData and iv is not returned. | |
lang | string | en | No | The language of the displayed user information |
success | function | No | The callback function for a successful API call | |
fail | function | No | Callback function for failed API call | |
complete | function | No | Callback function used when API call completed (always executed whether call succeeds or fails) |
Valid values of object.lang
Value | Description | Minimum Version |
---|---|---|
en | English | |
zh_CN | Simplified Chinese | |
zh_TW | Traditional Chinese |
# object.success callback function
# Parameters
# Object res
Property | Type | Description | Minimum Version |
---|---|---|---|
userInfo | UserInfo | User information object, excluding openid and other sensitive information. | |
rawData | string | Raw data string that excludes sensitive information and is used to calculate signatures. | |
signature | string | The string generated with SHA-1 (rawData + sessionkey), which is used to verify the user information. For details, see signature, verification, encryption, and decryption of user data. | |
encryptedData | string | The complete encrypted user data, including the sensitive data. For details, see signature, verification, encryption, and decryption of user data. | |
iv | string | The initial vector of the encryption algorithm. For details, see signature, verification, encryption, and decryption of user data. | |
cloudID | string | The Cloud ID corresponding to sensitive data. It is returned only in Mini Programs for which Cloud Base is enabled. The open data can be directly obtained via cloud call. See details. | 2.7.0 |
# API Adjustment Description
If this API is called after user authorization is obtained, the user information is returned. If this API is called without user authorization, the authorization pop-up window will not appear, and the fail callback is directly executed. (For details, see Announcement).
# Sample Code
// It must be called after user authorization is obtained.
wx.getUserInfo({
success: function(res) {
var userInfo = res.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var gender = userInfo.gender //Gender: 0 - Unknown; 1 - Male; 2 - Female
var province = userInfo.province
var city = userInfo.city
var country = userInfo.country
}
})
There are two ways to get sensitive data. One is to use the [Decryption Algorithm for Encrypted Data] ((open-ability/signature#decryption algorithm for encrypted data)). The acquired open data has the following json structure:
{
"openId": "OPENID",
"nickName": "NICKNAME",
"gender": GENDER,
"city": "CITY",
"province": "PROVINCE",
"country": "COUNTRY",
"avatarUrl": "AVATARURL",
"unionId": "UNIONID",
"watermark": {
"appid":"APPID",
"timestamp":TIMESTAMP
}
}
# Sample Code for the Mini Program User Information Components
<!-- To display only the user's profile photo and alias, use the <open-data /> component. -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>
<!--Call button for login authorization -->
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">Authorize the login</button>
<view wx:else>Upgrade your WeChat</view>
Page({
data: {
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function() {
// View whether user authorization is obtained
wx.getSetting({
success (res){
if (res.authSetting['scope.userInfo']) {
// User authorization is obtained. You can directly call the `getUserInfo` API to get the profile photo and alias.
wx.getUserInfo({
success: function(res) {
console.log(res.userInfo)
}
})
}
}
})
},
bindGetUserInfo (e) {
console.log(e.detail.userInfo)
}
})