Files
SmartChat/templates/index.html
lychang d10ec1f911 init
2025-11-01 02:40:17 +08:00

887 lines
29 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background-color: #f5f5f5;
color: #333;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
border-radius: 15px;
margin-bottom: 30px;
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}
.header h1 {
font-size: 2.5em;
margin-bottom: 10px;
}
.header p {
font-size: 1.1em;
opacity: 0.9;
margin-bottom: 15px;
}
.nav-links {
display: flex;
gap: 15px;
margin-top: 10px;
}
.nav-link {
background: rgba(255, 255, 255, 0.2);
color: white;
padding: 8px 16px;
border-radius: 20px;
text-decoration: none;
font-size: 0.9em;
transition: all 0.3s ease;
border: 1px solid rgba(255, 255, 255, 0.3);
}
.nav-link:hover {
background: rgba(255, 255, 255, 0.3);
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: 30px;
}
.stat-card {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 5px 15px rgba(0,0,0,0.08);
text-align: center;
transition: transform 0.3s ease;
}
.stat-card:hover {
transform: translateY(-5px);
}
.stat-number {
font-size: 2.5em;
font-weight: bold;
color: #667eea;
margin-bottom: 5px;
}
.stat-label {
color: #666;
font-size: 0.9em;
}
.view-switcher {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.view-btn {
padding: 10px 20px;
border: 2px solid #667eea;
background: white;
color: #667eea;
border-radius: 25px;
cursor: pointer;
transition: all 0.3s ease;
font-weight: bold;
}
.view-btn.active {
background: #667eea;
color: white;
}
.view-btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}
.chat-container {
display: grid;
grid-template-columns: 300px 1fr;
gap: 20px;
height: 70vh;
}
.sidebar {
background: white;
border-radius: 12px;
padding: 20px;
box-shadow: 0 5px 15px rgba(0,0,0,0.08);
overflow-y: auto;
}
.sidebar h3 {
margin-bottom: 15px;
color: #333;
border-bottom: 2px solid #667eea;
padding-bottom: 10px;
}
.group-list {
list-style: none;
margin-bottom: 20px;
}
.group-item {
padding: 15px;
margin-bottom: 10px;
background: #f8f9fa;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
border-left: 4px solid transparent;
}
.group-item:hover {
background: #e9ecef;
transform: translateX(5px);
}
.group-item.active {
background: #667eea;
color: white;
border-left-color: #764ba2;
}
.group-name {
font-weight: bold;
margin-bottom: 5px;
}
.group-info {
font-size: 0.8em;
color: #666;
display: flex;
justify-content: space-between;
}
.group-item.active .group-info {
color: rgba(255,255,255,0.8);
}
.chat-area {
background: white;
border-radius: 12px;
box-shadow: 0 5px 15px rgba(0,0,0,0.08);
display: flex;
flex-direction: column;
}
.chat-header {
padding: 20px;
border-bottom: 1px solid #eee;
background: #f8f9fa;
border-radius: 12px 12px 0 0;
display: flex;
justify-content: space-between;
align-items: center;
}
.current-group {
font-size: 1.2em;
font-weight: bold;
color: #333;
}
.group-stats {
font-size: 0.9em;
color: #666;
}
.chat-messages {
flex: 1;
overflow-y: auto;
overflow-x: hidden;
padding: 20px;
}
.message {
margin-bottom: 20px;
display: flex;
align-items: flex-start;
}
.message.own {
flex-direction: row-reverse;
}
.avatar {
width: 40px;
height: 40px;
border-radius: 50%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
margin: 0 10px;
}
.message-content {
max-width: 70%;
padding: 12px 16px;
border-radius: 18px;
background: #f1f3f5;
position: relative;
word-wrap: break-word;
overflow-wrap: break-word;
white-space: normal;
}
.message.own .message-content {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.message-sender {
font-weight: bold;
margin-bottom: 5px;
font-size: 0.9em;
}
.message-time {
font-size: 0.8em;
color: #999;
margin-top: 5px;
}
.message.own .message-time {
color: rgba(255,255,255,0.8);
}
.controls {
padding: 20px;
border-top: 1px solid #eee;
background: #f8f9fa;
border-radius: 0 0 12px 12px;
}
.load-more {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 24px;
border-radius: 25px;
cursor: pointer;
font-size: 1em;
transition: transform 0.3s ease;
}
.load-more:hover {
transform: scale(1.05);
}
.loading {
text-align: center;
padding: 20px;
color: #666;
}
.error {
background: #ffe6e6;
color: #d63031;
padding: 10px;
border-radius: 8px;
margin: 10px 0;
text-align: center;
}
@media (max-width: 768px) {
.chat-container {
grid-template-columns: 1fr;
}
.sidebar {
display: none;
}
.message-content {
max-width: 85%;
}
}
body, .container {
overflow-x: hidden;
}
/* 聊天记录按钮样式 */
.chatrecord-btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 8px 16px;
border-radius: 15px;
cursor: pointer;
font-size: 0.9em;
transition: all 0.3s ease;
text-align: left;
max-width: 100%;
word-wrap: break-word;
overflow-wrap: break-word;
white-space: normal;
}
.chatrecord-btn:hover {
transform: scale(1.05);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}
/* 模态框样式 */
.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
animation: fadeIn 0.3s;
}
.modal-content {
background-color: white;
margin: 5% auto;
padding: 0;
border-radius: 15px;
width: 80%;
max-width: 800px;
max-height: 80vh;
overflow: hidden;
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
animation: slideIn 0.3s;
}
.modal-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.modal-header h3 {
margin: 0;
font-size: 1.3em;
}
.close {
color: white;
font-size: 28px;
font-weight: bold;
cursor: pointer;
transition: opacity 0.3s;
}
.close:hover {
opacity: 0.7;
}
.modal-body {
padding: 0;
max-height: calc(80vh - 80px);
overflow-y: auto;
}
.chat-record-content {
padding: 20px;
}
.chat-record-item {
margin-bottom: 15px;
padding: 15px;
background: #f8f9fa;
border-radius: 10px;
border-left: 4px solid #667eea;
}
.chat-record-sender {
font-weight: bold;
margin-bottom: 5px;
color: #333;
}
.chat-record-text {
margin-bottom: 5px;
line-height: 1.4;
}
.chat-record-time {
font-size: 0.8em;
color: #666;
text-align: right;
}
.chat-record-image {
max-width: 200px;
max-height: 200px;
border-radius: 8px;
margin: 5px 0;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideIn {
from { transform: translateY(-50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
.container {
max-width: 100%;
overflow-x: hidden;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>企业微信聊天记录查看器</h1>
<p>基于企业微信会话数据库的实时聊天记录展示</p>
<div class="nav-links">
<a href="/" class="nav-link">聊天记录</a>
<a href="/qa" class="nav-link">问答列表</a>
</div>
</div>
<div class="stats-grid" id="statsGrid">
<div class="stat-card">
<div class="stat-number" id="userCount">0</div>
<div class="stat-label">用户数量</div>
</div>
<div class="stat-card">
<div class="stat-number" id="groupCount">0</div>
<div class="stat-label">群组数量</div>
</div>
<div class="stat-card">
<div class="stat-number" id="messageCount">0</div>
<div class="stat-label">消息总数</div>
</div>
<div class="stat-card">
<div class="stat-number" id="fileCount">0</div>
<div class="stat-label">文件数量</div>
</div>
</div>
<div class="view-switcher">
<button class="view-btn active" onclick="switchView('groups')" id="viewGroups">按群组查看</button>
</div>
<div class="chat-container">
<div class="sidebar">
<div id="groupsView">
<h3>群组列表</h3>
<ul class="group-list" id="groupList">
<li>加载中...</li>
</ul>
</div>
</div>
<div class="chat-area">
<div class="chat-header">
<div class="current-group" id="currentGroup">全部聊天记录</div>
<div class="group-stats" id="groupStats">
<span id="messageCountDisplay">消息数量: 加载中...</span>
<span id="latestTime">最新消息: 加载中...</span>
</div>
</div>
<div class="chat-messages" id="chatMessages">
<div class="loading">正在加载消息...</div>
</div>
<div class="controls">
<button class="load-more" onclick="loadMoreMessages()">加载更多消息</button>
</div>
</div>
</div>
</div>
<!-- 聊天记录模态框 -->
<div id="chatRecordModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<h3 id="modalTitle">聊天记录</h3>
<span class="close" onclick="closeChatRecordModal()">&times;</span>
</div>
<div class="modal-body">
<div id="chatRecordContent" class="chat-record-content">
<!-- 聊天记录内容将在这里动态生成 -->
</div>
</div>
</div>
</div>
<script>
let currentOffset = 0;
const limit = 50;
let currentView = 'groups';
let currentGroupId = null;
let groupMessages = [];
// 页面加载时初始化
document.addEventListener('DOMContentLoaded', function() {
loadStatistics();
loadGroupMessages();
loadMessages();
});
// 加载统计信息
async function loadStatistics() {
try {
const response = await fetch('/api/statistics');
const data = await response.json();
if (data.success) {
const stats = data.statistics;
document.getElementById('userCount').textContent = stats.user_count;
document.getElementById('groupCount').textContent = stats.group_count;
document.getElementById('messageCount').textContent = stats.message_count;
document.getElementById('fileCount').textContent = stats.file_count;
document.getElementById('latestTime').textContent = `最新消息时间: ${stats.latest_message_time}`;
}
} catch (error) {
console.error('加载统计信息失败:', error);
}
}
// 加载按群组分组的消息统计
async function loadGroupMessages() {
try {
const response = await fetch('/api/group_messages');
const data = await response.json();
const groupList = document.getElementById('groupList');
if (data.success) {
groupMessages = data.groups;
groupList.innerHTML = '';
if (groupMessages.length === 0) {
groupList.innerHTML = '<li>暂无群组消息</li>';
return;
}
groupMessages.forEach(group => {
const li = document.createElement('li');
li.className = 'group-item';
li.onclick = () => selectGroup(group.group_id);
const groupName = document.createElement('div');
groupName.className = 'group-name';
groupName.textContent = group.group_name || group.group_id;
const groupInfo = document.createElement('div');
groupInfo.className = 'group-info';
groupInfo.innerHTML = `
<span>${group.message_count || 0} 条消息</span>
<span>${group.member_count || 0} 人</span>
`;
li.appendChild(groupName);
li.appendChild(groupInfo);
groupList.appendChild(li);
});
// 默认选择第一个群组
if (currentView === 'groups' && groupMessages.length > 0) {
selectGroup(groupMessages[0].group_id);
}
} else {
groupList.innerHTML = '<li>加载失败</li>';
}
} catch (error) {
console.error('加载群组消息失败:', error);
}
}
// 选择群组
function selectGroup(groupId) {
currentGroupId = groupId;
currentOffset = 0;
// 更新选中状态
const groupItems = document.querySelectorAll('.group-item');
groupItems.forEach(item => {
item.classList.remove('active');
if (item.onclick && item.onclick.toString().includes(groupId)) {
item.classList.add('active');
}
});
// 更新群组信息
const selectedGroup = groupMessages.find(g => g.group_id === groupId);
if (selectedGroup) {
document.getElementById('currentGroup').textContent = selectedGroup.group_name || selectedGroup.group_id;
document.getElementById('messageCountDisplay').textContent = `消息数量: ${selectedGroup.message_count || 0}`;
document.getElementById('latestTime').textContent = `最新消息: ${selectedGroup.latest_message_time_formatted || '暂无消息'}`;
}
// 加载该群组的消息
loadMessages();
}
// 切换视图(只保留群组视图)
function switchView(view) {
currentView = view;
currentOffset = 0;
currentGroupId = null;
// 更新按钮状态
document.getElementById('viewGroups').classList.toggle('active', view === 'groups');
// 更新聊天区域标题
if (view === 'groups') {
document.getElementById('currentGroup').textContent = '请选择群组';
document.getElementById('messageCountDisplay').textContent = '消息数量: 0';
document.getElementById('latestTime').textContent = '最新消息: 暂无';
// 重置群组选中状态
const groupItems = document.querySelectorAll('.group-item');
groupItems.forEach(item => item.classList.remove('active'));
}
// 重新加载消息
loadMessages();
}
// 加载消息
async function loadMessages() {
try {
let url = `/api/messages?limit=${limit}&offset=${currentOffset}`;
// 如果是按群组查看,添加群组筛选参数
if (currentView === 'groups' && currentGroupId) {
url += `&group_id=${currentGroupId}`;
}
const response = await fetch(url);
const data = await response.json();
const chatMessages = document.getElementById('chatMessages');
if (data.success) {
if (currentOffset === 0) {
chatMessages.innerHTML = '';
}
if (data.messages.length === 0) {
if (currentOffset === 0) {
chatMessages.innerHTML = '<div class="loading">暂无消息记录</div>';
}
return;
}
data.messages.forEach(message => {
const messageDiv = createMessageElement(message);
chatMessages.appendChild(messageDiv);
});
// 滚动到底部(如果是第一次加载)
if (currentOffset === 0) {
chatMessages.scrollTop = chatMessages.scrollHeight;
}
currentOffset += data.messages.length;
} else {
chatMessages.innerHTML = '<div class="error">加载消息失败: ' + data.error + '</div>';
}
} catch (error) {
console.error('加载消息失败:', error);
document.getElementById('chatMessages').innerHTML = '<div class="error">加载消息失败: ' + error.message + '</div>';
}
}
// 创建消息元素
function createMessageElement(message) {
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
// 这里可以根据需要判断是否是自己的消息
// 暂时都显示为接收的消息
const avatar = document.createElement('div');
avatar.className = 'avatar';
avatar.textContent = (message.sender_name || message.sender || '?').charAt(0).toUpperCase();
const contentDiv = document.createElement('div');
contentDiv.className = 'message-content';
const senderDiv = document.createElement('div');
senderDiv.className = 'message-sender';
senderDiv.textContent = message.sender_name || message.sender || '未知用户';
const textDiv = document.createElement('div');
// 处理chatrecord类型的消息
if (message.type === 'chatrecord' && message.chatrecord_data) {
const chatrecordBtn = document.createElement('button');
chatrecordBtn.className = 'chatrecord-btn';
chatrecordBtn.textContent = message.display_content || '[聊天记录]';
chatrecordBtn.onclick = () => showChatRecordModal(message.chatrecord_data);
textDiv.appendChild(chatrecordBtn);
} else {
textDiv.textContent = message.display_content || message.content || '[空消息]';
}
const timeDiv = document.createElement('div');
timeDiv.className = 'message-time';
timeDiv.textContent = message.formatted_time || '未知时间';
contentDiv.appendChild(senderDiv);
contentDiv.appendChild(textDiv);
contentDiv.appendChild(timeDiv);
messageDiv.appendChild(avatar);
messageDiv.appendChild(contentDiv);
return messageDiv;
}
// 加载更多消息
function loadMoreMessages() {
loadMessages();
}
// 显示聊天记录模态框
function showChatRecordModal(chatrecordData) {
const modal = document.getElementById('chatRecordModal');
const modalTitle = document.getElementById('modalTitle');
const contentDiv = document.getElementById('chatRecordContent');
// 设置模态框标题
if (chatrecordData.chatrecord && chatrecordData.chatrecord.title) {
modalTitle.textContent = chatrecordData.chatrecord.title;
} else {
modalTitle.textContent = '聊天记录';
}
// 清空内容
contentDiv.innerHTML = '';
// 渲染聊天记录内容
if (chatrecordData.chatrecord && chatrecordData.chatrecord.item) {
chatrecordData.chatrecord.item.forEach((item, index) => {
const itemDiv = document.createElement('div');
itemDiv.className = 'chat-record-item';
try {
// 解析消息内容
const contentData = JSON.parse(item.content);
// 创建发送者信息
const senderDiv = document.createElement('div');
senderDiv.className = 'chat-record-sender';
senderDiv.textContent = item.from_chatroom ? '群聊' : '私聊';
// 创建消息内容
const textDiv = document.createElement('div');
textDiv.className = 'chat-record-text';
if (item.type === 'ChatRecordText') {
textDiv.textContent = contentData.content || '[文本消息]';
} else if (item.type === 'ChatRecordImage') {
const imgText = document.createElement('div');
imgText.textContent = '[图片]';
textDiv.appendChild(imgText);
if (contentData.sdkfileid) {
const img = document.createElement('img');
img.className = 'chat-record-image';
img.src = `/api/file/${contentData.sdkfileid}`;
img.alt = '聊天记录图片';
img.onerror = function() {
this.style.display = 'none';
};
textDiv.appendChild(img);
}
} else {
textDiv.textContent = `[${item.type}]`;
}
// 创建时间信息
const timeDiv = document.createElement('div');
timeDiv.className = 'chat-record-time';
if (item.msgtime) {
const date = new Date(item.msgtime * 1000);
timeDiv.textContent = date.toLocaleString('zh-CN');
} else {
timeDiv.textContent = '未知时间';
}
itemDiv.appendChild(senderDiv);
itemDiv.appendChild(textDiv);
itemDiv.appendChild(timeDiv);
contentDiv.appendChild(itemDiv);
} catch (error) {
console.error('解析聊天记录项失败:', error);
const errorDiv = document.createElement('div');
errorDiv.className = 'chat-record-item';
errorDiv.textContent = '[无法解析的消息]';
contentDiv.appendChild(errorDiv);
}
});
} else {
contentDiv.innerHTML = '<div class="chat-record-item">暂无聊天记录内容</div>';
}
// 显示模态框
modal.style.display = 'block';
}
// 关闭聊天记录模态框
function closeChatRecordModal() {
const modal = document.getElementById('chatRecordModal');
modal.style.display = 'none';
}
// 点击模态框外部关闭
window.onclick = function(event) {
const modal = document.getElementById('chatRecordModal');
if (event.target === modal) {
closeChatRecordModal();
}
}
// 自动刷新统计信息(每30秒)
setInterval(loadStatistics, 30000);
</script>
</body>
</html>