feat: 后台管理添加查看用户对话记录功能

This commit is contained in:
2026-04-27 18:38:44 +08:00
parent 0086eaa1d6
commit 1c3f7604c9
2 changed files with 175 additions and 0 deletions

View File

@@ -641,6 +641,36 @@ def get_user_conversations(user_id):
return jsonify(conversations)
@app.route('/api/user/<int:user_id>/conversations/<int:conv_id>', methods=['GET'])
def get_user_conversation_detail(user_id, conv_id):
"""获取单个对话详情"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
SELECT id, title, agent_id, messages, created_at, updated_at
FROM conversations WHERE id = ? AND user_id = ?
''', (conv_id, user_id))
row = cursor.fetchone()
conn.close()
if not row:
return jsonify({'error': '对话不存在'}), 404
conv = dict(row)
try:
conv['messages'] = json.loads(conv['messages']) if conv['messages'] else []
except:
conv['messages'] = []
conv['id'] = str(conv['id'])
conv['createdAt'] = int(datetime.strptime(conv['created_at'], '%Y-%m-%d %H:%M:%S').timestamp() * 1000) if conv['created_at'] else 0
conv['updatedAt'] = int(datetime.strptime(conv['updated_at'], '%Y-%m-%d %H:%M:%S').timestamp() * 1000) if conv['updated_at'] else 0
conv['agentId'] = conv['agent_id']
return jsonify(conv)
@app.route('/api/user/<int:user_id>/conversations', methods=['POST'])
def create_user_conversation(user_id):
"""创建新对话"""

View File

@@ -250,6 +250,7 @@ async function loadUsersPage(content) {
<td>
<div class="action-btns">
<button class="action-btn edit" onclick="showEditUserModal(${u.id})">编辑</button>
<button class="action-btn" style="background: #8b5cf6; color: white;" onclick="showUserConversations(${u.id}, '${u.username}')">查看对话</button>
<button class="action-btn" style="background: #f59e0b; color: white;" onclick="showResetPasswordModal(${u.id})">重置密码</button>
<button class="action-btn delete" onclick="deleteUser(${u.id})">删除</button>
</div>
@@ -396,6 +397,150 @@ async function deleteUser(id) {
loadPage('users');
}
// ==================== 查看用户对话记录 ====================
async function showUserConversations(userId, username) {
// 加载用户对话列表
const conversations = await fetchAPI(`/api/user/${userId}/conversations`);
const content = document.getElementById('mainContent');
content.innerHTML = `
<div class="content-header">
<h1 class="content-title">用户对话记录 - ${username}</h1>
<button class="add-btn" style="background: #718096;" onclick="loadPage('users')">返回用户列表</button>
</div>
<div class="stats-grid" style="grid-template-columns: repeat(3, 1fr);">
<div class="stat-card">
<div class="stat-icon">💬</div>
<div class="stat-value">${conversations.length}</div>
<div class="stat-label">对话总数</div>
</div>
<div class="stat-card">
<div class="stat-icon">🤖</div>
<div class="stat-value">${conversations.filter(c => c.agentId).length}</div>
<div class="stat-label">智能体对话</div>
</div>
<div class="stat-card">
<div class="stat-icon">📝</div>
<div class="stat-value">${conversations.reduce((sum, c) => sum + (c.messages?.length || 0), 0)}</div>
<div class="stat-label">消息总数</div>
</div>
</div>
<div class="data-table">
<table>
<thead>
<tr>
<th>ID</th>
<th>标题</th>
<th>智能体</th>
<th>消息数</th>
<th>创建时间</th>
<th>更新时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
${conversations.length === 0 ? '<tr><td colspan="7" style="text-align: center; color: #999;">暂无对话记录</td></tr>' :
conversations.map(conv => `
<tr>
<td>${conv.id}</td>
<td>${conv.title || '新对话'}</td>
<td>${conv.agentId ? getAgentName(conv.agentId) : '普通对话'}</td>
<td>${conv.messages?.length || 0}</td>
<td>${formatDate(conv.createdAt || conv.created_at)}</td>
<td>${formatDate(conv.updatedAt || conv.updated_at)}</td>
<td>
<div class="action-btns">
<button class="action-btn edit" onclick="showConversationMessages(${userId}, ${conv.id}, '${conv.title || '新对话'}')">查看详情</button>
<button class="action-btn delete" onclick="deleteUserConversation(${userId}, ${conv.id})">删除</button>
</div>
</td>
</tr>
`).join('')
}
</tbody>
</table>
</div>
`;
}
function getAgentName(agentId) {
const agent = agents.find(a => a.agent_id === agentId);
return agent ? `${agent.avatar} ${agent.name}` : agentId;
}
async function showConversationMessages(userId, convId, title) {
// 获取对话详情
const conv = await fetchAPI(`/api/user/${userId}/conversations/${convId}`);
const content = document.getElementById('mainContent');
const messages = conv.messages || [];
content.innerHTML = `
<div class="content-header">
<h1 class="content-title">对话详情 - ${title}</h1>
<button class="add-btn" style="background: #718096;" onclick="showUserConversations(${userId}, '用户')">返回对话列表</button>
</div>
<div style="background: white; padding: 16px; border-radius: 12px; margin-bottom: 16px;">
<div style="display: flex; gap: 16px; color: #718096;">
<span>💬 消息数: ${messages.length}</span>
<span>🤖 智能体: ${conv.agentId ? getAgentName(conv.agentId) : '普通对话'}</span>
<span>📅 创建: ${formatDate(conv.createdAt || conv.created_at)}</span>
</div>
</div>
<div class="data-table">
<table>
<thead>
<tr>
<th style="width: 60px;">序号</th>
<th style="width: 80px;">角色</th>
<th>内容</th>
<th style="width: 150px;">时间</th>
</tr>
</thead>
<tbody>
${messages.length === 0 ? '<tr><td colspan="4" style="text-align: center; color: #999;">暂无消息</td></tr>' :
messages.map((msg, idx) => `
<tr>
<td>${idx + 1}</td>
<td style="color: ${msg.role === 'user' ? '#3b82f6' : '#10b981'};">
${msg.role === 'user' ? '👤 用户' : '🤖 AI'}
</td>
<td style="max-width: 500px; white-space: pre-wrap; word-break: break-all;">
${escapeHtml(msg.content?.slice(0, 500) || '')}${msg.content?.length > 500 ? '...' : ''}
${msg.thinking ? `<div style="color: #f59e0b; margin-top: 8px; font-size: 12px;">💭 思考: ${escapeHtml(msg.thinking?.slice(0, 200) || '')}...</div>` : ''}
</td>
<td>${formatDate(msg.timestamp || msg.createdAt)}</td>
</tr>
`).join('')
}
</tbody>
</table>
</div>
`;
}
async function deleteUserConversation(userId, convId) {
if (!confirm('确定删除此对话?此操作不可恢复!')) return;
await fetchAPI(`/api/user/${userId}/conversations/${convId}`, 'DELETE');
showToast('删除成功');
showUserConversations(userId, '用户');
}
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// ==================== 大模型配置页面 ====================
async function loadLLMPage(content) {