wx_sdk.dart
5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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: "这里写你注册的appid",
doOnAndroid: true,
doOnIOS: true,
universalLink: "这里写你注册的universalLink");
}
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) {
model = WeChatShareImageModel(
WeChatImageToShare(uint8List: File(file).readAsBytesSync()),
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);
}
}