# invoke

当小程序中调用 [`wmpf.Channel.invoke`](/doc/oplatform/Miniprogram_Frame/api/mini_program/invoke) 时，Client 端需要使用 insert 操作完成异步调用。

## 传入参数

通过 ContentValues 传递

| 参数          | 类型   | 必填 | 说明                                                         |
| :------------ | :----- | :--- | :----------------------------------------------------------- |
| \_\_invoke_id__ | String | 是   | 调用 id ，用于后续获取对应的返回数据，每次调用的 id 都不一样 |
| command       | String | 是   | 小程序经 wmpf 透传给第三方 app 的指令名                      |
| data          | String | 否   | 小程序经 wmpf 透传给第三方 app 的指令参数                    |

## 返回参数

通过 insert 操作通知及 ContentValues 传递

| 参数          | 类型   | 必填 | 说明                                      |
| :------------ | :----- | :--- | :---------------------------------------- |
| \_\_invoke_id__ | String | 是   | 调用 id ，作为通知回调的凭据              |
| command       | String | 是   | 小程序经 wmpf 透传给第三方 app 的指令名   |
| data          | String | 否   | 小程序经 wmpf 透传给第三方 app 的指令参数 |

## 示例代码

```kotlin
override fun insert(p0: Uri, p1: ContentValues?): Uri? {
  when(sURIMatcher.match(p0)) {
    // 异步方法调用
    CODE_CALLBACK_INVOKE_CHANNEL -> {
      val invokeId = p1?.getAsString(InvokeChannelConstants.Key.INVOKE_ID)
      val command = p1?.getAsString(InvokeChannelConstants.Key.COMMAND)
      val sourceData = p1?.getAsString(InvokeChannelConstants.Key.DATA)

      Log.i(TAG, "invokeId: $invokeId, command: $command, sourceData: $sourceData")

      if (command.equals("test")) {
        // 实现特定的功能逻辑
        mHandler.post({
          val cv = ContentValues()
          cv.apply {
            put(InvokeChannelConstants.Key.INVOKE_ID, invokeId)
            put(InvokeChannelConstants.Key.COMMAND, command)
            put(InvokeChannelConstants.Key.DATA, "your data") // 返回执行结果给小程序端
          }
          try {
            context?.contentResolver?.insert(InvokeChannelConstants.ContentProvider.Cli2WMPF.URI_CALLBACK_INVOKE_CHANNEL, cv)
          } catch (e: Exception) {
            Log.e(TAG, "callback invoke channel error")
          }
        })
      }
    }
  }
  return null
}
```
