@JsPlugin(secondary = true)
;@JsEvent("事件名")
,当小程序 js 调用“事件名”时就会调用到@JsEvent
修饰的对应方法;@JsEvent
支持定义多个事件名;@JsPlugin(secondary = true)public class CustomPlugin extends BaseJsPlugin {@JsEvent("customAsyncEvent")public void custom(final RequestEvent req) {//获取参数//req.jsonParams//异步返回数据//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) {//获取参数//req.jsonParams//同步返回数据JSONObject jsonObject = new JSONObject();try {jsonObject.put("key", "value");} catch (JSONException e) {throw new RuntimeException(e);}return req.okSync(jsonObject);}/*** 测试覆盖系统API* @param req*/@JsEvent("getAppBaseInfo")public void getAppBaseInfo(final RequestEvent req) {//获取参数//req.jsonParams//异步返回数据//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 {//回调中间状态req.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);}}}
@JsEvent
定义的事件名称一致;{"extApi": [{//事件名,与“创建API”示例中@JsEvent定义的事件保持一致"name": "customSyncEvent",//是否同步调用,与“创建API”示例数据返回方式保持一致"sync": true,//定义参数//a.json格式可以嵌套//b.字符串参数value设置为""即可//c.数字参数value设置为0即可"params": {"name": "","age": 0,"object": {"key": "",}}},{"name": "customAsyncEvent2","sync": false,"params": {"name": "","age": ""}],//true:当自定义的API事件名与小程序SDK内置的方法名,会覆盖SDK内置的//API,最终调用会调用的开发者自定义的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) {//自定义JsApi配置MiniConfigData.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()//会覆盖系统API,然后调用到自定义API中
本页内容是否解决了您的问题?