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

message from wechat

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