mirror of
https://github.com/RxReader/tencent_kit.git
synced 2025-06-17 16:38:03 +08:00
升级Flutter 3.0
This commit is contained in:
57
example/lib/api/tencent_api.dart
Normal file
57
example/lib/api/tencent_api.dart
Normal file
@ -0,0 +1,57 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:tencent_kit_example/api/model/tencent_api_resp.dart';
|
||||
import 'package:tencent_kit_example/api/model/tencent_unionid_resp.dart';
|
||||
|
||||
class TencentApi {
|
||||
TencentApi._();
|
||||
|
||||
/// 用户信息
|
||||
/// https://wiki.connect.qq.com/get_user_info
|
||||
static Future<TencentUserInfoResp> getUserInfo({
|
||||
required String appId,
|
||||
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 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 String content = await encoding.decodeStream(response);
|
||||
return TencentUserInfoResp.fromJson(json.decode(content) as Map<String, dynamic>);
|
||||
}
|
||||
throw HttpException('HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
|
||||
});
|
||||
}
|
||||
|
||||
/// UnionID
|
||||
/// https://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D
|
||||
static Future<TencentUnionidResp> getUnionId({
|
||||
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 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 String callback = await encoding.decodeStream(response);
|
||||
// 腾讯有毒 callback( $json );
|
||||
final RegExp exp = RegExp(r'callback\( (.*) \)\;');
|
||||
final Match? match = exp.firstMatch(callback);
|
||||
if (match?.groupCount == 1) {
|
||||
final String? content = match!.group(1);
|
||||
if (content != null) {
|
||||
return TencentUnionidResp.fromJson(json.decode(content) as Map<String, dynamic>);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw HttpException('HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
|
||||
});
|
||||
}
|
||||
}
|
@ -4,19 +4,19 @@ import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:tencent_kit/tencent_kit.dart';
|
||||
import 'package:tencent_kit_example/model/tencent_api_resp.dart';
|
||||
import 'package:tencent_kit_example/model/tencent_unionid_resp.dart';
|
||||
import 'package:tencent_kit_example/tencent.dart';
|
||||
import 'package:tencent_kit_example/api/model/tencent_api_resp.dart';
|
||||
import 'package:tencent_kit_example/api/model/tencent_unionid_resp.dart';
|
||||
import 'package:tencent_kit_example/api/tencent_api.dart';
|
||||
|
||||
const String _TENCENT_APPID = 'your tencent appId';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
Tencent.instance.registerApp(appId: _TENCENT_APPID);
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
@ -26,6 +26,8 @@ class MyApp extends StatelessWidget {
|
||||
}
|
||||
|
||||
class Home extends StatefulWidget {
|
||||
const Home({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
return _HomeState();
|
||||
@ -40,7 +42,7 @@ class _HomeState extends State<Home> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_respSubs = Tencent.instance.respStream().listen(_listenLogin);
|
||||
_respSubs = Tencent.respStream().listen(_listenLogin);
|
||||
}
|
||||
|
||||
void _listenLogin(BaseResp resp) {
|
||||
@ -64,47 +66,50 @@ class _HomeState extends State<Home> {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Tencent Kit Demo'),
|
||||
title: Text('Tencent Kit Demo'),
|
||||
),
|
||||
body: ListView(
|
||||
children: <Widget>[
|
||||
ListTile(
|
||||
title: const Text('环境检查'),
|
||||
title: Text('注册APP'),
|
||||
onTap: () async {
|
||||
final String content =
|
||||
'QQ install: ${await Tencent.instance.isQQInstalled()}\nTIM install: ${await Tencent.instance.isTIMInstalled()}';
|
||||
await Tencent.registerApp(appId: _TENCENT_APPID);
|
||||
_showTips('注册APP', '注册成功');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: Text('环境检查'),
|
||||
onTap: () async {
|
||||
final String content = 'QQ install: ${await Tencent.isQQInstalled()}\nTIM install: ${await Tencent.isTIMInstalled()}';
|
||||
_showTips('环境检查', content);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('3.1.0 之后的版本请先获取权限'),
|
||||
title: Text('3.1.0 之后的版本请先获取权限'),
|
||||
onTap: () async {
|
||||
await Tencent.instance.setIsPermissionGranted(granted: true);
|
||||
await Tencent.setIsPermissionGranted(granted: true);
|
||||
_showTips('授权', '已授权获取设备信息/同意隐私协议');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('登录'),
|
||||
title: Text('登录'),
|
||||
onTap: () {
|
||||
Tencent.instance.login(
|
||||
Tencent.login(
|
||||
scope: <String>[TencentScope.GET_SIMPLE_USERINFO],
|
||||
);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('获取用户信息'),
|
||||
title: Text('获取用户信息'),
|
||||
onTap: () async {
|
||||
if ((_loginResp?.isSuccessful ?? false) &&
|
||||
!(_loginResp!.isExpired ?? true)) {
|
||||
final TencentUserInfoResp userInfo =
|
||||
await Tencent.instance.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}');
|
||||
}
|
||||
@ -112,57 +117,52 @@ class _HomeState extends State<Home> {
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('获取UnionID'),
|
||||
title: Text('获取UnionID'),
|
||||
onTap: () async {
|
||||
if ((_loginResp?.isSuccessful ?? false) &&
|
||||
!(_loginResp!.isExpired ?? true)) {
|
||||
final TencentUnionidResp unionid =
|
||||
await Tencent.instance.getUnionId(
|
||||
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}');
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('分享说说'),
|
||||
title: Text('分享说说'),
|
||||
onTap: () {
|
||||
Tencent.instance.shareMood(
|
||||
Tencent.shareMood(
|
||||
scene: TencentScene.SCENE_QZONE,
|
||||
summary: '分享测试',
|
||||
);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('文本分享'),
|
||||
title: Text('文本分享'),
|
||||
onTap: () {
|
||||
Tencent.instance.shareText(
|
||||
Tencent.shareText(
|
||||
scene: TencentScene.SCENE_QQ,
|
||||
summary: '分享测试',
|
||||
);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('图片分享'),
|
||||
title: Text('图片分享'),
|
||||
onTap: () async {
|
||||
final File file = await DefaultCacheManager().getSingleFile(
|
||||
'https://www.baidu.com/img/bd_logo1.png?where=super');
|
||||
await Tencent.instance.shareImage(
|
||||
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),
|
||||
);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: const Text('网页分享'),
|
||||
title: Text('网页分享'),
|
||||
onTap: () {
|
||||
Tencent.instance.shareWebpage(
|
||||
Tencent.shareWebpage(
|
||||
scene: TencentScene.SCENE_QQ,
|
||||
title: 'title',
|
||||
targetUrl: 'https://www.baidu.com/',
|
||||
|
@ -1,68 +0,0 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:tencent_kit/tencent_kit.dart';
|
||||
import 'package:tencent_kit_example/model/tencent_api_resp.dart';
|
||||
import 'package:tencent_kit_example/model/tencent_unionid_resp.dart';
|
||||
|
||||
extension MixerTencent on Tencent {
|
||||
/// 用户信息
|
||||
/// https://wiki.connect.qq.com/get_user_info
|
||||
Future<TencentUserInfoResp> getUserInfo({
|
||||
required String appId,
|
||||
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 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 String content = await encoding.decodeStream(response);
|
||||
return TencentUserInfoResp.fromJson(
|
||||
json.decode(content) as Map<String, dynamic>);
|
||||
}
|
||||
throw HttpException(
|
||||
'HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
|
||||
});
|
||||
}
|
||||
|
||||
/// UnionID
|
||||
/// https://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D
|
||||
Future<TencentUnionidResp> getUnionId({
|
||||
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 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 String callback = await encoding.decodeStream(response);
|
||||
// 腾讯有毒 callback( $json );
|
||||
final RegExp exp = RegExp(r'callback\( (.*) \)\;');
|
||||
final Match? match = exp.firstMatch(callback);
|
||||
if (match?.groupCount == 1) {
|
||||
final String? content = match!.group(1);
|
||||
if (content != null) {
|
||||
return TencentUnionidResp.fromJson(
|
||||
json.decode(content) as Map<String, dynamic>);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw HttpException(
|
||||
'HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user