Browse Source

refactor(message): 重构消息系统支持自定义消息格式

- 将礼物消息从文本消息改为自定义消息格式,使用 event 和 params 参数
- 更新聊天控制器中的消息发送逻辑,使用 sendCustomMessage 替代 sendTextMessage
- 修改礼物消息解析逻辑,同时支持新格式自定义消息和旧格式文本消息
- 更新直播间邀请消息为自定义消息格式,移除不必要的 type 字段
- 添加对旧格式特殊消息的过滤,避免显示 JSON 内容
- 优化消息类型判断逻辑,统一处理新旧格式的消息识别
master
Jolie 3 months ago
parent
commit
51077a5c7f
5 changed files with 62 additions and 28 deletions
  1. 25
      lib/controller/message/chat_controller.dart
  2. 7
      lib/widget/live/live_room_guest_list_dialog.dart
  3. 27
      lib/widget/message/gift_item.dart
  4. 16
      lib/widget/message/message_item.dart
  5. 15
      lib/widget/message/text_item.dart

25
lib/controller/message/chat_controller.dart

@ -1749,31 +1749,28 @@ class ChatController extends GetxController {
return false;
}
//
final giftMessageContent = jsonEncode({
'type': 'gift',
// Map<String, String>
final giftParams = <String, String>{
'giftProductId': gift.productId,
'giftProductTitle': gift.productTitle,
'giftMainPic': gift.mainPic,
'giftPrice': gift.unitSellingPrice,
'quantity': quantity,
});
// JSON
final content = '[GIFT:]$giftMessageContent';
'giftPrice': gift.unitSellingPrice.toString(),
'quantity': quantity.toString(),
};
// 使
final tempMessage = EMMessage.createTxtSendMessage(
// 使
final tempMessage = EMMessage.createCustomSendMessage(
targetId: userId,
content: content,
event: 'gift',
params: giftParams,
);
//
messages.add(tempMessage);
update();
//
final message = await IMManager.instance.sendTextMessage(content, userId);
//
final message = await IMManager.instance.sendCustomMessage(userId, 'gift', giftParams);
if (message != null) {
//
final index = messages.indexWhere((msg) => msg.msgId == tempMessage.msgId);

7
lib/widget/live/live_room_guest_list_dialog.dart

@ -632,9 +632,8 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
final cleanedAvatar = anchorAvatar.trim().replaceAll('`', '');
final cleanedCover = cleanedAvatar; // 使
//
final messageData = <String, String>{
'type': 'live_room_invite',
// type event 'live_room_invite'
final messageParams = <String, String>{
'channelId': channelId,
'anchorAvatar': cleanedAvatar,
'anchorName': anchorName,
@ -647,7 +646,7 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
final result = await IMManager.instance.sendCustomMessage(
targetUserId,
'live_room_invite',
messageData,
messageParams,
);
if (result != null) {

27
lib/widget/message/gift_item.dart

@ -25,14 +25,28 @@ class GiftItem extends StatelessWidget {
super.key,
});
/// 使JSON格式
/// params
Map<String, dynamic>? _parseGiftInfo() {
try {
//
if (message.body.type == MessageType.CUSTOM) {
final customBody = message.body as EMCustomMessageBody;
if (customBody.event == 'gift' && customBody.params != null) {
// Map<String, String> Map<String, dynamic>
final params = customBody.params!;
return {
'giftProductId': params['giftProductId'] ?? '',
'giftProductTitle': params['giftProductTitle'] ?? '',
'giftMainPic': params['giftMainPic'] ?? '',
'giftPrice': params['giftPrice'] ?? '0',
'quantity': int.tryParse(params['quantity'] ?? '1') ?? 1,
};
}
}
// [GIFT:]
if (message.body.type == MessageType.TXT) {
final textBody = message.body as EMTextMessageBody;
final content = textBody.content;
// [GIFT:]
if (content.startsWith('[GIFT:]')) {
final jsonStr = content.substring(7); // '[GIFT:]'
return jsonDecode(jsonStr) as Map<String, dynamic>;
@ -66,7 +80,12 @@ class GiftItem extends StatelessWidget {
int _getGiftQuantity() {
final giftInfo = _parseGiftInfo();
if (giftInfo != null) {
return giftInfo['quantity'] as int? ?? 1;
final quantity = giftInfo['quantity'];
if (quantity is int) {
return quantity;
} else if (quantity is String) {
return int.tryParse(quantity) ?? 1;
}
}
return 1;
}

16
lib/widget/message/message_item.dart

@ -41,14 +41,18 @@ class MessageItem extends StatelessWidget {
return false;
}
//
//
bool _isGiftMessage() {
try {
//
if (message.body.type == MessageType.CUSTOM) {
final customBody = message.body as EMCustomMessageBody;
return customBody.event == 'gift';
}
// [GIFT:]
if (message.body.type == MessageType.TXT) {
final textBody = message.body as EMTextMessageBody;
final content = textBody.content;
// [GIFT:]
return content.startsWith('[GIFT:]');
return textBody.content.startsWith('[GIFT:]');
}
} catch (e) {
//
@ -92,8 +96,8 @@ class MessageItem extends StatelessWidget {
);
}
//
if (message.body.type == MessageType.TXT && _isGiftMessage()) {
//
if (_isGiftMessage()) {
return GiftItem(
message: message,
isSentByMe: isSentByMe,

15
lib/widget/message/text_item.dart

@ -27,8 +27,23 @@ class TextItem extends StatelessWidget {
super.key,
});
///
bool _isLegacySpecialMessage() {
final content = textBody.content;
//
return content.startsWith('[GIFT:]') ||
content.startsWith('[ROOM:]') ||
content.startsWith('[CALL:]');
}
@override
Widget build(BuildContext context) {
// JSON
if (_isLegacySpecialMessage()) {
//
return SizedBox.shrink();
}
//
final revenueInfo = _getRevenueInfo();

Loading…
Cancel
Save