Taro.getUserInfo(option)
Gets user information.
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).
Type
(option?: Option) => Promise<SuccessCallbackResult>
Parameters
Option
Property | Type | Required | Description |
---|---|---|---|
lang | "en" | "zh_CN" | "zh_TW" | No | The language of the displayed user information |
withCredentials | boolean | No | Indicates whether to include login status information. When withCredentials is true, Taro.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. |
complete | (res: any) => void | No | The callback function used when the API call completed (always executed whether the call succeeds or fails) |
fail | (res: any) => void | No | The callback function for a failed API call |
success | (res: Result) => void | No | The callback function for a successful API call |
SuccessCallbackResult
Property | Type | Description |
---|---|---|
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. |
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. |
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. |
userInfo | UserInfo | User information object, excluding openid and other sensitive information. |
errMsg | string | Call result |
Sample Code
// It must be called after user authorization is obtained.
Taro.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. 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
}
}
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
Taro.getUserInfo | ✔️ |