Custom APIs

更新时间:
复制 MD 格式

Miniapps support custom APIs. If the existing APIs do not meet your requirements, you can extend them. Miniapp APIs reuse the JavaScript API (JSAPI) plugin mechanism of the H5 container. This lets you extend APIs using the H5 container's plugin mechanism and then call them directly from your miniapp.

Custom API

To create custom APIs, see the H5 container documentation for custom JSAPIs:

Note

Custom miniapp APIs only support calls from a page to native code. Native code cannot send events to a page.

Call an API in a miniapp

To call a custom API in a miniapp, use the following method:

my.call(API, param, callback)

Where:

  • API: The name of the custom API.

  • param: The parameters for the API call.

  • callback: The callback method for the API execution.

The following code provides an example of how to call the rpc method:

    my.call('rpc', {
      operationType: 'com.test.mb1001',
      requestData: [{
        tranCode: 'MB1001',
        customerType: 0,
        customerId: 0,
        UnitType: '7A238BD3-A90B-4458-885E-129230BCF7F1',
        sessionId: 'zzzzzzzzzzzzzzzz',
        serverIP: 'zzzzzzzzzzzzzzzz',
        mobileNo: username,
        password,
        optionFlag: 3,
      }]
    }, (res) => {
        // do your business here.
    })

For more information about the differences between miniapp and H5 calls, see the H5 container JSAPI RPC document.

Unregister a custom event

To unregister a custom event, see Unregister a custom event.