Skip to content

Commit 631b1bb

Browse files
HMWDLGYifei Xu
andauthored
iOS 和 Android 增加分享 gif 动图到聊天的封装 (#714)
双端测试通过 Co-authored-by: Yifei Xu <[email protected]>
1 parent e558d33 commit 631b1bb

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

android/src/main/kotlin/com/jarvan/fluwx/handlers/FluwxShareHandler.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import java.io.File
1616
import java.io.IOException
1717
import java.util.*
1818
import kotlin.coroutines.CoroutineContext
19+
import android.os.Build
20+
import io.flutter.plugin.common.MethodChannel.Result
1921

2022

2123
/***
@@ -65,6 +67,7 @@ internal interface FluwxShareHandler : CoroutineScope {
6567
"shareVideo" -> shareVideo(call, result)
6668
"shareWebPage" -> shareWebPage(call, result)
6769
"shareFile" -> shareFile(call, result)
70+
"shareEmoji" -> shareEmoji(call, result)
6871
else -> {
6972
result.notImplemented()
7073
}
@@ -266,6 +269,39 @@ internal interface FluwxShareHandler : CoroutineScope {
266269
}
267270
}
268271

272+
private fun shareEmoji(call: MethodCall, result: Result) {
273+
274+
val scene = call.argument<Int>("scene") ?: 0
275+
val title = call.argument<String>("title")
276+
val desc = call.argument<String>("description")
277+
val thumbData = call.argument<ByteArray>("thumbData")
278+
279+
val emojiMap = call.argument<Map<String, Any?>>("source")
280+
?: run { result.error("ARG", "emoji is null", null); return }
281+
282+
val emojiObj = WXEmojiObject().apply {
283+
when {
284+
emojiMap["uint8List"] != null -> emojiData = emojiMap["uint8List"] as ByteArray
285+
emojiMap["path"] != null -> emojiPath = ensurePublicPath(emojiMap["path"] as String)
286+
else -> { result.error("ARG", "gif source missing", null); return }
287+
}
288+
}
289+
290+
val msg = WXMediaMessage(emojiObj).apply {
291+
this.thumbData = thumbData
292+
this.title = title
293+
this.description = desc
294+
}
295+
296+
val req = SendMessageToWX.Req().apply {
297+
transaction = "emoji${System.currentTimeMillis()}"
298+
message = msg
299+
this.scene = scene
300+
}
301+
302+
result.success(WXAPiHandler.wxApi?.sendReq(req))
303+
}
304+
269305
private suspend fun sendRequestInMain(result: MethodChannel.Result, request: BaseReq) =
270306
withContext(Dispatchers.Main) {
271307
result.success(WXAPiHandler.wxApi?.sendReq(request))
@@ -326,6 +362,17 @@ internal interface FluwxShareHandler : CoroutineScope {
326362

327363
}
328364

365+
private fun loadBytesFromFlutterAsset(assetKey: String): ByteArray =
366+
context.assets.open(assetKey.removePrefix("flutterassets/")).use { it.readBytes() }
367+
368+
private fun ensurePublicPath(original: String): String {
369+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return original
370+
val src = File(original)
371+
val dst = File(context.externalCacheDir, src.name)
372+
if (!dst.exists()) src.copyTo(dst, overwrite = true)
373+
return dst.path
374+
}
375+
329376
private val supportFileProvider: Boolean
330377
get() = (WXAPiHandler.wxApi?.wxAppSupportAPI ?: 0) >= 0x27000D00
331378
private val targetHigherThanN: Boolean get() = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N

ios/Classes/FluwxPlugin.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ - (void)handleShare:(FlutterMethodCall *)call result:(FlutterResult)result {
509509
[self shareMiniProgram:call result:result];
510510
} else if ([@"shareFile" isEqualToString:call.method]) {
511511
[self shareFile:call result:result];
512+
} else if ([@"shareEmoji" isEqualToString:call.method]) {
513+
[self shareEmoji:call result:result];
512514
}
513515
}
514516

@@ -739,6 +741,37 @@ - (void)shareMiniProgram:(FlutterMethodCall *)call result:(FlutterResult)result
739741
});
740742
}
741743

744+
- (void)shareEmoji:(FlutterMethodCall *)call result:(FlutterResult)result {
745+
746+
NSNumber *sceneNum = call.arguments[fluwxKeyScene];
747+
enum WXScene scene = [self intToWeChatScene:sceneNum];
748+
749+
NSDictionary *sourceEmoji = call.arguments[keySource];
750+
FlutterStandardTypedData *flutterEmojiData = sourceEmoji[@"uint8List"];
751+
NSData *emojiData = flutterEmojiData != nil ? flutterEmojiData.data : nil;
752+
753+
FlutterStandardTypedData *flutterThumbData = call.arguments[fluwxKeyThumbData];
754+
NSData *thumbData = ![flutterThumbData isKindOfClass:[NSNull class]] ? flutterThumbData.data : nil;
755+
756+
NSString *msgSignature = call.arguments[fluwxKeyMsgSignature];
757+
NSString *thumbHash = call.arguments[fluwxKeyThumbDataHash];
758+
759+
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
760+
dispatch_async(globalQueue, ^{
761+
dispatch_async(dispatch_get_main_queue(), ^{
762+
763+
[self sendEmotionData:emojiData
764+
InScene:scene
765+
MsgSignature:msgSignature
766+
ThumbData:thumbData
767+
ThumbDataHash:thumbHash
768+
completion:^(BOOL success) {
769+
result(@(success));
770+
}];
771+
});
772+
});
773+
}
774+
742775
- (NSData *)getNsDataFromWeChatFile:(NSDictionary *)weChatFile {
743776
NSNumber *schema = weChatFile[@"schema"];
744777

lib/src/foundation/share_models.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,32 @@ class WeChatShareFileModel extends WeChatShareModel {
339339
};
340340
}
341341

342+
class WeChatShareEmojiModel extends WeChatShareModel {
343+
final WeChatImageToShare emoji;
344+
final WeChatScene scene;
345+
346+
WeChatShareEmojiModel(
347+
this.emoji, {
348+
this.scene = WeChatScene.session,
349+
super.title,
350+
super.description,
351+
super.msgSignature,
352+
super.thumbData,
353+
super.thumbDataHash,
354+
});
355+
356+
@override
357+
Map<String, dynamic> get arguments => {
358+
_scene: scene.index,
359+
_source: emoji.arguments,
360+
_title: title,
361+
_description: description,
362+
_msgSignature: msgSignature,
363+
_thumbData: thumbData,
364+
_thumbDataHash: thumbDataHash,
365+
};
366+
}
367+
342368
class WeChatImageToShare with _Argument {
343369
final Uint8List? uint8List;
344370
final String? localImagePath;

lib/src/method_channel/fluwx_method_channel.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class MethodChannelFluwx extends FluwxPlatform {
4545
WeChatShareWebPageModel: 'shareWebPage',
4646
WeChatShareMiniProgramModel: 'shareMiniProgram',
4747
WeChatShareFileModel: 'shareFile',
48+
WeChatShareEmojiModel: 'shareEmoji',
4849
};
4950

5051
final StreamController<WeChatResponse> _responseEventHandler =
@@ -100,9 +101,12 @@ class MethodChannelFluwx extends FluwxPlatform {
100101
Future<bool> open(OpenType target) async {
101102
switch (target) {
102103
case WeChatApp():
103-
return await methodChannel.invokeMethod('openWXApp', target.arguments) ?? false;
104+
return await methodChannel.invokeMethod(
105+
'openWXApp', target.arguments) ??
106+
false;
104107
case Browser():
105-
return await methodChannel.invokeMethod('openUrl', target.arguments) ?? false;
108+
return await methodChannel.invokeMethod('openUrl', target.arguments) ??
109+
false;
106110
case RankList():
107111
return await methodChannel.invokeMethod("openRankList") ?? false;
108112
case BusinessView():

0 commit comments

Comments
 (0)