格式化

This commit is contained in:
v7lin
2022-05-16 18:54:52 +08:00
parent 27f5f8cc7d
commit 39a9a43d8d
7 changed files with 76 additions and 58 deletions

View File

@ -15,16 +15,22 @@ class TencentApi {
required String openid,
required String accessToken,
}) {
return HttpClient().getUrl(Uri.parse('https://graph.qq.com/user/get_user_info?access_token=$accessToken&oauth_consumer_key=$appId&openid=$openid')).then((HttpClientRequest request) {
return HttpClient()
.getUrl(Uri.parse(
'https://graph.qq.com/user/get_user_info?access_token=$accessToken&oauth_consumer_key=$appId&openid=$openid'))
.then((HttpClientRequest request) {
return request.close();
}).then((HttpClientResponse response) async {
if (response.statusCode == HttpStatus.ok) {
final ContentType? contentType = response.headers.contentType;
final Encoding encoding = Encoding.getByName(contentType?.charset) ?? utf8;
final Encoding encoding =
Encoding.getByName(contentType?.charset) ?? utf8;
final String content = await encoding.decodeStream(response);
return TencentUserInfoResp.fromJson(json.decode(content) as Map<String, dynamic>);
return TencentUserInfoResp.fromJson(
json.decode(content) as Map<String, dynamic>);
}
throw HttpException('HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
throw HttpException(
'HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
});
}
@ -34,12 +40,16 @@ class TencentApi {
required String accessToken,
String unionid = '1',
}) {
return HttpClient().getUrl(Uri.parse('https://graph.qq.com/oauth2.0/me?access_token=$accessToken&unionid=$unionid')).then((HttpClientRequest request) {
return HttpClient()
.getUrl(Uri.parse(
'https://graph.qq.com/oauth2.0/me?access_token=$accessToken&unionid=$unionid'))
.then((HttpClientRequest request) {
return request.close();
}).then((HttpClientResponse response) async {
if (response.statusCode == HttpStatus.ok) {
final ContentType? contentType = response.headers.contentType;
final Encoding encoding = Encoding.getByName(contentType?.charset) ?? utf8;
final Encoding encoding =
Encoding.getByName(contentType?.charset) ?? utf8;
final String callback = await encoding.decodeStream(response);
// 腾讯有毒 callback( $json );
final RegExp exp = RegExp(r'callback\( (.*) \)\;');
@ -47,11 +57,13 @@ class TencentApi {
if (match?.groupCount == 1) {
final String? content = match!.group(1);
if (content != null) {
return TencentUnionidResp.fromJson(json.decode(content) as Map<String, dynamic>);
return TencentUnionidResp.fromJson(
json.decode(content) as Map<String, dynamic>);
}
}
}
throw HttpException('HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
throw HttpException(
'HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
});
}
}

View File

@ -70,6 +70,13 @@ class _HomeState extends State<Home> {
),
body: ListView(
children: <Widget>[
ListTile(
title: Text('3.1.0 之后的版本请先获取权限'),
onTap: () async {
await Tencent.setIsPermissionGranted(granted: true);
_showTips('授权', '已授权获取设备信息/同意隐私协议');
},
),
ListTile(
title: Text('注册APP'),
onTap: () async {
@ -80,17 +87,11 @@ class _HomeState extends State<Home> {
ListTile(
title: Text('环境检查'),
onTap: () async {
final String content = 'QQ install: ${await Tencent.isQQInstalled()}\nTIM install: ${await Tencent.isTIMInstalled()}';
final String content =
'QQ install: ${await Tencent.isQQInstalled()}\nTIM install: ${await Tencent.isTIMInstalled()}';
_showTips('环境检查', content);
},
),
ListTile(
title: Text('3.1.0 之后的版本请先获取权限'),
onTap: () async {
await Tencent.setIsPermissionGranted(granted: true);
_showTips('授权', '已授权获取设备信息/同意隐私协议');
},
),
ListTile(
title: Text('登录'),
onTap: () {
@ -102,14 +103,17 @@ class _HomeState extends State<Home> {
ListTile(
title: Text('获取用户信息'),
onTap: () async {
if ((_loginResp?.isSuccessful ?? false) && !(_loginResp!.isExpired ?? true)) {
final TencentUserInfoResp userInfo = await TencentApi.getUserInfo(
if ((_loginResp?.isSuccessful ?? false) &&
!(_loginResp!.isExpired ?? true)) {
final TencentUserInfoResp userInfo =
await TencentApi.getUserInfo(
appId: _TENCENT_APPID,
openid: _loginResp!.openid!,
accessToken: _loginResp!.accessToken!,
);
if (userInfo.isSuccessful) {
_showTips('用户信息', '${userInfo.nickname} - ${userInfo.gender} - ${userInfo.genderType}');
_showTips('用户信息',
'${userInfo.nickname} - ${userInfo.gender} - ${userInfo.genderType}');
} else {
_showTips('用户信息', '${userInfo.ret} - ${userInfo.msg}');
}
@ -119,14 +123,17 @@ class _HomeState extends State<Home> {
ListTile(
title: Text('获取UnionID'),
onTap: () async {
if ((_loginResp?.isSuccessful ?? false) && !(_loginResp!.isExpired ?? true)) {
if ((_loginResp?.isSuccessful ?? false) &&
!(_loginResp!.isExpired ?? true)) {
final TencentUnionidResp unionid = await TencentApi.getUnionId(
accessToken: _loginResp!.accessToken!,
);
if (unionid.isSuccessful) {
_showTips('UnionID', '${unionid.clientId} - ${unionid.openid} - ${unionid.unionid}');
_showTips('UnionID',
'${unionid.clientId} - ${unionid.openid} - ${unionid.unionid}');
} else {
_showTips('UnionID', '${unionid.error} - ${unionid.errorDescription}');
_showTips('UnionID',
'${unionid.error} - ${unionid.errorDescription}');
}
}
},
@ -152,7 +159,8 @@ class _HomeState extends State<Home> {
ListTile(
title: Text('图片分享'),
onTap: () async {
final File file = await DefaultCacheManager().getSingleFile('https://www.baidu.com/img/bd_logo1.png?where=super');
final File file = await DefaultCacheManager().getSingleFile(
'https://www.baidu.com/img/bd_logo1.png?where=super');
await Tencent.shareImage(
scene: TencentScene.SCENE_QQ,
imageUri: Uri.file(file.path),

View File

@ -5,23 +5,8 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tencent_kit_example/main.dart';
void main() {
testWidgets('Verify Platform version', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
});
testWidgets('smoke test', (WidgetTester tester) async {});
}

View File

@ -11,17 +11,22 @@ import 'tencent_kit_platform_interface.dart';
class MethodChannelTencentKit extends TencentKitPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
late final MethodChannel methodChannel = const MethodChannel('v7lin.github.io/tencent_kit')..setMethodCallHandler(_handleMethod);
late final MethodChannel methodChannel =
const MethodChannel('v7lin.github.io/tencent_kit')
..setMethodCallHandler(_handleMethod);
final StreamController<BaseResp> _respStreamController = StreamController<BaseResp>.broadcast();
final StreamController<BaseResp> _respStreamController =
StreamController<BaseResp>.broadcast();
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case 'onLoginResp':
_respStreamController.add(LoginResp.fromJson((call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
_respStreamController.add(LoginResp.fromJson(
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break;
case 'onShareResp':
_respStreamController.add(ShareMsgResp.fromJson((call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
_respStreamController.add(ShareMsgResp.fromJson(
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break;
}
}
@ -49,8 +54,7 @@ class MethodChannelTencentKit extends TencentKitPlatform {
'registerApp',
<String, dynamic>{
'appId': appId,
if (universalLink?.isNotEmpty ?? false)
'universalLink': universalLink,
if (universalLink?.isNotEmpty ?? false) 'universalLink': universalLink,
},
);
}
@ -97,8 +101,7 @@ class MethodChannelTencentKit extends TencentKitPlatform {
assert(scene == TencentScene.SCENE_QZONE);
assert((summary?.isNotEmpty ?? false) ||
((imageUris?.isNotEmpty ?? false) &&
imageUris!
.every((Uri element) => element.isScheme('file'))) ||
imageUris!.every((Uri element) => element.isScheme('file'))) ||
(videoUri != null && videoUri.isScheme('file')));
return methodChannel.invokeMethod<void>(
'shareMood',

View File

@ -29,14 +29,16 @@ abstract class TencentKitPlatform extends PlatformInterface {
required bool granted,
String? buildModel /* android.os.Build.MODEL */,
}) {
throw UnimplementedError('setIsPermissionGranted({required granted, buildModel}) has not been implemented.');
throw UnimplementedError(
'setIsPermissionGranted({required granted, buildModel}) has not been implemented.');
}
Future<void> registerApp({
required String appId,
String? universalLink,
}) {
throw UnimplementedError('registerApp({required appId, universalLink}) has not been implemented.');
throw UnimplementedError(
'registerApp({required appId, universalLink}) has not been implemented.');
}
Future<bool> isQQInstalled() {
@ -54,7 +56,8 @@ abstract class TencentKitPlatform extends PlatformInterface {
Future<void> login({
required List<String> scope,
}) {
throw UnimplementedError('login({required scope}) has not been implemented.');
throw UnimplementedError(
'login({required scope}) has not been implemented.');
}
Future<void> logout() {
@ -67,14 +70,16 @@ abstract class TencentKitPlatform extends PlatformInterface {
List<Uri>? imageUris,
Uri? videoUri,
}) {
throw UnimplementedError('shareMood({required scene, summary, imageUris, videoUri}) has not been implemented.');
throw UnimplementedError(
'shareMood({required scene, summary, imageUris, videoUri}) has not been implemented.');
}
Future<void> shareText({
required int scene,
required String summary,
}) {
throw UnimplementedError('shareText({required scene, required summary}) has not been implemented.');
throw UnimplementedError(
'shareText({required scene, required summary}) has not been implemented.');
}
Future<void> shareImage({
@ -83,7 +88,8 @@ abstract class TencentKitPlatform extends PlatformInterface {
String? appName,
int extInt = TencentQZoneFlag.DEFAULT,
}) {
throw UnimplementedError('shareImage({required scene, required imageUri, appName, extInt}) has not been implemented.');
throw UnimplementedError(
'shareImage({required scene, required imageUri, appName, extInt}) has not been implemented.');
}
Future<void> shareMusic({
@ -96,7 +102,8 @@ abstract class TencentKitPlatform extends PlatformInterface {
String? appName,
int extInt = TencentQZoneFlag.DEFAULT,
}) {
throw UnimplementedError('shareMusic({required scene, required title, summary, imageUri, required musicUrl, required targetUrl, appName, extInt}) has not been implemented.');
throw UnimplementedError(
'shareMusic({required scene, required title, summary, imageUri, required musicUrl, required targetUrl, appName, extInt}) has not been implemented.');
}
Future<void> shareWebpage({
@ -108,6 +115,7 @@ abstract class TencentKitPlatform extends PlatformInterface {
String? appName,
int extInt = TencentQZoneFlag.DEFAULT,
}) {
throw UnimplementedError('shareWebpage({required scene, required title, summary, imageUri, required targetUrl, appName, extInt}) has not been implemented.');
throw UnimplementedError(
'shareWebpage({required scene, required title, summary, imageUri, required targetUrl, appName, extInt}) has not been implemented.');
}
}

View File

@ -10,7 +10,7 @@ void main() {
setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
switch(methodCall.method) {
switch (methodCall.method) {
case 'isQQInstalled':
return true;
}

View File

@ -7,7 +7,9 @@ import 'package:tencent_kit/src/tencent_kit_method_channel.dart';
import 'package:tencent_kit/src/tencent_kit_platform_interface.dart';
import 'package:tencent_kit/tencent_kit.dart';
class MockTencentKitPlatform with MockPlatformInterfaceMixin implements TencentKitPlatform {
class MockTencentKitPlatform
with MockPlatformInterfaceMixin
implements TencentKitPlatform {
@override
Future<void> setIsPermissionGranted({
required bool granted,