@JsPlugin(secondary = true)public class CustomPlugin extends BaseJsPlugin {@JsEvent("customAsyncEvent")public void custom(final RequestEvent req) {// Get the parameters//req.jsonParams// Return the data asynchronously//req.fail();//req.ok();JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "test");} catch (JSONException e) {e.printStackTrace();}req.ok(jsonObject);}@JsEvent("customSyncEvent")public String custom1(final RequestEvent req) {// Get the parameters//req.jsonParams//Return the data synchronouslyJSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "value");} catch (JSONException e) {throw new RuntimeException(e);}return req.okSync(jsonObject);}/*** Test coverage system API* @param req*/@JsEvent("getAppBaseInfo")public void getAppBaseInfo(final RequestEvent req) {// Get the parameters//req.jsonParams// Return the data asynchronously//req.fail();//req.ok();JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "test");} catch (JSONException e) {e.printStackTrace();}req.ok(jsonObject);}@JsEvent("testState")public void testState(final RequestEvent req) {try {//Call back intermediate statereq.sendState(req, new JSONObject().put("progress", 1));req.sendState(req, new JSONObject().put("progress", 30));req.sendState(req, new JSONObject().put("progress", 60));req.sendState(req, new JSONObject().put("progress", 100));} catch (JSONException e) {e.printStackTrace();}JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "test");req.ok(jsonObject);} catch (JSONException e) {throw new RuntimeException(e);}}}
{"extApi": [{//Event name, must match the event defined in the "Create API" example with @JsEvent"name": "customSyncEvent",//Whether the call is synchronous, consistent with the return method in the "Creating API" example"sync": true,//Define parameters//a. JSON format can be nested//b. string parameter value can be to ""//c. numeric parameter value can be to 0"params": {"name": "","age": 0,"object": {"key": "",}}},{"name": "customAsyncEvent2","sync": false,"params": {"name": "","age": ""}],//true: If the custom API event name matches a built-in method name in the mini program SDK, it will override the SDK's built-in API.//API, and the final call will invoke the developer's custom API."overwriteWxApi": false}
@ProxyService(proxy = MiniAppProxy.class)public class MiniAppProxyImpl extends BaseMiniAppProxyImpl {@Overridepublic MiniConfigData configData(Context context, int configType, JSONObject params) {if(configType == MiniConfigData.TYPE_CUSTOM_JSAPI) {//Custom JsApi configurationMiniConfigData.CustomJsApiConfig customJsApiConfig = new MiniConfigData.CustomJsApiConfig();customJsApiConfig.jsApiConfigPath = "tcmpp/custom-config.json";return new MiniConfigData.Builder().customJsApiConfig(customJsApiConfig).build();}return null;}
{"extApi": [{"name": "customSyncEvent","sync": true,"params": {"name": "","age": ""}},{"name": "customAsyncEvent","sync": false,"params": {"name": "","age": ""}},{"name": "getAppBaseInfo","sync": false,"params": {}}"overwriteWxApi": true}
wx.customAsyncEvent({"name":"123","age":"18"})wx.getAppBaseInfo()//his will override the system API and call the custom API.
Was this page helpful?