| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import 'dart:io';
- import 'dart:math';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/services.dart';
- import 'package:fluwx/fluwx.dart';
- import 'package:http/http.dart' as http;
- import 'package:news_app/util/log.util.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:share_plus/share_plus.dart';
- /// @author: bo.zeng
- /// @email: cnhbwds@gmail.com
- /// @date: 2025 2025/4/22 17:48
- /// @description:
- Fluwx fluwx = Fluwx();
- void _initFluwx() async {
- await fluwx.registerApi(
- appId: 'wxa8a911c3f63cd6df',
- doOnAndroid: true,
- doOnIOS: true,
- universalLink: 'https://app.xhrbxxf.com/app',
- );
- var result = await fluwx.isWeChatInstalled;
- debugPrint('is installed $result');
- }
- //微信朋友圈
- Future<void> shareWeiXinPYUrl({
- required String title,
- required String url,
- }) async {
- _initFluwx();
- try {
- // 1. 加载本地图标资源
- final ByteData byteData = await rootBundle.load(
- 'assets/images/appIcon.png',
- );
- final Uint8List imageBytes = byteData.buffer.asUint8List();
- // 2. 创建临时文件
- final tempDir = await getTemporaryDirectory();
- final file = File('${tempDir.path}/logo.png');
- await file.writeAsBytes(imageBytes);
- final model = WeChatShareWebPageModel(
- url,
- thumbData: imageBytes,
- title: title,
- description: "新华新消费",
- scene: WeChatScene.timeline,
- );
- try {
- await fluwx.share(model);
- } catch (e) {
- consoleLog("分享网页异常:${e.toString()}");
- }
- } catch (e) {
- consoleLog('分享失败: $e');
- }
- }
- //微信好友分享
- Future<void> shareWeiXinUrl({
- required String title,
- required String url,
- }) async {
- _initFluwx();
- try {
- // 1. 加载本地图标资源
- final ByteData byteData = await rootBundle.load(
- 'assets/images/appIcon.png',
- );
- final Uint8List imageBytes = byteData.buffer.asUint8List();
- // 2. 创建临时文件
- final tempDir = await getTemporaryDirectory();
- final file = File('${tempDir.path}/logo.png');
- await file.writeAsBytes(imageBytes);
- final model = WeChatShareWebPageModel(
- url,
- thumbData: imageBytes,
- title: title,
- description: "新华新消费",
- scene: WeChatScene.session,
- );
- try {
- await fluwx.share(model);
- } catch (e) {
- consoleLog("分享网页异常:${e.toString()}");
- }
- } catch (e) {
- consoleLog('分享失败: $e');
- }
- }
- //系统分享
- Future<void> shareUrl({required String title, required String url}) async {
- try {
- // 1. 加载本地图标资源
- final ByteData byteData = await rootBundle.load(
- 'assets/images/appIcon.png',
- );
- final Uint8List imageBytes = byteData.buffer.asUint8List();
- // 2. 创建临时文件
- final tempDir = await getTemporaryDirectory();
- final file = File('${tempDir.path}/logo.png');
- await file.writeAsBytes(imageBytes);
- if (Platform.isIOS == true) {
- // 3. 使用 share_plus 分享图文
- final result = await SharePlus.instance.share(
- ShareParams(
- text: '$title\n$url',
- subject: title,
- previewThumbnail: XFile(file.path),
- files: [XFile(file.path)],
- ),
- );
- if (result.status == ShareResultStatus.success) {
- consoleLog("分享成功");
- }
- } else {
- // 3. 使用 share_plus 分享图文
- final result = await SharePlus.instance.share(
- ShareParams(
- text: '$title\n$url',
- subject: title,
- // files: [XFile(file.path)],
- ),
- );
- if (result.status == ShareResultStatus.success) {
- consoleLog("分享成功");
- }
- }
- } catch (e) {
- consoleLog('分享失败: $e');
- }
- }
- Future<void> shareImage({
- required String title,
- required String imageUrl,
- }) async {
- try {
- // 1. 下载网络图片
- final response = await http.get(Uri.parse(imageUrl));
- if (response.statusCode != 200) {
- throw Exception("Failed to download image: ${response.statusCode}");
- }
- // 2. 保存图片到临时目录
- final directory = await getTemporaryDirectory();
- final fileName = "shared_image_${Random().nextInt(1000)}.jpg";
- final file = File("${directory.path}/$fileName");
- await file.writeAsBytes(response.bodyBytes);
- // 3. 分享图片
- final result = await SharePlus.instance.share(
- ShareParams(title: title, files: [XFile(file.path)]),
- );
- if (result.status == ShareResultStatus.success) {
- consoleLog("分享成功");
- }
- } catch (e) {
- consoleLog("分享失败: $e");
- }
- }
|