share_util.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:fluwx/fluwx.dart';
  6. import 'package:http/http.dart' as http;
  7. import 'package:news_app/util/log.util.dart';
  8. import 'package:path_provider/path_provider.dart';
  9. import 'package:share_plus/share_plus.dart';
  10. /// @author: bo.zeng
  11. /// @email: cnhbwds@gmail.com
  12. /// @date: 2025 2025/4/22 17:48
  13. /// @description:
  14. Fluwx fluwx = Fluwx();
  15. void _initFluwx() async {
  16. await fluwx.registerApi(
  17. appId: 'wxa8a911c3f63cd6df',
  18. doOnAndroid: true,
  19. doOnIOS: true,
  20. universalLink: 'https://app.xhrbxxf.com/app',
  21. );
  22. var result = await fluwx.isWeChatInstalled;
  23. debugPrint('is installed $result');
  24. }
  25. //微信朋友圈
  26. Future<void> shareWeiXinPYUrl({
  27. required String title,
  28. required String url,
  29. }) async {
  30. _initFluwx();
  31. try {
  32. // 1. 加载本地图标资源
  33. final ByteData byteData = await rootBundle.load(
  34. 'assets/images/appIcon.png',
  35. );
  36. final Uint8List imageBytes = byteData.buffer.asUint8List();
  37. // 2. 创建临时文件
  38. final tempDir = await getTemporaryDirectory();
  39. final file = File('${tempDir.path}/logo.png');
  40. await file.writeAsBytes(imageBytes);
  41. final model = WeChatShareWebPageModel(
  42. url,
  43. thumbData: imageBytes,
  44. title: title,
  45. description: "新华报业传媒集团",
  46. scene: WeChatScene.timeline,
  47. );
  48. try {
  49. await fluwx.share(model);
  50. } catch (e) {
  51. consoleLog("分享网页异常:${e.toString()}");
  52. }
  53. } catch (e) {
  54. consoleLog('分享失败: $e');
  55. }
  56. }
  57. //微信好友分享
  58. Future<void> shareWeiXinUrl({
  59. required String title,
  60. required String url,
  61. }) async {
  62. _initFluwx();
  63. try {
  64. // 1. 加载本地图标资源
  65. final ByteData byteData = await rootBundle.load(
  66. 'assets/images/appIcon.png',
  67. );
  68. final Uint8List imageBytes = byteData.buffer.asUint8List();
  69. // 2. 创建临时文件
  70. final tempDir = await getTemporaryDirectory();
  71. final file = File('${tempDir.path}/logo.png');
  72. await file.writeAsBytes(imageBytes);
  73. final model = WeChatShareWebPageModel(
  74. url,
  75. thumbData: imageBytes,
  76. title: title,
  77. description: "新华报业传媒集团",
  78. scene: WeChatScene.session,
  79. );
  80. try {
  81. await fluwx.share(model);
  82. } catch (e) {
  83. consoleLog("分享网页异常:${e.toString()}");
  84. }
  85. } catch (e) {
  86. consoleLog('分享失败: $e');
  87. }
  88. }
  89. //系统分享
  90. Future<void> shareUrl({required String title, required String url}) async {
  91. try {
  92. // 1. 加载本地图标资源
  93. final ByteData byteData = await rootBundle.load(
  94. 'assets/images/appIcon.png',
  95. );
  96. final Uint8List imageBytes = byteData.buffer.asUint8List();
  97. // 2. 创建临时文件
  98. final tempDir = await getTemporaryDirectory();
  99. final file = File('${tempDir.path}/logo.png');
  100. await file.writeAsBytes(imageBytes);
  101. if (Platform.isIOS == true) {
  102. // 3. 使用 share_plus 分享图文
  103. final result = await SharePlus.instance.share(
  104. ShareParams(
  105. text: '$title\n$url',
  106. subject: title,
  107. previewThumbnail: XFile(file.path),
  108. files: [XFile(file.path)],
  109. ),
  110. );
  111. if (result.status == ShareResultStatus.success) {
  112. consoleLog("分享成功");
  113. }
  114. } else {
  115. // 3. 使用 share_plus 分享图文
  116. final result = await SharePlus.instance.share(
  117. ShareParams(
  118. text: '$title\n$url',
  119. subject: title,
  120. // files: [XFile(file.path)],
  121. ),
  122. );
  123. if (result.status == ShareResultStatus.success) {
  124. consoleLog("分享成功");
  125. }
  126. }
  127. } catch (e) {
  128. consoleLog('分享失败: $e');
  129. }
  130. }
  131. Future<void> shareImage({
  132. required String title,
  133. required String imageUrl,
  134. }) async {
  135. try {
  136. // 1. 下载网络图片
  137. final response = await http.get(Uri.parse(imageUrl));
  138. if (response.statusCode != 200) {
  139. throw Exception("Failed to download image: ${response.statusCode}");
  140. }
  141. // 2. 保存图片到临时目录
  142. final directory = await getTemporaryDirectory();
  143. final fileName = "shared_image_${Random().nextInt(1000)}.jpg";
  144. final file = File("${directory.path}/$fileName");
  145. await file.writeAsBytes(response.bodyBytes);
  146. // 3. 分享图片
  147. final result = await SharePlus.instance.share(
  148. ShareParams(title: title, files: [XFile(file.path)]),
  149. );
  150. if (result.status == ShareResultStatus.success) {
  151. consoleLog("分享成功");
  152. }
  153. } catch (e) {
  154. consoleLog("分享失败: $e");
  155. }
  156. }