Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat Small Program Service Platform Api


May 21, 2021 WeChat Mini Program Development Document


Table of contents


The service platform API

2.9.4

The APIs are all under the wx.serviceMarket object. The invokeService method can be configured for compatibility without relying on 2.9.4, as described in the Compatibility Configuration section at the bottom.

Starting with 2.11.1, the wx.serviceMarket API can also be used within the plug-in, which consumes the host's resources rather than the plug-in's resources at the time of the call.

invokeService

API provided by calling service provider

Involve

Receive an object, there is a field under the object:

Field name type Required Defaults illustrate
service string Yes Service Provider ID
api string Yes Yes Service API name
data Object no JSON data passed to the service API

return value

Returns a promise, if the call failed, then a Error object, if the call is successful, the resolve result is the object that is defined below:

Field name type Required Defaults illustrate
data Object String Yes

In Data, if the service provider requires some fields to be file URL, and at this time, you want to transfer the local file / large data to the URL as a field value, you can use the CDN method we provide to mark the corresponding value,WeChat automatically converts it to the CDN URL to the service provider when calling the service API.

error code

error code meaning
-1 Incorporate
-2 Call failure
-3 Logic failed
-6 APPID error
-7 API information error
-8 API information error
-10 API deduction failed
-11 Hit frequency

Sample code 1: OCR

When you select a picture from your phone, call the OCR service.The OCR service requires calling block pictures, and the way to receive pictures is the picture URL.The DATA structure of the OCR service requires the support of the Representative:

Field name type Required Defaults illustrate
img_url string Yes Picture URL
data_type number Yes Fixed 3, representing pictures in the form of URL
ocr_type number Yes OCR type, 1 for identification

The interface of the OCR needs to pass the picture URL. If you need to transfer the image selected by the phone to the URL, you can use the CDN method to mark the file path (or use any storage service and self-built storage service, you can also use cloud development.The cloud version storage service, but there is no CDN method to be convenient), and the following example is given:

// 选择图片
wx.chooseImage({
  count: 1,
  success: async function(res) {
    try {
      const invokeRes = await wx.serviceMarket.invokeService({
        service: 'wx79ac3de8be320b71',
        api: 'OcrAllInOne',
        data: {
          // 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
          img_url: new wx.serviceMarket.CDN({
            type: 'filePath',
            filePath: res.tempFilePaths[0],
          }),
          data_type: 3,
          ocr_type: 1
        },
      })

      console.log('invokeService success', invokeRes)
      wx.showModal({
        title: 'success',
        content: JSON.stringify(invokeRes),
      })
    } catch (err) {
      console.error('invokeService fail', err)
      wx.showModal({
        title: 'fail',
        content: err,
      })
    }
  },
  fail: function(res) {},
  complete: function(res) {},
})

Example code 2: Normal JSON protocol interface

Some services do not require a CDN secondary interface and can be called directly by JSON, for any of the following examples:

// 选择图片
wx.chooseImage({
  count: 1,
  success: function(res) {
    // 调用 OCR 服务
    wx.serviceMarket.invokeService({
      service: 'some_service_id',
      api: 'test',
      data: {
        type: 'x',
        name: 'y',
      },
    }).then(res => {
      console.log('invokeService success', res)
    }).catch(err => {
      console.error('invokeService fail', err)
    })
  },
  fail: function(err) {
    console.error(err)
  },
})

Cdn

Tags data that needs to be uploaded to a CDN file/large string and then converted to an HTTP URL must be used in invokeService.

The CDN method can receive three parameter types:

  • String
  • ArrayBuffer
  • The file path defines the object

When you define an object using a file path, the file content corresponding to the file path is automatically uploaded to the CDN and converted to a CDN URL when the service API is called, as follows:

Field Name Type Required Default Description Type String is the type that defines the object, and filePath FilePath String is the file path

into the ginseng

Receive an object with fields defined below:

The field name Type Required Description
type string Is Define the type of object, filePath is required
filePath string Is The file path

Compatibility configuration

The use of the wx.serviceMarket.invokeService API can be made unconstrained by the underlying library version by specifying the top-level field "servicemarket" in app.json/game.json: true, the applet will automatically contain compatible code for this API when the preview is released, and can also be used under 2.9.4. For use only on your phone, select the 2.9.4 base library for debugging in the tool.