From 535465474b740b3d9fd41cb6a198b68dd1f289ba Mon Sep 17 00:00:00 2001 From: abu <3109389044@qq.com> Date: Tue, 26 Aug 2025 21:21:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86push=E5=92=8Cchat=20SDK?= =?UTF-8?q?=E5=9C=A8=E5=AE=89=E5=8D=93=E7=AB=AF=E5=BC=82=E5=B8=B8=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/IM/controller/chat_controller.dart | 76 ++++++++++--------- .../controller/im_user_info_controller.dart | 5 +- lib/IM/global_badge.dart | 6 +- lib/IM/im_service.dart | 23 ++++-- lib/pages/chat/index.dart | 20 ++--- 5 files changed, 67 insertions(+), 63 deletions(-) diff --git a/lib/IM/controller/chat_controller.dart b/lib/IM/controller/chat_controller.dart index 709e147..5e4e786 100644 --- a/lib/IM/controller/chat_controller.dart +++ b/lib/IM/controller/chat_controller.dart @@ -20,7 +20,7 @@ class ChatController extends GetxController { } // 获取所有会话列表 - void getConversationList() async { + Future getConversationList() async { logger.e('开始获取会话列表数据'); if (isFinished.value) { // 拉取完数据了,直接结束 @@ -37,12 +37,13 @@ class ChatController extends GetxController { final List convList = res.data; for (var conv in convList) { - logger.w('基本会话: ${conv.conversation.toJson()}, 会话ID: ${conv.conversation.conversationID}'); + logger.w('基本会话: ${conv.conversation.toLogString()}'); } chatList.addAll(convList); // 不包含noFriend才执行加载数据逻辑,分页加载时候过滤 final hasNoFriend = chatList.any((item) => item.conversation.conversationGroupList?.contains(myConversationType.ConversationType.noFriend.name) ?? false); + logger.e('开始构建陌生人入口是否包含noFriend?:$hasNoFriend'); if (!hasNoFriend) { getNoFriendData(); } @@ -55,6 +56,7 @@ class ChatController extends GetxController { void getNoFriendData({V2TimConversation? csion}) async { // 检测会话列表是否已有陌生人消息菜单 final hasNoFriend = chatList.any((item) => item.conversation.conversationGroupList?.contains(myConversationType.ConversationType.noFriend.name) ?? false); + logger.w('检测是否存在nofriend入口:$hasNoFriend'); if (hasNoFriend) { // 已经有了入口 final ConversationViewModel matchItem = chatList.firstWhere( @@ -70,42 +72,42 @@ class ChatController extends GetxController { matchItem.conversation.lastMessage = csion!.lastMessage; matchItem.conversation.unreadCount = unreadTotal.data; chatList.refresh(); - return; - } - // 没有则执行创建逻辑 - final res = await ImService.instance.getConversationListByFilter( - filter: V2TimConversationFilter(conversationGroup: myConversationType.ConversationType.noFriend.name), - nextSeq: 0, - count: 1, - ); - if (res.success && res.data != null) { - final convList = res.data!.conversationList ?? []; - if (convList.isNotEmpty) { - // logger.i(res.data!.toJson()); - // 有陌生人消息,1.获取未读数,2.组装converstaionviewmodel - final unread = await ImService.instance.getUnreadMessageCountByFilter( - filter: V2TimConversationFilter( - conversationGroup: myConversationType.ConversationType.noFriend.name, - hasUnreadCount: true, - ), - ); - if (unread.success) { - final conv = convList.first; - final faceUrl = 'assets/images/notify/msr.png'; - conv.showName = '陌生人消息'; - conv.unreadCount = unread.data; - final createItem = ConversationViewModel( - conversation: conv, - faceUrl: faceUrl, + } else { + // 没有则执行创建逻辑 + final res = await ImService.instance.getConversationListByFilter( + filter: V2TimConversationFilter(conversationGroup: myConversationType.ConversationType.noFriend.name), + nextSeq: 0, + count: 1, + ); + if (res.success && res.data != null) { + final convList = res.data!.conversationList ?? []; + if (convList.isNotEmpty) { + // logger.i(res.data!.toJson()); + // 有陌生人消息,1.获取未读数,2.组装converstaionviewmodel + final unread = await ImService.instance.getUnreadMessageCountByFilter( + filter: V2TimConversationFilter( + conversationGroup: myConversationType.ConversationType.noFriend.name, + hasUnreadCount: true, + ), ); - final newList = List.from(chatList); - newList.add(createItem); - newList.sort((a, b) { - final atime = a.conversation.lastMessage?.timestamp ?? 0; - final btime = b.conversation.lastMessage?.timestamp ?? 0; - return btime.compareTo(atime); // 降序 - }); - chatList.value = newList; + if (unread.success) { + final conv = convList.first; + final faceUrl = 'assets/images/notify/msr.png'; + conv.showName = '陌生人消息'; + conv.unreadCount = unread.data; + final createItem = ConversationViewModel( + conversation: conv, + faceUrl: faceUrl, + ); + final newList = List.from(chatList); + newList.add(createItem); + newList.sort((a, b) { + final atime = a.conversation.lastMessage?.timestamp ?? 0; + final btime = b.conversation.lastMessage?.timestamp ?? 0; + return btime.compareTo(atime); // 降序 + }); + chatList.value = newList; + } } } } diff --git a/lib/IM/controller/im_user_info_controller.dart b/lib/IM/controller/im_user_info_controller.dart index f775352..d56ba08 100644 --- a/lib/IM/controller/im_user_info_controller.dart +++ b/lib/IM/controller/im_user_info_controller.dart @@ -8,8 +8,7 @@ class ImUserInfoController extends GetxController { @override void onInit() { super.onInit(); - refreshUserInfo(); - logger.i('IM用户信息初始化'); + logger.i('开始IM用户信息初始化'); } V2TimUserFullInfo? rawUserInfo; @@ -52,7 +51,7 @@ class ImUserInfoController extends GetxController { birthday.value = userInfo.birthday ?? 0; } - void refreshUserInfo() async { + Future refreshUserInfo() async { try { final updatedUserInfo = await ImService.instance.selfInfo(); if (updatedUserInfo.success) { diff --git a/lib/IM/global_badge.dart b/lib/IM/global_badge.dart index d8a6e52..c8b0852 100644 --- a/lib/IM/global_badge.dart +++ b/lib/IM/global_badge.dart @@ -38,7 +38,7 @@ class GlobalBadge extends GetxController { onConversationChanged: (List conversationList) async { logger.w('会话变更:会话分组:${conversationList.first.conversationGroupList},会话内容${conversationList.first.toLogString()}'); final ctl = Get.find(); - logger.w('当前会话列表内容:${ctl.chatList.toJson()}'); + logger.w('当前会话列表内容:${ctl.chatList.length}'); final updatedIds = conversationList.map((e) => e.conversationID).toSet(); logger.w('要变更的会话id:$updatedIds'); @@ -88,8 +88,8 @@ class GlobalBadge extends GetxController { ctl.chatList.refresh(); }, ); - final ctl = Get.find(); - ctl.getConversationList(); + // final ctl = Get.find(); + // ctl.getConversationList(); _initUnreadCount(); _addListener(); } diff --git a/lib/IM/im_service.dart b/lib/IM/im_service.dart index 7f931e1..5175293 100644 --- a/lib/IM/im_service.dart +++ b/lib/IM/im_service.dart @@ -63,19 +63,20 @@ class ImService { if (result.success) { logger.i("IM 登录成功:$userID"); - // 初始化push服务 - PushService().initPush( - sdkAppId: 1600080789, - appKey: 'vkFpe55aYqfV7Sk5uGaoxhEstJ3tcI9dquk7JwG1GloDSLD2HeMWeQweWWXgNlhC', - ); - // 初始化微信 SDK + + // 初始化会话数据 + final ctl = Get.find(); + await ctl.getConversationList(); + + /// 初始化微信 SDK await Wxsdk.init(); // 注册用户信息(基本信息+自定义信息) if (!Get.isRegistered()) { - Get.put(ImUserInfoController(), permanent: true); + final imInfo = Get.put(ImUserInfoController(), permanent: true); + await imInfo.refreshUserInfo(); } else { - Get.find().refreshUserInfo(); + await Get.find().refreshUserInfo(); } // 登录成功后注册高级消息监听器 @@ -91,6 +92,12 @@ class ImService { /// 注册消息未读数监听器 Get.put(GlobalBadge(), permanent: true); + + // 初始化push服务 + PushService().initPush( + sdkAppId: 1600080789, + appKey: 'vkFpe55aYqfV7Sk5uGaoxhEstJ3tcI9dquk7JwG1GloDSLD2HeMWeQweWWXgNlhC', + ); } else { logger.i("IM 登录失败:${result.code} - ${result.desc}"); Get.snackbar( diff --git a/lib/pages/chat/index.dart b/lib/pages/chat/index.dart index d5da694..0c52f64 100644 --- a/lib/pages/chat/index.dart +++ b/lib/pages/chat/index.dart @@ -325,6 +325,10 @@ class ChatPageState extends State { physics: BouncingScrollPhysics(), itemCount: chatList.length, itemBuilder: (context, index) { + final isNoFriend = chatList[index].conversation.conversationGroupList?.contains(ConversationType.noFriend.name) ?? false; + final isAdmin = chatList[index].isCustomAdmin != null && chatList[index].isCustomAdmin != '0'; + + logger.e(chatList[index].isCustomAdmin); return Ink( // color: chatList[index]['topMost'] == null ? Colors.white : Colors.grey[100], //置顶颜色 child: InkWell( @@ -351,14 +355,8 @@ class ChatPageState extends State { Text( chatList[index].conversation.showName ?? '未知', style: TextStyle( - fontSize: (chatList[index].conversation.conversationGroupList!.contains(ConversationType.noFriend.name)) || - chatList[index].isCustomAdmin != '0' - ? 20 - : 16, - fontWeight: (chatList[index].conversation.conversationGroupList!.contains(ConversationType.noFriend.name)) || - chatList[index].isCustomAdmin != '0' - ? FontWeight.bold - : FontWeight.normal), + fontSize: (isAdmin || isNoFriend) ? 20 : 16, + fontWeight: (isAdmin || isNoFriend) ? FontWeight.bold : FontWeight.normal), ), const SizedBox(height: 2.0), Text( @@ -377,8 +375,7 @@ class ChatPageState extends State { crossAxisAlignment: CrossAxisAlignment.end, children: [ Visibility( - visible: !(chatList[index].isCustomAdmin != '0' || - chatList[index].conversation.conversationGroupList!.contains(ConversationType.noFriend.name)), + visible: !(isAdmin || isNoFriend), child: Text( // 转成日期字符串显示 DateTime.fromMillisecondsSinceEpoch( @@ -396,8 +393,7 @@ class ChatPageState extends State { ], ), Visibility( - visible: chatList[index].isCustomAdmin != '0' || - chatList[index].conversation.conversationGroupList!.contains(ConversationType.noFriend.name), + visible: (isAdmin || isNoFriend), child: const Icon( Icons.arrow_forward_ios, color: Colors.blueGrey,