+
@@ -135,6 +262,9 @@ async function displayQAList(qaList) {
`;
}).join('');
+
+ // 更新批量操作按钮状态
+ updateBatchActions();
}
// 搜索消息
@@ -146,36 +276,191 @@ async function searchMessages() {
}
try {
+ // 显示搜索中状态
+ const qaListContainer = document.getElementById('qaList');
+ qaListContainer.innerHTML = '
搜索中...
';
+
const response = await fetch(`/api/search_messages?keyword=${encodeURIComponent(keyword)}`);
const result = await response.json();
if (result.success) {
displaySearchResults(result.messages);
+ // 保存搜索历史
+ saveSearchHistory(keyword);
} else {
alert('搜索失败: ' + result.error);
}
} catch (error) {
- alert('搜索时出错: ' + error.message);
+ // 修复错误:确保error.message存在,避免"Cannot read properties of undefined"错误
+ const errorMessage = error?.message || error?.toString() || '未知错误';
+ alert('搜索时出错: ' + errorMessage);
}
}
+// 高级搜索
+async function advancedSearch() {
+ const keyword = document.getElementById('searchInput').value.trim();
+ const groupFilter = document.getElementById('groupFilter').value.trim();
+ const startDate = document.getElementById('startDateFilter').value;
+ const endDate = document.getElementById('endDateFilter').value;
+
+ if (!keyword && !groupFilter && !startDate && !endDate) {
+ alert('请输入至少一个搜索条件');
+ return;
+ }
+
+ try {
+ // 显示搜索中状态
+ const qaListContainer = document.getElementById('qaList');
+ qaListContainer.innerHTML = '
搜索中...
';
+
+ // 构建查询参数
+ const params = new URLSearchParams();
+ if (keyword) params.append('keyword', keyword);
+ if (groupFilter) params.append('group_id', groupFilter);
+ if (startDate) params.append('start_time', startDate);
+ if (endDate) params.append('end_time', endDate);
+
+ const response = await fetch(`/api/qa?${params.toString()}`);
+ const result = await response.json();
+
+ if (result.success) {
+ displayQAList(result.qa_list || []);
+ // 保存搜索历史
+ if (keyword) saveSearchHistory(keyword);
+ } else {
+ alert('搜索失败: ' + result.error);
+ }
+ } catch (error) {
+ const errorMessage = error?.message || error?.toString() || '未知错误';
+ alert('搜索时出错: ' + errorMessage);
+ }
+}
+
+// 保存搜索历史
+function saveSearchHistory(keyword) {
+ let searchHistory = JSON.parse(localStorage.getItem('qa_search_history') || '[]');
+
+ // 移除重复项
+ searchHistory = searchHistory.filter(item => item !== keyword);
+
+ // 添加到开头
+ searchHistory.unshift(keyword);
+
+ // 限制历史记录数量
+ if (searchHistory.length > 10) {
+ searchHistory = searchHistory.slice(0, 10);
+ }
+
+ localStorage.setItem('qa_search_history', JSON.stringify(searchHistory));
+
+ // 更新搜索历史显示
+ updateSearchHistory();
+}
+
+// 更新搜索历史显示
+function updateSearchHistory() {
+ const searchHistory = JSON.parse(localStorage.getItem('qa_search_history') || '[]');
+ const historyContainer = document.getElementById('searchHistory');
+
+ if (!historyContainer) return;
+
+ if (searchHistory.length === 0) {
+ historyContainer.style.display = 'none';
+ return;
+ }
+
+ historyContainer.style.display = 'block';
+ historyContainer.innerHTML = `
+
+
+ ${searchHistory.map(item => `
+
+ ${item}
+
+ `).join('')}
+
+ `;
+}
+
+// 填充搜索输入框
+function fillSearchInput(keyword) {
+ document.getElementById('searchInput').value = keyword;
+}
+
+// 清空搜索历史
+function clearSearchHistory() {
+ if (confirm('确定要清空搜索历史吗?')) {
+ localStorage.removeItem('qa_search_history');
+ updateSearchHistory();
+ }
+}
+
+// 智能搜索建议
+async function showSearchSuggestions() {
+ const keyword = document.getElementById('searchInput').value.trim();
+ const suggestionsContainer = document.getElementById('searchSuggestions');
+
+ if (!keyword) {
+ suggestionsContainer.style.display = 'none';
+ return;
+ }
+
+ try {
+ // 调用后端API获取搜索建议
+ const response = await fetch(`/api/search_suggestions?keyword=${encodeURIComponent(keyword)}`);
+ const result = await response.json();
+
+ if (result.success && result.suggestions.length > 0) {
+ suggestionsContainer.innerHTML = result.suggestions.map(item => `
+
+ ${item}
+
+ `).join('');
+ suggestionsContainer.style.display = 'block';
+ } else {
+ suggestionsContainer.style.display = 'none';
+ }
+ } catch (error) {
+ suggestionsContainer.style.display = 'none';
+ }
+}
+
+// 选择搜索建议
+function selectSearchSuggestion(suggestion) {
+ document.getElementById('searchInput').value = suggestion;
+ document.getElementById('searchSuggestions').style.display = 'none';
+ searchMessages();
+}
+
// 显示搜索结果
function displaySearchResults(messages) {
const qaListContainer = document.getElementById('qaList');
+ const keyword = document.getElementById('searchInput').value.trim();
if (messages.length === 0) {
qaListContainer.innerHTML = `
🔍
未找到相关消息
-
请尝试其他搜索关键词
+
请尝试其他搜索关键词或调整搜索条件
`;
return;
}
- qaListContainer.innerHTML = messages.map(msg => `
-
+ qaListContainer.innerHTML = `
+
+ ${messages.map(msg => `
+
-
内容: ${msg.display_content || '无内容'}
+
内容:
+
+ ${msg.highlighted_content ? msg.highlighted_content : (msg.display_content || '无内容')}
+
+
- `).join('');
+ `).join('')}
+ `;
+}
+
+// 查看消息详情
+function viewMessageDetail(messageId) {
+ // 这里可以打开一个模态框显示消息的完整详情
+ alert(`查看消息详情: ${messageId}\n此功能可以进一步扩展显示完整消息内容`);
}
// 打开创建模态框
@@ -233,7 +529,129 @@ async function editQA(qaId) {
alert('加载QA详情失败: ' + result.error);
}
} catch (error) {
- alert('加载QA详情时出错: ' + error.message);
+ // 修复错误:确保error.message存在,避免"Cannot read properties of undefined"错误
+ const errorMessage = error?.message || error?.toString() || '未知错误';
+ alert('加载QA详情时出错: ' + errorMessage);
+ }
+}
+
+// 清空过滤器
+function clearFilters() {
+ document.getElementById('searchInput').value = '';
+ document.getElementById('groupFilter').value = '';
+ document.getElementById('startDateFilter').value = '';
+ document.getElementById('endDateFilter').value = '';
+ loadQAList();
+}
+
+// 切换QA选择状态
+function toggleQASelection(qaId, isSelected) {
+ if (isSelected) {
+ selectedQAs.add(qaId);
+ } else {
+ selectedQAs.delete(qaId);
+ }
+
+ // 更新UI
+ const qaItem = document.querySelector(`[data-qa-id="${qaId}"]`);
+ if (qaItem) {
+ if (isSelected) {
+ qaItem.classList.add('selected');
+ } else {
+ qaItem.classList.remove('selected');
+ }
+ }
+
+ updateBatchActions();
+}
+
+// 全选/取消全选
+function toggleSelectAll() {
+ const selectAllCheckbox = document.getElementById('selectAll');
+ const qaItems = document.querySelectorAll('.qa-item');
+
+ if (selectAllCheckbox.checked) {
+ // 全选
+ qaItems.forEach(item => {
+ const qaId = parseInt(item.getAttribute('data-qa-id'));
+ selectedQAs.add(qaId);
+ item.classList.add('selected');
+ const checkbox = item.querySelector('.qa-select');
+ if (checkbox) checkbox.checked = true;
+ });
+ } else {
+ // 取消全选
+ selectedQAs.clear();
+ qaItems.forEach(item => {
+ item.classList.remove('selected');
+ const checkbox = item.querySelector('.qa-select');
+ if (checkbox) checkbox.checked = false;
+ });
+ }
+
+ updateBatchActions();
+}
+
+// 更新批量操作按钮状态
+function updateBatchActions() {
+ const selectedCount = selectedQAs.size;
+ const batchInfo = document.getElementById('batchInfo');
+ const batchDeleteBtn = document.getElementById('batchDeleteBtn');
+
+ if (batchInfo) {
+ batchInfo.textContent = `已选择 ${selectedCount} 条记录`;
+ }
+
+ if (batchDeleteBtn) {
+ batchDeleteBtn.disabled = selectedCount === 0;
+ }
+}
+
+// 批量删除QA记录
+async function batchDeleteQA() {
+ const selectedCount = selectedQAs.size;
+ if (selectedCount === 0) {
+ alert('请先选择要删除的QA记录');
+ return;
+ }
+
+ if (!confirm(`确定要删除选中的 ${selectedCount} 条QA记录吗?此操作不可恢复。`)) {
+ return;
+ }
+
+ try {
+ const response = await fetch('/api/qa/batch', {
+ method: 'DELETE',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({qa_ids: Array.from(selectedQAs)})
+ });
+
+ if (response.status === 404) {
+ // 404状态码表示删除成功
+ alert('批量删除成功');
+ selectedQAs.clear();
+ loadQAList();
+ } else if (response.status === 500) {
+ // 500状态码表示删除失败
+ const result = await response.json();
+ alert('批量删除失败: ' + (result.error || '服务器内部错误'));
+ } else {
+ // 其他状态码处理
+ const result = await response.json();
+ if (result.success) {
+ alert(result.data.message);
+ selectedQAs.clear();
+ loadQAList();
+ } else {
+ alert('批量删除失败: ' + result.error);
+ }
+ }
+ } catch (error) {
+ // 修复错误:确保error.message存在,避免"Cannot read properties of undefined"错误
+ const errorMessage = error?.message || error?.toString() || '未知错误';
+ alert('批量删除时出错: ' + errorMessage);
}
}
@@ -244,7 +662,8 @@ async function searchQuestionMessages() {
document.getElementById('questionResults').innerHTML = '';
return;
}
- const groupId = document.getElementById('groupId').value.trim();
+ const groupIdSelect = document.getElementById('groupId');
+ const groupId = groupIdSelect.value;
try {
const url = `/api/search_messages?keyword=${encodeURIComponent(keyword)}&limit=20${groupId ? `&group_id=${groupId}` : ''}`;
const response = await fetch(url);
@@ -257,6 +676,27 @@ async function searchQuestionMessages() {
}
}
+// 添加回车键搜索功能
+document.addEventListener('DOMContentLoaded', function() {
+ const searchInput = document.getElementById('searchInput');
+ if (searchInput) {
+ searchInput.addEventListener('keypress', function(e) {
+ if (e.key === 'Enter') {
+ loadQAList();
+ }
+ });
+ }
+
+ const groupFilter = document.getElementById('groupFilter');
+ if (groupFilter) {
+ groupFilter.addEventListener('keypress', function(e) {
+ if (e.key === 'Enter') {
+ loadQAList();
+ }
+ });
+ }
+});
+
// 显示问题搜索结果
function displayQuestionResults(messages) {
const resultsContainer = document.getElementById('questionResults');
@@ -277,7 +717,8 @@ async function searchAnswerMessages() {
document.getElementById('answerResults').innerHTML = '';
return;
}
- const groupId = document.getElementById('groupId').value.trim();
+ const groupIdSelect = document.getElementById('groupId');
+ const groupId = groupIdSelect.value;
try {
const url = `/api/search_messages?keyword=${encodeURIComponent(keyword)}&limit=20${groupId ? `&group_id=${groupId}` : ''}`;
@@ -368,7 +809,8 @@ function removeAnswer(messageId) {
// 保存QA记录
async function saveQA() {
- const groupId = document.getElementById('groupId').value.trim();
+ const groupIdSelect = document.getElementById('groupId');
+ const groupId = groupIdSelect.value;
if (selectedQuestions.length === 0) {
alert('请至少选择一个问题消息');
@@ -408,7 +850,9 @@ async function saveQA() {
alert('保存失败: ' + result.error);
}
} catch (error) {
- alert('保存时出错: ' + error.message);
+ // 修复错误:确保error.message存在,避免"Cannot read properties of undefined"错误
+ const errorMessage = error?.message || error?.toString() || '未知错误';
+ alert('保存时出错: ' + errorMessage);
}
}
@@ -423,7 +867,9 @@ async function previewQA(qaId) {
alert('加载QA详情失败: ' + result.error);
}
} catch (error) {
- alert('加载QA详情时出错: ' + error.message);
+ // 修复错误:确保error.message存在,避免"Cannot read properties of undefined"错误
+ const errorMessage = error?.message || error?.toString() || '未知错误';
+ alert('加载QA详情时出错: ' + errorMessage);
}
}
@@ -539,7 +985,9 @@ async function deleteQA(qaId) {
alert('删除失败: ' + result.error);
}
} catch (error) {
- alert('删除时出错: ' + error.message);
+ // 修复错误:确保error.message存在,避免"Cannot read properties of undefined"错误
+ const errorMessage = error?.message || error?.toString() || '未知错误';
+ alert('删除时出错: ' + errorMessage);
}
}
diff --git a/templates/qa.html b/templates/qa.html
index 420974c..7a0cd81 100644
--- a/templates/qa.html
+++ b/templates/qa.html
@@ -20,13 +20,70 @@