From 1fde07b0f1462dacd7756967c2a416883ae2a75c Mon Sep 17 00:00:00 2001 From: zhongweiguang Date: Sat, 17 Jul 2021 11:12:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=8E=88=E6=9D=83=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 +- .../com/finogeeks/mop/demo/MainActivity.java | 8 ++ .../finogeeks/mop/demo/MopApplication.java | 58 +++++++++++- .../mop/demo/customapi/user/LoginApi.java | 92 +++++++++++++++++++ .../mop/demo/customapi/user/ProfileApi.java | 32 +++++++ app/src/main/res/layout/activity_main.xml | 13 ++- app/src/main/res/values/strings.xml | 1 + 7 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/finogeeks/mop/demo/customapi/user/LoginApi.java create mode 100644 app/src/main/java/com/finogeeks/mop/demo/customapi/user/ProfileApi.java diff --git a/app/build.gradle b/app/build.gradle index 22d841e..f604629 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -69,6 +69,6 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - implementation 'com.finogeeks.lib:finapplet:2.24.3' + implementation 'com.finogeeks.lib:finapplet:2.25.1' implementation "com.tencent.tbs.tbssdk:sdk:43967" } \ No newline at end of file diff --git a/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java b/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java index 32b273a..cc0c645 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java +++ b/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java @@ -64,5 +64,13 @@ public class MainActivity extends AppCompatActivity { FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fc8934aefb8c600019e9747", params); } }); + + Button btnAppletLogin = findViewById(R.id.btn_applet_login); + btnAppletLogin.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "60f051ea525ea10001c0bd22"); + } + }); } } \ No newline at end of file diff --git a/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java b/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java index 23e0898..04a98e0 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java +++ b/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java @@ -1,5 +1,6 @@ package com.finogeeks.mop.demo; +import android.app.Activity; import android.graphics.Color; import android.widget.Toast; @@ -7,20 +8,47 @@ import androidx.multidex.MultiDexApplication; import com.finogeeks.lib.applet.client.FinAppClient; import com.finogeeks.lib.applet.client.FinAppConfig; +import com.finogeeks.lib.applet.client.FinAppProcessClient; import com.finogeeks.lib.applet.interfaces.FinCallback; +import com.finogeeks.lib.applet.interfaces.IApi; +import com.finogeeks.lib.applet.sdk.api.IAppletApiManager; import com.finogeeks.mop.demo.customapi.CustomApi; import com.finogeeks.mop.demo.customapi.CustomH5Api; +import com.finogeeks.mop.demo.customapi.user.LoginApi; +import com.finogeeks.mop.demo.customapi.user.ProfileApi; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; public class MopApplication extends MultiDexApplication { - private static final String TAG = "SampleApplication"; - @Override public void onCreate() { super.onCreate(); if (FinAppClient.INSTANCE.isFinAppProcess(this)) { - // 小程序进程不执行任何初始化操作 + // 小程序进程 + // 小程序进程中注册api的方法能获取到小程序所在activity对象,可以用做创建对话框的context参数) + FinAppProcessClient.INSTANCE.setCallback(new FinAppProcessClient.Callback() { + @Override + public List getRegisterExtensionApis(@NotNull Activity activity) { + ArrayList apis = new ArrayList<>(); + apis.add(new LoginApi(activity)); + apis.add(new ProfileApi()); + return apis; + } + + @Nullable + @Override + public List getRegisterExtensionWebApis(@NotNull Activity activity) { + return null; + } + }); return; } @@ -77,6 +105,30 @@ public class MopApplication extends MultiDexApplication { FinAppClient.INSTANCE.getExtensionWebApiManager().registerApi(new CustomH5Api(MopApplication.this)); // 设置IAppletHandler实现类 FinAppClient.INSTANCE.setAppletHandler(new AppletHandler(getApplicationContext())); + + // 在主进程设置"小程序进程调用主进程"的处理方法 + // 开发者也可以选择在主进程其他合适的代码位置设置处理方法 + FinAppClient.INSTANCE.getAppletApiManager() + .setAppletProcessCallHandler(new IAppletApiManager.AppletProcessCallHandler() { + @Override + public void onAppletProcessCall(@NotNull String name, + @Nullable String params, + @Nullable FinCallback callback) { + if (callback != null) { + if (name.equals(LoginApi.API_NAME_LOGIN)) { + // 从主进程获取登录信息,返回给小程序进程 + // 这里返回的是虚拟的用户登录信息,开发者请从APP里面自行获取用户登录信息 + JSONObject jsonObject = new JSONObject(); + try { + jsonObject.put("userId", "123"); + } catch (JSONException e) { + e.printStackTrace(); + } + callback.onSuccess(jsonObject.toString()); + } + } + } + }); } @Override diff --git a/app/src/main/java/com/finogeeks/mop/demo/customapi/user/LoginApi.java b/app/src/main/java/com/finogeeks/mop/demo/customapi/user/LoginApi.java new file mode 100644 index 0000000..65dad0b --- /dev/null +++ b/app/src/main/java/com/finogeeks/mop/demo/customapi/user/LoginApi.java @@ -0,0 +1,92 @@ +package com.finogeeks.mop.demo.customapi.user; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; + +import com.finogeeks.lib.applet.api.AbsApi; +import com.finogeeks.lib.applet.client.FinAppProcessClient; +import com.finogeeks.lib.applet.interfaces.FinCallback; +import com.finogeeks.lib.applet.interfaces.ICallback; + +import org.json.JSONException; +import org.json.JSONObject; + +public class LoginApi extends AbsApi { + + public final static String API_NAME_LOGIN = "login"; // 小程序基础库调用的api名称 + private final Activity activity; + + public LoginApi(Activity activity) { + this.activity = activity; + } + + @Override + public String[] apis() { + return new String[]{API_NAME_LOGIN}; + } + + @Override + public void invoke(String event, JSONObject param, ICallback callback) { + if (event.equals(API_NAME_LOGIN)) { + showAuthDialog(callback); + } + } + + /** + * 显示获取用户登录信息的授权提示对话框 + */ + private void showAuthDialog(final ICallback callback) { + // 是否需要显示授权提示对话框请开发者按照产品需求自行处理 + new AlertDialog.Builder(activity) + .setTitle("是否同意授权获取用户登录信息?") + .setNegativeButton("拒绝", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + callback.onFail(); + } + }) + .setPositiveButton("同意", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + loginMainProcess(callback); + } + }) + .show(); + } + + /** + * 从主进程获取用户登录信息 + */ + private void loginMainProcess(final ICallback callback) { + // 小程序进程调用主进程,在主进程获取用户信息后返回给小程序进程 + FinAppProcessClient.INSTANCE.getAppletProcessApiManager() + .callInMainProcess(API_NAME_LOGIN, null, new FinCallback() { + @Override + public void onSuccess(final String result) { + // 需要在主线程调用callback方法 + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + try { + callback.onSuccess(new JSONObject(result)); + } catch (JSONException e) { + e.printStackTrace(); + callback.onFail(); + } + } + }); + } + + @Override + public void onError(int code, String error) { + callback.onFail(); + } + + @Override + public void onProgress(int status, String info) { + } + }); + } + +} diff --git a/app/src/main/java/com/finogeeks/mop/demo/customapi/user/ProfileApi.java b/app/src/main/java/com/finogeeks/mop/demo/customapi/user/ProfileApi.java new file mode 100644 index 0000000..32cd114 --- /dev/null +++ b/app/src/main/java/com/finogeeks/mop/demo/customapi/user/ProfileApi.java @@ -0,0 +1,32 @@ +package com.finogeeks.mop.demo.customapi.user; + +import com.finogeeks.lib.applet.api.AbsApi; +import com.finogeeks.lib.applet.interfaces.ICallback; + +import org.json.JSONException; +import org.json.JSONObject; + +public class ProfileApi extends AbsApi { + + private final static String API_NAME_GET_USER_PROFILE = "getUserProfile"; // 小程序基础库调用的api名称 + + @Override + public String[] apis() { + return new String[]{API_NAME_GET_USER_PROFILE}; + } + + @Override + public void invoke(String event, JSONObject param, ICallback callback) { + if (event.equals(API_NAME_GET_USER_PROFILE)) { + JSONObject jsonObject = new JSONObject(); + try { + // 这里返回的是虚拟的用户个人信息,开发者请从APP里面自行获取用户个人信息 + jsonObject.put("nickName", "张三"); + } catch (JSONException e) { + e.printStackTrace(); + } + callback.onSuccess(jsonObject); + } + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8f6bb0f..e49549d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -67,8 +67,19 @@ android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="@string/fin_clip_custom_h5_api" - app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintBottom_toTopOf="@+id/btn_applet_login" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_custom_api" /> + +