diff --git a/android/src/main/java/io/github/v7lin/tencent_kit/TencentKitPlugin.java b/android/src/main/java/io/github/v7lin/tencent_kit/TencentKitPlugin.java index 8707f57..0c8310b 100644 --- a/android/src/main/java/io/github/v7lin/tencent_kit/TencentKitPlugin.java +++ b/android/src/main/java/io/github/v7lin/tencent_kit/TencentKitPlugin.java @@ -160,6 +160,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { result.success(tencent != null && isAppInstalled(applicationContext, "com.tencent.tim")); } else if ("login".equals(call.method)) { login(call, result); + } else if ("loginServerSide".equals(call.method)) { + loginServerSide(call, result); } else if ("logout".equals(call.method)) { logout(call, result); } else if ("shareMood".equals(call.method)) { @@ -185,6 +187,14 @@ private void login(@NonNull MethodCall call, @NonNull Result result) { result.success(null); } + private void loginServerSide(@NonNull MethodCall call, @NonNull Result result) { + final String scope = call.argument("scope"); + if (tencent != null) { + tencent.loginServerSide(activityPluginBinding.getActivity(), scope, loginListener); + } + result.success(null); + } + private IUiListener loginListener = new IUiListener() { @Override public void onComplete(Object o) { diff --git a/example/lib/main.dart b/example/lib/main.dart index bc50f86..855325f 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -107,6 +107,14 @@ class _HomeState extends State { ); }, ), + ListTile( + title: Text('登录(Server-Side)'), + onTap: () { + TencentKitPlatform.instance.loginServerSide( + scope: [TencentScope.kGetUserInfo], + ); + }, + ), ListTile( title: Text('获取用户信息'), onTap: () async { diff --git a/ios/Classes/TencentKitPlugin.m b/ios/Classes/TencentKitPlugin.m index 0230e1e..f263b12 100644 --- a/ios/Classes/TencentKitPlugin.m +++ b/ios/Classes/TencentKitPlugin.m @@ -67,6 +67,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result([NSNumber numberWithBool:[TencentOAuth iphoneTIMInstalled]]); } else if ([@"login" isEqualToString:call.method]) { [self login:call result:result]; + } else if ([@"loginServerSide" isEqualToString:call.method]) { + [self loginServerSide:call result:result]; } else if ([@"logout" isEqualToString:call.method]) { [self logout:call result:result]; } else if ([@"shareMood" isEqualToString:call.method]) { @@ -88,6 +90,17 @@ - (void)login:(FlutterMethodCall *)call result:(FlutterResult)result { if (_oauth != nil) { NSString *scope = call.arguments[@"scope"]; NSArray *permissions = [scope componentsSeparatedByString:@","]; + _oauth.authMode = kAuthModeClientSideToken; + [_oauth authorize:permissions]; + } + result(nil); +} + +- (void)loginServerSide:(FlutterMethodCall *)call result:(FlutterResult)result { + if (_oauth != nil) { + NSString *scope = call.arguments[@"scope"]; + NSArray *permissions = [scope componentsSeparatedByString:@","]; + _oauth.authMode = kAuthModeServerSideCode; [_oauth authorize:permissions]; } result(nil); @@ -278,6 +291,11 @@ - (void)tencentDidLogin { if (_oauth.accessToken != nil && _oauth.accessToken.length > 0) { NSString *openId = _oauth.openId; NSString *accessToken = _oauth.accessToken; + if (_oauth.authMode == kAuthModeServerSideCode) { + // 将 code 的值赋给 accessToken, 避免字段功能混淆 + // 同时官方文档也有说明通过此接口获取的 code 实际上就是 accessToken + accessToken = [_oauth getServerSideCode]; + } long long expiresIn = ceil(_oauth.expirationDate.timeIntervalSinceNow); // 向上取整 long long createAt = [[NSDate date] timeIntervalSince1970] * 1000.0; diff --git a/lib/src/tencent_kit_method_channel.dart b/lib/src/tencent_kit_method_channel.dart index 44b747f..8f9026e 100644 --- a/lib/src/tencent_kit_method_channel.dart +++ b/lib/src/tencent_kit_method_channel.dart @@ -83,6 +83,18 @@ class MethodChannelTencentKit extends TencentKitPlatform { ); } + @override + Future loginServerSide({ + required List scope, + }) { + return methodChannel.invokeMethod( + 'loginServerSide', + { + 'scope': scope.join(','), + }, + ); + } + @override Future logout() { return methodChannel.invokeMethod('logout'); diff --git a/lib/src/tencent_kit_platform_interface.dart b/lib/src/tencent_kit_platform_interface.dart index abb5c0f..a95e667 100644 --- a/lib/src/tencent_kit_platform_interface.dart +++ b/lib/src/tencent_kit_platform_interface.dart @@ -65,6 +65,14 @@ abstract class TencentKitPlatform extends PlatformInterface { 'login({required scope}) has not been implemented.'); } + /// 登录(Server-Side) + Future loginServerSide({ + required List scope, + }) { + throw UnimplementedError( + 'loginServerSide({required scope}) has not been implemented.'); + } + /// 登出 Future logout() { throw UnimplementedError('logout() has not been implemented.'); diff --git a/test/tencent_kit_test.dart b/test/tencent_kit_test.dart index aa24fb5..8d3fd8e 100644 --- a/test/tencent_kit_test.dart +++ b/test/tencent_kit_test.dart @@ -46,6 +46,13 @@ class MockTencentKitPlatform throw UnimplementedError(); } + @override + Future loginServerSide({ + required List scope, + }) { + throw UnimplementedError(); + } + @override Future logout() { throw UnimplementedError();