wx_sdk.dart 5.68 KB
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
// import 'check.dart';
import 'package:fluwx/fluwx.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart' show rootBundle;

class WxSdk {
  // static bool wxIsInstalled;

  static Future<Uint8List> loadAsset(String assets, {suffix = ".png"}) async {
    ByteData data = await rootBundle.load('assets/${assets}${suffix}');
    return Uint8List.sublistView(data);
  }

  static Future<Uint8List?> fetchImageAsUint8List(String imageUrl) async {
    try {
      final response = await http.get(Uri.parse(imageUrl));
      if (response.statusCode == 200) {
        return Uint8List.fromList(response.bodyBytes);
      } else {
        print('Failed to load image: ${response.statusCode}');
        return null;
      }
    } catch (e) {
      print('Error fetching image: $e');
      return null;
    }
  }

  static Future init() async {
    Fluwx fluwx = Fluwx();
    fluwx.registerApi(
        appId: "wx4d105e36a36a6139",
        doOnAndroid: true,
        doOnIOS: true,
        universalLink: "https://xt04.colorcun.com/link/OneKey/");
  }

  static Future<bool> wxIsInstalled() async {
    Fluwx fluwx = Fluwx();
    return await fluwx.isWeChatInstalled;
  }

  /**
   * 分享图片到微信,
   * file=本地路径
   * url=网络地址
   * asset=内置在app的资源图片
   * scene=分享场景,1好友会话,2朋友圈,3收藏
   */
  static void ShareImage(
      {String? title,
      String? decs,
      String? file,
      String? url,
      String? asset,
      int scene = 1}) async {
    Fluwx fluwx = Fluwx();
    WeChatScene wxScene = WeChatScene.session;
    if (scene == 2) {
      wxScene = WeChatScene.timeline;
    } else if (scene == 3) {
      wxScene = WeChatScene.favorite;
    }
    WeChatShareImageModel model;

    if (file != null) {
      if (Platform.isIOS) {
        model = WeChatShareImageModel(
            WeChatImageToShare(uint8List: File(file).readAsBytesSync()),
            title: title,
            description: decs,
            scene: wxScene);
      } else {
        model = WeChatShareImageModel(
            WeChatImageToShare(localImagePath: File(file).path),
            title: title,
            description: decs,
            scene: wxScene);
      }
    } else if (url != null) {
      model = WeChatShareImageModel(
          WeChatImageToShare(uint8List: await fetchImageAsUint8List(url)),
          title: title,
          description: decs,
          scene: wxScene);
    } else if (asset != null) {
      model = WeChatShareImageModel(
          WeChatImageToShare(uint8List: await loadAsset(asset)),
          title: title,
          description: decs,
          scene: wxScene);
    } else {
      throw Exception("缺少图片资源信息");
    }
    fluwx.share(model);
  }

  /**
   * 分享文本
   * content=分享内容
   * scene=分享场景,1好友会话,2朋友圈,3收藏
   */
  static void ShareText(String content, {String? title, int scene = 1}) {
    Fluwx fluwx = Fluwx();
    WeChatScene wxScene = WeChatScene.session;
    if (scene == 2) {
      wxScene = WeChatScene.timeline;
    } else if (scene == 3) {
      wxScene = WeChatScene.favorite;
    }
    WeChatShareTextModel model =
        WeChatShareTextModel(content, title: title, scene: wxScene);
    fluwx.share(model);
  }

/***
 * 分享视频
 * videoUrl=视频网上地址
 * thumbFile=缩略图本地路径
   * scene=分享场景,1好友会话,2朋友圈,3收藏
 */
  static void ShareVideo(String videoUrl,
      {String? thumbFile, String? title, String? desc, int scene = 1}) {
    Fluwx fluwx = Fluwx();
    WeChatScene wxScene = WeChatScene.session;
    if (scene == 2) {
      wxScene = WeChatScene.timeline;
    } else if (scene == 3) {
      wxScene = WeChatScene.favorite;
    }
    // WeChatImage? image;
    // if (thumbFile != null) {
    //   image = WeChatImage.file(File(thumbFile));
    // }
    var model = WeChatShareVideoModel(
        videoUrl: videoUrl,
        thumbData: thumbFile == null ? null : File(thumbFile).readAsBytesSync(),
        title: title,
        description: desc,
        scene: wxScene);
    fluwx.share(model);
  }

  /**
   * 分享链接
   * url=链接
   * thumbFile=缩略图本地路径
   * scene=分享场景,1好友会话,2朋友圈,3收藏
   */

  static bool strNoEmpty(String? str) {
    if (str == null || str == '') {
      return false;
    }
    return true;
  }

  static void ShareUrl(String url,
      {String? thumbFile,
      Uint8List? thumbBytes,
      String? title,
      String? desc,
      int scene = 1,
      String? networkThumb,
      String? assetThumb}) async {
    Fluwx fluwx = Fluwx();
    desc = desc ?? "";
    title = title ?? "";
    if (desc.length > 54) {
      desc = desc.substring(0, 54) + "...";
    }
    if (title.length > 20) {
      title = title.substring(0, 20) + "...";
    }
    WeChatScene wxScene = WeChatScene.session;
    if (scene == 2) {
      wxScene = WeChatScene.timeline;
    } else if (scene == 3) {
      wxScene = WeChatScene.favorite;
    }
    Uint8List? image;
    if (thumbFile != null) {
      image = File(thumbFile).readAsBytesSync();
    } else if (thumbBytes != null) {
      image = thumbBytes;
    } else if (strNoEmpty(networkThumb)) {
      image = networkThumb == null
          ? null
          : await fetchImageAsUint8List(networkThumb);
    } else if (strNoEmpty(assetThumb)) {
      image = assetThumb == null
          ? null
          : await loadAsset(assetThumb, suffix: ".png");
    }
    var model = WeChatShareWebPageModel(
      url,
      thumbData: image,
      title: title,
      description: desc,
      scene: wxScene,
    );
    fluwx.share(model);
  }
}