wechat.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. listenShareMsg(WechatSdkResp resp){
  34. }
  35. // wechat 登录
  36. void wechatLogin(WeChatLogin weChatLogin) async {
  37. if (await _wechat.isInstalled() && await _wechat.isSupportApi()) {
  38. _wechat.auth(
  39. scope: <String>[WechatScope.SNSAPI_USERINFO],
  40. state: 'auth',
  41. );
  42. print(
  43. "wechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLoginwechatLogin");
  44. if (_auth == null) {
  45. _auth = _wechat.authResp().listen((WechatAuthResp resp) {
  46. _authResp = resp;
  47. String content = 'auth: ${resp.errorCode} ${resp.errorMsg}';
  48. if (_authResp != null &&
  49. _authResp.errorCode == WechatSdkResp.ERRORCODE_SUCCESS) {
  50. weChatLogin.call(_authResp.code);
  51. } else {
  52. weChatLogin.call(null);
  53. }
  54. });
  55. }
  56. } else {
  57. weChatLogin.call(null);
  58. }
  59. }
  60. void wechatSharePage() {
  61. _wechat.shareWebpage(
  62. scene: WechatScene.TIMELINE,
  63. webpageUrl: 'https://www.baidu.com',
  64. );
  65. }
  66. // void wechatShareImage() async {
  67. // Dio dio = new Dio();
  68. // Directory documentsDir = await getApplicationDocumentsDirectory();
  69. // String documentsPath = documentsDir.path;
  70. // String path = "$documentsPath/demo/game.jpg";
  71. // await dio.download("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=229276664,935794386&fm=26&gp=0.jpg", "$path",
  72. // options: Options(headers: {HttpHeaders.acceptEncodingHeader: "*"}), // disable gzip
  73. // onReceiveProgress: (received, total) {
  74. // if (total != -1) {}
  75. // });
  76. // print(path);
  77. // await _wechat.shareImage(scene: WechatScene.SESSION, imageData: File(path).readAsBytesSync());
  78. // }
  79. Future<void> wechatShareImage(String path, String type) async {
  80. if (type == "chat") {
  81. // 这是 聊天
  82. return _wechat.shareImage(
  83. scene: WechatScene.SESSION, imageData: File(path).readAsBytesSync());
  84. } else if (type == "friend") {
  85. // 这是朋友圈
  86. return _wechat.shareImage(
  87. scene: WechatScene.TIMELINE, imageData: File(path).readAsBytesSync());
  88. }
  89. }
  90. Future<void> wechatShareLink(String url, String type, String username) async {
  91. if (type == "chat") {
  92. await _wechat.shareWebpage(
  93. scene: WechatScene.SESSION,
  94. webpageUrl: url,
  95. title: "$username分享了他的运动记录,快来围观吧~",
  96. description: "$username分享了他的运动记录,快来围观吧~"
  97. );
  98. } else if (type == "friend") {
  99. await _wechat.shareWebpage(
  100. scene: WechatScene.TIMELINE,
  101. webpageUrl: url,
  102. title: "$username分享了他的运动记录,快来围观吧~",
  103. description: "$username分享了他的运动记录,快来围观吧~"
  104. );
  105. }
  106. return;
  107. }
  108. void dispose() {
  109. _auth?.cancel();
  110. _auth = null;
  111. _share?.cancel();
  112. _share = null;
  113. _pay?.cancel();
  114. _pay = null;
  115. _miniProgram?.cancel();
  116. _miniProgram = null;
  117. _wechat = null;
  118. super.dispose();
  119. }
  120. }