From c96ef9a4c6edaea889f702ccf86e38aa298fe67d Mon Sep 17 00:00:00 2001 From: kangxuyao Date: Tue, 14 Apr 2020 17:08:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/build.gradle | 16 +++++++++++++++- example/ios/Podfile.lock | 8 ++++---- ios/Classes/Api/MOP_openApplet.m | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 3e6229e..c4cdbb7 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -12,6 +12,13 @@ buildscript { repositories { google() jcenter() + maven { + url "https://gradle.finogeeks.club/repository/finogeeks/" + credentials { + username "gradle" + password "ftjk@@123321" + } + } maven { url "https://gradle.finogeeks.club/repository/applet/" credentials { @@ -34,6 +41,13 @@ rootProject.allprojects { repositories { google() jcenter() + maven { + url "https://gradle.finogeeks.club/repository/finogeeks/" + credentials { + username "gradle" + password "ftjk@@123321" + } + } maven { url "https://gradle.finogeeks.club/repository/applet/" credentials { @@ -77,6 +91,6 @@ kapt { } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.finogeeks.lib:finapplet:+' + implementation 'com.finogeeks.lib:finapplet:2.1.9' } diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 83a574e..554cb53 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1,8 +1,8 @@ PODS: - - FinApplet (2.0.196) + - FinApplet (2.1.5) - Flutter (1.0.0) - mop (0.1.0): - - FinApplet + - FinApplet (= 2.1.5) - Flutter DEPENDENCIES: @@ -20,9 +20,9 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/mop/ios" SPEC CHECKSUMS: - FinApplet: 629fb114471bb68eea3aace7fd9c28a7ce6326f8 + FinApplet: adb43373a01ff90832969b1534d40c08344063f6 Flutter: 0e3d915762c693b495b44d77113d4970485de6ec - mop: e3eac9de90d3e331268e9c9e8308e233e540ad1c + mop: c0e0dfb65c6a482e47f0fffc9a6576d88e7636b5 PODFILE CHECKSUM: b66559db98de00d11e349a06f9e603856ed75d6e diff --git a/ios/Classes/Api/MOP_openApplet.m b/ios/Classes/Api/MOP_openApplet.m index 3bf7da8..feb08f0 100644 --- a/ios/Classes/Api/MOP_openApplet.m +++ b/ios/Classes/Api/MOP_openApplet.m @@ -26,7 +26,7 @@ } }]; }else{ - [[FATClient sharedClient] startRemoteApplet:self.appId sequence:self.sequence startParams:self.params InParentViewController:currentVC transitionStyle:FATTranstionStylePush completion:^(BOOL result, NSError *error) { + [[FATClient sharedClient] startRemoteApplet:self.appId sequence:self.sequence startParams:self.params InParentViewController:currentVC transitionStyle:FATTranstionStyleUp completion:^(BOOL result, NSError *error) { NSLog(@"result:%d---error:%@", result, error); if (result){ success(@{}); From 9c1611313570437df9883f5b83814331565a4e95 Mon Sep 17 00:00:00 2001 From: kangxuyao Date: Tue, 14 Apr 2020 17:12:06 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/build.gradle b/android/build.gradle index c4cdbb7..cadfae4 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -91,6 +91,6 @@ kapt { } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.finogeeks.lib:finapplet:2.1.9' + implementation 'com.finogeeks.lib:finapplet:2.1.13' } From 717507f7ea2704e104a2cf9e1eb230eb2547779a Mon Sep 17 00:00:00 2001 From: kangxuyao Date: Sun, 26 Apr 2020 14:57:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?flutter=20sdk=E5=A2=9E=E5=8A=A0mop=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 + android/build.gradle | 2 +- .../java/com/finogeeks/mop/MopPlugin.java | 118 ++++++------- .../com/finogeeks/mop/api/ApisManager.java | 9 +- .../mop/api/mop/AppletHandlerModule.java | 160 ++++++++++++++++++ .../mop/api/mop/AppletManageModule.java | 54 ++++++ .../finogeeks/mop/api/mop/AppletModule.java | 1 + .../mop/api/mop/ExtensionApiModule.java | 80 +++++++++ .../mop/service/MopPluginService.java | 23 ++- .../com/finogeeks/mop/utils/GsonUtil.java | 19 +++ example/pubspec.lock | 2 +- ios/Classes/Api/MOPAppletDelegate.h | 17 ++ ios/Classes/Api/MOPAppletDelegate.m | 53 ++++++ ios/Classes/Api/MOP_clearApplets.h | 16 ++ ios/Classes/Api/MOP_clearApplets.m | 21 +++ ios/Classes/Api/MOP_closeAllApplets.h | 16 ++ ios/Classes/Api/MOP_closeAllApplets.m | 20 +++ ios/Classes/Api/MOP_currentApplet.h | 16 ++ ios/Classes/Api/MOP_currentApplet.m | 35 ++++ ios/Classes/Api/MOP_registerAppletHandler.h | 16 ++ ios/Classes/Api/MOP_registerAppletHandler.m | 22 +++ ios/Classes/Api/MOP_registerExtensionApi.h | 18 ++ ios/Classes/Api/MOP_registerExtensionApi.m | 36 ++++ ios/Classes/MopPlugin.h | 2 + ios/Classes/MopPlugin.m | 21 ++- ios/mop.podspec | 4 +- lib/api.dart | 33 ++++ lib/mop.dart | 90 +++++++++- publish.sh | 0 pubspec.yaml | 5 +- 30 files changed, 823 insertions(+), 88 deletions(-) create mode 100644 android/src/main/java/com/finogeeks/mop/api/mop/AppletHandlerModule.java create mode 100644 android/src/main/java/com/finogeeks/mop/api/mop/AppletManageModule.java create mode 100644 android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java create mode 100644 android/src/main/java/com/finogeeks/mop/utils/GsonUtil.java create mode 100644 ios/Classes/Api/MOPAppletDelegate.h create mode 100644 ios/Classes/Api/MOPAppletDelegate.m create mode 100644 ios/Classes/Api/MOP_clearApplets.h create mode 100644 ios/Classes/Api/MOP_clearApplets.m create mode 100644 ios/Classes/Api/MOP_closeAllApplets.h create mode 100644 ios/Classes/Api/MOP_closeAllApplets.m create mode 100644 ios/Classes/Api/MOP_currentApplet.h create mode 100644 ios/Classes/Api/MOP_currentApplet.m create mode 100644 ios/Classes/Api/MOP_registerAppletHandler.h create mode 100644 ios/Classes/Api/MOP_registerAppletHandler.m create mode 100644 ios/Classes/Api/MOP_registerExtensionApi.h create mode 100644 ios/Classes/Api/MOP_registerExtensionApi.m create mode 100644 lib/api.dart mode change 100644 => 100755 publish.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 95ccdd1..3a16422 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## 0.6.0 +增加api接口 ## 0.5.0 ## 0.4.0 支持小程序最新依赖 diff --git a/android/build.gradle b/android/build.gradle index cadfae4..00da670 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -91,6 +91,6 @@ kapt { } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.finogeeks.lib:finapplet:2.1.13' + implementation 'com.finogeeks.lib:finapplet:2.1.17' } diff --git a/android/src/main/java/com/finogeeks/mop/MopPlugin.java b/android/src/main/java/com/finogeeks/mop/MopPlugin.java index 9ec335d..0740f8c 100644 --- a/android/src/main/java/com/finogeeks/mop/MopPlugin.java +++ b/android/src/main/java/com/finogeeks/mop/MopPlugin.java @@ -19,81 +19,81 @@ import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry.Registrar; -/** MopPlugin */ +/** + * MopPlugin + */ public class MopPlugin implements MethodCallHandler { - private static final String LOG_TAG = MopPlugin.class.getSimpleName(); + private static final String LOG_TAG = MopPlugin.class.getSimpleName(); - private static final String CHANNEL = "mop"; - private PluginRegistry.Registrar registrar; - private FlutterInterface flutterInterface; - private MopPluginDelegate delegate; + private static final String CHANNEL = "mop"; + private PluginRegistry.Registrar registrar; + private FlutterInterface flutterInterface; + private MopPluginDelegate delegate; + /** + * Plugin registration. + */ + public static void registerWith(Registrar registrar) { + final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL); + final MopPluginDelegate delegate = new MopPluginDelegate(registrar.activity()); + final MopPlugin instance = new MopPlugin(registrar, delegate); + channel.setMethodCallHandler(instance); + final EventChannel eventChannel = new EventChannel(registrar.messenger(), "plugins.mop.finogeeks.com/mop_event"); + MopEventStream mopEventStream = new MopEventStream(); + eventChannel.setStreamHandler(mopEventStream); + MopPluginService.getInstance().initialize(registrar.activity(), mopEventStream, channel); + } - /** - * Plugin registration. - */ - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL); - final MopPluginDelegate delegate = new MopPluginDelegate(registrar.activity()); - final MopPlugin instance = new MopPlugin(registrar,delegate); - channel.setMethodCallHandler(instance); - final EventChannel eventChannel=new EventChannel(registrar.messenger(),"plugins.mop.finogeeks.com/mop_event"); - MopEventStream mopEventStream = new MopEventStream(); - eventChannel.setStreamHandler(mopEventStream); - MopPluginService.getInstance().initialize(registrar.activity(),mopEventStream); + MopPlugin(PluginRegistry.Registrar registrar, MopPluginDelegate delegate) { + this.registrar = registrar; + this.flutterInterface = new FlutterInterface(); + this.delegate = delegate; + } - } + @Override + public void onMethodCall(MethodCall call, final Result result) { + registrar.addActivityResultListener(delegate); + ICallback callback = new ICallback() { + @Override + public void onSuccess(Object data) { + Map obj = new HashMap(); - MopPlugin(PluginRegistry.Registrar registrar,MopPluginDelegate delegate) { - this.registrar = registrar; - this.flutterInterface = new FlutterInterface(); - this.delegate = delegate; - } + obj.put("success", true); + if (data != null) + obj.put("data", data); + obj.put("retMsg", "ok"); + result.success(obj); + } - @Override - public void onMethodCall(MethodCall call, final Result result) { - registrar.addActivityResultListener(delegate); - ICallback callback = new ICallback() { - @Override - public void onSuccess(Object data) { - Map obj = new HashMap(); + @Override + public void onFail(Object error) { + Map obj = new HashMap(); + obj.put("success", false); + obj.put("retMsg", error == null ? "" : error); + result.success(obj); + } - obj.put("success",true); - if (data != null) - obj.put("data",data); - obj.put("retMsg","ok"); - result.success(obj); - } + @Override + public void onCancel(Object cancel) { + result.notImplemented(); + } - @Override - public void onFail(Object error) { - Map obj = new HashMap(); - obj.put("success",false); - obj.put("retMsg",error==null?"":error); - result.success(obj); - } + @Override + public void startActivityForResult(Intent intent, int requestCode) { - @Override - public void onCancel(Object cancel) { - result.notImplemented(); - } - - @Override - public void startActivityForResult(Intent intent, int requestCode) { - - } - }; - Log.d(LOG_TAG,"mopplugin: invoke " + call.method); - Event event= new Event(call.method,call.arguments,callback); - delegate.setEvent(event); - this.flutterInterface.invokeHandler(event); + } + }; + Log.d(LOG_TAG, "mopplugin: invoke " + call.method); + Event event = new Event(call.method, call.arguments, callback); + delegate.setEvent(event); + this.flutterInterface.invokeHandler(event); // if (call.method.equals("getPlatformVersion")) { // result.success("Android " + android.os.Build.VERSION.RELEASE); // } else { // result.notImplemented(); // } - } + } } diff --git a/android/src/main/java/com/finogeeks/mop/api/ApisManager.java b/android/src/main/java/com/finogeeks/mop/api/ApisManager.java index 85637fc..1d3152b 100644 --- a/android/src/main/java/com/finogeeks/mop/api/ApisManager.java +++ b/android/src/main/java/com/finogeeks/mop/api/ApisManager.java @@ -3,8 +3,11 @@ package com.finogeeks.mop.api; import android.app.Activity; import android.text.TextUtils; +import com.finogeeks.mop.api.mop.AppletHandlerModule; +import com.finogeeks.mop.api.mop.AppletManageModule; import com.finogeeks.mop.api.mop.AppletModule; import com.finogeeks.mop.api.mop.BaseModule; +import com.finogeeks.mop.api.mop.ExtensionApiModule; import com.finogeeks.mop.interfaces.Event; import com.finogeeks.mop.interfaces.IApi; @@ -29,6 +32,7 @@ public class ApisManager { mActivity = activity; initSdkApi(activity); } + /** * api功能调用 * @@ -42,6 +46,7 @@ public class ApisManager { return null; } + /** * api功能调用 * @@ -59,7 +64,9 @@ public class ApisManager { private void initSdkApi(Activity activity) { add(new BaseModule(activity)); add(new AppletModule(activity)); - + add(new AppletManageModule(activity)); + add(new AppletHandlerModule(activity)); + add(new ExtensionApiModule(activity)); } private void add(IApi api) { diff --git a/android/src/main/java/com/finogeeks/mop/api/mop/AppletHandlerModule.java b/android/src/main/java/com/finogeeks/mop/api/mop/AppletHandlerModule.java new file mode 100644 index 0000000..aba595e --- /dev/null +++ b/android/src/main/java/com/finogeeks/mop/api/mop/AppletHandlerModule.java @@ -0,0 +1,160 @@ +package com.finogeeks.mop.api.mop; + +import android.content.Context; +import android.graphics.Bitmap; +import android.os.Handler; +import android.os.Looper; +import android.util.Log; + +import com.finogeeks.lib.applet.client.FinAppClient; +import com.finogeeks.lib.applet.page.view.moremenu.MoreMenuItem; +import com.finogeeks.lib.applet.sdk.api.IAppletHandler; +import com.finogeeks.mop.api.BaseApi; +import com.finogeeks.mop.interfaces.ICallback; +import com.finogeeks.mop.service.MopPluginService; +import com.finogeeks.mop.utils.GsonUtil; +import com.google.gson.reflect.TypeToken; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; + +import io.flutter.plugin.common.MethodChannel; + +public class AppletHandlerModule extends BaseApi { + + private Handler handler = new Handler(Looper.getMainLooper()); + + public AppletHandlerModule(Context context) { + super(context); + } + + @Override + public String[] apis() { + return new String[]{"registerAppletHandler"}; + } + + @Override + public void invoke(String event, Map param, ICallback callback) { + Log.d("AppletHandlerModule", "registerAppletHandler"); + MethodChannel channel = MopPluginService.getInstance().getMethodChannel(); + FinAppClient.INSTANCE.getAppletApiManager().setAppletHandler(new IAppletHandler() { + @Override + public void shareAppMessage(@NotNull String s, @Nullable Bitmap bitmap, @NotNull IAppletCallback iAppletCallback) { + Log.d("MopPlugin", "shareAppMessage:" + s); + Map params = new HashMap<>(); + params.put("appletInfo", GsonUtil.gson.fromJson(s, new TypeToken>() { + }.getType())); + handler.post(() -> { + channel.invokeMethod("extensionApi:forwardApplet", params, new MethodChannel.Result() { + @Override + public void success(Object result) { + iAppletCallback.onSuccess(null); + } + + @Override + public void error(String errorCode, String errorMessage, Object errorDetails) { + iAppletCallback.onFailure(); + } + + @Override + public void notImplemented() { + iAppletCallback.onFailure(); + } + }); + }); + } + + @Nullable + @Override + public Map getUserInfo() { + Log.d("AppletHandlerModule", "getUserInfo"); + CountDownLatch latch = new CountDownLatch(1); + final Map[] ret = new Map[1]; + handler.post(() -> { + channel.invokeMethod("extensionApi:getUserInfo", null, new MethodChannel.Result() { + @Override + public void success(Object result) { + ret[0] = (Map) result; + latch.countDown(); + } + + @Override + public void error(String errorCode, String errorMessage, Object errorDetails) { + latch.countDown(); + } + + @Override + public void notImplemented() { + latch.countDown(); + } + }); + }); + try { + latch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (ret[0].size() > 0) + return ret[0]; + else + return null; + } + + @Nullable + @Override + public List getRegisteredMoreMenuItems(@NotNull String s) { + CountDownLatch latch = new CountDownLatch(1); + List moreMenuItems = new ArrayList<>(); + Map params = new HashMap<>(); + params.put("appId", s); + handler.post(() -> { + channel.invokeMethod("extensionApi:getCustomMenus", params, new MethodChannel.Result() { + @Override + public void success(Object result) { + List> ret = (List>) result; + if (ret != null) { + for (Map map : ret) { + moreMenuItems.add(new MoreMenuItem((Integer) map.get("menuId"), 0, (String) map.get("title"), true)); + } + } + latch.countDown(); + } + + @Override + public void error(String errorCode, String errorMessage, Object errorDetails) { + latch.countDown(); + } + + @Override + public void notImplemented() { + latch.countDown(); + } + }); + }); + try { + latch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return moreMenuItems; + } + + @Override + public void onRegisteredMoreMenuItemClicked(@NotNull String s, int i) { + Map params = new HashMap<>(); + params.put("appId", s); + params.put("menuId", i); + handler.post(() -> { + channel.invokeMethod("extensionApi:onCustomMenuClick", params); + }); + } + }); + callback.onSuccess(null); + } +} diff --git a/android/src/main/java/com/finogeeks/mop/api/mop/AppletManageModule.java b/android/src/main/java/com/finogeeks/mop/api/mop/AppletManageModule.java new file mode 100644 index 0000000..12e7528 --- /dev/null +++ b/android/src/main/java/com/finogeeks/mop/api/mop/AppletManageModule.java @@ -0,0 +1,54 @@ +package com.finogeeks.mop.api.mop; + +import android.content.Context; + +import com.finogeeks.lib.applet.client.FinAppClient; +import com.finogeeks.lib.applet.db.entity.FinApplet; +import com.finogeeks.mop.api.BaseApi; +import com.finogeeks.mop.interfaces.ICallback; + +import java.util.HashMap; +import java.util.Map; + + +public class AppletManageModule extends BaseApi { + + public AppletManageModule(Context context) { + super(context); + } + + @Override + public String[] apis() { + return new String[]{"currentApplet", "closeAllApplets", "clearApplets"}; + } + + @Override + public void invoke(String event, Map param, ICallback callback) { + if (event.equals("currentApplet")) { + String appId = FinAppClient.INSTANCE.getAppletApiManager().getCurrentAppletId(); + if (appId != null) { + FinApplet applet = FinAppClient.INSTANCE.getAppletApiManager().getUsedApplet(appId); + if (applet != null) { + Map res = new HashMap<>(); + res.put("appId", applet.getId()); + res.put("name", applet.getName()); + res.put("icon", applet.getIcon()); + res.put("description", applet.getDescription()); + res.put("version", applet.getVersion()); + res.put("thumbnail", applet.getThumbnail()); + callback.onSuccess(res); + } else { + callback.onSuccess(null); + } + } else { + callback.onSuccess(null); + } + } else if (event.equals("closeAllApplets")) { + FinAppClient.INSTANCE.finishAllRunningApplets(); + callback.onSuccess(null); + } else if (event.equals("clearApplets")) { + FinAppClient.INSTANCE.getAppletApiManager().clearApplets(); + callback.onSuccess(null); + } + } +} diff --git a/android/src/main/java/com/finogeeks/mop/api/mop/AppletModule.java b/android/src/main/java/com/finogeeks/mop/api/mop/AppletModule.java index aa3d195..0d3f247 100644 --- a/android/src/main/java/com/finogeeks/mop/api/mop/AppletModule.java +++ b/android/src/main/java/com/finogeeks/mop/api/mop/AppletModule.java @@ -50,6 +50,7 @@ public class AppletModule extends BaseApi { FinAppClient.INSTANCE.getAppletApiManager().startApplet(mContext, appId, sequence, null); } } else { + Log.d("MopPlugin", "openApplet:params:" + param); FinAppClient.INSTANCE.getAppletApiManager().startApplet(mContext, appId, params); } callback.onSuccess(new HashMap()); diff --git a/android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java b/android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java new file mode 100644 index 0000000..70269da --- /dev/null +++ b/android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java @@ -0,0 +1,80 @@ +package com.finogeeks.mop.api.mop; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.util.Log; + +import com.finogeeks.lib.applet.client.FinAppClient; +import com.finogeeks.mop.api.BaseApi; +import com.finogeeks.mop.interfaces.ICallback; +import com.finogeeks.mop.service.MopPluginService; +import com.finogeeks.mop.utils.GsonUtil; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Map; + +import io.flutter.plugin.common.MethodChannel; + + +public class ExtensionApiModule extends BaseApi { + + private Handler handler = new Handler(Looper.getMainLooper()); + + public ExtensionApiModule(Context context) { + super(context); + } + + @Override + public String[] apis() { + return new String[]{"registerExtensionApi"}; + } + + @Override + public void invoke(String s, Map param, ICallback iCallback) { + MethodChannel channel = MopPluginService.getInstance().getMethodChannel(); + String name = (String) param.get("name"); + FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new com.finogeeks.lib.applet.api.BaseApi(getContext()) { + @Override + public String[] apis() { + return new String[]{name}; + } + + @Override + public void invoke(String s, JSONObject jsonObject, com.finogeeks.lib.applet.interfaces.ICallback iCallback) { + Log.d("MopPlugin", "invoke extensionApi:" + s + ",params:" + jsonObject); + Map params = GsonUtil.gson.fromJson(jsonObject.toString(), HashMap.class); + handler.post(() -> { + channel.invokeMethod("extensionApi:" + name, params, new MethodChannel.Result() { + @Override + public void success(Object result) { + String json = GsonUtil.gson.toJson(result); + JSONObject ret = null; + if (json != null && !json.equals("null")) + try { + ret = new JSONObject(json); + } catch (JSONException e) { + e.printStackTrace(); + } + iCallback.onSuccess(ret); + } + + @Override + public void error(String errorCode, String errorMessage, Object errorDetails) { + iCallback.onFail(); + } + + @Override + public void notImplemented() { + iCallback.onFail(); + } + }); + }); + } + }); + + } +} diff --git a/android/src/main/java/com/finogeeks/mop/service/MopPluginService.java b/android/src/main/java/com/finogeeks/mop/service/MopPluginService.java index ddd532c..209be21 100644 --- a/android/src/main/java/com/finogeeks/mop/service/MopPluginService.java +++ b/android/src/main/java/com/finogeeks/mop/service/MopPluginService.java @@ -1,12 +1,13 @@ package com.finogeeks.mop.service; import android.app.Activity; -import android.app.Application; import android.content.Context; import com.finogeeks.mop.MopEventStream; import com.finogeeks.mop.api.ApisManager; +import io.flutter.plugin.common.MethodChannel; + public class MopPluginService { private final static String TAG = MopPluginService.class.getSimpleName(); private static volatile MopPluginService _instance = null; @@ -17,6 +18,7 @@ public class MopPluginService { private Context mContext; private Activity mActivity; + private MethodChannel mMethodChannel; MopPluginService() { } @@ -32,20 +34,33 @@ public class MopPluginService { return _instance; } - public ApisManager getApisManager() { return this.apisManager;} - public MopEventStream getMopEventStream() {return this.mopEventStream;} - public void initialize(Activity activity, MopEventStream mopEventStream) { + public ApisManager getApisManager() { + return this.apisManager; + } + + public MopEventStream getMopEventStream() { + return this.mopEventStream; + } + + public void initialize(Activity activity, MopEventStream mopEventStream, MethodChannel methodChannel) { this.mopEventStream = mopEventStream; this.mContext = activity.getApplicationContext(); this.apisManager = new ApisManager(activity); this.mActivity = activity; + this.mMethodChannel = methodChannel; } + public Context getContext() { return mContext; } + public Activity getActivity() { return mActivity; } + + public MethodChannel getMethodChannel() { + return mMethodChannel; + } } diff --git a/android/src/main/java/com/finogeeks/mop/utils/GsonUtil.java b/android/src/main/java/com/finogeeks/mop/utils/GsonUtil.java new file mode 100644 index 0000000..7906cb4 --- /dev/null +++ b/android/src/main/java/com/finogeeks/mop/utils/GsonUtil.java @@ -0,0 +1,19 @@ +package com.finogeeks.mop.utils; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.util.Map; + +public class GsonUtil { + public static Gson gson = new Gson(); + + public static Map toMap(Object object) { + String str = gson.toJson(object); + if (str != null) { + return gson.fromJson(str, new TypeToken>() { + }.getType()); + } + return null; + } +} diff --git a/example/pubspec.lock b/example/pubspec.lock index 3014d7a..2b97951 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -101,7 +101,7 @@ packages: path: ".." relative: true source: path - version: "0.4.0" + version: "0.6.0" path: dependency: transitive description: diff --git a/ios/Classes/Api/MOPAppletDelegate.h b/ios/Classes/Api/MOPAppletDelegate.h new file mode 100644 index 0000000..c44aa0c --- /dev/null +++ b/ios/Classes/Api/MOPAppletDelegate.h @@ -0,0 +1,17 @@ +// +// MOPAppletDelegate.h +// mop +// +// Created by 康旭耀 on 2020/4/20. +// + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface MOPAppletDelegate : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/Classes/Api/MOPAppletDelegate.m b/ios/Classes/Api/MOPAppletDelegate.m new file mode 100644 index 0000000..9f00dfd --- /dev/null +++ b/ios/Classes/Api/MOPAppletDelegate.m @@ -0,0 +1,53 @@ +// +// MOPAppletDelegate.m +// mop +// +// Created by 康旭耀 on 2020/4/20. +// + +#import "MOPAppletDelegate.h" +#import "MopPlugin.h" + +@implementation MOPAppletDelegate + +- (void)forwardAppletWithInfo:(NSDictionary *)contentInfo completion:(void (^)(FATExtensionCode, NSDictionary *))completion +{ + NSLog(@"forwardAppletWithInfo:%@",contentInfo); + FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel]; + [channel invokeMethod:@"extensionApi:forwardApplet" arguments:contentInfo result:^(id _Nullable result) { + if([result isKindOfClass:[FlutterError class]]|| [result isKindOfClass:[FlutterMethodNotImplemented class] ]) + { + completion(FATExtensionCodeFailure,nil); + }else + { + completion(FATExtensionCodeSuccess,result); + } + }]; +} + +- (NSDictionary *)getUserInfoWithAppletInfo:(FATAppletInfo *)appletInfo +{ + NSLog(@"getUserInfoWithAppletInfo"); + __block NSDictionary *userInfo; + FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel]; + dispatch_group_t group = dispatch_group_create(); + dispatch_group_enter(group); + [channel invokeMethod:@"extensionApi:getUserInfo" arguments:nil result:^(id _Nullable result) { + userInfo = result; + dispatch_group_leave(group); + }]; + dispatch_group_wait(group, DISPATCH_TIME_FOREVER); + return userInfo; +} + +- (NSArray> *)customMenusInMoreItemAtPath:(NSString *)path +{ + return nil; +} + +- (void)customMenu:(id)customMenu didClickAtPath:(NSString *)path +{ + +} + +@end diff --git a/ios/Classes/Api/MOP_clearApplets.h b/ios/Classes/Api/MOP_clearApplets.h new file mode 100644 index 0000000..e9f4e9e --- /dev/null +++ b/ios/Classes/Api/MOP_clearApplets.h @@ -0,0 +1,16 @@ +// +// MOP_clearApplets.h +// mop +// +// Created by 康旭耀 on 2020/4/16. +// + +#import "MOPBaseApi.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MOP_clearApplets : MOPBaseApi + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/Classes/Api/MOP_clearApplets.m b/ios/Classes/Api/MOP_clearApplets.m new file mode 100644 index 0000000..e4f22a0 --- /dev/null +++ b/ios/Classes/Api/MOP_clearApplets.m @@ -0,0 +1,21 @@ +// +// MOP_clearApplets.m +// mop +// +// Created by 康旭耀 on 2020/4/16. +// + +#import "MOP_clearApplets.h" +#import + +@implementation MOP_clearApplets + +- (void)setupApiWithSuccess:(void (^)(NSDictionary * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel +{ + NSLog(@"clearApplets"); + [[FATClient sharedClient]clearMemoryCache]; + [[FATClient sharedClient]clearLocalApplets]; + success(@{}); +} + +@end diff --git a/ios/Classes/Api/MOP_closeAllApplets.h b/ios/Classes/Api/MOP_closeAllApplets.h new file mode 100644 index 0000000..0890fb2 --- /dev/null +++ b/ios/Classes/Api/MOP_closeAllApplets.h @@ -0,0 +1,16 @@ +// +// MOP_closeAllApplets.h +// mop +// +// Created by 康旭耀 on 2020/4/16. +// + +#import "MOPBaseApi.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MOP_closeAllApplets : MOPBaseApi + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/Classes/Api/MOP_closeAllApplets.m b/ios/Classes/Api/MOP_closeAllApplets.m new file mode 100644 index 0000000..b737361 --- /dev/null +++ b/ios/Classes/Api/MOP_closeAllApplets.m @@ -0,0 +1,20 @@ +// +// MOP_closeAllApplets.m +// mop +// +// Created by 康旭耀 on 2020/4/16. +// + +#import "MOP_closeAllApplets.h" +#import + +@implementation MOP_closeAllApplets + +- (void)setupApiWithSuccess:(void (^)(NSDictionary * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel +{ + NSLog(@"closeAllApplets"); + [[FATClient sharedClient] closeAllApplets]; + success(@{}); +} + +@end diff --git a/ios/Classes/Api/MOP_currentApplet.h b/ios/Classes/Api/MOP_currentApplet.h new file mode 100644 index 0000000..7e752ce --- /dev/null +++ b/ios/Classes/Api/MOP_currentApplet.h @@ -0,0 +1,16 @@ +// +// MOP_currentApplet.h +// mop +// +// Created by 康旭耀 on 2020/4/16. +// + +#import "MOPBaseApi.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MOP_currentApplet : MOPBaseApi + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/Classes/Api/MOP_currentApplet.m b/ios/Classes/Api/MOP_currentApplet.m new file mode 100644 index 0000000..bfbd498 --- /dev/null +++ b/ios/Classes/Api/MOP_currentApplet.m @@ -0,0 +1,35 @@ +// +// MOP_currentApplet.m +// mop +// +// Created by 康旭耀 on 2020/4/16. +// + +#import "MOP_currentApplet.h" + +#import + +@implementation MOP_currentApplet + + +- (void)setupApiWithSuccess:(void (^)(NSDictionary * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel +{ + FATAppletInfo *info = [[FATClient sharedClient] currentApplet]; + if(info != nil) + { + NSMutableDictionary *dic = [NSMutableDictionary dictionary]; + dic[@"appId"] = info.appId; + dic[@"name"]= info.appTitle; + dic[@"icon"]= info.appAvatar; + dic[@"description"]=info.appDescription; + dic[@"version"] = info.appVersion; + dic[@"thumbnail"]=info.appThumbnail; + success(dic); + } + else + { + success(@{}); + } +} + +@end diff --git a/ios/Classes/Api/MOP_registerAppletHandler.h b/ios/Classes/Api/MOP_registerAppletHandler.h new file mode 100644 index 0000000..1305a1a --- /dev/null +++ b/ios/Classes/Api/MOP_registerAppletHandler.h @@ -0,0 +1,16 @@ +// +// MOP_registerAppletHandler.h +// mop +// +// Created by 康旭耀 on 2020/4/20. +// + +#import "MOPBaseApi.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MOP_registerAppletHandler : MOPBaseApi + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/Classes/Api/MOP_registerAppletHandler.m b/ios/Classes/Api/MOP_registerAppletHandler.m new file mode 100644 index 0000000..2fb6f63 --- /dev/null +++ b/ios/Classes/Api/MOP_registerAppletHandler.m @@ -0,0 +1,22 @@ +// +// MOP_registerAppletHandler.m +// mop +// +// Created by 康旭耀 on 2020/4/20. +// + +#import "MOP_registerAppletHandler.h" +#import "MopPlugin.h" +#import "MOPAppletDelegate.h" +#import + +@implementation MOP_registerAppletHandler + +- (void)setupApiWithSuccess:(void (^)(NSDictionary * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel +{ + NSLog(@"MOP_registerAppletHandler"); + [[FATClient sharedClient] setDelegate:[[MOPAppletDelegate alloc] init]]; + +} + +@end diff --git a/ios/Classes/Api/MOP_registerExtensionApi.h b/ios/Classes/Api/MOP_registerExtensionApi.h new file mode 100644 index 0000000..0cd9fe5 --- /dev/null +++ b/ios/Classes/Api/MOP_registerExtensionApi.h @@ -0,0 +1,18 @@ +// +// MOP_registerExtensionApi.h +// mop +// +// Created by 康旭耀 on 2020/4/20. +// + +#import "MOPBaseApi.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MOP_registerExtensionApi : MOPBaseApi + +@property NSString* name; + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/Classes/Api/MOP_registerExtensionApi.m b/ios/Classes/Api/MOP_registerExtensionApi.m new file mode 100644 index 0000000..d9f1035 --- /dev/null +++ b/ios/Classes/Api/MOP_registerExtensionApi.m @@ -0,0 +1,36 @@ +// +// MOP_registerExtensionApi.m +// mop +// +// Created by 康旭耀 on 2020/4/20. +// + +#import "MOP_registerExtensionApi.h" +#import "MopPlugin.h" +#import + +@implementation MOP_registerExtensionApi + +- (void)setupApiWithSuccess:(void (^)(NSDictionary * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel +{ + NSLog(@"MOP_registerExtensionApi"); + FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel]; + [[FATClient sharedClient] registerExtensionApi:self.name handle:^(id param, FATExtensionApiCallback callback) { + NSLog(@"invoke ExtensionApi:"); + NSLog(@"%@",self.name); + NSLog(@"%@",param); + NSString* api = [@"extensionApi:" stringByAppendingString:self.name]; + [channel invokeMethod:api arguments:param result:^(id _Nullable result) { + if([result isKindOfClass:[FlutterError class]]|| [result isKindOfClass:[FlutterMethodNotImplemented class] ]) + { + callback(FATExtensionCodeFailure,nil); + }else + { + callback(FATExtensionCodeSuccess,result); + } + }]; + }]; + success(@{}); +} + +@end diff --git a/ios/Classes/MopPlugin.h b/ios/Classes/MopPlugin.h index c61a8e6..6c2a24e 100644 --- a/ios/Classes/MopPlugin.h +++ b/ios/Classes/MopPlugin.h @@ -6,5 +6,7 @@ @interface MopPlugin : NSObject @property MopEventStream *mopEventStreamHandler; +@property FlutterMethodChannel *methodChannel; ++ (instancetype) instance; @end diff --git a/ios/Classes/MopPlugin.m b/ios/Classes/MopPlugin.m index 8620415..0ee5cb8 100644 --- a/ios/Classes/MopPlugin.m +++ b/ios/Classes/MopPlugin.m @@ -27,20 +27,27 @@ @implementation MopPlugin + +static MopPlugin *_instance; + + (void)registerWithRegistrar:(NSObject*)registrar { FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"mop" binaryMessenger:[registrar messenger]]; - MopPlugin* instance = [[MopPlugin alloc] init]; - [registrar addMethodCallDelegate:instance channel:channel]; - - FlutterEventChannel *mopEventChannel = [FlutterEventChannel eventChannelWithName:@"plugins.mop.finogeeks.com/mop_event" binaryMessenger:[registrar messenger]]; - instance.mopEventStreamHandler = [[MopEventStream alloc] init]; - [mopEventChannel setStreamHandler:instance.mopEventStreamHandler]; - + _instance = [[MopPlugin alloc] init]; + [registrar addMethodCallDelegate:_instance channel:channel]; + _instance.methodChannel = channel; + FlutterEventChannel *mopEventChannel = [FlutterEventChannel eventChannelWithName:@"plugins.mop.finogeeks.com/mop_event" binaryMessenger:[registrar messenger]]; + _instance.mopEventStreamHandler = [[MopEventStream alloc] init]; + [mopEventChannel setStreamHandler:_instance.mopEventStreamHandler]; } ++ (instancetype)instance{ + return _instance; +} + + - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if ([@"getPlatformVersion" isEqualToString:call.method]) { result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]); diff --git a/ios/mop.podspec b/ios/mop.podspec index c08454b..005a141 100644 --- a/ios/mop.podspec +++ b/ios/mop.podspec @@ -16,7 +16,7 @@ A new flutter plugin project. s.public_header_files = 'Classes/**/*.h' s.dependency 'Flutter' s.ios.deployment_target = '8.0' - - s.dependency 'FinApplet' + + s.dependency 'FinApplet', '2.1.14' end diff --git a/lib/api.dart b/lib/api.dart new file mode 100644 index 0000000..c1af4cd --- /dev/null +++ b/lib/api.dart @@ -0,0 +1,33 @@ +class CustomMenu { + int menuId; + String image; + String title; + + Map toJson() => + {"menuId": menuId, "image": image, "title": title}; +} + +abstract class AppletHandler { + /// + /// 转发小程序 + /// + /// + /// + void forwardApplet(Map appletInfo); + + /// + ///获取用户信息 + /// "userId" + /// "nickName" + /// "avatarUrl" + /// "jwt" + /// "accessToken" + /// + Future> getUserInfo(); + + /// 获取自定义菜单 + Future> getCustomMenus(String appId); + + ///自定义菜单点击处理 + Future onCustomMenuClick(String appId, int menuId); +} diff --git a/lib/mop.dart b/lib/mop.dart index e33c5df..cdb0821 100644 --- a/lib/mop.dart +++ b/lib/mop.dart @@ -1,10 +1,14 @@ import 'dart:async'; +import 'dart:math'; import 'package:flutter/services.dart'; +import 'package:mop/api.dart'; typedef MopEventCallback = void Function(dynamic event); typedef MopEventErrorCallback = void Function(dynamic event); +typedef ExtensionApiHandler = Future Function(dynamic params); + class Mop { static final Mop _instance = new Mop._internal(); MethodChannel _channel; @@ -12,6 +16,8 @@ class Mop { int eventId = 0; List> _mopEventQueye = >[]; + Map _extensionApis = {}; + factory Mop() { return _instance; } @@ -20,6 +26,7 @@ class Mop { print('mop: _internal'); // init _channel = new MethodChannel('mop'); + _channel.setMethodCallHandler(_handlePlatformMethodCall); _mopEventChannel = new EventChannel('plugins.mop.finogeeks.com/mop_event'); _mopEventChannel.receiveBroadcastStream().listen((dynamic value) { print('matrix: receiveBroadcastStream $value'); @@ -40,6 +47,17 @@ class Mop { return version; } + Future _handlePlatformMethodCall(MethodCall call) async { + print("_handlePlatformMethodCall: method:${call.method}"); + if (call.method.startsWith("extensionApi:")) { + final name = call.method.substring("extensionApi:".length); + final handler = _extensionApis[name]; + if (handler != null) { + return await handler(call.arguments); + } + } + } + /// /// /// initialize mop miniprogram engine. @@ -72,17 +90,71 @@ class Mop { /// Future openApplet(final String appId, {final String path, final String query, final int sequence}) async { - Map params; - if (path != '') { - params = { - 'appId': appId, - 'params': {'path': path, 'query': query} - }; - } else { - params = {'appId': appId}; - } + Map params = {'appId': appId}; + Map param = {}; + if (path != null) param["path"] = path; + if (query != null) param["query"] = query; + if (param.length > 0) params["params"] = param; if (sequence != null) params["sequence"] = sequence; final Map ret = await _channel.invokeMethod('openApplet', params); return ret; } + + /// + /// get current using applet + /// 获取当前正在使用的小程序信息 + /// {appId,name,icon,description,version,thumbnail} + /// + /// + Future> currentApplet() async { + final ret = await _channel.invokeMapMethod("currentApplet"); + return Map.from(ret); + } + + /// + /// close all running applets + /// 关闭当前打开的所有小程序 + /// + Future closeAllApplets() async { + return await _channel.invokeMethod("closeAllApplets"); + } + + /// + /// clear applets cache + /// 清除缓存的小程序 + /// + Future clearApplets() async { + return await _channel.invokeMethod("clearApplets"); + } + + /// + /// register handler to provide custom info or behaviour + /// 注册小程序事件处理 + /// + void registerAppletHandler(AppletHandler handler) { + _extensionApis["forwardApplet"] = (params) async { + handler.forwardApplet(Map.from(params["appletInfo"])); + }; + _extensionApis["getUserInfo"] = (params) { + return handler.getUserInfo(); + }; + _extensionApis["getCustomMenus"] = (params) async { + final res = await handler.getCustomMenus(params["appId"]); + res?.map((e) => e.toJson()); + return res; + }; + _extensionApis["onCustomMenuClick"] = (params) { + return handler.onCustomMenuClick(params["appId"], params["menuId"]); + }; + _channel.invokeMethod("registerAppletHandler"); + } + + /// + /// register extension api + /// 注册拓展api + /// + void registerExtensionApi(String name, ExtensionApiHandler handler) { + _extensionApis[name] = handler; + _channel.invokeMethod("registerExtensionApi", {"name": name}); + } } diff --git a/publish.sh b/publish.sh old mode 100644 new mode 100755 diff --git a/pubspec.yaml b/pubspec.yaml index 2286e24..e7226c0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,9 +1,6 @@ name: mop description: A Finogeeks MiniProgram Flutter SDK. -version: 0.5.0 -authors: - - finogeeks - - kang34814 +version: 0.6.0 homepage: https://github.com/finogeeks/mop-flutter-sdk environment: