tencent.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:sport/utils/toast.dart';
  4. import 'package:tencent_kit/tencent_kit.dart';
  5. typedef QqLogin = void Function(
  6. TencentLoginResp login, TencentUserInfoResp userInfo);
  7. mixin TencentMixin<T extends StatefulWidget> on State<T> {
  8. static const String _TENCENT_APPID = '1110701531';
  9. Tencent _tencent = Tencent()..registerApp(appId: _TENCENT_APPID);
  10. StreamSubscription<TencentLoginResp> _qqlogin;
  11. StreamSubscription<TencentShareResp> _qqshare;
  12. TencentLoginResp _loginResp;
  13. StreamController<TencentUserInfoResp> _qqloginRespStream;
  14. StreamSubscription<TencentUserInfoResp> _qqloginRespSubscription;
  15. void initState() {
  16. super.initState();
  17. _qqlogin = _tencent.loginResp().listen(_listenLogin);
  18. _qqshare = _tencent.shareResp().listen(_listenShare);
  19. }
  20. void dispose() {
  21. _qqlogin?.cancel();
  22. _qqlogin = null;
  23. _qqshare?.cancel();
  24. _qqshare = null;
  25. _qqloginRespSubscription?.cancel();
  26. _qqloginRespSubscription = null;
  27. super.dispose();
  28. }
  29. Future<bool> qqSupport() async {
  30. return await _tencent.isQQInstalled() || await _tencent.isTIMInstalled();
  31. }
  32. void qqlogin(QqLogin login) {
  33. _qqloginRespStream = StreamController<TencentUserInfoResp>();
  34. _qqloginRespSubscription = _qqloginRespStream.stream.listen((event) {
  35. login.call(_loginResp, event);
  36. });
  37. _tencent?.login(
  38. scope: <String>[TencentScope.GET_SIMPLE_USERINFO],
  39. );
  40. }
  41. void qqShare(String filePath) async {
  42. await _tencent.shareImage(
  43. scene: TencentScene.SCENE_QQ,
  44. imageUri: Uri.file(filePath),
  45. );
  46. }
  47. void qqShareLink(String url) async{
  48. await _tencent.shareWebpage(scene: TencentScene.SCENE_QQ, title: "分享页面" ,targetUrl: url);
  49. }
  50. void _listenLogin(TencentLoginResp resp) async {
  51. _loginResp = resp;
  52. if (_loginResp != null &&
  53. _loginResp.isSuccessful()&&
  54. !_loginResp.isExpired()) {
  55. TencentUserInfoResp userInfo = await _tencent.getUserInfo(
  56. appId: _TENCENT_APPID,
  57. openid: _loginResp.openid,
  58. accessToken: _loginResp.accessToken,
  59. );
  60. if (userInfo.isSuccessful()) {
  61. print(
  62. '用户信息 ${userInfo.nickname} - ${userInfo.gender} - ${userInfo.genderType} ${userInfo.figureurl1}');
  63. } else {
  64. ToastUtil.show("获取用户信息:${userInfo.ret} - ${userInfo.msg}");
  65. }
  66. _qqloginRespStream.add(userInfo);
  67. } else {
  68. _qqloginRespStream.add(null);
  69. }
  70. }
  71. void _listenShare(TencentShareResp resp) {
  72. String content = 'share: ${resp.ret} - ${resp.msg}';
  73. print('分享$content}');
  74. }
  75. }