fix: 空关键词搜索时不调用 API,避免错误 6017

- searchFriendByKey 添加空关键词检查
- searchGroupByKey 添加空关键词检查
- searchMsgByKey 添加空关键词检查
- 当关键词为空时直接清理数据,不请求 SDK
This commit is contained in:
Max 2026-01-07 20:45:51 +08:00
parent 1271c27b41
commit 61aee808d0
1 changed files with 39 additions and 11 deletions

View File

@ -24,12 +24,17 @@ import 'package:tencent_cloud_chat_uikit/data_services/message/message_services.
import 'package:tencent_cloud_chat_uikit/data_services/conversation/conversation_services.dart';
import 'package:tencent_cloud_chat_uikit/data_services/group/group_services.dart';
enum KeywordListMatchType { V2TIM_KEYWORD_LIST_MATCH_TYPE_OR, V2TIM_KEYWORD_LIST_MATCH_TYPE_AND }
enum KeywordListMatchType {
V2TIM_KEYWORD_LIST_MATCH_TYPE_OR,
V2TIM_KEYWORD_LIST_MATCH_TYPE_AND
}
class TUISearchViewModel extends ChangeNotifier {
final FriendshipServices _friendshipServices = serviceLocator<FriendshipServices>();
final FriendshipServices _friendshipServices =
serviceLocator<FriendshipServices>();
final MessageService _messageService = serviceLocator<MessageService>();
final ConversationService _conversationService = serviceLocator<ConversationService>();
final ConversationService _conversationService =
serviceLocator<ConversationService>();
final GroupServices _groupServices = serviceLocator<GroupServices>();
List<V2TimFriendInfoResult>? friendList = [];
@ -46,7 +51,8 @@ class TUISearchViewModel extends ChangeNotifier {
List<V2TimConversation?> conversationList = [];
Future<List<V2TimConversation?>?> initConversationMsg() async {
final conversationResult = await _conversationService.getConversationList(nextSeq: "0", count: 500);
final conversationResult = await _conversationService.getConversationList(
nextSeq: "0", count: 500);
final conversationListData = conversationResult?.conversationList;
conversationList = conversationListData ?? [];
notifyListeners();
@ -62,20 +68,33 @@ class TUISearchViewModel extends ChangeNotifier {
}
void searchFriendByKey(String searchKey) async {
final searchResult =
await _friendshipServices.searchFriends(searchParam: V2TimFriendSearchParam(keywordList: [searchKey]));
// API
if (searchKey.trim().isEmpty) {
friendList = [];
notifyListeners();
return;
}
final searchResult = await _friendshipServices.searchFriends(
searchParam: V2TimFriendSearchParam(keywordList: [searchKey]));
friendList = searchResult;
notifyListeners();
}
void searchGroupByKey(String searchKey) async {
final searchResult =
await _groupServices.searchGroups(searchParam: V2TimGroupSearchParam(keywordList: [searchKey]));
// API
if (searchKey.trim().isEmpty) {
groupList = [];
notifyListeners();
return;
}
final searchResult = await _groupServices.searchGroups(
searchParam: V2TimGroupSearchParam(keywordList: [searchKey]));
groupList = searchResult.data ?? [];
notifyListeners();
}
void getMsgForConversation(String keyword, String conversationId, int page) async {
void getMsgForConversation(
String keyword, String conversationId, int page) async {
void clearData() {
currentMsgListForConversation = [];
totalMsgInConversationCount = 0;
@ -99,8 +118,9 @@ class TUISearchViewModel extends ChangeNotifier {
type: KeywordListMatchType.V2TIM_KEYWORD_LIST_MATCH_TYPE_OR.index,
));
if (searchResult.code == 0 && searchResult.data != null) {
final messageSearchResultItems = searchResult.data!.messageSearchResultItems!
.firstWhereOrNull((element) => element.conversationID == conversationId);
final messageSearchResultItems =
searchResult.data!.messageSearchResultItems!.firstWhereOrNull(
(element) => element.conversationID == conversationId);
totalMsgInConversationCount = messageSearchResultItems?.messageCount ?? 0;
currentMsgListForConversation = [
...currentMsgListForConversation,
@ -111,6 +131,14 @@ class TUISearchViewModel extends ChangeNotifier {
}
void searchMsgByKey(String searchKey, bool isFirst) async {
// API
if (searchKey.trim().isEmpty) {
msgPage = 0;
msgList = [];
totalMsgCount = 0;
notifyListeners();
return;
}
if (isFirst == true) {
msgPage = 0;
msgList = [];