my.getOpenUserInfo
Get the basic information about a user. This feature requires the user to deliberately trigger to activate the function. This function is not directly called by the API but rather waits for when the user has activated it by clicking a <button>
component. If the Mini Program wants to get userId, please call my.getAuthCode
.
For more information, please refer to the obtain basic member information.
Use Attention
You need to set the value of the <button>
component open-type
to getAuthorize
and set the value of the scope
to userInfo
. After the user clicks the authorization button, the Mini Program can get the user information returned by the my.getOpenUserInfo
JSAPI.my.getOpenUserInfo
will send a network request to the server to obtain user information, so it may be take some time before the callback function invoked.
Parameters
Name | Type | Required | Description |
success | Function | No | Callback function upon call success. |
fail | Function | No | Callback function upon call failure. |
complete | Function | No | Callback function upon call completion (to be executed upon either call success or failure). |
Success Callback Function
The incoming parameter is of the Object type with the following attributes:
Name | Type | Required | Description |
code | String | NO | The result code. |
msg | String | NO | The result message. |
avatar | String | NO | User avatar image url. |
nickName | String | NO | User nickName. |
gender | String | NO | User gender. "m" is male,"f" is female. |
countryCode | String | NO | The code of the country where user is located. it should follow ISO 3166-1 alpha-2 code standard, such as 'US', 'SG'. |
province | String | NO | The province where user is located. |
city | String | NO | The city where user is located. |
These fields are returned every time, but it will be an empty string if the app does not return the related information.
The maximum length of these fields are 128 bytes except avatar,the maximum length of avatar is 2048 bytes.
Sample Code
<!-- .axml -->
<button
a:if="{{canIUseAuthButton}}"
open-type="getAuthorize"
onGetAuthorize="onGetAuthorize"
onError="onAuthError"
scope='userInfo'>
</button>
Button Property Description
Name | Description |
open-type | getAuthorize (Must be this value). |
scope | userInfo (Must be this value). |
onGetAuthorize | Authorization success callback (The Mini Program can call my.getOpenUserInfo to get information in this callback). |
onError | Authorization failure callback (Including user rejection and system exceptions). |
Get User Basic Information
After the user clicks the consent, the user basic information can be obtained through my.getOpenUserInfo().
// .js
onGetAuthorize(res) {
my.getOpenUserInfo({
fail: (res) => {
},
success: (res) => {
let userInfo = JSON.parse(res.response).response
}
});
}
Return Res Object In the Success Callback
- An example of a successfully res object returned is as follows:
{
"response": "{\"response\": {\"code\": \"10000\",\"msg\": \"Success\",\"countryCode\": \"code\",\"gender\": \"f\",\"nickName\": \"XXX\",\"avatar\": \"https://cdn/images/partner/XXXXXXXX\",\"city\": \"city\",\"province\": \"province\"}}"
}
- If the function package of "Get Basic User Information" is not connected, the format example of returned res message is as follows:
{
"response":"{\"response\":{\"code\":\"40006\",\"msg\":\"Insufficient Permissions\",\"subCode\":\"isv.insufficient-isv-permissions\",\"subMsg\": \"Insufficient permissions\"}}"
}