Open Interface
Login
login
TIP
The API usage is as follows: wx.login(Object object)
- Function description: Call the interface to obtain login credentials, and then exchange the credentials for user login status information.
TIP
- The v2 version (only supported by public clouds) follows the Luffa standard, and you can refer to the practical tutorial instructions. It can be called directly in the IDE (minimum supported version: 2.1.111).
- The v1 version needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content. In the IDE, you can use the mock panel to mock the return value.
- For version control rules, please refer to apiVersion Configuration.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- object.success callback function parameters: Object res
| Properties | Type | Description |
|---|---|---|
| code | string | User login credentials (valid for five minutes). Developers need to call the call in the server background jscode2session , and use code to exchange for openid, session_key and other information |
- object.fail callback function parameter: Object err
| Properties | Type | Description |
|---|---|---|
| errMsg | string | Error message |
| errno | Number | errno error code, for detailed description of the error code, refer to server error code |
- Sample code:
wx.login({
success(res) {
if (res.code) {
// Initiate a network request
wx.request({
url: "https://example.com/onLogin",
data: {
code: res.code,
},
});
} else {
console.log("Login failed!" + res.errMsg);
}
},
});checkSession
TIP
The API usage is as follows: wx.checkSession(Object object)
- Function description: Check whether the login state is expired.
TIP
- The v2 version (only supported by public clouds) follows the Luffa standard, and you can refer to the practical tutorial instructions. It can be called directly in the IDE (minimum supported version: 2.1.111).
- session_key is unique. When using the applet, the same user has only one valid session_key at the same time.
- The user login state obtained through the wx.login interface has a certain timeliness. Developers can call the wx.checkSession interface to check whether the current user login state is valid.
- The verification object of wx.checkSession is the login state session_key corresponding to the last code operation. A successful call indicates that the session_key has not expired, and a failed call indicates that the session_key has expired.
- After the login state expires, the developer can call wx.login again to obtain a new user login state.
- The v1 version needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content. You can use the mock panel in the IDE to mock the return value.
- For version control rules, please refer to apiVersion Configuration.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- Sample code:
wx.checkSession({
success() {
// The session_key has not expired and remains valid for this lifecycle.
},
fail() {
// The session_key has expired, and the login process needs to be executed again.
wx.login(); // Log in again.
},
});Account information
TIP
Account information related API Mini Program supports, but not supported by mini games
getAccountInfoSync
TIP
The API usage is as follows:Object wx.getAccountInfoSync()
- Functional description: Get the current account information. The online mini program version number can only be obtained in the official version of the mini program, and cannot be obtained in the development version and trial version.
- Return value: Object,Account information
| Properties | Type | Description |
|---|---|---|
| miniProgram | Object | Mini program account information |
- miniProgram structure attribute
| Structure properties | Type | Description |
|---|---|---|
| appId | string | Mini program appId |
| envVersion | string | Mini program version, legal value is: develop:Development version trial:Experience version release:Official version |
| version | string | Online mini program version number |
User information
TIP
Account information related API Mini Program supports, but not supported by mini games
getUserProfile
TIP
The API usage is as follows:wx.getUserProfile(Object object)
Function description: IDE does not currently support it. This API needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content
You can use the mock panel in the IDE to mock the return valueGet user information. It can only be called after the page generates a click event (for example, in the callback of bindtap on the button). The authorization window will pop up for each request, and userInfo will be returned after the user agrees. This interface is used to replace wx.getUserInfo, see User Information Interface Adjustment Instructions
**Return value:**Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| lang | string | en | No | The language for displaying user information, legal value is: en:English zh_CN: Traditional Chinese |
| desc | string | - | Yes | State the purpose of obtaining user personal information, no more than 30 characters |
| success | Function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
getUserInfo
TIP
The API usage is as follows: wx.getUserInfo(Object object)
Functional description: IDE currently does not support it. This API needs to be debugged with the host client. The content returned on the real machine is provided by the host client. The host's return value can follow the Luffa standard or customize the return content.
In the IDE, you can use the mock panel to mock the return value.Obtain user information, user authorization scope.userInfo is required during use.
Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| lang | string | en | No | The language for displaying user information, legal value is: en:English zh_CN: Traditional Chinese |
| success | Function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- object.success callback function parameters: Object res
| Properties | Type | Description |
|---|---|---|
| userInfo | UserInfo | User information object |
- Example
- Mini program user information component example code (user authorization)
<!-- If only displaying user avatar and nickname, the <open-data /> component can be used -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>
<!-- The button must be used for login authorization -->
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">Login authorization</button>
<view wx:else>Please upgrade the host client version.</view>Page({
data: {
canIUse: wx.canIUse("button.open-type.getUserInfo"),
},
onLoad: function () {
// Check if the user has already authorized.
wx.getSetting({
success(res) {
if (res.authSetting["scope.userInfo"]) {
// Already authorized, can directly call getUserInfo to get user profile photo and nickname.
wx.getUserInfo({
success: function (res) {
console.log(res.userInfo);
},
});
}
},
});
},
bindGetUserInfo(e) {
console.log(e.detail.userInfo);
},
});- Sample code:
// Must be called when the user has already authorized.
wx.getUserInfo({
success: function (res) {
var userInfo = res.userInfo;
var nickName = userInfo.nickName;
var avatarUrl = userInfo.avatarUrl;
},
});userInfo
- Functional description: User information
- Parameters and descriptions:
| Properties | Type | Description |
|---|---|---|
| nickName | string | User nickname |
| avatarUrl | string | The URL of the user's avatar image. The last value of the URL represents the square avatar size (there are 0, 46, 64, 96, 132 values |
| gender | number | User gender. No longer returned, legal values 0: Unknown 1: Male 2: Female |
| country | string | User's country. No more returns |
| province | string | User's region. No more returns |
| city | string | User's city. No more returns |
| language | string | Display the language used by country, province, and city. Forced to return 'zh_TW', legal values en:English zh_TW: Traditional Chinese |
Settings
AuthSetting
- Functional description: User authorization setting information.
- Parameters and descriptions:
| Properties | Description |
|---|---|
| boolean scope.userLocation | Whether to authorize geographic location, corresponding interface wx.getLocation, Whether to authorize geographic location, corresponding interface wx.chooseLocation |
| boolean scope.writePhotosAlbum | Whether to authorize saving to album wx.saveImageToPhotosAlbum |
| boolean scope.camera | Whether to authorize camera, corresponding to <camera /> component |
| boolean scope.addFriend | Allow to be added as a friend, actively call wx.authorize Interface for authorization |
getSetting
TIP
The API usage is as follows: wx.getSetting(Object object)
- Functional description: Get the user's current settings.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- object.success callback function parameters: Object res。
| Properties | Type | Description |
|---|---|---|
| authSetting | AuthSetting | User authorization result |
- Sample code:
wx.getSetting({
success(res) {
console.log(res.authSetting);
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
},
});openSetting
TIP
The API usage is as follows: wx.openSetting(Object object)
- Functional description: Call up the client applet settings interface, return the operation result set by the user, after the user clicks, you can jump to open the settings page and manage the authorization information.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- object.success callback function parameters: Object res。
| Properties | Type | Description |
|---|---|---|
| authSetting | AuthSetting | User authorization result |
wx.openSetting({
success(res) {
console.log(res.authSetting);
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
},
});wx.openSetting({
success(res) {
console.log(res.authSetting);
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
},
});Biometric authentication
TIP
Biometric authentication related API Mini Program supports, but not supported by mini games
checkIsSoterEnrolledInDevice
TIP
The API usage is as follows: wx.checkIsSoterEnrolledInDevice(Object object)
- Functional description: Verify whether the biometric information is entered in the device.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- object.success callback function parameters: Object res。
| Properties | Type | Description |
|---|---|---|
| isEnrolled | boolean | Whether the information has been entered |
| errMsg | string | Error message |
- Sample code:
wx.checkIsSoterEnrolledInDevice({
success(res) {
console.log(res.isEnrolled);
},
});checkIsSupportSoterAuthentication
TIP
The API usage is as follows: wx.checkIsSupportSoterAuthentication(Object object)
- Functional description: Check whether the machine supports biometric authentication, callback success if supported, callback fail if not supported.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- Sample code:
wx.checkIsSupportSoterAuthentication({
success() {
// Support biometric authentication
},
});startSoterAuthentication
TIP
The API usage is as follows: wx.startSoterAuthentication(Object object)
- Functional description: Start biometric authentication.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| authContent | string | '' | No | Verification description, that is, the dialog prompt content displayed on the interface during the identification process |
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- Sample code:
wx.startSoterAuthentication({
authContent: "Unlock biometric authentication",
success() {
// Authentication successful
},
});Authorization
TIP
The API usage is as follows: wx.authorize(Object object)
- Functional description: Initiate an authorization request to the user in advance. After the call, a pop-up window will immediately ask the user whether to agree to authorize the applet to use a certain function or obtain certain user data, but the corresponding interface will not be actually called. If the user has agreed to the authorization before, no pop-up window will appear and the result will be returned directly as success.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| scope | string | - | Yes | Scopes that need to obtain permissions See scope for details |
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- Sample code:
// You can query whether the user has authorized the scope of "scope.record" using wx.getSetting
wx.getSetting({
success(res) {
if (!res.authSetting["scope.record"]) {
wx.authorize({
scope: "scope.record",
success() {
// Once the user has granted permission for the mini program to use the recording feature, subsequent calls to the wx.startRecord API will not prompt the user for permission again.
wx.startRecord();
},
});
}
},
});Subscription Message
requestSubscribeMessage
TIP
The API usage is as follows: wx.requestSubscribeMessage(Object object)
- Functional description: Call up the client mini program subscription message interface and return the operation result of the user's subscription message. The user's subscription status for the relevant template message can be obtained through the wx.getSetting Interface
TIP
- Only supported by public clouds.
- One-time template id and permanent template id cannot be used at the same time.
- In one authorization call, the template title corresponding to each tmplId cannot be the same. If the same one appears, only one is retained.
- Parameters and descriptions: Object object。
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| tmplIds | Array | - | Yes | A collection of message template ids that need to be subscribed. A maximum of 3 messages can be subscribed in one call. The message template id is configured in the console [Mini Program Management-Subscribe Message]. The template title corresponding to each tmplId needs to be different, otherwise it will be filtered. |
| success | function | - | No | Callback function for successful interface call |
| fail | Function | - | No | Callback function for failed interface call |
| complete | Function | - | No | Callback function for interface call end (will be executed for both successful and failed calls) |
- Sample code:
| Properties | Type | Description |
|---|---|---|
| errMsg | String | When the interface call is successful, the errMsg value is 'requestSubscribeMessage:ok' |
| [TEMPLATE_ID: string] | String | [TEMPLATE_ID] is a dynamic key, that is, the template id, and the values include 'accept', 'reject', 'ban', and 'filter' 'accept':Indicates that the user agrees to subscribe to the template message corresponding to the id 'reject':Indicates that the user refuses to subscribe to the template message corresponding to the id 'ban':Indicates that it has been banned by the background 'filter':Indicates that the template has been filtered by the background because the template title has the same name For example, { errMsg: "requestSubscribeMessage:ok", zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: "accept"} means the user agrees to subscribe to the message zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE |
- object.fail callback function parameter: Object err
| Properties | Type | Description |
|---|---|---|
| errMsg | String | Interface call failure error message |
| errno | Number | Interface call failure error code |
- Error code
| errCode | errMsg | Description |
|---|---|---|
| 10001 | TmplIds can't be empty | Parameters are empty |
| 10002 | Request list fail | Network problem, message list request failed |
| 10003 | Request subscribe fail | Network problem, subscription request failed to send |
| 20001 | No template data return, verify the template id exist | No template data, usually template ID The message does not exist or does not correspond to the template type. |
| 20002 | Templates type must be same | The template message type has both one-time and permanent. |
| 20003 | Templates count out of max bounds | The number of template messages exceeds the upper limit. |
| 20004 | The main switch is switched off | The user has turned off the main switch and cannot subscribe. |
| 20005 | This mini program was banned from subscribing messages | The mini program is banned. |
| 20013 | Reject DeviceMsg Template | Subscribing to device messages through this interface is not allowed. |
- Sample code:
wx.requestSubscribeMessage({
tmplIds: [''],
success: (res) => {
console.log('requestSubscribeMessage===success', res)
}
})