diff --git a/lib/im/im_manager.dart b/lib/im/im_manager.dart index 1b44a1a..a9ac66c 100644 --- a/lib/im/im_manager.dart +++ b/lib/im/im_manager.dart @@ -203,31 +203,24 @@ class IMManager { } // 处理金币数值:只对接收到的消息处理金币标签 - // 注意:根据实际API,金币数值可能通过 operatorId 或消息 attributes 传递 + // 从消息 attributes 中的 revenueInfo 获取金币信息并直接显示 // 只处理接收到的消息(自己发送的消息不显示金币标签) if (message.direction == MessageDirection.RECEIVE) { try { - double? coinValue; + String? revenueInfo; - // 方法1:尝试从消息 attributes 中获取金币数值 + // 从消息 attributes 中获取 revenueInfo if (message.attributes != null) { - final coinValueStr = message.attributes!['coin_value'] as String?; - if (coinValueStr != null && coinValueStr.isNotEmpty) { - coinValue = double.tryParse(coinValueStr); - } - } - - // 方法2:如果 attributes 中没有,尝试从 operatorId 解析(如果它是数值) - if (coinValue == null && operatorId.isNotEmpty) { - coinValue = double.tryParse(operatorId); + revenueInfo = message.attributes!['revenueInfo'] as String?; } - // 如果获取到金币数值,确保存储到消息的 attributes 中 - if (coinValue != null && coinValue > 0) { + // 如果获取到 revenueInfo,确保存储到消息的 attributes 中(用于UI显示) + if (revenueInfo != null && revenueInfo.isNotEmpty) { if (message.attributes == null) { message.attributes = {}; } - message.attributes!['coin_value'] = coinValue.toString(); + // 将 revenueInfo 存储到 coin_value 中,以便UI组件可以直接使用 + message.attributes!['coin_value'] = revenueInfo; // 通知对应的 ChatController 更新消息 final fromId = message.from; @@ -240,7 +233,7 @@ class IMManager { controller.messages[index] = message; controller.update(); if (Get.isLogEnable) { - Get.log('✅ [IMManager] 已更新接收消息的金币数值: msgId=${message.msgId}, coinValue=$coinValue'); + Get.log('✅ [IMManager] 已更新接收消息的金币信息: msgId=${message.msgId}, revenueInfo=$revenueInfo'); } } } @@ -248,7 +241,7 @@ class IMManager { } } catch (e) { if (Get.isLogEnable) { - Get.log('⚠️ [IMManager] 处理金币数值失败: $e'); + Get.log('⚠️ [IMManager] 处理金币信息失败: $e'); } } } diff --git a/lib/widget/message/image_item.dart b/lib/widget/message/image_item.dart index d08ccad..a85cf9a 100644 --- a/lib/widget/message/image_item.dart +++ b/lib/widget/message/image_item.dart @@ -232,8 +232,8 @@ class _ImageItemState extends State { @override Widget build(BuildContext context) { - // 检查是否有金币数值(只对接收的消息显示) - final coinValue = _getCoinValue(); + // 检查是否有金币信息(只对接收的消息显示) + final revenueInfo = _getRevenueInfo(); return Column( children: [ @@ -286,10 +286,10 @@ class _ImageItemState extends State { ), ), // 金币标签(只对接收的消息显示) - if (!widget.isSentByMe && coinValue != null) + if (!widget.isSentByMe && revenueInfo != null) Padding( padding: EdgeInsets.only(top: 10.h), - child: _buildCoinLabel(coinValue), + child: _buildCoinLabel(revenueInfo), ), ], ), @@ -651,14 +651,20 @@ class _ImageItemState extends State { } } - // 获取金币数值 - double? _getCoinValue() { + // 获取金币信息(从 revenueInfo 或 coin_value 中获取) + String? _getRevenueInfo() { try { final attributes = widget.message.attributes; - if (attributes != null && attributes.containsKey('coin_value')) { + if (attributes != null) { + // 优先从 revenueInfo 获取 + final revenueInfo = attributes['revenueInfo'] as String?; + if (revenueInfo != null && revenueInfo.isNotEmpty) { + return revenueInfo; + } + // 如果没有 revenueInfo,从 coin_value 获取(可能是之前存储的 revenueInfo 值) final coinValueStr = attributes['coin_value'] as String?; if (coinValueStr != null && coinValueStr.isNotEmpty) { - return double.tryParse(coinValueStr); + return coinValueStr; } } } catch (e) { @@ -668,7 +674,7 @@ class _ImageItemState extends State { } // 构建金币标签 - Widget _buildCoinLabel(double coinValue) { + Widget _buildCoinLabel(String revenueInfo) { return Container( padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h), decoration: BoxDecoration( @@ -685,7 +691,7 @@ class _ImageItemState extends State { ), SizedBox(width: 4.w), Text( - '+${coinValue.toStringAsFixed(2)}', + revenueInfo, style: TextStyle( fontSize: 12.sp, color: Color.fromRGBO(255, 132, 0, 1), diff --git a/lib/widget/message/text_item.dart b/lib/widget/message/text_item.dart index 769cc01..36da0b7 100644 --- a/lib/widget/message/text_item.dart +++ b/lib/widget/message/text_item.dart @@ -25,8 +25,8 @@ class TextItem extends StatelessWidget { @override Widget build(BuildContext context) { - // 检查是否有金币数值(只对接收的消息显示) - final coinValue = _getCoinValue(); + // 检查是否有金币信息(只对接收的消息显示) + final revenueInfo = _getRevenueInfo(); return Column( children: [ @@ -82,10 +82,10 @@ class TextItem extends StatelessWidget { ), ), // 金币标签(只对接收的消息显示) - if (!isSentByMe && coinValue != null) + if (!isSentByMe && revenueInfo != null) Padding( padding: EdgeInsets.only(top: 10.h), - child: _buildCoinLabel(coinValue), + child: _buildCoinLabel(revenueInfo), ), ], ), @@ -135,14 +135,20 @@ class TextItem extends StatelessWidget { ); } - // 获取金币数值 - double? _getCoinValue() { + // 获取金币信息(从 revenueInfo 或 coin_value 中获取) + String? _getRevenueInfo() { try { final attributes = message.attributes; - if (attributes != null && attributes.containsKey('coin_value')) { + if (attributes != null) { + // 优先从 revenueInfo 获取 + final revenueInfo = attributes['revenueInfo'] as String?; + if (revenueInfo != null && revenueInfo.isNotEmpty) { + return revenueInfo; + } + // 如果没有 revenueInfo,从 coin_value 获取(可能是之前存储的 revenueInfo 值) final coinValueStr = attributes['coin_value'] as String?; if (coinValueStr != null && coinValueStr.isNotEmpty) { - return double.tryParse(coinValueStr); + return coinValueStr; } } } catch (e) { @@ -152,7 +158,7 @@ class TextItem extends StatelessWidget { } // 构建金币标签 - Widget _buildCoinLabel(double coinValue) { + Widget _buildCoinLabel(String revenueInfo) { return Container( padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h), decoration: BoxDecoration( @@ -169,7 +175,7 @@ class TextItem extends StatelessWidget { ), SizedBox(width: 4.w), Text( - '+${coinValue.toStringAsFixed(2)}', + revenueInfo, style: TextStyle( fontSize: 12.sp, color: Color.fromRGBO(255, 132, 0, 1), diff --git a/lib/widget/message/video_item.dart b/lib/widget/message/video_item.dart index ae4e8de..8c8f818 100644 --- a/lib/widget/message/video_item.dart +++ b/lib/widget/message/video_item.dart @@ -203,8 +203,8 @@ class _VideoItemState extends State { @override Widget build(BuildContext context) { - // 检查是否有金币数值(只对接收的消息显示) - final coinValue = _getCoinValue(); + // 检查是否有金币信息(只对接收的消息显示) + final revenueInfo = _getRevenueInfo(); return Column( children: [ @@ -376,10 +376,10 @@ class _VideoItemState extends State { ], ), // 金币标签(只对接收的消息显示) - if (!widget.isSentByMe && coinValue != null) + if (!widget.isSentByMe && revenueInfo != null) Padding( padding: EdgeInsets.only(top: 10.h), - child: _buildCoinLabel(coinValue), + child: _buildCoinLabel(revenueInfo), ), ], ), @@ -459,14 +459,20 @@ class _VideoItemState extends State { } } - // 获取金币数值 - double? _getCoinValue() { + // 获取金币信息(从 revenueInfo 或 coin_value 中获取) + String? _getRevenueInfo() { try { final attributes = widget.message.attributes; - if (attributes != null && attributes.containsKey('coin_value')) { + if (attributes != null) { + // 优先从 revenueInfo 获取 + final revenueInfo = attributes['revenueInfo'] as String?; + if (revenueInfo != null && revenueInfo.isNotEmpty) { + return revenueInfo; + } + // 如果没有 revenueInfo,从 coin_value 获取(可能是之前存储的 revenueInfo 值) final coinValueStr = attributes['coin_value'] as String?; if (coinValueStr != null && coinValueStr.isNotEmpty) { - return double.tryParse(coinValueStr); + return coinValueStr; } } } catch (e) { @@ -476,7 +482,7 @@ class _VideoItemState extends State { } // 构建金币标签 - Widget _buildCoinLabel(double coinValue) { + Widget _buildCoinLabel(String revenueInfo) { return Container( padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h), decoration: BoxDecoration( @@ -493,7 +499,7 @@ class _VideoItemState extends State { ), SizedBox(width: 4.w), Text( - '+${coinValue.toStringAsFixed(2)}', + revenueInfo, style: TextStyle( fontSize: 12.sp, color: Color.fromRGBO(255, 132, 0, 1), diff --git a/lib/widget/message/voice_item.dart b/lib/widget/message/voice_item.dart index cb3bc82..51363ec 100644 --- a/lib/widget/message/voice_item.dart +++ b/lib/widget/message/voice_item.dart @@ -235,8 +235,8 @@ class _VoiceItemState extends State with TickerProviderStateMixin { // 判断当前音频是否正在播放 final isPlaying = _playerManager.isPlaying(widget.messageId); - // 检查是否有金币数值(只对接收的消息显示) - final coinValue = _getCoinValue(); + // 检查是否有金币信息(只对接收的消息显示) + final revenueInfo = _getRevenueInfo(); return Column( children: [ @@ -309,10 +309,10 @@ class _VoiceItemState extends State with TickerProviderStateMixin { _handlePlayPause(); }), // 金币标签(只对接收的消息显示) - if (!widget.isSentByMe && coinValue != null) + if (!widget.isSentByMe && revenueInfo != null) Padding( padding: EdgeInsets.only(top: 10.h), - child: _buildCoinLabel(coinValue), + child: _buildCoinLabel(revenueInfo), ), ], ), @@ -355,15 +355,21 @@ class _VoiceItemState extends State with TickerProviderStateMixin { ); } - // 获取金币数值 - double? _getCoinValue() { + // 获取金币信息(从 revenueInfo 或 coin_value 中获取) + String? _getRevenueInfo() { try { if (widget.message != null) { final attributes = widget.message!.attributes; - if (attributes != null && attributes.containsKey('coin_value')) { + if (attributes != null) { + // 优先从 revenueInfo 获取 + final revenueInfo = attributes['revenueInfo'] as String?; + if (revenueInfo != null && revenueInfo.isNotEmpty) { + return revenueInfo; + } + // 如果没有 revenueInfo,从 coin_value 获取(可能是之前存储的 revenueInfo 值) final coinValueStr = attributes['coin_value'] as String?; if (coinValueStr != null && coinValueStr.isNotEmpty) { - return double.tryParse(coinValueStr); + return coinValueStr; } } } @@ -374,7 +380,7 @@ class _VoiceItemState extends State with TickerProviderStateMixin { } // 构建金币标签 - Widget _buildCoinLabel(double coinValue) { + Widget _buildCoinLabel(String revenueInfo) { return Container( padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h), decoration: BoxDecoration( @@ -391,7 +397,7 @@ class _VoiceItemState extends State with TickerProviderStateMixin { ), SizedBox(width: 4.w), Text( - '+${coinValue.toStringAsFixed(2)}', + revenueInfo, style: TextStyle( fontSize: 12.sp, color: Color.fromRGBO(255, 132, 0, 1),