1
0
Fork 0

添加小程序登录授权示例

master
zhongweiguang 2021-07-17 11:12:48 +08:00
parent c94335bb1a
commit 1fde07b0f1
7 changed files with 201 additions and 5 deletions

View File

@ -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"
}

View File

@ -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");
}
});
}
}

View File

@ -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<IApi> getRegisterExtensionApis(@NotNull Activity activity) {
ArrayList<IApi> apis = new ArrayList<>();
apis.add(new LoginApi(activity));
apis.add(new ProfileApi());
return apis;
}
@Nullable
@Override
public List<IApi> 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<String> 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

View File

@ -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<String>() {
@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) {
}
});
}
}

View File

@ -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);
}
}
}

View File

@ -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" />
<Button
android:id="@+id/btn_applet_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/fin_clip_applet_login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_h5_api" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -5,6 +5,7 @@
<string name="fin_clip_profile_applet">智能对账单</string>
<string name="fin_clip_custom_api">自定义小程序API示例</string>
<string name="fin_clip_custom_h5_api">自定义H5 API示例</string>
<string name="fin_clip_applet_login">小程序登录授权示例</string>
<string name="fin_clip_input_content_hint">请输入内容</string>
<string name="fin_clip_confirm">确定</string>
<string name="fin_clip_get_input_content_failed">获取输入内容失败</string>