wechat.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:dio/dio.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:tencent_kit/tencent_kit.dart';
  7. import 'package:wechat_kit/wechat_kit.dart';
  8. import '../utils/toast.dart';
  9. const String WECHAT_APPID = 'wx9455bd2742259ef7';
  10. const String WECHAT_UNIVERSAL_LINK = 'your wechat universal link'; // iOS 请配置
  11. const String WECHAT_APPSECRET = 'c58946da98393252077073c29c828d08';
  12. const String WECHAT_MINIAPPID = 'your wechat miniAppId';
  13. typedef WeChatLogin = void Function(String code);
  14. mixin WechatMixin<T extends StatefulWidget> on State<T> {
  15. Wechat _wechat;
  16. StreamSubscription<WechatAuthResp> _auth;
  17. StreamSubscription<WechatSdkResp> _share;
  18. StreamSubscription<WechatPayResp> _pay;
  19. StreamSubscription<WechatLaunchMiniProgramResp> _miniProgram;
  20. WechatAuthResp _authResp;
  21. void initState() {
  22. super.initState();
  23. _wechat = Wechat()
  24. ..registerApp(
  25. appId: WECHAT_APPID,
  26. universalLink: WECHAT_UNIVERSAL_LINK,
  27. );
  28. // _auth = _wechat.authResp().listen(_listenAuth);
  29. // _share = _wechat.shareMsgResp().listen(_listenShareMsg);
  30. // _pay = _wechat.payResp().listen(_listenPay);
  31. // _miniProgram = _wechat.launchMiniProgramResp().listen(_listenMiniProgram);
  32. }
  33. // wechat 登录
  34. void wechatLogin(WeChatLogin weChatLogin) async {
  35. if (await _wechat.isInstalled() && await _wechat.isSupportApi()) {
  36. _wechat.auth(
  37. scope: <String>[WechatScope.SNSAPI_USERINFO],
  38. state: 'auth',
  39. );
  40. print(
  41. "wechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLogin");
  42. if (_auth == null) {
  43. _auth = _wechat.authResp().listen((WechatAuthResp resp) {
  44. _authResp = resp;
  45. String content = 'auth: ${resp.errorCode} ${resp.errorMsg}';
  46. if (_authResp != null &&
  47. _authResp.errorCode == WechatSdkResp.ERRORCODE_SUCCESS) {
  48. weChatLogin.call(_authResp.code);
  49. } else {
  50. weChatLogin.call(null);
  51. }
  52. });
  53. }
  54. } else {
  55. weChatLogin.call(null);
  56. }
  57. }
  58. void wechatSharePage() {
  59. _wechat.shareWebpage(
  60. scene: WechatScene.TIMELINE,
  61. webpageUrl: 'https://www.baidu.com',
  62. );
  63. }
  64. // void wechatShareImage() async {
  65. // Dio dio = new Dio();
  66. // Directory documentsDir = await getApplicationDocumentsDirectory();
  67. // String documentsPath = documentsDir.path;
  68. // String path = "$documentsPath/demo/game.jpg";
  69. // await dio.download("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=229276664,935794386&fm=26&gp=0.jpg", "$path",
  70. // options: Options(headers: {HttpHeaders.acceptEncodingHeader: "*"}), // disable gzip
  71. // onReceiveProgress: (received, total) {
  72. // if (total != -1) {}
  73. // });
  74. // print(path);
  75. // await _wechat.shareImage(scene: WechatScene.SESSION, imageData: File(path).readAsBytesSync());
  76. // }
  77. void wechatShareImage(String path, String type) async {
  78. if (type == "chat") {
  79. // 这是 聊天
  80. await _wechat.shareImage(
  81. scene: WechatScene.SESSION, imageData: File(path).readAsBytesSync());
  82. } else if (type == "friend") {
  83. // 这是朋友圈
  84. await _wechat.shareImage(
  85. scene: WechatScene.TIMELINE, imageData: File(path).readAsBytesSync());
  86. }
  87. }
  88. void wechatShareLink(String url, String type, String username) async {
  89. if (type == "chat") {
  90. _wechat.shareWebpage(
  91. scene: WechatScene.SESSION,
  92. webpageUrl: url,
  93. title: "$username分享了他的运动记录,快来围观吧~",
  94. description: "$username分享了他的运动记录,快来围观吧~"
  95. );
  96. } else if (type == "friend") {
  97. _wechat.shareWebpage(
  98. scene: WechatScene.TIMELINE,
  99. webpageUrl: url,
  100. title: "$username分享了他的运动记录,快来围观吧~",
  101. description: "$username分享了他的运动记录,快来围观吧~"
  102. );
  103. }
  104. }
  105. void dispose() {
  106. _auth?.cancel();
  107. _auth = null;
  108. _share?.cancel();
  109. _share = null;
  110. _pay?.cancel();
  111. _pay = null;
  112. _miniProgram?.cancel();
  113. _miniProgram = null;
  114. _wechat = null;
  115. super.dispose();
  116. }
  117. }