Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8199773ef6 | |||
| d153986f3d | |||
| e9357577cb | |||
| 24ba04b3e3 | |||
| d1ddd340c0 | |||
| 6ab58cb363 | |||
| ba8d0ae679 | |||
| 52d98e88c6 | |||
| 25dff98583 | |||
| 6452e4c976 | |||
| f85991064f | |||
| c725aeb192 | |||
| 423f3aa717 | |||
| 2f60e30169 | |||
| 4442d5056a | |||
| 3411de1612 |
12
.gitignore
vendored
Normal file
12
.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.pyd
|
||||
.Python
|
||||
*.so
|
||||
*.egg-info/
|
||||
dist/
|
||||
build/
|
||||
*.db
|
||||
data.db
|
||||
node_modules/
|
||||
1123
backend/app.py
Normal file
1123
backend/app.py
Normal file
File diff suppressed because it is too large
Load Diff
2
backend/requirements.txt
Normal file
2
backend/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
flask>=2.0.0
|
||||
flask-cors>=3.0.0
|
||||
4
backend/start.sh
Executable file
4
backend/start.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
cd "$(dirname "$0")"
|
||||
pip install -r requirements.txt -q
|
||||
python app.py
|
||||
482
www/admin.html
Normal file
482
www/admin.html
Normal file
@@ -0,0 +1,482 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>AI助手 - 后台管理</title>
|
||||
<link rel="stylesheet" href="admin.css?v=3.6.6">
|
||||
<style>
|
||||
:root {
|
||||
--primary: #667eea;
|
||||
--primary-dark: #5a67d8;
|
||||
--bg-color: #f5f7fa;
|
||||
--card-bg: #ffffff;
|
||||
--text-color: #2d3748;
|
||||
--text-light: #718096;
|
||||
--border-color: #e2e8f0;
|
||||
--danger: #e53e3e;
|
||||
--success: #22c55e;
|
||||
--warning: #f59e0b;
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: var(--bg-color);
|
||||
color: var(--text-color);
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
/* 登录页面 */
|
||||
.login-page {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
background: linear-gradient(135deg, var(--primary) 0%, #764ba2 100%);
|
||||
}
|
||||
|
||||
.login-card {
|
||||
background: white;
|
||||
padding: 40px;
|
||||
border-radius: 16px;
|
||||
width: 350px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.login-logo {
|
||||
font-size: 48px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.login-title {
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.login-input {
|
||||
width: 100%;
|
||||
padding: 12px 16px;
|
||||
border: 2px solid var(--border-color);
|
||||
border-radius: 10px;
|
||||
font-size: 16px;
|
||||
margin-bottom: 16px;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.login-input:focus {
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.login-btn {
|
||||
width: 100%;
|
||||
padding: 14px;
|
||||
background: linear-gradient(135deg, var(--primary) 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
font-size: 16px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.login-btn:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
/* 主页面 */
|
||||
.admin-page {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.admin-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 16px 24px;
|
||||
background: white;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.admin-logo {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.admin-logo span {
|
||||
font-size: 28px;
|
||||
}
|
||||
|
||||
.admin-header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.admin-logout {
|
||||
padding: 8px 16px;
|
||||
background: var(--danger);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* 侧边栏 */
|
||||
.admin-container {
|
||||
display: flex;
|
||||
height: calc(100vh - 60px);
|
||||
}
|
||||
|
||||
.admin-sidebar {
|
||||
width: 220px;
|
||||
background: white;
|
||||
border-right: 1px solid var(--border-color);
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.sidebar-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 12px 16px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.sidebar-item:hover {
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
}
|
||||
|
||||
.sidebar-item.active {
|
||||
background: linear-gradient(135deg, var(--primary) 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.sidebar-icon {
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
/* 内容区 */
|
||||
.admin-content {
|
||||
flex: 1;
|
||||
padding: 24px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.content-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.content-title {
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
padding: 10px 20px;
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* 数据卡片 */
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
gap: 16px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stat-icon {
|
||||
font-size: 32px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 28px;
|
||||
font-weight: 600;
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 14px;
|
||||
color: var(--text-light);
|
||||
}
|
||||
|
||||
/* 数据表格 */
|
||||
.data-table {
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.data-table table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.data-table th, .data-table td {
|
||||
padding: 14px 16px;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.data-table th {
|
||||
background: var(--bg-color);
|
||||
font-weight: 600;
|
||||
color: var(--text-light);
|
||||
}
|
||||
|
||||
.data-table tr:last-child td {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.data-table tr:hover td {
|
||||
background: rgba(102, 126, 234, 0.05);
|
||||
}
|
||||
|
||||
/* 操作按钮 */
|
||||
.action-btns {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.action-btn {
|
||||
padding: 6px 12px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.action-btn.edit {
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.action-btn.delete {
|
||||
background: var(--danger);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.action-btn.default {
|
||||
background: var(--success);
|
||||
color: white;
|
||||
}
|
||||
|
||||
/* 默认标记 */
|
||||
.default-badge {
|
||||
padding: 4px 8px;
|
||||
background: var(--success);
|
||||
color: white;
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
/* 模态框 */
|
||||
.modal {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0,0,0,0.5);
|
||||
display: none;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.modal.show {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background: white;
|
||||
padding: 24px;
|
||||
border-radius: 16px;
|
||||
max-width: 500px;
|
||||
width: 90%;
|
||||
max-height: 80vh;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.modal-title {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.modal-close {
|
||||
font-size: 24px;
|
||||
cursor: pointer;
|
||||
color: var(--text-light);
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
display: block;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.form-input, .form-select, .form-textarea {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.form-input:focus, .form-select:focus, .form-textarea:focus {
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.form-textarea {
|
||||
resize: vertical;
|
||||
min-height: 100px;
|
||||
}
|
||||
|
||||
.form-submit {
|
||||
width: 100%;
|
||||
padding: 14px;
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
/* Toast */
|
||||
.toast {
|
||||
position: fixed;
|
||||
top: 80px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background: var(--text-color);
|
||||
color: white;
|
||||
padding: 12px 24px;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s;
|
||||
z-index: 2000;
|
||||
}
|
||||
|
||||
.toast.show {
|
||||
opacity: 1;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- 登录页面 -->
|
||||
<div class="login-page" id="loginPage">
|
||||
<div class="login-card">
|
||||
<div class="login-logo">🤖</div>
|
||||
<h1 class="login-title">后台管理系统</h1>
|
||||
<input type="text" class="login-input" id="loginUsername" placeholder="用户名">
|
||||
<input type="password" class="login-input" id="loginPassword" placeholder="密码">
|
||||
<button class="login-btn" id="loginBtn">登录</button>
|
||||
<p style="margin-top: 16px; color: #999; font-size: 12px;">默认: admin / admin123</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 管理页面 -->
|
||||
<div class="admin-page" id="adminPage">
|
||||
<header class="admin-header">
|
||||
<div class="admin-logo">
|
||||
<span>🤖</span>
|
||||
AI助手 - 后台管理
|
||||
</div>
|
||||
<div class="admin-header-right">
|
||||
<span id="adminName">管理员</span>
|
||||
<button class="admin-logout" id="logoutBtn">退出</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="admin-container">
|
||||
<aside class="admin-sidebar">
|
||||
<div class="sidebar-item active" data-page="stats">
|
||||
<span class="sidebar-icon">📊</span>
|
||||
<span>统计信息</span>
|
||||
</div>
|
||||
<div class="sidebar-item" data-page="users">
|
||||
<span class="sidebar-icon">👥</span>
|
||||
<span>用户管理</span>
|
||||
</div>
|
||||
<div class="sidebar-item" data-page="llm">
|
||||
<span class="sidebar-icon">🧠</span>
|
||||
<span>大模型配置</span>
|
||||
</div>
|
||||
<div class="sidebar-item" data-page="agents">
|
||||
<span class="sidebar-icon">🤖</span>
|
||||
<span>智能体管理</span>
|
||||
</div>
|
||||
<div class="sidebar-item" data-page="chat">
|
||||
<span class="sidebar-icon">💬</span>
|
||||
<span>对话配置</span>
|
||||
</div>
|
||||
<div class="sidebar-item" data-page="tools">
|
||||
<span class="sidebar-icon">🔧</span>
|
||||
<span>工具配置</span>
|
||||
</div>
|
||||
<div class="sidebar-item" data-page="system">
|
||||
<span class="sidebar-icon">⚙️</span>
|
||||
<span>系统设置</span>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<main class="admin-content" id="mainContent">
|
||||
<!-- 内容区动态加载 -->
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Toast -->
|
||||
<div class="toast" id="toast"></div>
|
||||
|
||||
<!-- 模态框 -->
|
||||
<div class="modal" id="modal">
|
||||
<div class="modal-content" id="modalContent">
|
||||
<!-- 动态内容 -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="admin.js?v=3.6.6"></script>
|
||||
</body>
|
||||
</html>
|
||||
1312
www/admin.js
Normal file
1312
www/admin.js
Normal file
File diff suppressed because it is too large
Load Diff
471
www/app.js
471
www/app.js
@@ -19,39 +19,18 @@ let isLoading = false;
|
||||
// 当前页面状态
|
||||
let currentPage = 'chats'; // chats | agents | profile
|
||||
|
||||
// 系统智能体数据(完整列表)
|
||||
let systemAgents = [
|
||||
// 热门智能体
|
||||
{ id: 'assistant', name: '通用助手', avatar: '🤖', category: 'hot', desc: '能回答各类问题,帮助写作、分析、解答疑惑', systemPrompt: '你是一个智能助手,能够回答各类问题,帮助用户解决问题。', heat: 9500 },
|
||||
{ id: 'writer', name: '写作助手', avatar: '✍️', category: 'hot', desc: '专注于文章写作、文案创作、内容润色', systemPrompt: '你是一个专业的写作助手,擅长各类文章写作、文案创作和内容润色。', heat: 8800 },
|
||||
{ id: 'coder', name: '编程助手', avatar: '👨💻', category: 'hot', desc: '精通编程语言,解答技术问题,生成代码', systemPrompt: '你是一个专业的编程助手,精通各类编程语言,能够解答技术问题并生成高质量代码。', heat: 8500 },
|
||||
{ id: 'translator', name: '翻译助手', avatar: '🌐', category: 'hot', desc: '多语言翻译,精准表达,文化适配', systemPrompt: '你是一个专业的翻译助手,精通多语言翻译,能够精准表达并适配文化差异。', heat: 7200 },
|
||||
|
||||
// 工作助手
|
||||
{ id: 'assistant-work', name: '工作助手', avatar: '💼', category: 'work', desc: '职场问题解答,工作效率提升', systemPrompt: '你是一个工作助手,帮助解决职场问题,提升工作效率。', heat: 5000 },
|
||||
{ id: 'ppt', name: 'PPT助手', avatar: '📊', category: 'work', desc: 'PPT内容生成,结构优化,设计建议', systemPrompt: '你是一个PPT助手,擅长PPT内容生成、结构优化和设计建议。', heat: 4500 },
|
||||
{ id: 'excel', name: 'Excel助手', avatar: '📈', category: 'work', desc: 'Excel公式、数据分析、表格优化', systemPrompt: '你是一个Excel助手,精通Excel公式、数据分析和表格优化。', heat: 4000 },
|
||||
|
||||
// 学习助手
|
||||
{ id: 'teacher', name: '学习助手', avatar: '📚', category: 'study', desc: '知识讲解,学习方法,考试辅导', systemPrompt: '你是一个学习助手,擅长知识讲解、学习方法指导和考试辅导。', heat: 5500 },
|
||||
{ id: 'english', name: '英语助手', avatar: '🔤', category: 'study', desc: '英语学习,语法纠正,口语练习', systemPrompt: '你是一个英语助手,帮助英语学习、语法纠正和口语练习。', heat: 5000 },
|
||||
{ id: 'math', name: '数学助手', avatar: '🔢', category: 'study', desc: '数学解题,公式推导,概念讲解', systemPrompt: '你是一个数学助手,擅长数学解题、公式推导和概念讲解。', heat: 4500 },
|
||||
|
||||
// 生活助手
|
||||
{ id: 'health', name: '健康助手', avatar: '🏥', category: 'life', desc: '健康咨询,养生建议,运动指导', systemPrompt: '你是一个健康助手,提供健康咨询、养生建议和运动指导。', heat: 3500 },
|
||||
{ id: 'travel', name: '旅行助手', avatar: '✈️', category: 'life', desc: '旅行规划,景点推荐,美食指南', systemPrompt: '你是一个旅行助手,擅长旅行规划、景点推荐和美食指南。', heat: 3200 },
|
||||
{ id: 'food', name: '美食助手', avatar: '🍳', category: 'life', desc: '菜谱推荐,烹饪技巧,营养搭配', systemPrompt: '你是一个美食助手,提供菜谱推荐、烹饪技巧和营养搭配建议。', heat: 3000 },
|
||||
];
|
||||
// 系统智能体数据(从后台API加载)
|
||||
let systemAgents = [];
|
||||
|
||||
// 用户智能体界面显示的智能体(默认显示热门智能体)
|
||||
// 用户智能体界面显示的智能体
|
||||
let agents = [];
|
||||
|
||||
// 用户添加的智能体(按类别分组)
|
||||
let myAgents = {
|
||||
hot: ['assistant', 'writer', 'coder', 'translator'],
|
||||
work: ['assistant-work', 'ppt', 'excel'],
|
||||
study: ['teacher', 'english', 'math'],
|
||||
life: ['health', 'travel', 'food']
|
||||
basic: [],
|
||||
work: [],
|
||||
study: [],
|
||||
life: []
|
||||
};
|
||||
|
||||
// 收藏的智能体列表
|
||||
@@ -59,7 +38,7 @@ let favoriteAgents = [];
|
||||
|
||||
// 置顶的智能体列表(按类别)
|
||||
let pinnedAgents = {
|
||||
hot: [],
|
||||
basic: [],
|
||||
work: [],
|
||||
study: [],
|
||||
life: []
|
||||
@@ -67,6 +46,9 @@ let pinnedAgents = {
|
||||
|
||||
let currentAgent = null; // 当前选中的智能体
|
||||
|
||||
// 后台配置
|
||||
let backendConfig = null; // 从API获取的配置
|
||||
|
||||
// 用户状态
|
||||
let currentUser = null; // 当前登录用户 { username, password, registeredAt }
|
||||
|
||||
@@ -79,12 +61,12 @@ let dailyUsage = {
|
||||
agentMessages: 0 // 智能体消息数
|
||||
};
|
||||
|
||||
// 未登录用户限制
|
||||
const GUEST_LIMITS = {
|
||||
maxChatSessionsPerDay: 1, // 每天最多1个对话会话
|
||||
maxChatMessagesPerDay: 20, // 每天最多20条对话消息
|
||||
maxAgentPerDay: 1, // 每天只能用1个智能体(通用助手)
|
||||
maxAgentMessagesPerDay: 20 // 每天最多20条智能体消息
|
||||
// 未登录用户限制(从后台配置加载)
|
||||
let GUEST_LIMITS = {
|
||||
maxChatSessionsPerDay: 1,
|
||||
maxChatMessagesPerDay: 20,
|
||||
maxAgentPerDay: 1,
|
||||
maxAgentMessagesPerDay: 20
|
||||
};
|
||||
|
||||
// 获取今日日期字符串
|
||||
@@ -93,6 +75,59 @@ function getTodayDate() {
|
||||
return `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// 从后台API加载配置
|
||||
async function loadBackendConfig() {
|
||||
try {
|
||||
// 使用相对路径,自动适应当前域名
|
||||
const API_BASE = window.location.origin;
|
||||
const res = await fetch(`${API_BASE}/api/config`);
|
||||
backendConfig = await res.json();
|
||||
|
||||
// 加载智能体列表
|
||||
if (backendConfig.agents) {
|
||||
systemAgents = backendConfig.agents.map(a => ({
|
||||
id: a.agent_id,
|
||||
name: a.name,
|
||||
avatar: a.avatar,
|
||||
category: a.category,
|
||||
desc: a.description,
|
||||
systemPrompt: a.system_prompt,
|
||||
heat: a.heat || 0,
|
||||
tags: a.tags || '',
|
||||
enable_tools: a.enable_tools || ''
|
||||
}));
|
||||
|
||||
// 初始化用户智能体列表(按类别)
|
||||
systemAgents.forEach(agent => {
|
||||
if (!myAgents[agent.category]) {
|
||||
myAgents[agent.category] = [];
|
||||
}
|
||||
myAgents[agent.category].push(agent.id);
|
||||
});
|
||||
}
|
||||
|
||||
// 加载游客限制配置
|
||||
if (backendConfig.system?.guestLimits) {
|
||||
GUEST_LIMITS = {
|
||||
maxChatSessionsPerDay: backendConfig.system.guestLimits.chatSessions || 1,
|
||||
maxChatMessagesPerDay: backendConfig.system.guestLimits.chatMessages || 20,
|
||||
maxAgentPerDay: 1, // 游客只能用通用助手
|
||||
maxAgentMessagesPerDay: backendConfig.system.guestLimits.agentMessages || 20
|
||||
};
|
||||
}
|
||||
|
||||
updateAgentsDisplay();
|
||||
console.log('后台配置已加载', backendConfig);
|
||||
} catch (e) {
|
||||
console.error('加载后台配置失败', e);
|
||||
// 使用默认配置
|
||||
systemAgents = [
|
||||
{ id: 'assistant', name: '通用助手', avatar: '🤖', category: 'basic', desc: '能回答各类问题,帮助写作、分析、解答疑惑', systemPrompt: '你是一个智能助手,能够回答各类问题,帮助用户解决问题。', heat: 9500, tags: 'hot' },
|
||||
];
|
||||
updateAgentsDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
// 检查并重置每日使用统计
|
||||
function checkDailyUsage() {
|
||||
const today = getTodayDate();
|
||||
@@ -228,9 +263,10 @@ function getRemainingQuota() {
|
||||
}
|
||||
|
||||
// 获取使用智能体的对话列表(按时间倒序)
|
||||
// 只返回真正有对话消息的记录(messages.length > 0)
|
||||
function getAgentConversationHistory(limit = 5) {
|
||||
return conversations
|
||||
.filter(conv => conv.agentId) // 筛选有智能体的对话
|
||||
.filter(conv => conv.agentId && conv.messages && conv.messages.length > 0) // 筛选有智能体且有消息的对话
|
||||
.sort((a, b) => b.updatedAt - a.updatedAt) // 按更新时间倒序
|
||||
.slice(0, limit)
|
||||
.map(conv => {
|
||||
@@ -321,7 +357,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
checkDailyUsage();
|
||||
|
||||
// 根据用户配置更新显示的智能体
|
||||
updateAgentsDisplay();
|
||||
// updateAgentsDisplay(); // 先不调用,等后台配置加载后再更新
|
||||
|
||||
// 加载当前页面状态
|
||||
const savedPage = localStorage.getItem('currentPage');
|
||||
@@ -329,8 +365,13 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
currentPage = savedPage;
|
||||
}
|
||||
|
||||
// 显示主页
|
||||
showMainPage();
|
||||
// 加载后台配置并显示主页
|
||||
loadBackendConfig().then(() => {
|
||||
showMainPage();
|
||||
}).catch(() => {
|
||||
// 即使加载失败也显示主页
|
||||
showMainPage();
|
||||
});
|
||||
});
|
||||
|
||||
// 根据用户配置更新显示的智能体
|
||||
@@ -644,8 +685,11 @@ function bindChatsPageEvents() {
|
||||
// ==================== 智能体页面 ====================
|
||||
|
||||
function renderAgentsPage() {
|
||||
// 根据用户配置筛选智能体
|
||||
const hotAgents = agents.filter(a => a.category === 'hot');
|
||||
// 根据后台配置筛选智能体
|
||||
// 热门智能体 = 标签中包含 "hot"
|
||||
const hotAgents = agents.filter(a => a.tags && a.tags.split(',').includes('hot'));
|
||||
// 其他类别 = 根据 category 字段分类
|
||||
const basicAgents = agents.filter(a => a.category === 'basic');
|
||||
const workAgents = agents.filter(a => a.category === 'work');
|
||||
const studyAgents = agents.filter(a => a.category === 'study');
|
||||
const lifeAgents = agents.filter(a => a.category === 'life');
|
||||
@@ -698,7 +742,7 @@ function renderAgentsPage() {
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
<!-- 热门智能体 -->
|
||||
<!-- 热门智能体(标签为hot) -->
|
||||
${hotAgents.length > 0 ? `
|
||||
<div class="agents-section">
|
||||
<div class="section-title">🔥 热门智能体</div>
|
||||
@@ -1032,7 +1076,10 @@ function showAgentDiscoverPage() {
|
||||
const sortedAgents = [...systemAgents].sort((a, b) => b.heat - a.heat);
|
||||
|
||||
// 分类显示
|
||||
const hotAgents = systemAgents.filter(a => a.category === 'hot');
|
||||
// 热门智能体 = 标签中包含 "hot"
|
||||
const hotAgents = systemAgents.filter(a => a.tags && a.tags.split(',').includes('hot'));
|
||||
// 其他类别
|
||||
const basicAgents = systemAgents.filter(a => a.category === 'basic');
|
||||
const workAgents = systemAgents.filter(a => a.category === 'work');
|
||||
const studyAgents = systemAgents.filter(a => a.category === 'study');
|
||||
const lifeAgents = systemAgents.filter(a => a.category === 'life');
|
||||
@@ -1053,37 +1100,55 @@ function showAgentDiscoverPage() {
|
||||
<input type="text" id="discoverSearchInput" placeholder="搜索智能体名称或描述...">
|
||||
</div>
|
||||
|
||||
<!-- 热门智能体 -->
|
||||
<!-- 热门智能体(标签为hot) -->
|
||||
${hotAgents.length > 0 ? `
|
||||
<div class="discover-section">
|
||||
<div class="discover-section-title">🔥 热门智能体</div>
|
||||
<div class="discover-grid" id="discoverHotGrid">
|
||||
${hotAgents.map(agent => renderDiscoverCard(agent)).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
<!-- 基础智能体 -->
|
||||
${basicAgents.length > 0 ? `
|
||||
<div class="discover-section">
|
||||
<div class="discover-section-title">⭐ 基础智能体</div>
|
||||
<div class="discover-grid" id="discoverBasicGrid">
|
||||
${basicAgents.map(agent => renderDiscoverCard(agent)).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
<!-- 工作助手 -->
|
||||
${workAgents.length > 0 ? `
|
||||
<div class="discover-section">
|
||||
<div class="discover-section-title">💼 工作助手</div>
|
||||
<div class="discover-grid" id="discoverWorkGrid">
|
||||
${workAgents.map(agent => renderDiscoverCard(agent)).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
<!-- 学习助手 -->
|
||||
${studyAgents.length > 0 ? `
|
||||
<div class="discover-section">
|
||||
<div class="discover-section-title">📚 学习助手</div>
|
||||
<div class="discover-grid" id="discoverStudyGrid">
|
||||
${studyAgents.map(agent => renderDiscoverCard(agent)).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
<!-- 生活助手 -->
|
||||
${lifeAgents.length > 0 ? `
|
||||
<div class="discover-section">
|
||||
<div class="discover-section-title">🏠 生活助手</div>
|
||||
<div class="discover-grid" id="discoverLifeGrid">
|
||||
${lifeAgents.map(agent => renderDiscoverCard(agent)).join('')}
|
||||
</div>
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
@@ -1445,7 +1510,7 @@ function renderProfilePage() {
|
||||
</div>
|
||||
|
||||
<div class="profile-footer">
|
||||
<p>AI助手 v3.5.1</p>
|
||||
<p>AI助手 v3.6.0</p>
|
||||
<p>基于智谱 GLM-4.5-Air</p>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1555,14 +1620,43 @@ function showEditProfilePage() {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取完整用户信息
|
||||
const users = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
|
||||
const fullUser = users.find(u => u.username === currentUser.username);
|
||||
|
||||
// 如果用户已登录且有ID,从 backend 获取完整用户信息
|
||||
if (currentUser.id) {
|
||||
fetch(`/api/admin/users/${currentUser.id}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data && data.id) {
|
||||
// 更新 currentUser 的完整信息
|
||||
currentUser.avatar = data.avatar || '👤';
|
||||
currentUser.signature = data.signature || '';
|
||||
currentUser.email = data.email || '';
|
||||
currentUser.gender = data.gender || '';
|
||||
currentUser.age = data.age || '';
|
||||
currentUser.region = data.region || '';
|
||||
currentUser.phone = data.phone || '';
|
||||
|
||||
// 渲染编辑页面
|
||||
renderEditProfilePage();
|
||||
} else {
|
||||
// 使用本地数据
|
||||
renderEditProfilePage();
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
// 使用本地数据
|
||||
renderEditProfilePage();
|
||||
});
|
||||
} else {
|
||||
// 游客用户,使用本地数据
|
||||
renderEditProfilePage();
|
||||
}
|
||||
}
|
||||
|
||||
function renderEditProfilePage() {
|
||||
const avatar = currentUser.avatar || '👤';
|
||||
const signature = currentUser.signature || '';
|
||||
const phone = fullUser?.phone || '';
|
||||
const email = fullUser?.email || '';
|
||||
const phone = currentUser.phone || '';
|
||||
const email = currentUser.email || '';
|
||||
const gender = currentUser.gender || '';
|
||||
const age = currentUser.age || '';
|
||||
const region = currentUser.region || '';
|
||||
@@ -1708,26 +1802,6 @@ function handleSaveProfile() {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查用户名是否被其他人占用
|
||||
const users = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
|
||||
if (newUsername !== currentUser.username) {
|
||||
if (users.find(u => u.username === newUsername)) {
|
||||
showToast('用户名已被占用');
|
||||
return;
|
||||
}
|
||||
// 更新用户名
|
||||
const userIndex = users.findIndex(u => u.username === currentUser.username);
|
||||
if (userIndex >= 0) {
|
||||
users[userIndex].username = newUsername;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新邮箱
|
||||
const userIndex = users.findIndex(u => u.username === currentUser.username);
|
||||
if (userIndex >= 0) {
|
||||
users[userIndex].email = newEmail;
|
||||
}
|
||||
|
||||
// 更新 currentUser
|
||||
currentUser.avatar = newAvatar;
|
||||
currentUser.username = newUsername;
|
||||
@@ -1735,12 +1809,42 @@ function handleSaveProfile() {
|
||||
currentUser.gender = newGender;
|
||||
currentUser.age = newAge ? parseInt(newAge) : '';
|
||||
currentUser.region = newRegion;
|
||||
currentUser.email = newEmail;
|
||||
|
||||
saveCurrentUser();
|
||||
localStorage.setItem('registeredUsers', JSON.stringify(users));
|
||||
|
||||
showToast('保存成功');
|
||||
switchPage('profile');
|
||||
// 如果用户已登录,调用 backend API 更新
|
||||
if (currentUser.id) {
|
||||
fetch(`/api/user/${currentUser.id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
username: newUsername,
|
||||
email: newEmail,
|
||||
avatar: newAvatar,
|
||||
signature: newSignature,
|
||||
gender: newGender,
|
||||
age: newAge ? parseInt(newAge) : null,
|
||||
region: newRegion
|
||||
})
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
saveCurrentUser();
|
||||
showToast('保存成功');
|
||||
switchPage('profile');
|
||||
} else {
|
||||
showToast(data.error || '保存失败');
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
showToast('网络错误,请重试');
|
||||
});
|
||||
} else {
|
||||
// 未登录用户,保存到本地
|
||||
saveCurrentUser();
|
||||
showToast('保存成功');
|
||||
switchPage('profile');
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 消息通知页面 ====================
|
||||
@@ -2382,18 +2486,38 @@ function handleLogin() {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查本地存储的用户
|
||||
const users = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
|
||||
const user = users.find(u => u.username === username && u.password === password);
|
||||
|
||||
if (user) {
|
||||
currentUser = { username: user.username, registeredAt: user.registeredAt };
|
||||
saveCurrentUser();
|
||||
showToast('登录成功');
|
||||
showMainPage();
|
||||
} else {
|
||||
showToast('用户名或密码错误');
|
||||
}
|
||||
// 调用后台登录API
|
||||
fetch('/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password })
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
currentUser = {
|
||||
id: data.user.id,
|
||||
username: data.user.username,
|
||||
phone: data.user.phone,
|
||||
email: data.user.email || '',
|
||||
avatar: data.user.avatar || '👤',
|
||||
signature: data.user.signature || '',
|
||||
registeredAt: Date.now()
|
||||
};
|
||||
saveCurrentUser();
|
||||
|
||||
// 从 backend 加载用户对话数据
|
||||
loadUserConversations().then(() => {
|
||||
showToast('登录成功');
|
||||
showMainPage();
|
||||
});
|
||||
} else {
|
||||
showToast(data.error || '用户名或密码错误');
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
showToast('网络错误,请重试');
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 注册页面 ====================
|
||||
@@ -2412,7 +2536,7 @@ function showRegisterPage() {
|
||||
<div class="auth-form">
|
||||
<div class="auth-input-group">
|
||||
<label>用户名</label>
|
||||
<input type="text" id="registerUsername" placeholder="请输入用户名(3-20字符)" autocomplete="username">
|
||||
<input type="text" id="registerUsername" placeholder="请输入用户名(2-20字符)" autocomplete="username">
|
||||
</div>
|
||||
<div class="auth-input-group">
|
||||
<label>手机号 <span class="required">*</span></label>
|
||||
@@ -2498,8 +2622,8 @@ function handleRegister() {
|
||||
const passwordConfirm = document.getElementById('registerPasswordConfirm')?.value;
|
||||
|
||||
// 验证用户名
|
||||
if (!username || username.length < 3 || username.length > 20) {
|
||||
showToast('用户名需要3-20个字符');
|
||||
if (!username || username.length < 2 || username.length > 20) {
|
||||
showToast('用户名需要2-20个字符');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2510,7 +2634,7 @@ function handleRegister() {
|
||||
}
|
||||
|
||||
// 验证邮箱(可选)
|
||||
if (!validateEmail(email)) {
|
||||
if (email && !validateEmail(email)) {
|
||||
showToast('请输入正确的邮箱格式');
|
||||
return;
|
||||
}
|
||||
@@ -2526,37 +2650,45 @@ function handleRegister() {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查用户名是否已存在
|
||||
const users = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
|
||||
if (users.find(u => u.username === username)) {
|
||||
showToast('用户名已存在');
|
||||
return;
|
||||
}
|
||||
// 调用后台注册API(验证码暂时跳过,传随机值)
|
||||
const code = Math.floor(100000 + Math.random() * 900000).toString();
|
||||
|
||||
// 检查手机号是否已注册
|
||||
if (users.find(u => u.phone === phone)) {
|
||||
showToast('该手机号已注册');
|
||||
return;
|
||||
}
|
||||
|
||||
// 注册新用户
|
||||
const newUser = {
|
||||
username,
|
||||
phone,
|
||||
email: email || '',
|
||||
password,
|
||||
registeredAt: Date.now()
|
||||
};
|
||||
|
||||
users.push(newUser);
|
||||
localStorage.setItem('registeredUsers', JSON.stringify(users));
|
||||
|
||||
// 自动登录
|
||||
currentUser = { username: newUser.username, phone: newUser.phone, registeredAt: newUser.registeredAt };
|
||||
saveCurrentUser();
|
||||
|
||||
showToast('注册成功');
|
||||
showMainPage();
|
||||
fetch('/api/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
phone,
|
||||
email: email || '',
|
||||
password,
|
||||
code
|
||||
})
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// 注册成功,自动登录
|
||||
currentUser = {
|
||||
id: data.user_id,
|
||||
username: username,
|
||||
phone: phone,
|
||||
email: email || '',
|
||||
registeredAt: Date.now()
|
||||
};
|
||||
saveCurrentUser();
|
||||
|
||||
// 加载对话数据(新用户为空)
|
||||
loadUserConversations().then(() => {
|
||||
showToast('注册成功');
|
||||
showMainPage();
|
||||
});
|
||||
} else {
|
||||
showToast(data.error || '注册失败');
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
showToast('网络错误,请重试');
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 限制提示 ====================
|
||||
@@ -3411,6 +3543,17 @@ function deleteConversation(id) {
|
||||
|
||||
conversations = conversations.filter(c => c.id !== id);
|
||||
saveConversations();
|
||||
|
||||
// 如果用户已登录,从 backend 删除
|
||||
if (currentUser && currentUser.id) {
|
||||
const convId = parseInt(id);
|
||||
if (convId > 0) {
|
||||
fetch(`/api/user/${currentUser.id}/conversations/${convId}`, {
|
||||
method: 'DELETE'
|
||||
}).catch(e => console.error('删除对话失败:', e));
|
||||
}
|
||||
}
|
||||
|
||||
showConversationList();
|
||||
}
|
||||
|
||||
@@ -4081,7 +4224,91 @@ function setupScrollListener() {
|
||||
|
||||
// 保存对话列表
|
||||
function saveConversations() {
|
||||
// 保存到本地存储(离线可用)
|
||||
localStorage.setItem('conversations', JSON.stringify(conversations));
|
||||
|
||||
// 如果用户已登录,同步到 backend
|
||||
if (currentUser && currentUser.id) {
|
||||
syncConversationsToBackend();
|
||||
}
|
||||
}
|
||||
|
||||
// 同步对话数据到 backend
|
||||
async function syncConversationsToBackend() {
|
||||
if (!currentUser || !currentUser.id) return;
|
||||
|
||||
try {
|
||||
// 批量同步所有对话
|
||||
for (const conv of conversations) {
|
||||
const convId = parseInt(conv.id);
|
||||
if (convId > 0) {
|
||||
// 更新现有对话
|
||||
await fetch(`/api/user/${currentUser.id}/conversations/${convId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: conv.title,
|
||||
messages: conv.messages,
|
||||
agentId: conv.agentId
|
||||
})
|
||||
});
|
||||
} else {
|
||||
// 新对话,需要创建
|
||||
const res = await fetch(`/api/user/${currentUser.id}/conversations`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: conv.title,
|
||||
messages: conv.messages,
|
||||
agentId: conv.agentId
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.success && data.id) {
|
||||
// 更新本地ID为backend ID
|
||||
conv.id = data.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 同步失败,静默处理(下次登录时会重新加载)
|
||||
console.error('同步对话失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 从 backend 加载用户对话数据
|
||||
async function loadUserConversations() {
|
||||
if (!currentUser || !currentUser.id) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/user/${currentUser.id}/conversations`);
|
||||
const data = await res.json();
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
// 合并本地和云端数据(云端优先)
|
||||
const localConvIds = conversations.map(c => c.id);
|
||||
|
||||
for (const cloudConv of data) {
|
||||
const localIdx = localConvIds.indexOf(cloudConv.id);
|
||||
if (localIdx >= 0) {
|
||||
// 更新本地对话(云端数据优先)
|
||||
conversations[localIdx] = cloudConv;
|
||||
} else {
|
||||
// 添加云端对话
|
||||
conversations.push(cloudConv);
|
||||
}
|
||||
}
|
||||
|
||||
// 按更新时间排序
|
||||
conversations.sort((a, b) => b.updatedAt - a.updatedAt);
|
||||
|
||||
// 保存到本地
|
||||
localStorage.setItem('conversations', JSON.stringify(conversations));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('加载对话失败:', e);
|
||||
// 使用本地数据
|
||||
}
|
||||
}
|
||||
|
||||
// 显示提示
|
||||
|
||||
Reference in New Issue
Block a user