首先有两个库
wechat_kit 基本分享 小程序分享等
wechat_kit | Flutter PackageA powerful Flutter plugin allowing developers to auth/share/pay with natvie Android & iOS Wechat SDKs.https://pub-web.flutter-io.cn/packages/wechat_kit需要调用微信sdk的api 还有使用下面的库
wechat_kit_extension | Dart PackageAn extension package for wechat_kit, including WeChat API integrations.https://pub-web.flutter-io.cn/packages/wechat_kit_extension
一、基本分享功能、
yaml 配置wechat_kit
dependencies:
wechat_kit: ^${latestTag}
# wechat_kit:
# git:
# url: https://github.com/RxReader/wechat_kit.gitwechat_kit:
# ios: no_pay # 默认 pay
app_id: ${your wechat app id}
universal_link: https://${your applinks domain}/universal_link/${example_app}/wechat/
进行封装
自己根据实际情况 进行下面的配置
static const String kAppId = '';
static const String kUniversalLink =
'';
static const String kAppSecret = '';static const String kMini = '';
static const String weixiCustomerService =
'';
static const String corpId = '';
封装代码
class WeChatConfig {
static const String kAppId = '';
static const String kUniversalLink =
'';
static const String kAppSecret = '';
static const String kMini = '';
static const String weixiCustomerService =
'';
static const String corpId = '';
static register() {
WechatKitPlatform.instance.registerApp(
appId: kAppId,
universalLink: kUniversalLink,
);
WechatKitPlatform.instance.reqStream().listen((event) {
final info = event.toJson();
// print("[WeChat] $info");
if (info["messageExt"] != null && info["messageExt"] != "") {
Get.to(() => ArticleDes(
communityArticlesData: ValueNotifier(
CommunityArticlesData(
id: info["messageExt"],
),
),
));
}
});
}
static Future login() {
return WechatKitPlatform.instance.auth(
scope: <String>[WechatScope.kSNSApiUserInfo],
state: 'auth',
);
}
static Future launchMiniProgram(String path) {
String dev_path;
if (release) {
dev_path = '$path&environment=pro';
} else {
dev_path = '$path&environment=test';
}
return WechatKitPlatform.instance.launchMiniProgram(
userName: kMini,
path: dev_path,
type:
release ? WechatMiniProgram.kRelease : WechatMiniProgram.kPreview);
}
static Future shareMiniProgram(
String title, String path, String webpageUrl, Uint8List uint8list) async {
if (!await WechatKitPlatform.instance.isInstalled()) {
if (Platform.isIOS) {
await launchUrl(Uri.parse(Config.wxIosInstallAddress));
} else {
await launchUrl(Uri.parse(Config.wxAndroidInstallAddress));
}
return;
}
return WechatKitPlatform.instance
.shareMiniProgram(
scene: WechatScene.kSession,
title: title,
path: path,
webpageUrl: webpageUrl,
hdImageData: uint8list,
type: release
? WechatMiniProgram.kRelease
: WechatMiniProgram.kPreview,
userName: kMini)
.catchError((e) {
Get.log('===$e');
});
}
static Future shareLinkToWeChat({
String? title,
String? description,
required String webpageUrl,
Uint8List? uint8list,
}) async {
if (!await WechatKitPlatform.instance.isInstalled()) {
if (Platform.isIOS) {
await launchUrl(Uri.parse(Config.wxIosInstallAddress));
} else {
await launchUrl(Uri.parse(Config.wxAndroidInstallAddress));
}
return;
}
try {
await WechatKitPlatform.instance.shareWebpage(
title: title,
description: description,
scene: WechatScene.kSession,
webpageUrl: webpageUrl,
thumbData: uint8list,
);
} catch (e) {
// Handle any potential errors
}
}
static Future shareImg(
{String? title,
String? description,
Uint8List? imageData,
Uint8List? thumbData,
Uri? imageUri}) async {
if (!await WechatKitPlatform.instance.isInstalled()) {
if (Platform.isIOS) {
await launchUrl(Uri.parse(Config.wxIosInstallAddress));
} else {
await launchUrl(Uri.parse(Config.wxAndroidInstallAddress));
}
return;
}
return WechatKitPlatform.instance
.shareImage(
scene: WechatScene.kSession,
title: 'app',
description: description,
imageData: imageData,
thumbData: thumbData,
imageUri: imageUri)
.catchError((e) {
Get.log('===$e');
});
}
static Future openCustomerServiceChat() {
return WechatKitPlatform.instance
.openCustomerServiceChat(corpId: corpId, url: weixiCustomerService);
}
//通过ImageProvider读取Image
static Future<ui.Image> loadImageByProvider(
ImageProvider provider, {
ImageConfiguration config = ImageConfiguration.empty,
}) async {
Completer<ui.Image> completer = Completer<ui.Image>(); //完成的回调
late ImageStreamListener listener;
ImageStream stream = provider.resolve(config); //获取图片流
listener = ImageStreamListener((ImageInfo frame, bool sync) {
//监听
final ui.Image image = frame.image;
completer.complete(image); //完成
stream.removeListener(listener); //移除监听
});
stream.addListener(listener); //添加监听
return completer.future; //返回
}
//通过[Uint8List]获取图片,默认宽高[width][height]
static Future<ui.Image> loadImageByUint8List(
Uint8List list, {
required int width,
required int height,
}) async {
ui.Codec codec = await ui.instantiateImageCodec(list,
targetWidth: width, targetHeight: height);
ui.FrameInfo frame = await codec.getNextFrame();
return frame.image;
}
static Future<String> saveImage(Uint8List imageBytes) async {
final directory = await getApplicationDocumentsDirectory();
final String path =
'${directory.path}/image_${DateTime.now().millisecondsSinceEpoch}.png';
final file = File(path);
await file.writeAsBytes(imageBytes);
return path;
}
static Future<Uint8List> widgetToImage(GlobalKey _globalKey,
{double pixelRatio = 1}) async {
Completer<Uint8List> completer = Completer();
RenderRepaintBoundary render =
_globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
ui.Image image = await render.toImage(pixelRatio: pixelRatio);
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
completer.complete(byteData?.buffer.asUint8List());
return completer.future;
}
///图片压缩
static Future<Uint8List> compressAsset(Uint8List u, int w, int h) async {
if (u.buffer.lengthInBytes > (128 * 1024)) {
print("===开始压缩图片${u.buffer.lengthInBytes ~/ 1024}");
var list = await FlutterImageCompress.compressWithList(u,
minHeight: w, minWidth: h, quality: 95, format: CompressFormat.jpeg);
print("===压缩过后图片${list.buffer.lengthInBytes ~/ 1024}");
if (list.buffer.lengthInBytes > (128 * 1024)) {
print("===继续压缩图片大小");
list = await compressAsset(list, w, h);
}
return list;
} else {
return u;
}
}
///widget转图片 如果大于128kb 进行压缩
static Future<Uint8List> widgetToMiniImage(GlobalKey _globalKey,
{double pixelRatio = 1}) async {
Completer<Uint8List> completer = Completer();
Get.log('=_globalKey=${_globalKey.currentContext?.width}');
Get.log('=_globalKey=${_globalKey.currentContext?.height}');
RenderRepaintBoundary render =
_globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
ui.Image image = await render.toImage(pixelRatio: 3);
double scale = calcScale(
srcWidth: image.width.toDouble(), srcHeight: image.height.toDouble());
Get.log('=$scale image=${image.width}');
Get.log('=image=${image.height}');
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
ui.Codec codec = await ui.instantiateImageCodec(
byteData!.buffer.asUint8List(),
targetWidth: 500,
targetHeight: 400);
ui.FrameInfo frame = await codec.getNextFrame();
Get.log('=frame=${frame.image.width}');
Get.log('=frame=${frame.image.height}');
byteData = await frame.image.toByteData(format: ui.ImageByteFormat.png);
Uint8List u = await compressAsset(byteData!.buffer.asUint8List(),
image.width ~/ scale, image.height ~/ scale);
Get.log('=len=${(u.buffer.lengthInBytes) / 1024}');
completer.complete(u);
return completer.future;
}
static double calcScale({
required double srcWidth,
required double srcHeight,
}) {
var scaleW = srcWidth / 500;
var scaleH = srcHeight / 400;
var scale = math.max(1.0, math.min(scaleW, scaleH));
return scale;
}
static Future<Uint8List> imGToMiniImage(ui.Image img,
{double pixelRatio = 1}) async {
Completer<Uint8List> completer = Completer();
ui.Image image = img;
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
ui.Codec codec = await ui.instantiateImageCodec(
byteData!.buffer.asUint8List(),
targetWidth: 300,
targetHeight: 230);
ui.FrameInfo frame = await codec.getNextFrame();
Get.log('=frame=${frame.image.width}');
Get.log('=frame=${frame.image.height}');
byteData = await frame.image.toByteData(format: ui.ImageByteFormat.png);
if ((byteData?.buffer.lengthInBytes ?? 0) >= 131072) {}
completer.complete(byteData?.buffer.asUint8List());
return completer.future;
}
}
h5打开app通过 wx-open-launch-app 标签进行打开 携带参数extinfo app端接收
App获取开放标签\中的extinfo数据 | 微信开放文档
接收逻辑如下
WechatKitPlatform.instance.reqStream().listen((event) {
final info = event.toJson();
// print("[WeChat] $info");
if (info["messageExt"] != null && info["messageExt"] != "") {
// 做跳转等操作
}
});
判断是否下载微信,进行不同平台微信下载
static const wxIosInstallAddress =
"https://apps.apple.com/cn/app/wechat/id414478124";
static const wxAndroidInstallAddress = "https://weixin.qq.com/";
if (!await WechatKitPlatform.instance.isInstalled()) {
if (Platform.isIOS) {
await launchUrl(Uri.parse(Config.wxIosInstallAddress));
} else {
await launchUrl(Uri.parse(Config.wxAndroidInstallAddress));
}
return;
}