提交 ba1f5abe authored 作者: JarvanMo's avatar JarvanMo

message from wechat

上级 7034b2f6
...@@ -29,6 +29,8 @@ import io.flutter.plugin.common.MethodChannel ...@@ -29,6 +29,8 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry import io.flutter.plugin.common.PluginRegistry
import java.util.concurrent.atomic.AtomicBoolean
/** FluwxPlugin */ /** FluwxPlugin */
class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
...@@ -47,6 +49,9 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, ...@@ -47,6 +49,9 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
private var fluwxChannel: MethodChannel? = null private var fluwxChannel: MethodChannel? = null
private var context: Context? = null private var context: Context? = null
private val attemptToResumeMsgFromWxFlag = AtomicBoolean(false)
private var activityPluginBinding: ActivityPluginBinding? = null
private fun handelIntent(intent: Intent) { private fun handelIntent(intent: Intent) {
intent.getStringExtra(KEY_FLUWX_REQUEST_INFO_EXT_MSG)?.let { intent.getStringExtra(KEY_FLUWX_REQUEST_INFO_EXT_MSG)?.let {
...@@ -96,10 +101,22 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, ...@@ -96,10 +101,22 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
call.method == "openWeChatInvoice" -> openWeChatInvoice(call, result) call.method == "openWeChatInvoice" -> openWeChatInvoice(call, result)
call.method == "openUrl" -> openUrl(call, result) call.method == "openUrl" -> openUrl(call, result)
call.method == "openRankList" -> openRankList(result) call.method == "openRankList" -> openRankList(result)
call.method == "attemptToResumeMsgFromWx" -> attemptToResumeMsgFromWx(result)
else -> result.notImplemented() else -> result.notImplemented()
} }
} }
private fun attemptToResumeMsgFromWx(result: Result) {
if (attemptToResumeMsgFromWxFlag.compareAndSet(false, true)) {
activityPluginBinding?.activity?.intent?.let {
FluwxRequestHandler.handleRequestInfoFromIntent(it)
}
result.success(null)
} else {
result.error("attemptToResumeMsgFromWx error", null, null)
}
}
private fun openWeChatInvoice(call: MethodCall, result: Result) { private fun openWeChatInvoice(call: MethodCall, result: Result) {
if (WXAPiHandler.wxApi == null) { if (WXAPiHandler.wxApi == null) {
result.error("Unassigned WxApi", "please config wxapi first", null) result.error("Unassigned WxApi", "please config wxapi first", null)
...@@ -127,6 +144,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, ...@@ -127,6 +144,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
shareHandler?.onDestroy() shareHandler?.onDestroy()
authHandler?.removeAllListeners() authHandler?.removeAllListeners()
activityPluginBinding = null
} }
override fun onDetachedFromActivity() { override fun onDetachedFromActivity() {
...@@ -141,6 +159,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, ...@@ -141,6 +159,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
override fun onAttachedToActivity(binding: ActivityPluginBinding) { override fun onAttachedToActivity(binding: ActivityPluginBinding) {
// WXAPiHandler.setContext(binding.activity.applicationContext) // WXAPiHandler.setContext(binding.activity.applicationContext)
activityPluginBinding = binding
handelIntent(binding.activity.intent) handelIntent(binding.activity.intent)
FluwxRequestHandler.handleRequestInfoFromIntent(binding.activity.intent) FluwxRequestHandler.handleRequestInfoFromIntent(binding.activity.intent)
shareHandler?.permissionHandler = PermissionHandler(binding.activity) shareHandler?.permissionHandler = PermissionHandler(binding.activity)
...@@ -287,6 +306,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, ...@@ -287,6 +306,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
result.success(false) result.success(false)
} }
} }
private fun openRankList(result: Result) { private fun openRankList(result: Result) {
val req = OpenRankList.Req() val req = OpenRankList.Req()
WXAPiHandler.wxApi?.sendReq(req, SendReqCallback { WXAPiHandler.wxApi?.sendReq(req, SendReqCallback {
......
...@@ -29,6 +29,7 @@ import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_BUNDLE ...@@ -29,6 +29,7 @@ import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_BUNDLE
import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_EXT_MSG import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_EXT_MSG
import com.jarvan.fluwx.utils.startFlutterActivity import com.jarvan.fluwx.utils.startFlutterActivity
import com.tencent.mm.opensdk.modelbase.BaseReq import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelmsg.LaunchFromWX
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX
import java.security.cert.Extension import java.security.cert.Extension
...@@ -42,6 +43,8 @@ object FluwxRequestHandler { ...@@ -42,6 +43,8 @@ object FluwxRequestHandler {
val type = getInt("_wxapi_command_type", -9999) val type = getInt("_wxapi_command_type", -9999)
if (type == 4) { if (type == 4) {
handleShowMessageFromWXBundle(this) handleShowMessageFromWXBundle(this)
} else if (type == 6) {
handleWXLaunchFromWXBundle(this)
} }
} }
} }
...@@ -49,45 +52,65 @@ object FluwxRequestHandler { ...@@ -49,45 +52,65 @@ object FluwxRequestHandler {
private fun handleShowMessageFromWXBundle(bundle: Bundle) = private fun handleShowMessageFromWXBundle(bundle: Bundle) =
handleWXShowMessageFromWX(ShowMessageFromWX.Req(bundle)) handleWXShowMessageFromWX(ShowMessageFromWX.Req(bundle))
private fun handleWXLaunchFromWXBundle(bundle: Bundle) =
handleWXLaunchFromWX(LaunchFromWX.Req(bundle))
private fun handleRequest(req: BaseReq) { private fun handleRequest(req: BaseReq) {
when (req) { when (req) {
is ShowMessageFromWX.Req -> handleWXShowMessageFromWX(req) is ShowMessageFromWX.Req -> handleWXShowMessageFromWX(req)
is LaunchFromWX.Req -> handleWXLaunchFromWX(req)
} }
} }
private fun handleWXShowMessageFromWX(req: ShowMessageFromWX.Req) { private fun handleWXShowMessageFromWX(req: ShowMessageFromWX.Req) {
val result = mapOf( val result = mapOf(
"extMsg" to req.message.messageExt "extMsg" to req.message.messageExt,
"messageAction" to req.message.messageAction,
"description" to req.message.description,
"lang" to req.lang,
"description" to req.country,
) )
FluwxPlugin.extMsg = req.message.messageExt;
FluwxPlugin.extMsg = req.message.messageExt
FluwxPlugin.callingChannel?.invokeMethod("onWXShowMessageFromWX", result) FluwxPlugin.callingChannel?.invokeMethod("onWXShowMessageFromWX", result)
} }
private fun handleWXLaunchFromWX(req: LaunchFromWX.Req) {
val result = mapOf(
"extMsg" to req.messageExt,
"messageAction" to req.messageAction,
"lang" to req.lang,
"country" to req.country,
)
FluwxPlugin.callingChannel?.invokeMethod("onWXLaunchFromWX", result)
}
private fun defaultOnReqDelegate(baseReq: BaseReq, activity: Activity) { private fun defaultOnReqDelegate(baseReq: BaseReq, activity: Activity) {
// FIXME: 可能是官方的Bug,从微信拉起APP的Intent类型不对,无法跳转回Flutter Activity // FIXME: 可能是官方的Bug,从微信拉起APP的Intent类型不对,无法跳转回Flutter Activity
// 稳定复现场景:微信版本为7.0.5,小程序SDK为2.7.7 // 稳定复现场景:微信版本为7.0.5,小程序SDK为2.7.7
if (baseReq.type == 4) {
// com.tencent.mm.opensdk.constants.ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX = 4 // com.tencent.mm.opensdk.constants.ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX = 4
if (!WXAPiHandler.coolBoot) { if (!WXAPiHandler.coolBoot) {
handleRequest(baseReq) handleRequest(baseReq)
activity.startFlutterActivity() activity.startFlutterActivity()
} else { } else {
when (baseReq) { when (baseReq) {
is ShowMessageFromWX.Req -> { is ShowMessageFromWX.Req -> {
activity.startFlutterActivity( activity.startFlutterActivity(
wxRequestBundle = Bundle().apply { wxRequestBundle = Bundle().apply {
baseReq.toBundle(this) baseReq.toBundle(this)
}, },
bundle = Bundle().apply { bundle = Bundle().apply {
putString( putString(
KEY_FLUWX_REQUEST_INFO_EXT_MSG, KEY_FLUWX_REQUEST_INFO_EXT_MSG,
baseReq.message.messageExt baseReq.message.messageExt
) )
}) })
WXAPiHandler.coolBoot = false WXAPiHandler.coolBoot = false
}
} }
} }
} }
} }
......
差异被折叠。
...@@ -69,7 +69,7 @@ The capability of implementing WeChat SDKs in Flutter. With Fluwx, developers ca ...@@ -69,7 +69,7 @@ The capability of implementing WeChat SDKs in Flutter. With Fluwx, developers ca
end end
s.subspec 'no_pay' do |sp| s.subspec 'no_pay' do |sp|
sp.dependency 'OpenWeChatSDKNoPay','~> 2.0.2+1' sp.dependency 'OpenWeChatSDKNoPay','~> 2.0.2+2'
sp.frameworks = 'CoreGraphics', 'Security', 'WebKit' sp.frameworks = 'CoreGraphics', 'Security', 'WebKit'
sp.libraries = 'c++', 'z', 'sqlite3.0' sp.libraries = 'c++', 'z', 'sqlite3.0'
if debug_logging if debug_logging
......
...@@ -94,6 +94,12 @@ class Fluwx { ...@@ -94,6 +94,12 @@ class Fluwx {
return FluwxPlatform.instance.pay(which); return FluwxPlatform.instance.pay(which);
} }
/// Try to reload data from cold boot. For example, the app is launched by mini program and
/// we can get ext message by calling this.
Future<void> attemptToResumeMsgFromWx() async {
return FluwxPlatform.instance.attemptToResumeMsgFromWx();
}
/// Subscribe responses from WeChat /// Subscribe responses from WeChat
subscribeResponse(Function(WeChatResponse response) listener) { subscribeResponse(Function(WeChatResponse response) listener) {
_responseListeners.add(listener); _responseListeners.add(listener);
......
...@@ -183,9 +183,7 @@ class MethodChannelFluwx extends FluwxPlatform { ...@@ -183,9 +183,7 @@ class MethodChannelFluwx extends FluwxPlatform {
switch (which) { switch (which) {
case Payment(): case Payment():
return await methodChannel.invokeMethod( return await methodChannel.invokeMethod(
'payWithFluwx', 'payWithFluwx', which.arguments);
which.arguments
);
case HongKongWallet(): case HongKongWallet():
return await methodChannel.invokeMethod( return await methodChannel.invokeMethod(
'payWithHongKongWallet', which.arguments); 'payWithHongKongWallet', which.arguments);
...@@ -205,6 +203,11 @@ class MethodChannelFluwx extends FluwxPlatform { ...@@ -205,6 +203,11 @@ class MethodChannelFluwx extends FluwxPlatform {
return await methodChannel.invokeMethod('stopAuthByQRCode'); return await methodChannel.invokeMethod('stopAuthByQRCode');
} }
@override
Future<void> attemptToResumeMsgFromWx() async {
return await methodChannel.invokeMethod("attemptToResumeMsgFromWx");
}
@override @override
Future<bool> get isSupportOpenBusinessView async => Future<bool> get isSupportOpenBusinessView async =>
await methodChannel.invokeMethod("checkSupportOpenBusinessView"); await methodChannel.invokeMethod("checkSupportOpenBusinessView");
......
...@@ -113,6 +113,10 @@ abstract class FluwxPlatform extends PlatformInterface { ...@@ -113,6 +113,10 @@ abstract class FluwxPlatform extends PlatformInterface {
throw UnimplementedError('authBy() has not been implemented.'); throw UnimplementedError('authBy() has not been implemented.');
} }
Future<void> attemptToResumeMsgFromWx() {
throw UnimplementedError('authBy() has not been implemented.');
}
Future<bool> get isSupportOpenBusinessView async { Future<bool> get isSupportOpenBusinessView async {
throw UnimplementedError( throw UnimplementedError(
'isSupportOpenBusinessView() has not been implemented.'); 'isSupportOpenBusinessView() has not been implemented.');
......
...@@ -48,6 +48,8 @@ Map<String, _WeChatResponseInvoker> _nameAndResponseMapper = { ...@@ -48,6 +48,8 @@ Map<String, _WeChatResponseInvoker> _nameAndResponseMapper = {
WeChatOpenBusinessViewResponse.fromMap(argument), WeChatOpenBusinessViewResponse.fromMap(argument),
"onOpenWechatInvoiceResponse": (Map argument) => "onOpenWechatInvoiceResponse": (Map argument) =>
WeChatOpenInvoiceResponse.fromMap(argument), WeChatOpenInvoiceResponse.fromMap(argument),
"onWXLaunchFromWX": (Map argument) =>
WeChatLaunchFromWXRequest.fromMap(argument),
}; };
sealed class WeChatResponse { sealed class WeChatResponse {
...@@ -213,8 +215,32 @@ class WeChatQRCodeScannedResponse extends WeChatResponse { ...@@ -213,8 +215,32 @@ class WeChatQRCodeScannedResponse extends WeChatResponse {
// 获取微信打开App时携带的参数 // 获取微信打开App时携带的参数
class WeChatShowMessageFromWXRequest extends WeChatResponse { class WeChatShowMessageFromWXRequest extends WeChatResponse {
final String? country;
final String? lang;
final String? messageAction;
final String? description;
WeChatShowMessageFromWXRequest.fromMap(Map map) WeChatShowMessageFromWXRequest.fromMap(Map map)
: extMsg = map['extMsg'], : extMsg = map['extMsg'],
country = map['country'],
messageAction = map['messageAction'],
description = map["description"],
lang = map["lang"],
super._(0, '');
final String? extMsg;
}
class WeChatLaunchFromWXRequest extends WeChatResponse {
final String? country;
final String? lang;
final String? messageAction;
WeChatLaunchFromWXRequest.fromMap(Map map)
: extMsg = map['extMsg'],
country = map['country'],
messageAction = map['messageAction'],
lang = map["lang"],
super._(0, ''); super._(0, '');
final String? extMsg; final String? extMsg;
......
...@@ -90,6 +90,12 @@ class MockFluwxPlatform ...@@ -90,6 +90,12 @@ class MockFluwxPlatform
throw UnimplementedError(); throw UnimplementedError();
} }
@override
Future<void> attemptToResumeMsgFromWx() {
// TODO: implement attemptToResumeMsgFromWx
throw UnimplementedError();
}
} }
void main() { void main() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论