From f0974bf1ee4e647c00f749707017841dc3685436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cyanghuaxuan=E2=80=9D?= <“646903573@qq.com”> Date: Wed, 24 Feb 2021 14:30:58 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96android=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=90=8E=E7=9A=84=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flutter_alibc/FlutterAlibcHandle.java | 42 +++++++++++++++---- .../com/wxwx/flutter_alibc/WebActivity.java | 5 ++- lib/alibc_model.dart | 4 +- lib/flutter_alibc.dart | 20 +++++++-- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/android/src/main/java/com/wxwx/flutter_alibc/FlutterAlibcHandle.java b/android/src/main/java/com/wxwx/flutter_alibc/FlutterAlibcHandle.java index 2f5a0a1..9345d93 100644 --- a/android/src/main/java/com/wxwx/flutter_alibc/FlutterAlibcHandle.java +++ b/android/src/main/java/com/wxwx/flutter_alibc/FlutterAlibcHandle.java @@ -25,6 +25,7 @@ import com.alibaba.baichuan.android.trade.AlibcTradeSDK; import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback; import com.alibaba.baichuan.trade.common.utils.AlibcLogger; +import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry.Registrar; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.MethodCall; @@ -49,14 +50,16 @@ public class FlutterAlibcHandle { private static FlutterAlibcHandle handle; private static Registrar register; + private static boolean isFirst = true; + private static Result result; //第一次调用getInstance register不能为空 public static FlutterAlibcHandle getInstance(Registrar register) { - synchronized (FlutterAlibcHandle.class) { - handle = new FlutterAlibcHandle(); - FlutterAlibcHandle.register = register; - } + synchronized (FlutterAlibcHandle.class) { + handle = new FlutterAlibcHandle(); + FlutterAlibcHandle.register = register; + } return handle; } @@ -169,6 +172,7 @@ public class FlutterAlibcHandle { intent.putExtra("url", url); intent.putExtra("arguments", map); register.activity().startActivity(intent); + } /** @@ -180,12 +184,35 @@ public class FlutterAlibcHandle { public void openByUrl(MethodCall call, Result result) { AlibcShowParams showParams = new AlibcShowParams(); AlibcTaokeParams taokeParams = new AlibcTaokeParams("", "", ""); - + FlutterAlibcHandle.result=result; showParams.setBackUrl(call.argument(key_BackUrl)); ///渠道授权 if ((Boolean) call.argument(login_And_Auth)) { ///先APP授权 + if(isFirst){ + isFirst=false; + register.addActivityResultListener(new PluginRegistry.ActivityResultListener() { + @Override + public boolean onActivityResult(int requestCode, int resultCode, Intent data) { + if (FlutterAlibcHandle.result == null) { + return false; + } + if (requestCode == 1111) { + if (resultCode == 222222) { + FlutterAlibcHandle.result.success(true); + } else if (resultCode == 111111) { + FlutterAlibcHandle.result.success(false); + } + + return true; + } else { + return false; + } + + } + }); + } if (!AlibcLogin.getInstance().isLogin()) { @@ -195,7 +222,8 @@ public class FlutterAlibcHandle { Log.d("阿里百川打开WebView", "webview"); Intent intent = new Intent(register.context(), WebActivity.class); intent.putExtra("url", (String) call.argument("url")); - register.activity().startActivity(intent); + register.activity().startActivityForResult(intent, 1111); + } @Override @@ -207,7 +235,7 @@ public class FlutterAlibcHandle { Log.d("阿里百川打开WebView", "webview"); Intent intent = new Intent(register.context(), WebActivity.class); intent.putExtra("url", (String) call.argument("url")); - register.activity().startActivity(intent); + register.activity().startActivityForResult(intent, 1111); } ///后渠道信息 return; diff --git a/android/src/main/java/com/wxwx/flutter_alibc/WebActivity.java b/android/src/main/java/com/wxwx/flutter_alibc/WebActivity.java index 73deb06..92ab92c 100644 --- a/android/src/main/java/com/wxwx/flutter_alibc/WebActivity.java +++ b/android/src/main/java/com/wxwx/flutter_alibc/WebActivity.java @@ -169,10 +169,13 @@ public class WebActivity extends Activity { try { if (html.contains("成功")) {//成功 - Toast.makeText(WebActivity.this, "授权成功", Toast.LENGTH_SHORT).show(); + // Toast.makeText(WebActivity.this, "授权成功", Toast.LENGTH_SHORT).show(); + setResult(222222); finish(); } else {//失败 + setResult(111111); mainWeb.setAlpha(0); + finish(); } } catch (Exception e) { diff --git a/lib/alibc_model.dart b/lib/alibc_model.dart index b831a5e..7e91142 100644 --- a/lib/alibc_model.dart +++ b/lib/alibc_model.dart @@ -50,9 +50,9 @@ class TaokeParams { // url打开或者page打开等的数据 class TradeResult { // 错误码,0为成功,非0为失败 - final String errorCode; + String errorCode; // 错误信息 - final String errorMessage; + String errorMessage; TradeResultType type; diff --git a/lib/flutter_alibc.dart b/lib/flutter_alibc.dart index e5ef406..0c35482 100644 --- a/lib/flutter_alibc.dart +++ b/lib/flutter_alibc.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -20,6 +21,7 @@ class FlutterAlibc { // 通信的桥接类 static final MethodChannel _channel = const MethodChannel("flutter_alibc"); + // ..setMethodCallHandler(_handler); static Future get platformVersion async { @@ -125,10 +127,10 @@ class FlutterAlibc { AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall, TaokeParams taokeParams, String backUrl, - bool isAuth=false + bool isAuth = false }) async { Map taoKe = AlibcTools.getTaokeMap(taokeParams); - Map result = await _channel.invokeMethod("openByUrl", { + var result = await _channel.invokeMethod("openByUrl", { "url": url, "openType": openType.index, "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode, @@ -136,9 +138,21 @@ class FlutterAlibc { "schemeType": schemeType.index, "taokeParams": taoKe, "backUrl": backUrl, - "auth":isAuth + "auth": isAuth }); + if (isAuth && Platform.isAndroid) { + TradeResult tradeResult = TradeResult("-1", ""); + if (result != null && result) { + ///授权成功 + tradeResult.errorCode = "0"; + return tradeResult; + } else { + ///授权失败 + tradeResult.errorCode = "-1"; + return tradeResult; + } + } TradeResult tradeResult = AlibcTools.getTradeResult(result); return tradeResult; }