May 19, 2021 WeChat Mini Program Development Document
To get user information, withCredentials is true when you need to call the wx.login interface first
OBJECT parameter description:
The name of the argument | Type | Required | Description | The lowest version |
---|---|---|---|---|
withCredentials | Boolean | Whether | Whether to bring login status information | 1.1.0 |
Lang | String | Whether | Specify the language in which the user information is returned, zh_CN Chinese Simplified, zh_TW Chinese Traditional, en English | 1.4.0 |
success | Function | Whether | The interface calls a successful callback function | |
fail | Function | Whether | The interface calls the failed callback function | |
complete | Function | Whether | Callback function at end of interface call (call succeeds, fails are executed) |
Note: When withCredentials is true, wx.login has been called before and the login state has not expired, the data returned will contain sensitive information such as encryptedData, iv, and when withCredentials is false, no login status is required, and the data returned does not contain sensitive information such as encryptedData, iv.
Success returns a description of the parameters:Parameters | Type | Description |
---|---|---|
userInfo | OBJECT | User information objects that do not contain sensitive information such as openid |
rawData | String | The original data string, which does not include sensitive information, is used to calculate the signature. |
signature | String | Use sha1 (rawData and sessionkey) to get a string that checks user information and refers to the document signature. |
encryptedData | String | Encrypt data including complete user information, including sensitive data, see Encryption data decision algorithm |
iv | String | Initial vector of an encryption algorithm, see Encryption data decision algorithm |
Sample code:
wx.getUserInfo({
success: function(res) {
var userInfo = res.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var gender = userInfo.gender //性别 0:未知、1:男、2:女
var province = userInfo.province
var city = userInfo.city
var country = userInfo.country
}
})
EncryptedData decrypts the following JSON structure, see Encryption data decision algorithm
{
"openId": "OPENID",
"nickName": "NICKNAME",
"gender": GENDER,
"city": "CITY",
"province": "PROVINCE",
"country": "COUNTRY",
"avatarUrl": "AVATARURL",
"unionId": "UNIONID",
"watermark":
{
"appid":"APPID",
"timestamp":TIMESTAMP
}
}
tip
:
wx.getUserInfo
authorization, so be compatible with scenarios in which the user refuses authorization.
If the developer has multiple mobile apps, website apps, and public accounts (including applets), you can distinguish user uniqueness by unionid, because the user's unionid is unique as long as it's a mobile app, website app, and public account (including a small program) under the same WeChat open platform account. In other words, the same user, unionid is the same for different applications under the same WeChat open platform.
Apps, public numbers, and small programs of the same subject under the same WeChat open platform, if the user has paid attention to the public number, or has logged into the app or public number, then when the user opens the small program, the developer can obtain the user's UnionID directly through
wx.login
without the user's re-authorization.
WeChat open platform binding small program process
Prerequisite: WeChat open platform account must have completed the developer qualification
Developer Qualification Process:
Sign in to WeChat Open Platform (open.weixin.qq.com) - Account Center - Developer Qualification
Binding process:
Sign in to WeChat Open Platform (open.weixin.qq.com) - Management Center - Public Account - Binding Public Accounts