mirror of
https://github.com/RxReader/wechat_kit.git
synced 2025-08-26 08:41:03 +08:00
整理
This commit is contained in:
@ -45,6 +45,7 @@ import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import io.flutter.embedding.engine.plugins.FlutterPlugin;
|
import io.flutter.embedding.engine.plugins.FlutterPlugin;
|
||||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
|
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
|
||||||
@ -170,6 +171,7 @@ public final class WechatKitPlugin implements FlutterPlugin, ActivityAware, Plug
|
|||||||
private final IDiffDevOAuth qrauth = DiffDevOAuthFactory.getDiffDevOAuth();
|
private final IDiffDevOAuth qrauth = DiffDevOAuthFactory.getDiffDevOAuth();
|
||||||
|
|
||||||
private IWXAPI iwxapi;
|
private IWXAPI iwxapi;
|
||||||
|
private AtomicBoolean handleInitialWXReqFlag = new AtomicBoolean(false);
|
||||||
|
|
||||||
// --- FlutterPlugin
|
// --- FlutterPlugin
|
||||||
|
|
||||||
@ -377,16 +379,20 @@ public final class WechatKitPlugin implements FlutterPlugin, ActivityAware, Plug
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleInitialWXReq(@NonNull MethodCall call, @NonNull Result result) {
|
private void handleInitialWXReq(@NonNull MethodCall call, @NonNull Result result) {
|
||||||
final Activity activity = activityPluginBinding != null ? activityPluginBinding.getActivity() : null;
|
if (handleInitialWXReqFlag.compareAndSet(false, true)) {
|
||||||
if (activity != null) {
|
final Activity activity = activityPluginBinding != null ? activityPluginBinding.getActivity() : null;
|
||||||
final Intent resp = WechatCallbackActivity.extraCallback(activity.getIntent());
|
if (activity != null) {
|
||||||
if (resp != null) {
|
final Intent resp = WechatCallbackActivity.extraCallback(activity.getIntent());
|
||||||
if (iwxapi != null) {
|
if (resp != null) {
|
||||||
iwxapi.handleIntent(resp, iwxapiEventHandler);
|
if (iwxapi != null) {
|
||||||
|
iwxapi.handleIntent(resp, iwxapiEventHandler);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.success(null);
|
||||||
|
} else {
|
||||||
|
result.error("FAILED", null, null);
|
||||||
}
|
}
|
||||||
result.success(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleAuthCall(@NonNull MethodCall call, @NonNull Result result) {
|
private void handleAuthCall(@NonNull MethodCall call, @NonNull Result result) {
|
||||||
|
@ -78,6 +78,12 @@ class _HomeState extends State<Home> {
|
|||||||
),
|
),
|
||||||
body: ListView(
|
body: ListView(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
|
ListTile(
|
||||||
|
title: const Text('微信回调 - 冷启'),
|
||||||
|
onTap: () async {
|
||||||
|
await Wechat.instance.handleInitialWXReq();
|
||||||
|
},
|
||||||
|
),
|
||||||
ListTile(
|
ListTile(
|
||||||
title: const Text('环境检查'),
|
title: const Text('环境检查'),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
#import <WechatAuthSDK.h>
|
#import <WechatAuthSDK.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef void(^WechatKitWXReqRunnable)(void);
|
||||||
|
|
||||||
@interface WechatKitPlugin () <WXApiDelegate, WechatAuthAPIDelegate>
|
@interface WechatKitPlugin () <WXApiDelegate, WechatAuthAPIDelegate>
|
||||||
|
|
||||||
@end
|
@end
|
||||||
@ -14,6 +16,9 @@
|
|||||||
@implementation WechatKitPlugin {
|
@implementation WechatKitPlugin {
|
||||||
FlutterMethodChannel *_channel;
|
FlutterMethodChannel *_channel;
|
||||||
WechatAuthSDK *_qrauth;
|
WechatAuthSDK *_qrauth;
|
||||||
|
BOOL _isRunning;
|
||||||
|
BOOL _handleInitialWXReqFlag;
|
||||||
|
WechatKitWXReqRunnable _initialWXReqRunnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
|
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
|
||||||
@ -26,6 +31,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
static NSString *const METHOD_REGISTERAPP = @"registerApp";
|
static NSString *const METHOD_REGISTERAPP = @"registerApp";
|
||||||
|
static NSString *const METHOD_HANDLEINITIALWXREQ = @"handleInitialWXReq";
|
||||||
static NSString *const METHOD_ISINSTALLED = @"isInstalled";
|
static NSString *const METHOD_ISINSTALLED = @"isInstalled";
|
||||||
static NSString *const METHOD_ISSUPPORTAPI = @"isSupportApi";
|
static NSString *const METHOD_ISSUPPORTAPI = @"isSupportApi";
|
||||||
static NSString *const METHOD_ISSUPPORTSTATEAPI = @"isSupportStateAPI";
|
static NSString *const METHOD_ISSUPPORTSTATEAPI = @"isSupportStateAPI";
|
||||||
@ -49,8 +55,9 @@ static NSString *const METHOD_OPENCUSTOMERSERVICECHAT = @"openCustomerServiceCha
|
|||||||
#ifndef NO_PAY
|
#ifndef NO_PAY
|
||||||
static NSString *const METHOD_PAY = @"pay";
|
static NSString *const METHOD_PAY = @"pay";
|
||||||
#endif
|
#endif
|
||||||
static NSString *const METHOD_LAUNCHFROMWX = @"launchFromWX";
|
|
||||||
static NSString *const METHOD_SHOWMESSAGEFROMWX = @"showMessageFromWX";
|
static NSString *const METHOD_ONLAUNCHFROMWXREQ = @"onLaunchFromWXReq";
|
||||||
|
static NSString *const METHOD_ONSHOWMESSAGEFROMWXREQ = @"onShowMessageFromWXReq";
|
||||||
|
|
||||||
static NSString *const METHOD_ONAUTHRESP = @"onAuthResp";
|
static NSString *const METHOD_ONAUTHRESP = @"onAuthResp";
|
||||||
static NSString *const METHOD_ONOPENURLRESP = @"onOpenUrlResp";
|
static NSString *const METHOD_ONOPENURLRESP = @"onOpenUrlResp";
|
||||||
@ -135,6 +142,8 @@ static NSString *const ARGUMENT_KEY_RESULT_AUTHCODE = @"authCode";
|
|||||||
_channel = channel;
|
_channel = channel;
|
||||||
_qrauth = [[WechatAuthSDK alloc] init];
|
_qrauth = [[WechatAuthSDK alloc] init];
|
||||||
_qrauth.delegate = self;
|
_qrauth.delegate = self;
|
||||||
|
_isRunning = NO;
|
||||||
|
_handleInitialWXReqFlag = NO;
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@ -145,7 +154,19 @@ static NSString *const ARGUMENT_KEY_RESULT_AUTHCODE = @"authCode";
|
|||||||
NSString *appId = call.arguments[ARGUMENT_KEY_APPID];
|
NSString *appId = call.arguments[ARGUMENT_KEY_APPID];
|
||||||
NSString *universalLink = call.arguments[ARGUMENT_KEY_UNIVERSALLINK];
|
NSString *universalLink = call.arguments[ARGUMENT_KEY_UNIVERSALLINK];
|
||||||
[WXApi registerApp:appId universalLink:universalLink];
|
[WXApi registerApp:appId universalLink:universalLink];
|
||||||
|
_isRunning = YES;
|
||||||
result(nil);
|
result(nil);
|
||||||
|
} else if ([METHOD_HANDLEINITIALWXREQ isEqualToString:call.method]) {
|
||||||
|
if (!_handleInitialWXReqFlag) {
|
||||||
|
_handleInitialWXReqFlag = YES;
|
||||||
|
if (_initialWXReqRunnable != nil) {
|
||||||
|
_initialWXReqRunnable();
|
||||||
|
_initialWXReqRunnable = nil;
|
||||||
|
}
|
||||||
|
result(nil);
|
||||||
|
} else {
|
||||||
|
result([FlutterError errorWithCode:@"FAILED" message:nil details:nil]);
|
||||||
|
}
|
||||||
} else if ([METHOD_ISINSTALLED isEqualToString:call.method]) {
|
} else if ([METHOD_ISINSTALLED isEqualToString:call.method]) {
|
||||||
result([NSNumber numberWithBool:[WXApi isWXAppInstalled]]);
|
result([NSNumber numberWithBool:[WXApi isWXAppInstalled]]);
|
||||||
} else if ([METHOD_ISSUPPORTAPI isEqualToString:call.method]) {
|
} else if ([METHOD_ISSUPPORTAPI isEqualToString:call.method]) {
|
||||||
@ -447,14 +468,30 @@ static NSString *const ARGUMENT_KEY_RESULT_AUTHCODE = @"authCode";
|
|||||||
[dictionary setValue:launchFromWXReq.message.messageExt forKey:ARGUMENT_KEY_RESULT_MESSAGEEXT];
|
[dictionary setValue:launchFromWXReq.message.messageExt forKey:ARGUMENT_KEY_RESULT_MESSAGEEXT];
|
||||||
[dictionary setValue:launchFromWXReq.lang forKey:ARGUMENT_KEY_RESULT_LANG];
|
[dictionary setValue:launchFromWXReq.lang forKey:ARGUMENT_KEY_RESULT_LANG];
|
||||||
[dictionary setValue:launchFromWXReq.country forKey:ARGUMENT_KEY_RESULT_COUNTRY];
|
[dictionary setValue:launchFromWXReq.country forKey:ARGUMENT_KEY_RESULT_COUNTRY];
|
||||||
//
|
if (_isRunning) {
|
||||||
|
[_channel invokeMethod:METHOD_ONLAUNCHFROMWXREQ arguments:dictionary];
|
||||||
|
} else {
|
||||||
|
__weak typeof(self) weakSelf = self;
|
||||||
|
_initialWXReqRunnable = ^() {
|
||||||
|
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||||
|
[strongSelf -> _channel invokeMethod:METHOD_ONLAUNCHFROMWXREQ arguments:dictionary];
|
||||||
|
};
|
||||||
|
}
|
||||||
} else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) {
|
} else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) {
|
||||||
ShowMessageFromWXReq *showMessageFromWXReq = (ShowMessageFromWXReq *)req;
|
ShowMessageFromWXReq *showMessageFromWXReq = (ShowMessageFromWXReq *)req;
|
||||||
[dictionary setValue:showMessageFromWXReq.message.messageAction forKey:ARGUMENT_KEY_RESULT_MESSAGEACTION];
|
[dictionary setValue:showMessageFromWXReq.message.messageAction forKey:ARGUMENT_KEY_RESULT_MESSAGEACTION];
|
||||||
[dictionary setValue:showMessageFromWXReq.message.messageExt forKey:ARGUMENT_KEY_RESULT_MESSAGEEXT];
|
[dictionary setValue:showMessageFromWXReq.message.messageExt forKey:ARGUMENT_KEY_RESULT_MESSAGEEXT];
|
||||||
[dictionary setValue:showMessageFromWXReq.lang forKey:ARGUMENT_KEY_RESULT_LANG];
|
[dictionary setValue:showMessageFromWXReq.lang forKey:ARGUMENT_KEY_RESULT_LANG];
|
||||||
[dictionary setValue:showMessageFromWXReq.country forKey:ARGUMENT_KEY_RESULT_COUNTRY];
|
[dictionary setValue:showMessageFromWXReq.country forKey:ARGUMENT_KEY_RESULT_COUNTRY];
|
||||||
//
|
if (_isRunning) {
|
||||||
|
[_channel invokeMethod:METHOD_ONSHOWMESSAGEFROMWXREQ arguments:dictionary];
|
||||||
|
} else {
|
||||||
|
__weak typeof(self) weakSelf = self;
|
||||||
|
_initialWXReqRunnable = ^() {
|
||||||
|
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||||
|
[strongSelf -> _channel invokeMethod:METHOD_ONSHOWMESSAGEFROMWXREQ arguments:dictionary];
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ class Wechat {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
/// 微信回调 - 冷启
|
||||||
Future<void> handleInitialWXReq() {
|
Future<void> handleInitialWXReq() {
|
||||||
return _channel.invokeMethod<void>(_METHOD_HANDLEINITIALWXREQ);
|
return _channel.invokeMethod<void>(_METHOD_HANDLEINITIALWXREQ);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user