mirror of
https://github.com/RxReader/tencent_kit.git
synced 2025-05-17 23:36:12 +08:00
增加对 ServerSide 登录模式的支持 (#102)
* feat: login with serverside * test: login with serverside * feat: login with serverside(iOS)
This commit is contained in:
@ -160,6 +160,8 @@ public class TencentKitPlugin implements FlutterPlugin, ActivityAware, ActivityR
|
||||
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 @@ public class TencentKitPlugin implements FlutterPlugin, ActivityAware, ActivityR
|
||||
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) {
|
||||
|
@ -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 {
|
||||
|
@ -67,6 +67,8 @@ enum TencentRetCode {
|
||||
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 @@ enum TencentRetCode {
|
||||
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 @@ enum TencentRetCode {
|
||||
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;
|
||||
|
@ -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');
|
||||
|
@ -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.');
|
||||
|
@ -46,6 +46,13 @@ class MockTencentKitPlatform
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> loginServerSide({
|
||||
required List<String> scope,
|
||||
}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> logout() {
|
||||
throw UnimplementedError();
|
||||
|
Reference in New Issue
Block a user