1116 lines
34 KiB
HTML
1116 lines
34 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>问答管理系统</title>
|
||
<style>
|
||
* {
|
||
margin: 0;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
body {
|
||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||
background-color: #f5f5f5;
|
||
color: #333;
|
||
line-height: 1.6;
|
||
}
|
||
|
||
.container {
|
||
max-width: 1200px;
|
||
margin: 0 auto;
|
||
padding: 20px;
|
||
}
|
||
|
||
.header {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
padding: 30px 0;
|
||
text-align: center;
|
||
border-radius: 10px;
|
||
margin-bottom: 30px;
|
||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
||
}
|
||
|
||
.header h1 {
|
||
font-size: 2.5em;
|
||
margin-bottom: 10px;
|
||
}
|
||
|
||
.header p {
|
||
font-size: 1.1em;
|
||
opacity: 0.9;
|
||
}
|
||
|
||
.controls {
|
||
background: white;
|
||
padding: 20px;
|
||
border-radius: 10px;
|
||
margin-bottom: 20px;
|
||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||
}
|
||
|
||
.search-box {
|
||
display: flex;
|
||
gap: 10px;
|
||
margin-bottom: 15px;
|
||
}
|
||
|
||
.search-box input {
|
||
flex: 1;
|
||
padding: 10px;
|
||
border: 1px solid #ddd;
|
||
border-radius: 5px;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.btn {
|
||
padding: 10px 20px;
|
||
border: none;
|
||
border-radius: 5px;
|
||
cursor: pointer;
|
||
font-size: 14px;
|
||
transition: all 0.3s ease;
|
||
}
|
||
|
||
.btn-primary {
|
||
background: #667eea;
|
||
color: white;
|
||
}
|
||
|
||
.btn-primary:hover {
|
||
background: #5a6fd8;
|
||
transform: translateY(-2px);
|
||
}
|
||
|
||
.btn-success {
|
||
background: #28a745;
|
||
color: white;
|
||
}
|
||
|
||
.btn-success:hover {
|
||
background: #218838;
|
||
}
|
||
|
||
.btn-danger {
|
||
background: #dc3545;
|
||
color: white;
|
||
}
|
||
|
||
.btn-danger:hover {
|
||
background: #c82333;
|
||
}
|
||
|
||
.qa-list {
|
||
background: white;
|
||
border-radius: 10px;
|
||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||
overflow: hidden;
|
||
}
|
||
|
||
.qa-item {
|
||
padding: 20px;
|
||
border-bottom: 1px solid #eee;
|
||
transition: background-color 0.3s ease;
|
||
}
|
||
|
||
.qa-item:hover {
|
||
background-color: #f8f9fa;
|
||
}
|
||
|
||
.qa-item:last-child {
|
||
border-bottom: none;
|
||
}
|
||
|
||
.qa-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: 15px;
|
||
}
|
||
|
||
.qa-id {
|
||
font-weight: bold;
|
||
color: #667eea;
|
||
font-size: 1.2em;
|
||
}
|
||
|
||
.qa-group {
|
||
background: #e9ecef;
|
||
padding: 5px 10px;
|
||
border-radius: 15px;
|
||
font-size: 0.9em;
|
||
}
|
||
|
||
.qa-content {
|
||
margin-bottom: 15px;
|
||
}
|
||
|
||
.qa-section {
|
||
margin-bottom: 10px;
|
||
}
|
||
|
||
.qa-section h4 {
|
||
color: #495057;
|
||
margin-bottom: 5px;
|
||
font-size: 1em;
|
||
}
|
||
|
||
.message-list {
|
||
background: #f8f9fa;
|
||
padding: 10px;
|
||
border-radius: 5px;
|
||
max-height: 150px;
|
||
overflow-y: auto;
|
||
}
|
||
|
||
.message-item {
|
||
padding: 5px;
|
||
margin-bottom: 5px;
|
||
background: white;
|
||
border-radius: 3px;
|
||
font-size: 0.9em;
|
||
border-left: 3px solid #667eea;
|
||
}
|
||
|
||
.qa-actions {
|
||
display: flex;
|
||
gap: 10px;
|
||
}
|
||
|
||
.modal {
|
||
display: none;
|
||
position: fixed;
|
||
z-index: 1000;
|
||
left: 0;
|
||
top: 0;
|
||
width: 100%;
|
||
height: 100%;
|
||
background-color: rgba(0, 0, 0, 0.5);
|
||
}
|
||
|
||
.modal-content {
|
||
background-color: white;
|
||
margin: 5% auto;
|
||
padding: 30px;
|
||
border-radius: 10px;
|
||
width: 80%;
|
||
max-width: 800px;
|
||
max-height: 80vh;
|
||
overflow-y: auto;
|
||
}
|
||
|
||
.modal-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
.close {
|
||
font-size: 28px;
|
||
font-weight: bold;
|
||
cursor: pointer;
|
||
color: #aaa;
|
||
}
|
||
|
||
.close:hover {
|
||
color: #000;
|
||
}
|
||
|
||
.form-group {
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
.form-group label {
|
||
display: block;
|
||
margin-bottom: 5px;
|
||
font-weight: bold;
|
||
}
|
||
|
||
.form-group input,
|
||
.form-group textarea {
|
||
width: 100%;
|
||
padding: 10px;
|
||
border: 1px solid #ddd;
|
||
border-radius: 5px;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.message-selector {
|
||
border: 1px solid #ddd;
|
||
border-radius: 5px;
|
||
padding: 15px;
|
||
margin-bottom: 15px;
|
||
}
|
||
|
||
.search-results {
|
||
max-height: 200px;
|
||
overflow-y: auto;
|
||
border: 1px solid #ddd;
|
||
border-radius: 5px;
|
||
margin-top: 10px;
|
||
}
|
||
|
||
.search-result-item {
|
||
padding: 10px;
|
||
border-bottom: 1px solid #eee;
|
||
cursor: pointer;
|
||
transition: background-color 0.2s;
|
||
}
|
||
|
||
.search-result-item:hover {
|
||
background-color: #f0f0f0;
|
||
}
|
||
|
||
.selected-messages {
|
||
background: #f8f9fa;
|
||
padding: 10px;
|
||
border-radius: 5px;
|
||
margin-top: 10px;
|
||
}
|
||
|
||
.selected-message {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 5px;
|
||
margin-bottom: 5px;
|
||
background: white;
|
||
border-radius: 3px;
|
||
}
|
||
|
||
.loading {
|
||
text-align: center;
|
||
padding: 20px;
|
||
color: #666;
|
||
}
|
||
|
||
.empty-state {
|
||
text-align: center;
|
||
padding: 40px;
|
||
color: #666;
|
||
}
|
||
|
||
.empty-state i {
|
||
font-size: 48px;
|
||
margin-bottom: 10px;
|
||
opacity: 0.5;
|
||
}
|
||
|
||
/* 聊天记录预览样式 */
|
||
.chat-preview {
|
||
background: white;
|
||
border-radius: 10px;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.chat-header {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
padding: 20px;
|
||
text-align: center;
|
||
}
|
||
|
||
.chat-header h3 {
|
||
margin: 0 0 10px 0;
|
||
font-size: 1.5em;
|
||
}
|
||
|
||
.chat-info {
|
||
opacity: 0.9;
|
||
font-size: 0.9em;
|
||
}
|
||
|
||
.chat-container {
|
||
padding: 20px;
|
||
max-height: 400px;
|
||
overflow-y: auto;
|
||
background: #f8f9fa;
|
||
}
|
||
|
||
.chat-message {
|
||
display: flex;
|
||
margin-bottom: 15px;
|
||
animation: fadeIn 0.3s ease;
|
||
}
|
||
|
||
.chat-message.question {
|
||
flex-direction: row;
|
||
}
|
||
|
||
.chat-message.answer {
|
||
flex-direction: row-reverse;
|
||
}
|
||
|
||
.chat-message.other {
|
||
flex-direction: row;
|
||
opacity: 0.7;
|
||
}
|
||
|
||
.message-avatar {
|
||
width: 40px;
|
||
height: 40px;
|
||
border-radius: 50%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
font-size: 18px;
|
||
margin: 0 10px;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.chat-message.question .message-avatar {
|
||
background: #e3f2fd;
|
||
color: #1976d2;
|
||
}
|
||
|
||
.chat-message.answer .message-avatar {
|
||
background: #f3e5f5;
|
||
color: #7b1fa2;
|
||
}
|
||
|
||
.chat-message.other .message-avatar {
|
||
background: #f5f5f5;
|
||
color: #757575;
|
||
}
|
||
|
||
.message-content {
|
||
flex: 1;
|
||
max-width: 70%;
|
||
}
|
||
|
||
.message-header {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-bottom: 5px;
|
||
gap: 10px;
|
||
}
|
||
|
||
.sender {
|
||
font-weight: bold;
|
||
color: #333;
|
||
}
|
||
|
||
.time {
|
||
font-size: 0.8em;
|
||
color: #666;
|
||
}
|
||
|
||
.message-type-badge {
|
||
padding: 2px 8px;
|
||
border-radius: 12px;
|
||
font-size: 0.7em;
|
||
font-weight: bold;
|
||
}
|
||
|
||
.message-type-badge.question {
|
||
background: #e3f2fd;
|
||
color: #1976d2;
|
||
}
|
||
|
||
.message-type-badge.answer {
|
||
background: #f3e5f5;
|
||
color: #7b1fa2;
|
||
}
|
||
|
||
.message-type-badge.other {
|
||
background: #f5f5f5;
|
||
color: #757575;
|
||
}
|
||
|
||
.message-text {
|
||
background: white;
|
||
padding: 12px 15px;
|
||
border-radius: 18px;
|
||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
||
line-height: 1.4;
|
||
word-wrap: break-word;
|
||
}
|
||
|
||
.chat-message.question .message-text {
|
||
background: #e3f2fd;
|
||
border-bottom-left-radius: 5px;
|
||
}
|
||
|
||
.chat-message.answer .message-text {
|
||
background: #f3e5f5;
|
||
border-bottom-right-radius: 5px;
|
||
}
|
||
|
||
.chat-message.other .message-text {
|
||
background: #f5f5f5;
|
||
border-radius: 18px;
|
||
}
|
||
|
||
.message-meta {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
margin-top: 5px;
|
||
font-size: 0.7em;
|
||
color: #999;
|
||
}
|
||
|
||
.chat-summary {
|
||
background: white;
|
||
padding: 15px 20px;
|
||
border-top: 1px solid #eee;
|
||
display: flex;
|
||
justify-content: space-around;
|
||
}
|
||
|
||
.summary-item {
|
||
text-align: center;
|
||
}
|
||
|
||
.summary-label {
|
||
display: block;
|
||
font-size: 0.8em;
|
||
color: #666;
|
||
margin-bottom: 5px;
|
||
}
|
||
|
||
.summary-value {
|
||
display: block;
|
||
font-size: 1.2em;
|
||
font-weight: bold;
|
||
color: #667eea;
|
||
}
|
||
|
||
@keyframes fadeIn {
|
||
from {
|
||
opacity: 0;
|
||
transform: translateY(10px);
|
||
}
|
||
to {
|
||
opacity: 1;
|
||
transform: translateY(0);
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<div class="header">
|
||
<h1>问答管理系统</h1>
|
||
<p>管理问题和答案的关联关系</p>
|
||
</div>
|
||
|
||
<div class="controls">
|
||
<div class="search-box">
|
||
<input type="text" id="searchInput" placeholder="搜索消息内容...">
|
||
<button class="btn btn-primary" onclick="searchMessages()">搜索消息</button>
|
||
</div>
|
||
<button class="btn btn-success" onclick="openCreateModal()">创建新QA</button>
|
||
</div>
|
||
|
||
<div class="qa-list" id="qaList">
|
||
<div class="loading">加载中...</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 创建/编辑QA模态框 -->
|
||
<div id="qaModal" class="modal">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h2 id="modalTitle">创建QA记录</h2>
|
||
<span class="close" onclick="closeModal()">×</span>
|
||
</div>
|
||
|
||
<form id="qaForm">
|
||
<div class="form-group">
|
||
<label for="groupId">群组ID:</label>
|
||
<input type="text" id="groupId" placeholder="输入群组ID(可选)">
|
||
</div>
|
||
|
||
<div class="message-selector">
|
||
<h4>选择问题消息</h4>
|
||
<div class="search-box">
|
||
<input type="text" id="questionSearch" placeholder="搜索问题消息..."
|
||
oninput="searchQuestionMessages()">
|
||
</div>
|
||
<div class="search-results" id="questionResults"></div>
|
||
<div class="selected-messages">
|
||
<h5>已选择的问题消息:</h5>
|
||
<div id="selectedQuestions"></div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="message-selector">
|
||
<h4>选择答案消息</h4>
|
||
<div class="search-box">
|
||
<input type="text" id="answerSearch" placeholder="搜索答案消息..." oninput="searchAnswerMessages()">
|
||
</div>
|
||
<div class="search-results" id="answerResults"></div>
|
||
<div class="selected-messages">
|
||
<h5>已选择的答案消息:</h5>
|
||
<div id="selectedAnswers"></div>
|
||
</div>
|
||
</div>
|
||
|
||
<div style="text-align: right; margin-top: 20px;">
|
||
<button type="button" class="btn btn-primary" onclick="saveQA()">保存</button>
|
||
<button type="button" class="btn" onclick="closeModal()" style="margin-left: 10px;">取消</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 预览模态框 -->
|
||
<div id="previewModal" class="modal">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h2>QA预览</h2>
|
||
<span class="close" onclick="closePreviewModal()">×</span>
|
||
</div>
|
||
|
||
<div id="previewContent"></div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
let currentQA = null;
|
||
let selectedQuestions = [];
|
||
let selectedAnswers = [];
|
||
|
||
// 页面加载时获取QA列表
|
||
document.addEventListener('DOMContentLoaded', function () {
|
||
loadQAList();
|
||
});
|
||
|
||
// 加载QA列表
|
||
async function loadQAList() {
|
||
try {
|
||
const response = await fetch('/api/qa');
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
displayQAList(result.qa_list);
|
||
} else {
|
||
alert('加载QA列表失败: ' + result.error);
|
||
}
|
||
} catch (error) {
|
||
alert('加载QA列表时出错: ' + error.message);
|
||
}
|
||
}
|
||
|
||
// 显示QA列表
|
||
async function displayQAList(qaList) {
|
||
const qaListContainer = document.getElementById('qaList');
|
||
|
||
if (qaList.length === 0) {
|
||
qaListContainer.innerHTML = `
|
||
<div class="empty-state">
|
||
<div>📝</div>
|
||
<h3>暂无QA记录</h3>
|
||
<p>点击"创建新QA"按钮开始创建第一个问答记录</p>
|
||
</div>
|
||
`;
|
||
return;
|
||
}
|
||
|
||
// 获取所有消息的详细内容
|
||
let allMessageIds = [];
|
||
qaList.forEach(qa => {
|
||
allMessageIds = [...allMessageIds, ...qa.question_list, ...qa.answer_list];
|
||
});
|
||
|
||
let messageMap = {};
|
||
if (allMessageIds.length > 0) {
|
||
try {
|
||
const response = await fetch('/api/messages_by_ids', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify({message_ids: allMessageIds})
|
||
});
|
||
const result = await response.json();
|
||
if (result.success) {
|
||
result.messages.forEach(msg => {
|
||
messageMap[msg.id] = msg;
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.error('获取消息详情时出错:', error);
|
||
}
|
||
}
|
||
|
||
qaListContainer.innerHTML = qaList.map(qa => {
|
||
// 获取第一个问题的内容作为显示
|
||
let questionContent = '暂无问题内容';
|
||
if (qa.question_list && qa.question_list.length > 0) {
|
||
const firstQuestionId = qa.question_list[0];
|
||
const message = messageMap[firstQuestionId];
|
||
if (message && message.content) {
|
||
questionContent = message.content;
|
||
} else {
|
||
questionContent = `问题消息ID: ${firstQuestionId}`;
|
||
}
|
||
}
|
||
|
||
// 截取问题内容,超过30个字用省略号
|
||
if (questionContent.length > 30) {
|
||
questionContent = questionContent.substring(0, 30) + '...';
|
||
}
|
||
|
||
return `
|
||
<div class="qa-item">
|
||
<div class="qa-header">
|
||
<div class="qa-id">QA #${qa.id}</div>
|
||
<div class="qa-group">${qa.groupId || '无群组'}</div>
|
||
</div>
|
||
|
||
<div class="qa-content">
|
||
<div class="qa-question">
|
||
<strong>问题:</strong> ${questionContent}
|
||
</div>
|
||
<div class="qa-stats">
|
||
<span class="stat">问题: ${qa.question_list.length}条</span>
|
||
<span class="stat">答案: ${qa.answer_list.length}条</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="qa-actions">
|
||
<button class="btn btn-primary" onclick="previewQA(${qa.id})">预览</button>
|
||
<button class="btn" onclick="editQA(${qa.id})">编辑</button>
|
||
<button class="btn btn-danger" onclick="deleteQA(${qa.id})">删除</button>
|
||
</div>
|
||
</div>
|
||
`;
|
||
}).join('');
|
||
}
|
||
|
||
// 搜索消息
|
||
async function searchMessages() {
|
||
const keyword = document.getElementById('searchInput').value.trim();
|
||
if (!keyword) {
|
||
alert('请输入搜索关键词');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch(`/api/search_messages?keyword=${encodeURIComponent(keyword)}`);
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
displaySearchResults(result.messages);
|
||
} else {
|
||
alert('搜索失败: ' + result.error);
|
||
}
|
||
} catch (error) {
|
||
alert('搜索时出错: ' + error.message);
|
||
}
|
||
}
|
||
|
||
// 显示搜索结果
|
||
function displaySearchResults(messages) {
|
||
const qaListContainer = document.getElementById('qaList');
|
||
|
||
if (messages.length === 0) {
|
||
qaListContainer.innerHTML = `
|
||
<div class="empty-state">
|
||
<div>🔍</div>
|
||
<h3>未找到相关消息</h3>
|
||
<p>请尝试其他搜索关键词</p>
|
||
</div>
|
||
`;
|
||
return;
|
||
}
|
||
|
||
qaListContainer.innerHTML = messages.map(msg => `
|
||
<div class="qa-item">
|
||
<div class="qa-header">
|
||
<div class="qa-id">消息ID: ${msg.id}</div>
|
||
<div class="qa-group">${msg.formatted_time || '未知时间'}</div>
|
||
</div>
|
||
|
||
<div class="qa-content">
|
||
<div class="message-item">
|
||
<strong>类型:</strong> ${msg.type} |
|
||
<strong>发送者:</strong> ${msg.sender || '未知'} |
|
||
<strong>接收者:</strong> ${msg.receiver || '未知'}
|
||
</div>
|
||
<div class="message-item" style="margin-top: 10px;">
|
||
<strong>内容:</strong> ${msg.content || '无内容'}
|
||
</div>
|
||
</div>
|
||
|
||
<div class="qa-actions">
|
||
<button class="btn btn-primary" onclick="addToQuestion('${msg.id}')">设为问题</button>
|
||
<button class="btn btn-success" onclick="addToAnswer('${msg.id}')">设为答案</button>
|
||
</div>
|
||
</div>
|
||
`).join('');
|
||
}
|
||
|
||
// 打开创建模态框
|
||
function openCreateModal() {
|
||
currentQA = null;
|
||
selectedQuestions = [];
|
||
selectedAnswers = [];
|
||
|
||
document.getElementById('modalTitle').textContent = '创建QA记录';
|
||
document.getElementById('groupId').value = '';
|
||
updateSelectedMessages();
|
||
|
||
document.getElementById('qaModal').style.display = 'block';
|
||
}
|
||
|
||
// 编辑QA
|
||
async function editQA(qaId) {
|
||
try {
|
||
const response = await fetch(`/api/qa/${qaId}`);
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
currentQA = result.qa;
|
||
selectedQuestions = [...currentQA.question_list];
|
||
selectedAnswers = [...currentQA.answer_list];
|
||
|
||
document.getElementById('modalTitle').textContent = '编辑QA记录';
|
||
document.getElementById('groupId').value = currentQA.groupId || '';
|
||
updateSelectedMessages();
|
||
|
||
document.getElementById('qaModal').style.display = 'block';
|
||
} else {
|
||
alert('加载QA详情失败: ' + result.error);
|
||
}
|
||
} catch (error) {
|
||
alert('加载QA详情时出错: ' + error.message);
|
||
}
|
||
}
|
||
|
||
// 搜索问题消息
|
||
async function searchQuestionMessages() {
|
||
const keyword = document.getElementById('questionSearch').value.trim();
|
||
if (!keyword) {
|
||
document.getElementById('questionResults').innerHTML = '';
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch(`/api/search_messages?keyword=${encodeURIComponent(keyword)}&limit=20`);
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
displayQuestionResults(result.messages);
|
||
}
|
||
} catch (error) {
|
||
console.error('搜索问题消息时出错:', error);
|
||
}
|
||
}
|
||
|
||
// 显示问题搜索结果
|
||
function displayQuestionResults(messages) {
|
||
const resultsContainer = document.getElementById('questionResults');
|
||
|
||
resultsContainer.innerHTML = messages.map(msg => `
|
||
<div class="search-result-item" onclick="selectQuestion('${msg.id}', '${msg.content.replace(/'/g, "\\'").substring(0, 100)}')">
|
||
<div><strong>ID:</strong> ${msg.id}</div>
|
||
<div><strong>内容:</strong> ${msg.content ? msg.content.substring(0, 100) + (msg.content.length > 100 ? '...' : '') : '无内容'}</div>
|
||
<div><strong>时间:</strong> ${msg.formatted_time || '未知'}</div>
|
||
</div>
|
||
`).join('');
|
||
}
|
||
|
||
// 搜索答案消息
|
||
async function searchAnswerMessages() {
|
||
const keyword = document.getElementById('answerSearch').value.trim();
|
||
if (!keyword) {
|
||
document.getElementById('answerResults').innerHTML = '';
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch(`/api/search_messages?keyword=${encodeURIComponent(keyword)}&limit=20`);
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
displayAnswerResults(result.messages);
|
||
}
|
||
} catch (error) {
|
||
console.error('搜索答案消息时出错:', error);
|
||
}
|
||
}
|
||
|
||
// 显示答案搜索结果
|
||
function displayAnswerResults(messages) {
|
||
const resultsContainer = document.getElementById('answerResults');
|
||
|
||
resultsContainer.innerHTML = messages.map(msg => `
|
||
<div class="search-result-item" onclick="selectAnswer('${msg.id}', '${msg.content.replace(/'/g, "\\'").substring(0, 100)}')">
|
||
<div><strong>ID:</strong> ${msg.id}</div>
|
||
<div><strong>内容:</strong> ${msg.content ? msg.content.substring(0, 100) + (msg.content.length > 100 ? '...' : '') : '无内容'}</div>
|
||
<div><strong>时间:</strong> ${msg.formatted_time || '未知'}</div>
|
||
</div>
|
||
`).join('');
|
||
}
|
||
|
||
// 选择问题消息
|
||
function selectQuestion(messageId, content) {
|
||
if (!selectedQuestions.includes(messageId)) {
|
||
selectedQuestions.push(messageId);
|
||
updateSelectedMessages();
|
||
}
|
||
}
|
||
|
||
// 选择答案消息
|
||
function selectAnswer(messageId, content) {
|
||
if (!selectedAnswers.includes(messageId)) {
|
||
selectedAnswers.push(messageId);
|
||
updateSelectedMessages();
|
||
}
|
||
}
|
||
|
||
// 从搜索结果添加为问题
|
||
function addToQuestion(messageId) {
|
||
if (!selectedQuestions.includes(messageId)) {
|
||
selectedQuestions.push(messageId);
|
||
alert('已添加到问题列表');
|
||
}
|
||
}
|
||
|
||
// 从搜索结果添加为答案
|
||
function addToAnswer(messageId) {
|
||
if (!selectedAnswers.includes(messageId)) {
|
||
selectedAnswers.push(messageId);
|
||
alert('已添加到答案列表');
|
||
}
|
||
}
|
||
|
||
// 更新已选择的消息显示
|
||
function updateSelectedMessages() {
|
||
document.getElementById('selectedQuestions').innerHTML = selectedQuestions.map(id => `
|
||
<div class="selected-message">
|
||
<span>消息ID: ${id}</span>
|
||
<button class="btn btn-danger" onclick="removeQuestion('${id}')" style="padding: 2px 8px; font-size: 12px;">移除</button>
|
||
</div>
|
||
`).join('');
|
||
|
||
document.getElementById('selectedAnswers').innerHTML = selectedAnswers.map(id => `
|
||
<div class="selected-message">
|
||
<span>消息ID: ${id}</span>
|
||
<button class="btn btn-danger" onclick="removeAnswer('${id}')" style="padding: 2px 8px; font-size: 12px;">移除</button>
|
||
</div>
|
||
`).join('');
|
||
}
|
||
|
||
// 移除问题消息
|
||
function removeQuestion(messageId) {
|
||
selectedQuestions = selectedQuestions.filter(id => id !== messageId);
|
||
updateSelectedMessages();
|
||
}
|
||
|
||
// 移除答案消息
|
||
function removeAnswer(messageId) {
|
||
selectedAnswers = selectedAnswers.filter(id => id !== messageId);
|
||
updateSelectedMessages();
|
||
}
|
||
|
||
// 保存QA记录
|
||
async function saveQA() {
|
||
const groupId = document.getElementById('groupId').value.trim();
|
||
|
||
if (selectedQuestions.length === 0) {
|
||
alert('请至少选择一个问题消息');
|
||
return;
|
||
}
|
||
|
||
if (selectedAnswers.length === 0) {
|
||
alert('请至少选择一个答案消息');
|
||
return;
|
||
}
|
||
|
||
const qaData = {
|
||
groupId: groupId,
|
||
question: selectedQuestions,
|
||
answer: selectedAnswers
|
||
};
|
||
|
||
try {
|
||
const url = currentQA ? `/api/qa/${currentQA.id}` : '/api/qa';
|
||
const method = currentQA ? 'PUT' : 'POST';
|
||
|
||
const response = await fetch(url, {
|
||
method: method,
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify(qaData)
|
||
});
|
||
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
alert(currentQA ? 'QA记录更新成功' : 'QA记录创建成功');
|
||
closeModal();
|
||
loadQAList();
|
||
} else {
|
||
alert('保存失败: ' + result.error);
|
||
}
|
||
} catch (error) {
|
||
alert('保存时出错: ' + error.message);
|
||
}
|
||
}
|
||
|
||
// 预览QA
|
||
async function previewQA(qaId) {
|
||
try {
|
||
const response = await fetch(`/api/qa/${qaId}`);
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
displayPreview(result.qa);
|
||
} else {
|
||
alert('加载QA详情失败: ' + result.error);
|
||
}
|
||
} catch (error) {
|
||
alert('加载QA详情时出错: ' + error.message);
|
||
}
|
||
}
|
||
|
||
// 显示预览
|
||
async function displayPreview(qa) {
|
||
const previewContent = document.getElementById('previewContent');
|
||
|
||
// 获取所有消息的详细内容
|
||
let allMessages = [];
|
||
const allMessageIds = [...qa.question_list, ...qa.answer_list];
|
||
|
||
if (allMessageIds.length > 0) {
|
||
try {
|
||
const response = await fetch('/api/messages_by_ids', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify({message_ids: allMessageIds})
|
||
});
|
||
const result = await response.json();
|
||
if (result.success) {
|
||
allMessages = result.messages;
|
||
}
|
||
} catch (error) {
|
||
console.error('获取消息详情时出错:', error);
|
||
}
|
||
}
|
||
|
||
// 创建消息映射
|
||
const messageMap = {};
|
||
allMessages.forEach(msg => {
|
||
messageMap[msg.id] = msg;
|
||
});
|
||
|
||
// 按时间排序所有消息
|
||
const sortedMessages = allMessages.sort((a, b) => {
|
||
return new Date(a.time) - new Date(b.time);
|
||
});
|
||
|
||
previewContent.innerHTML = `
|
||
<div class="chat-preview">
|
||
<div class="chat-header">
|
||
<h3>QA #${qa.id} - 聊天记录预览</h3>
|
||
<div class="chat-info">群组: ${qa.groupId || '无群组'} | 消息数量: ${sortedMessages.length}</div>
|
||
</div>
|
||
|
||
<div class="chat-container">
|
||
${sortedMessages.map(msg => {
|
||
|
||
const isQuestion = qa.question_list.includes(msg.id);
|
||
const isAnswer = qa.answer_list.includes(msg.id);
|
||
const messageType = isQuestion ? 'question' : (isAnswer ? 'answer' : 'other');
|
||
|
||
return `
|
||
<div class="chat-message ${messageType}">
|
||
<div class="message-avatar">
|
||
${messageType === 'question' ? '❓' : (messageType === 'answer' ? '💬' : '📝')}
|
||
</div>
|
||
<div class="message-content">
|
||
<div class="message-header">
|
||
<span class="sender">${msg.sender || '未知用户'}</span>
|
||
<span class="time">${msg.formatted_time || '未知时间'}</span>
|
||
<span class="message-type-badge ${messageType}">${messageType === 'question' ? '问题' : (messageType === 'answer' ? '答案' : '其他')}</span>
|
||
</div>
|
||
<div class="message-text">${msg.content || '无内容'}</div>
|
||
<div class="message-meta">
|
||
<span class="message-id">消息ID: ${msg.id}</span>
|
||
<span class="message-type">类型: ${msg.type || '未知'}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
`;
|
||
}).join('')}
|
||
</div>
|
||
|
||
<div class="chat-summary">
|
||
<div class="summary-item">
|
||
<span class="summary-label">问题数量:</span>
|
||
<span class="summary-value">${qa.question_list.length}</span>
|
||
</div>
|
||
<div class="summary-item">
|
||
<span class="summary-label">答案数量:</span>
|
||
<span class="summary-value">${qa.answer_list.length}</span>
|
||
</div>
|
||
<div class="summary-item">
|
||
<span class="summary-label">总消息数:</span>
|
||
<span class="summary-value">${sortedMessages.length}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
`;
|
||
|
||
document.getElementById('previewModal').style.display = 'block';
|
||
}
|
||
|
||
// 删除QA
|
||
async function deleteQA(qaId) {
|
||
if (!confirm('确定要删除这个QA记录吗?此操作不可恢复。')) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch(`/api/qa/${qaId}`, {
|
||
method: 'DELETE'
|
||
});
|
||
|
||
const result = await response.json();
|
||
|
||
if (result.success) {
|
||
alert('QA记录删除成功');
|
||
loadQAList();
|
||
} else {
|
||
alert('删除失败: ' + result.error);
|
||
}
|
||
} catch (error) {
|
||
alert('删除时出错: ' + error.message);
|
||
}
|
||
}
|
||
|
||
// 关闭模态框
|
||
function closeModal() {
|
||
document.getElementById('qaModal').style.display = 'none';
|
||
}
|
||
|
||
// 关闭预览模态框
|
||
function closePreviewModal() {
|
||
document.getElementById('previewModal').style.display = 'none';
|
||
}
|
||
|
||
// 点击模态框外部关闭
|
||
window.onclick = function (event) {
|
||
const modal = document.getElementById('qaModal');
|
||
const previewModal = document.getElementById('previewModal');
|
||
|
||
if (event.target === modal) {
|
||
modal.style.display = 'none';
|
||
}
|
||
if (event.target === previewModal) {
|
||
previewModal.style.display = 'none';
|
||
}
|
||
}
|
||
</script>
|
||
</body>
|
||
</html> |