tencent.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/material.dart';
  5. import 'package:sport/sharesdk/tencent_api_resp.dart';
  6. import 'package:sport/utils/toast.dart';
  7. import 'package:tencent_kit/tencent_kit.dart';
  8. extension MixerTencent on Tencent {
  9. /// 用户信息
  10. /// https://wiki.connect.qq.com/get_user_info
  11. Future<TencentUserInfoResp> getUserInfo({
  12. required String appId,
  13. required String openid,
  14. required String accessToken,
  15. }) {
  16. 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) {
  17. return request.close();
  18. }).then((HttpClientResponse response) async {
  19. if (response.statusCode == HttpStatus.ok) {
  20. final ContentType? contentType = response.headers.contentType;
  21. final Encoding encoding = Encoding.getByName(contentType?.charset) ?? utf8;
  22. final String content = await encoding.decodeStream(response);
  23. return TencentUserInfoResp.fromJson(json.decode(content) as Map<String, dynamic>);
  24. }
  25. throw HttpException('HttpResponse statusCode: ${response.statusCode}, reasonPhrase: ${response.reasonPhrase}.');
  26. });
  27. }
  28. }
  29. mixin TencentMixin<T extends StatefulWidget> on State<T> {
  30. static const String _TENCENT_APPID = '1110701531';
  31. late Tencent _tencent;
  32. late final StreamSubscription<BaseResp> _respSubs;
  33. void _listenResp(BaseResp resp) async {
  34. if (resp.isSuccessful != true) return;
  35. if (resp is LoginResp) {
  36. final TencentUserInfoResp userInfo = await Tencent.instance.getUserInfo(
  37. appId: _TENCENT_APPID,
  38. openid: resp.openid!,
  39. accessToken: resp.accessToken!,
  40. );
  41. listenTencentLoginMsg(resp, userInfo);
  42. } else if (resp is ShareMsgResp) {
  43. listenTencentShareMsg(resp);
  44. }
  45. }
  46. listenTencentLoginMsg(LoginResp loginResp, TencentUserInfoResp resp) {}
  47. listenTencentShareMsg(ShareMsgResp resp) {}
  48. @override
  49. void initState() {
  50. super.initState();
  51. _tencent = Tencent.instance;
  52. _respSubs = _tencent.respStream().listen(_listenResp);
  53. }
  54. _initSDK() async {
  55. await _tencent.setIsPermissionGranted(granted: true);
  56. await _tencent.registerApp(appId: _TENCENT_APPID);
  57. }
  58. @override
  59. void dispose() {
  60. _respSubs.cancel();
  61. super.dispose();
  62. }
  63. Future<bool> tencentSupport() async {
  64. return await _tencent.isQQInstalled() || await _tencent.isTIMInstalled();
  65. }
  66. void tencentLogin() async {
  67. await _initSDK();
  68. if ((await tencentSupport()) == true) {
  69. _tencent.login(
  70. scope: <String>[TencentScope.GET_SIMPLE_USERINFO],
  71. );
  72. } else {
  73. ToastUtil.show("您没有安装腾讯QQ客户端");
  74. }
  75. }
  76. void tencentShare(String filePath) async {
  77. await _initSDK();
  78. await _tencent.shareImage(
  79. scene: TencentScene.SCENE_QQ,
  80. imageUri: Uri.file(filePath),
  81. );
  82. }
  83. void tencentShareLink(String url) async {
  84. await _initSDK();
  85. await _tencent.shareWebpage(scene: TencentScene.SCENE_QQ, title: "分享页面", targetUrl: url);
  86. }
  87. }