# API
The Weixin Mini Program development framework provides a rich WeChat native API that can easily activate the capabilities offered by WeChat, such as access to user information, local storage, payment functions, and so on.Please refer to the API documentation for details.
In general, the Weixin Mini Program API has the following types:
# Event Listening API
We agree that APIs starting withonare used to listen for an event to trigger, such as: wx.onSocketOpen , wx.onCompassChange et al.
This type of API accepts a callback function as a parameter, which is called when an event is triggered, and the relevant data is passed in as a parameter.
Code examples
wx.onCompassChange(function (res) {
console.log(res.direction)
})
# Synchronization API
We agree that all APIs ending withSyncare synchronization APIs, such as wx.setStorageSync , wx.getSystemInfoSync etc.In addition, there are some other synchronization APIs, such as wx.createWorker , wx.getBackgroundAudioManager Etc. See instructions in the API documentation for details.
The result of the execution of the synchronization API can be obtained directly from the function return value, throwing an exception if the execution goes wrong.
Code examples
try {
wx.setStorageSync('key', 'value')
} catch (e) {
console.error(e)
}
# Asynchronous API
Most APIs are asynchronous APIs, such as wx.request , wx.login , etc.This type of API interface typically accepts aObjectparameter that supports specifying the following fields as needed to receive the result of the interface call:
Object parameter explaination
| Parameter Name | type | Required to fill in | Introductions |
|---|---|---|---|
| success | function | no | The interface calls a successful callback function |
| fail | function | no | A callback function that fails to call an interface |
| complete | function | no | The interface calls a callback function that ends (both successful and failed calls are executed) |
| Other | Any | - | Other parameters defined by the interface |
Parameters for callback functions
success,fail,completeThe function call passes a Object] type argument with the following fields:
| attribute | type | Introductions |
|---|---|---|
| errMsg | string | Error message, if the call successfully returns${apiName}: ok |
| errCode | number | Error code, only part of the API support, specific meaning please refer to the corresponding API documentation, success is0. |
| Other | Any | Other data returned by the interface |
The execution result of the asynchronous API needs to be obtained through the corresponding callback function passed in the parameter of typeObject.Some asynchronous APIs also have fallback values, which can be used to implement richer functionality, such as wx.request , wx.connectSocket etc.
Code examples
wx.login({
success(res) {
console.log(res.code)
}
})
# Asynchronous API returns Promise
Since version {% version (2.10.2)%} of the base library, the asynchronous API supports callback and promise calls.When the interface parameter Object does not contain success / fail / complete, it will return a promise by default, otherwise it will still be executed by callback, no return value.
# Note
- Some interfaces such as
downloadFile,request,uploadFile,connectSocket,createCamera( MiniGame )They have their own return values, and their promisify needs to be encapsulated by the developer. - When there is no callback parameter, the asynchronous interface returns a promise.If the function call fails to enter the fail logic, an error will occur
Uncaught (in promise), developers can catch by catching. - Wx.onUnhandledRejection can listen for unhandled Promise rejection events.
Code examples
// Callback form call
wx.chooseImage({
success(res) {
console.log('res:', res)
}
})
// Promise-Form Call
wx.chooseImage().then(res => console.log('res: ', res))
# Cloud Development API
When you open and use WeChat cloud development , you can use the cloud development API to directly call the cloud function on the Weixin Mini Program end.
Code examples
wx.cloud.callFunction({
// 云函数名称
name: 'cloudFunc',
// 传给云函数的参数
data: {
a: 1,
b: 2,
},
success: function(res) {
console.log(res.result) // 示例
},
fail: console.error
})
// In addition, cloud functions also support promise form calls