Initial Commit
This commit is contained in:
commit
1d71a02738
237 changed files with 64293 additions and 0 deletions
457
pages/search/global-search.js
Normal file
457
pages/search/global-search.js
Normal file
|
|
@ -0,0 +1,457 @@
|
|||
// 🔍 全局搜索页面逻辑
|
||||
const messageSearchManager = require('../../utils/message-search-manager.js');
|
||||
const app = getApp();
|
||||
|
||||
Page({
|
||||
data: {
|
||||
// 系统信息
|
||||
statusBarHeight: 44,
|
||||
|
||||
// 搜索状态
|
||||
searchKeyword: '',
|
||||
searchFocus: true,
|
||||
searchType: 'all',
|
||||
isSearching: false,
|
||||
isLoadingMore: false,
|
||||
|
||||
// 会话搜索
|
||||
conversationId: null,
|
||||
conversationName: '',
|
||||
isConversationSearch: false,
|
||||
|
||||
// 搜索结果
|
||||
searchResults: [],
|
||||
searchTotal: 0,
|
||||
currentPage: 1,
|
||||
hasMoreResults: false,
|
||||
searchStatusText: '',
|
||||
|
||||
// 搜索历史和建议
|
||||
searchHistory: [],
|
||||
hotSearches: ['图片', '文件', '链接', '表情包'],
|
||||
|
||||
// 防抖定时器
|
||||
searchTimer: null
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
console.log('🔍 全局搜索页面加载', options);
|
||||
|
||||
// 获取系统信息
|
||||
this.getSystemInfo();
|
||||
|
||||
// 初始化搜索管理器
|
||||
this.initSearchManager();
|
||||
|
||||
// 加载搜索历史
|
||||
this.loadSearchHistory();
|
||||
|
||||
// 处理会话内搜索
|
||||
if (options.conversationId) {
|
||||
this.setData({
|
||||
conversationId: options.conversationId,
|
||||
conversationName: decodeURIComponent(options.conversationName || '聊天'),
|
||||
isConversationSearch: true
|
||||
});
|
||||
|
||||
// 更新页面标题
|
||||
wx.setNavigationBarTitle({
|
||||
title: `在"${this.data.conversationName}"中搜索`
|
||||
});
|
||||
}
|
||||
|
||||
// 处理传入的搜索关键词
|
||||
if (options.keyword) {
|
||||
this.setData({
|
||||
searchKeyword: options.keyword,
|
||||
searchFocus: false
|
||||
});
|
||||
this.performSearch();
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
console.log('🔍 全局搜索页面显示');
|
||||
|
||||
// 刷新搜索历史
|
||||
this.loadSearchHistory();
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
console.log('🔍 全局搜索页面卸载');
|
||||
|
||||
// 清理定时器
|
||||
if (this.data.searchTimer) {
|
||||
clearTimeout(this.data.searchTimer);
|
||||
}
|
||||
},
|
||||
|
||||
// 获取系统信息
|
||||
getSystemInfo() {
|
||||
try {
|
||||
const windowInfo = wx.getWindowInfo();
|
||||
this.setData({
|
||||
statusBarHeight: windowInfo.statusBarHeight || 44
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取系统信息失败:', error);
|
||||
this.setData({
|
||||
statusBarHeight: 44
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 初始化搜索管理器
|
||||
async initSearchManager() {
|
||||
try {
|
||||
await messageSearchManager.init();
|
||||
console.log('✅ 搜索管理器初始化完成');
|
||||
} catch (error) {
|
||||
console.error('❌ 搜索管理器初始化失败:', error);
|
||||
}
|
||||
},
|
||||
|
||||
// 加载搜索历史
|
||||
loadSearchHistory() {
|
||||
const history = messageSearchManager.getSearchHistory();
|
||||
this.setData({
|
||||
searchHistory: history.slice(0, 10) // 只显示前10条
|
||||
});
|
||||
},
|
||||
|
||||
// 🔍 ===== 搜索输入处理 =====
|
||||
|
||||
// 搜索输入
|
||||
onSearchInput(e) {
|
||||
const keyword = e.detail.value;
|
||||
this.setData({
|
||||
searchKeyword: keyword
|
||||
});
|
||||
|
||||
// 防抖搜索
|
||||
this.searchWithDebounce(keyword);
|
||||
},
|
||||
|
||||
// 搜索确认
|
||||
onSearchConfirm(e) {
|
||||
const keyword = e.detail.value.trim();
|
||||
if (keyword) {
|
||||
this.performSearch();
|
||||
}
|
||||
},
|
||||
|
||||
// 防抖搜索
|
||||
searchWithDebounce(keyword) {
|
||||
// 清除之前的定时器
|
||||
if (this.data.searchTimer) {
|
||||
clearTimeout(this.data.searchTimer);
|
||||
}
|
||||
|
||||
// 如果关键词为空,清除结果
|
||||
if (!keyword.trim()) {
|
||||
this.setData({
|
||||
searchResults: [],
|
||||
searchTotal: 0,
|
||||
hasMoreResults: false
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置新的定时器
|
||||
const timer = setTimeout(() => {
|
||||
this.performSearch();
|
||||
}, 500);
|
||||
|
||||
this.setData({
|
||||
searchTimer: timer
|
||||
});
|
||||
},
|
||||
|
||||
// 清除搜索
|
||||
clearSearch() {
|
||||
this.setData({
|
||||
searchKeyword: '',
|
||||
searchResults: [],
|
||||
searchTotal: 0,
|
||||
hasMoreResults: false,
|
||||
searchFocus: true
|
||||
});
|
||||
},
|
||||
|
||||
// 🔍 ===== 搜索执行 =====
|
||||
|
||||
// 执行搜索
|
||||
async performSearch() {
|
||||
const keyword = this.data.searchKeyword.trim();
|
||||
if (!keyword) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🔍 执行搜索:', keyword);
|
||||
|
||||
try {
|
||||
this.setData({
|
||||
isSearching: true,
|
||||
currentPage: 1,
|
||||
searchResults: [],
|
||||
searchStatusText: '正在搜索...'
|
||||
});
|
||||
|
||||
// 调用搜索管理器
|
||||
const searchOptions = {
|
||||
type: this.data.searchType,
|
||||
page: 1
|
||||
};
|
||||
|
||||
// 如果是会话内搜索,添加会话ID
|
||||
if (this.data.isConversationSearch && this.data.conversationId) {
|
||||
searchOptions.conversationId = this.data.conversationId;
|
||||
}
|
||||
|
||||
const result = await messageSearchManager.searchMessages(keyword, searchOptions);
|
||||
|
||||
if (result.success) {
|
||||
// 处理搜索结果
|
||||
const processedResults = this.processSearchResults(result.data.messages);
|
||||
|
||||
// 更新搜索状态文本
|
||||
const statusText = this.data.isConversationSearch
|
||||
? `在"${this.data.conversationName}"中找到 ${result.data.total} 条结果`
|
||||
: `找到 ${result.data.total} 条相关结果`;
|
||||
|
||||
this.setData({
|
||||
searchResults: processedResults,
|
||||
searchTotal: result.data.total,
|
||||
hasMoreResults: result.data.hasMore,
|
||||
currentPage: 1,
|
||||
searchStatusText: statusText
|
||||
});
|
||||
|
||||
console.log(`🔍 搜索完成,找到 ${result.data.total} 条结果`);
|
||||
|
||||
} else {
|
||||
console.error('❌ 搜索失败:', result.error);
|
||||
wx.showToast({
|
||||
title: result.error || '搜索失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 搜索异常:', error);
|
||||
wx.showToast({
|
||||
title: '搜索出错',
|
||||
icon: 'none'
|
||||
});
|
||||
|
||||
} finally {
|
||||
this.setData({
|
||||
isSearching: false
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 加载更多结果
|
||||
async loadMoreResults() {
|
||||
if (!this.data.hasMoreResults || this.data.isLoadingMore) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🔍 加载更多搜索结果');
|
||||
|
||||
try {
|
||||
this.setData({
|
||||
isLoadingMore: true
|
||||
});
|
||||
|
||||
const nextPage = this.data.currentPage + 1;
|
||||
const searchOptions = {
|
||||
type: this.data.searchType,
|
||||
page: nextPage
|
||||
};
|
||||
|
||||
// 如果是会话内搜索,添加会话ID
|
||||
if (this.data.isConversationSearch && this.data.conversationId) {
|
||||
searchOptions.conversationId = this.data.conversationId;
|
||||
}
|
||||
|
||||
const result = await messageSearchManager.searchMessages(this.data.searchKeyword, searchOptions);
|
||||
|
||||
if (result.success) {
|
||||
const processedResults = this.processSearchResults(result.data.messages);
|
||||
const allResults = [...this.data.searchResults, ...processedResults];
|
||||
|
||||
this.setData({
|
||||
searchResults: allResults,
|
||||
hasMoreResults: result.data.hasMore,
|
||||
currentPage: nextPage
|
||||
});
|
||||
|
||||
console.log(`🔍 加载更多完成,当前共 ${allResults.length} 条结果`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 加载更多失败:', error);
|
||||
wx.showToast({
|
||||
title: '加载失败',
|
||||
icon: 'none'
|
||||
});
|
||||
|
||||
} finally {
|
||||
this.setData({
|
||||
isLoadingMore: false
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 处理搜索结果
|
||||
processSearchResults(messages) {
|
||||
return messages.map(message => {
|
||||
// 格式化时间
|
||||
const formattedTime = this.formatMessageTime(message.timestamp);
|
||||
|
||||
// 获取会话名称
|
||||
const conversationName = this.getConversationName(message);
|
||||
|
||||
return {
|
||||
...message,
|
||||
formattedTime: formattedTime,
|
||||
conversationName: conversationName
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
// 格式化消息时间
|
||||
formatMessageTime(timestamp) {
|
||||
const now = new Date();
|
||||
const messageTime = new Date(timestamp);
|
||||
const diffMs = now.getTime() - messageTime.getTime();
|
||||
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (diffDays === 0) {
|
||||
// 今天
|
||||
return messageTime.toLocaleTimeString('zh-CN', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
} else if (diffDays === 1) {
|
||||
// 昨天
|
||||
return '昨天 ' + messageTime.toLocaleTimeString('zh-CN', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
} else if (diffDays < 7) {
|
||||
// 一周内
|
||||
const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
|
||||
return `周${weekdays[messageTime.getDay()]}`;
|
||||
} else {
|
||||
// 更早
|
||||
return messageTime.toLocaleDateString('zh-CN', {
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 获取会话名称
|
||||
getConversationName(message) {
|
||||
// 这里可以根据conversationId获取会话名称
|
||||
// 暂时返回默认值
|
||||
if (message.conversationId) {
|
||||
return message.conversationName || '群聊';
|
||||
}
|
||||
return '私聊';
|
||||
},
|
||||
|
||||
// 🔍 ===== 搜索类型切换 =====
|
||||
|
||||
// 切换搜索类型
|
||||
changeSearchType(e) {
|
||||
const type = e.currentTarget.dataset.type;
|
||||
if (type === this.data.searchType) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🔍 切换搜索类型:', type);
|
||||
|
||||
this.setData({
|
||||
searchType: type
|
||||
});
|
||||
|
||||
// 如果有搜索关键词,重新搜索
|
||||
if (this.data.searchKeyword.trim()) {
|
||||
this.performSearch();
|
||||
}
|
||||
},
|
||||
|
||||
// 🔍 ===== 搜索历史管理 =====
|
||||
|
||||
// 选择历史搜索项
|
||||
selectHistoryItem(e) {
|
||||
const keyword = e.currentTarget.dataset.keyword;
|
||||
this.setData({
|
||||
searchKeyword: keyword,
|
||||
searchFocus: false
|
||||
});
|
||||
this.performSearch();
|
||||
},
|
||||
|
||||
// 删除历史搜索项
|
||||
removeHistoryItem(e) {
|
||||
const keyword = e.currentTarget.dataset.keyword;
|
||||
messageSearchManager.removeSearchHistoryItem(keyword);
|
||||
this.loadSearchHistory();
|
||||
},
|
||||
|
||||
// 清除搜索历史
|
||||
clearSearchHistory() {
|
||||
wx.showModal({
|
||||
title: '清除搜索历史',
|
||||
content: '确定要清除所有搜索历史吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
messageSearchManager.clearSearchHistory();
|
||||
this.loadSearchHistory();
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 选择热门搜索
|
||||
selectHotSearch(e) {
|
||||
const keyword = e.currentTarget.dataset.keyword;
|
||||
this.setData({
|
||||
searchKeyword: keyword,
|
||||
searchFocus: false
|
||||
});
|
||||
this.performSearch();
|
||||
},
|
||||
|
||||
// 🔍 ===== 搜索结果操作 =====
|
||||
|
||||
// 打开消息
|
||||
openMessage(e) {
|
||||
const message = e.currentTarget.dataset.message;
|
||||
console.log('🔍 打开消息:', message.id);
|
||||
|
||||
// 跳转到聊天页面并定位到该消息
|
||||
this.jumpToMessage(e);
|
||||
},
|
||||
|
||||
// 跳转到消息
|
||||
jumpToMessage(e) {
|
||||
const message = e.currentTarget.dataset.message;
|
||||
|
||||
// 跳转到聊天页面
|
||||
wx.navigateTo({
|
||||
url: `/pages/chat/chat?conversationId=${message.conversationId}&messageId=${message.id}`
|
||||
});
|
||||
},
|
||||
|
||||
// 🔍 ===== 页面导航 =====
|
||||
|
||||
// 返回上一页
|
||||
goBack() {
|
||||
wx.navigateBack();
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue