Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加对 ServerSide 登录模式的支持 #102

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ class _HomeState extends State<Home> {
);
},
),
ListTile(
title: Text('登录(Server-Side)'),
onTap: () {
TencentKitPlatform.instance.loginServerSide(
scope: <String>[TencentScope.kGetUserInfo],
);
},
),
ListTile(
title: Text('获取用户信息'),
onTap: () async {
Expand Down
18 changes: 18 additions & 0 deletions ios/Classes/TencentKitPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions lib/src/tencent_kit_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ class MethodChannelTencentKit extends TencentKitPlatform {
);
}

@override
Future<void> loginServerSide({
required List<String> scope,
}) {
return methodChannel.invokeMethod<void>(
'loginServerSide',
<String, dynamic>{
'scope': scope.join(','),
},
);
}

@override
Future<void> logout() {
return methodChannel.invokeMethod<void>('logout');
Expand Down
8 changes: 8 additions & 0 deletions lib/src/tencent_kit_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ abstract class TencentKitPlatform extends PlatformInterface {
'login({required scope}) has not been implemented.');
}

/// 登录(Server-Side)
Future<void> loginServerSide({
required List<String> scope,
}) {
throw UnimplementedError(
'loginServerSide({required scope}) has not been implemented.');
}

/// 登出
Future<void> logout() {
throw UnimplementedError('logout() has not been implemented.');
Expand Down
7 changes: 7 additions & 0 deletions test/tencent_kit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class MockTencentKitPlatform
throw UnimplementedError();
}

@override
Future<void> loginServerSide({
required List<String> scope,
}) {
throw UnimplementedError();
}

@override
Future<void> logout() {
throw UnimplementedError();
Expand Down
Loading