wechat.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:sport/utils/toast.dart';
  5. import 'package:wechat_kit/wechat_kit.dart';
  6. const String WECHAT_APPID = 'wx9455bd2742259ef7';
  7. const String WECHAT_UNIVERSAL_LINK = 'https://xie.hiyd.com/'; // iOS 请配置
  8. const String WECHAT_APPSECRET = 'c58946da98393252077073c29c828d08';
  9. const String WECHAT_MINIAPPID = 'your wechat miniAppId';
  10. mixin WechatMixin<T extends StatefulWidget> on State<T> {
  11. late Wechat _wechat;
  12. late final StreamSubscription<BaseResp> _respSubs;
  13. void _listenResp(BaseResp resp) {
  14. if(resp.isSuccessful != true) return;
  15. if (resp is AuthResp) {
  16. listenWechatLoginMsg(resp);
  17. } else if (resp is ShareMsgResp) {
  18. listenWechatShareMsg(resp);
  19. } else if (resp is PayResp) {
  20. } else if (resp is LaunchMiniProgramResp) {
  21. }
  22. }
  23. listenWechatShareMsg(ShareMsgResp resp) {}
  24. listenWechatLoginMsg(AuthResp resp) {}
  25. @override
  26. void initState() {
  27. super.initState();
  28. _wechat = Wechat.instance;
  29. _respSubs = Wechat.instance.respStream().listen(_listenResp);
  30. }
  31. @override
  32. void dispose() {
  33. _respSubs.cancel();
  34. super.dispose();
  35. }
  36. Future<bool> wechatSupport() async {
  37. return await _wechat.isInstalled() || await _wechat.isSupportApi();
  38. }
  39. // wechat 登录
  40. Future<bool> wechatLogin() async {
  41. await _wechat.registerApp(
  42. appId: WECHAT_APPID,
  43. universalLink: WECHAT_UNIVERSAL_LINK,
  44. );
  45. if (await _wechat.isInstalled() && await _wechat.isSupportApi()) {
  46. _wechat.auth(
  47. scope: <String>[WechatScope.SNSAPI_USERINFO],
  48. state: 'auth',
  49. );
  50. return true;
  51. }else {
  52. ToastUtil.show("您没有安装微信客户端");
  53. }
  54. return false;
  55. }
  56. void wechatSharePage() async{
  57. await _wechat.registerApp(
  58. appId: WECHAT_APPID,
  59. universalLink: WECHAT_UNIVERSAL_LINK,
  60. );
  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. await _wechat.registerApp(
  81. appId: WECHAT_APPID,
  82. universalLink: WECHAT_UNIVERSAL_LINK,
  83. );
  84. if (type == "chat") {
  85. // 这是 聊天
  86. return _wechat.shareImage(scene: WechatScene.SESSION, imageUri: File(path).uri);
  87. } else if (type == "friend") {
  88. // 这是朋友圈
  89. return _wechat.shareImage(scene: WechatScene.TIMELINE, imageUri: File(path).uri);
  90. }
  91. }
  92. Future<void> wechatShareLink(String url, String type, String username) async {
  93. await _wechat.registerApp(
  94. appId: WECHAT_APPID,
  95. universalLink: WECHAT_UNIVERSAL_LINK,
  96. );
  97. if (type == "chat") {
  98. await _wechat.shareWebpage(scene: WechatScene.SESSION, webpageUrl: url, title: "$username分享了他的运动记录,快来围观吧~", description: "$username分享了他的运动记录,快来围观吧~");
  99. } else if (type == "friend") {
  100. await _wechat.shareWebpage(scene: WechatScene.TIMELINE, webpageUrl: url, title: "$username分享了他的运动记录,快来围观吧~", description: "$username分享了他的运动记录,快来围观吧~");
  101. }
  102. return;
  103. }
  104. Future<void> wechatShareVideo(String path, String type) async {
  105. await _wechat.registerApp(
  106. appId: WECHAT_APPID,
  107. universalLink: WECHAT_UNIVERSAL_LINK,
  108. );
  109. if (type == "chat") {
  110. // 这是 聊天
  111. return _wechat.shareVideo(scene: WechatScene.SESSION, videoUrl: File(path).uri.toString());
  112. } else if (type == "friend") {
  113. // 这是朋友圈
  114. return _wechat.shareVideo(scene: WechatScene.TIMELINE, videoUrl: File(path).uri.toString());
  115. }
  116. }
  117. }