Files
TrulyMEM-TrueHumanMEM-local/ui/templates/settings.html

806 lines
37 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>设置 - TrulyMEM</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Courier New', monospace;
background: #0a0a1a;
color: #ffffff;
min-height: 100vh;
display: flex; align-items: center; justify-content: center;
position: relative;
}
body::before {
content: ''; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background:
radial-gradient(2px 2px at 20px 30px, #eee, transparent),
radial-gradient(2px 2px at 40px 70px, rgba(255,255,255,0.8), transparent),
radial-gradient(1px 1px at 90px 40px, #fff, transparent);
background-repeat: repeat;
background-size: 200px 100px;
animation: twinkle 5s ease-in-out infinite alternate;
z-index: 0;
}
@keyframes twinkle { 0% { opacity: 0.5; } 100% { opacity: 1; } }
.settings-container {
position: relative; z-index: 1; width: 480px; padding: 40px;
background: rgba(10, 10, 26, 0.9);
border-radius: 12px;
border: 1px solid rgba(100, 100, 255, 0.3);
box-shadow: 0 0 20px rgba(68, 136, 255, 0.2), 0 0 60px rgba(68, 136, 255, 0.1), inset 0 0 20px rgba(68, 136, 255, 0.05);
backdrop-filter: blur(10px);
}
.settings-title {
text-align: center; font-size: 26px; margin-bottom: 25px;
color: #4488ff; text-shadow: 0 0 10px rgba(68, 136, 255, 0.5);
letter-spacing: 2px;
}
.section-title {
font-size: 16px; color: #8888cc; margin: 20px 0 15px;
border-bottom: 1px solid rgba(100, 100, 255, 0.2);
padding-bottom: 6px; letter-spacing: 1px;
}
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; color: #aaaacc; font-size: 14px; }
.form-group input {
width: 100%; padding: 10px 14px;
background: rgba(20, 20, 40, 0.8);
border: 1px solid rgba(100, 100, 255, 0.3);
border-radius: 6px; color: #ffffff;
font-family: 'Courier New', monospace; font-size: 14px;
transition: all 0.3s;
}
.form-group input:focus {
outline: none; border-color: #4488ff; box-shadow: 0 0 10px rgba(68, 136, 255, 0.3);
}
.form-group input::placeholder { color: #555577; }
.toggle-row {
display: flex; align-items: center; justify-content: space-between;
padding: 12px 0; border-bottom: 1px solid rgba(100, 100, 255, 0.1);
}
.toggle-label { color: #ccccee; font-size: 14px; }
.toggle-desc { color: #7777aa; font-size: 12px; margin-top: 2px; }
.toggle-switch {
position: relative; width: 48px; height: 26px; cursor: pointer; flex-shrink: 0;
}
.toggle-switch input { display: none; }
.toggle-slider {
position: absolute; inset: 0;
background: rgba(60, 60, 80, 0.8);
border-radius: 13px; transition: all 0.3s;
border: 1px solid rgba(100, 100, 255, 0.2);
}
.toggle-slider::after {
content: ''; position: absolute; width: 20px; height: 20px;
left: 2px; bottom: 2px; background: #6666aa;
border-radius: 50%; transition: all 0.3s;
}
.toggle-switch input:checked + .toggle-slider {
background: rgba(68, 136, 255, 0.4);
border-color: rgba(68, 136, 255, 0.6);
}
.toggle-switch input:checked + .toggle-slider::after {
left: 24px; background: #4488ff;
}
.btn {
width: 100%; padding: 12px;
background: linear-gradient(135deg, rgba(68, 136, 255, 0.3), rgba(68, 136, 255, 0.1));
border: 1px solid rgba(68, 136, 255, 0.5);
border-radius: 6px; color: #ffffff;
font-family: 'Courier New', monospace; font-size: 14px;
cursor: pointer; transition: all 0.3s; margin-top: 8px;
}
.btn:hover {
background: linear-gradient(135deg, rgba(68, 136, 255, 0.5), rgba(68, 136, 255, 0.3));
box-shadow: 0 0 15px rgba(68, 136, 255, 0.4);
}
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.back-link {
display: block; text-align: center; margin-top: 20px; color: #4488ff;
text-decoration: none; cursor: pointer; font-size: 13px; transition: color 0.3s;
}
.back-link:hover { color: #66aaff; }
.logout-link {
display: block; text-align: center; margin-top: 8px; color: #ff4488;
text-decoration: none; cursor: pointer; font-size: 13px; transition: color 0.3s;
}
.logout-link:hover { color: #ff66aa; }
.success-msg, .error-msg {
display: none; margin-top: 12px; padding: 10px;
border-radius: 6px; font-size: 13px; text-align: center;
}
.success-msg { background: rgba(68, 255, 136, 0.15); border: 1px solid rgba(68, 255, 136, 0.4); color: #6bff9b; }
.error-msg { background: rgba(255, 68, 68, 0.15); border: 1px solid rgba(255, 68, 68, 0.4); color: #ff6b6b; }
.success-msg.show, .error-msg.show { display: block; }
.tui-status {
text-align: center; font-size: 12px; color: #666688; margin-top: 5px;
}
/* 用户管理样式 */
.user-section { margin-top: 20px; }
.user-table {
width: 100%; border-collapse: collapse; margin-top: 10px;
font-size: 13px;
}
.user-table th {
text-align: left; padding: 8px; color: #8888cc;
border-bottom: 1px solid rgba(100, 100, 255, 0.2);
}
.user-table td {
padding: 8px; border-bottom: 1px solid rgba(100, 100, 255, 0.1);
color: #ccccee;
}
.btn-small {
padding: 4px 10px; font-size: 12px;
background: rgba(255, 68, 68, 0.2);
border: 1px solid rgba(255, 68, 68, 0.4);
border-radius: 4px; color: #ff6b6b;
cursor: pointer; transition: all 0.3s;
}
.btn-small:hover {
background: rgba(255, 68, 68, 0.4);
}
.btn-small:disabled {
opacity: 0.4; cursor: not-allowed;
}
.btn-add {
margin-top: 10px; padding: 8px 16px;
background: rgba(68, 136, 255, 0.2);
border: 1px solid rgba(68, 136, 255, 0.4);
border-radius: 6px; color: #4488ff;
cursor: pointer; font-size: 13px; transition: all 0.3s;
}
.btn-add:hover {
background: rgba(68, 136, 255, 0.4);
}
/* 弹窗样式 */
.modal-overlay {
display: none; position: fixed; top: 0; left: 0;
width: 100%; height: 100%; background: rgba(0, 0, 0, 0.7);
z-index: 1000; align-items: center; justify-content: center;
}
.modal-overlay.show { display: flex; }
.modal {
background: rgba(10, 10, 26, 0.95);
border: 1px solid rgba(100, 100, 255, 0.3);
border-radius: 12px; padding: 30px; width: 400px;
}
.modal-title {
font-size: 18px; color: #4488ff; margin-bottom: 20px;
text-align: center;
}
.modal .form-group { margin-bottom: 15px; }
.modal .btn {
margin-top: 15px;
}
.modal .btn-cancel {
background: rgba(100, 100, 100, 0.2);
border-color: rgba(100, 100, 100, 0.4);
margin-top: 10px;
}
.modal .btn-cancel:hover {
background: rgba(100, 100, 100, 0.4);
}
.current-user {
color: #4488ff; font-weight: bold;
}
</style>
</head>
<body>
<div class="settings-container">
<h1 class="settings-title"><i class="fas fa-cog"></i> 设置</h1>
<!-- API 配置 -->
<div class="section-title"><i class="fas fa-plug"></i> API 配置</div>
<form id="apiConfigForm">
<div class="form-group">
<label for="settings_api_key">API Key</label>
<input type="password" id="settings_api_key" placeholder="输入 API Key">
</div>
<div class="form-group">
<label for="settings_base_url">Base URL</label>
<input type="text" id="settings_base_url" placeholder="https://api.deepseek.com">
</div>
<div class="form-group">
<label for="settings_model">Model</label>
<input type="text" id="settings_model" placeholder="deepseek-chat">
</div>
<div class="form-group">
<label for="settings_timeout">消息超时(秒)</label>
<input type="number" id="settings_timeout" placeholder="1200" min="60" step="60">
</div>
<button type="submit" class="btn" id="saveApiConfigBtn">保 存 配 置</button>
</form>
<div id="apiConfigStatus" style="text-align:center;font-size:12px;color:#666688;margin-top:5px;"></div>
<!-- 工具调用限制 -->
<div class="section-title"><i class="fas fa-sliders-h"></i> 工具调用限制</div>
<div class="limits-hint" style="color:#7777aa;font-size:12px;margin-bottom:10px;">AI 每轮对话中各类工具的最大调用次数</div>
<form id="toolLimitsForm">
<div class="form-group">
<label for="settings_persona_update">人设更新 <span style="color:#7777aa;font-size:12px;">(persona_update_max)</span></label>
<input type="number" id="settings_persona_update" min="0" max="100">
</div>
<div class="form-group">
<label for="settings_task_update">任务写入 <span style="color:#7777aa;font-size:12px;">(task_update_max)</span></label>
<input type="number" id="settings_task_update" min="1" max="200">
</div>
<div class="form-group">
<label for="settings_task_query">任务查询 <span style="color:#7777aa;font-size:12px;">(task_query_max)</span></label>
<input type="number" id="settings_task_query" min="1" max="200">
</div>
<div class="form-group">
<label for="settings_memory_query">记忆查询 <span style="color:#7777aa;font-size:12px;">(memory_query_max)</span></label>
<input type="number" id="settings_memory_query" min="1" max="200">
</div>
<div class="form-group">
<label for="settings_memory_update">记忆写入 <span style="color:#7777aa;font-size:12px;">(memory_update_max)</span></label>
<input type="number" id="settings_memory_update" min="1" max="200">
</div>
<button type="submit" class="btn" id="saveLimitsBtn">保 存 限 制</button>
</form>
<div id="limitsStatus" style="text-align:center;font-size:12px;color:#666688;margin-top:5px;"></div>
<!-- 修改密码 -->
<div class="section-title"><i class="fas fa-key"></i> 修改密码</div>
<form id="passwordForm">
<div class="form-group">
<label for="current_password">当前密码</label>
<input type="password" id="current_password" placeholder="输入当前密码" required>
</div>
<div class="form-group">
<label for="new_password">新密码</label>
<input type="password" id="new_password" placeholder="至少 6 位" required minlength="6">
</div>
<div class="form-group">
<label for="confirm_password">确认新密码</label>
<input type="password" id="confirm_password" placeholder="再次输入新密码" required>
</div>
<button type="submit" class="btn" id="changePwdBtn">更 新 密 码</button>
</form>
<!-- HTTPS 配置 -->
<div class="section-title"><i class="fas fa-lock"></i> HTTPS / SSL</div>
<div class="toggle-row">
<div>
<div class="toggle-label">启用 HTTPS</div>
<div class="toggle-desc">开启后使用 HTTPS 协议,需提供有效的证书和密钥文件</div>
</div>
<label class="toggle-switch">
<input type="checkbox" id="enableSslToggle">
<span class="toggle-slider"></span>
</label>
</div>
<div id="sslCertFields" style="margin-top:12px;">
<div class="form-group">
<label for="ssl_cert_path">SSL 证书路径</label>
<input type="text" id="ssl_cert_path" placeholder="/etc/letsencrypt/live/example.com/fullchain.pem">
</div>
<div class="form-group">
<label for="ssl_key_path">SSL 密钥路径</label>
<input type="text" id="ssl_key_path" placeholder="/etc/letsencrypt/live/example.com/privkey.pem">
</div>
<button class="btn" id="saveSslConfigBtn">保 存 HTTPS 配 置</button>
<div id="sslStatus" style="text-align:center;font-size:12px;color:#666688;margin-top:5px;"></div>
<div style="text-align:center;font-size:11px;color:#555577;margin-top:5px;">
<i class="fas fa-info-circle"></i> 修改后需重启服务生效:<code>systemctl restart trulymem-web</code>
</div>
</div>
<!-- TUI 服务控制 -->
<div class="section-title"><i class="fas fa-terminal"></i> TUI 终端服务</div>
<div class="toggle-row">
<div>
<div class="toggle-label">启用终端 TUI 服务</div>
<div class="toggle-desc">控制终端文本界面是否允许连接</div>
</div>
<label class="toggle-switch">
<input type="checkbox" id="enableTuiToggle">
<span class="toggle-slider"></span>
</label>
</div>
<div class="tui-status" id="tuiStatus">状态加载中...</div>
<!-- 保存结果提示 -->
<div class="success-msg" id="successMsg"></div>
<div class="error-msg" id="errorMsg"></div>
<!-- 用户管理(仅管理员可见) -->
<div id="adminSection" style="display:none;">
<div class="section-title"><i class="fas fa-users"></i> 用户管理</div>
<div class="user-section">
<table class="user-table" id="userTable">
<thead>
<tr>
<th>用户名</th>
<th>角色</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody id="userTableBody">
<!-- 用户列表将在这里动态生成 -->
</tbody>
</table>
<button class="btn-add" id="addUserBtn">+ 添加用户</button>
</div>
</div>
<a href="/graph.html" class="back-link"><i class="fas fa-arrow-left"></i> 返回星图</a>
<a href="#" class="logout-link" onclick="event.preventDefault(); fetch('/api/logout', {method: 'POST'}).then(() => window.location.href='/login');"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
</div>
<!-- 添加用户弹窗 -->
<div class="modal-overlay" id="addUserModal">
<div class="modal">
<div class="modal-title">添加用户</div>
<form id="addUserForm">
<div class="form-group">
<label for="new_username">用户名</label>
<input type="text" id="new_username" placeholder="输入新用户名" required>
</div>
<div class="form-group">
<label for="new_user_password">密码</label>
<input type="password" id="new_user_password" placeholder="至少 6 位" required minlength="6">
</div>
<button type="submit" class="btn" id="confirmAddBtn">添加</button>
<button type="button" class="btn btn-cancel" id="cancelAddBtn">取消</button>
</form>
<div class="success-msg" id="addUserSuccess"></div>
<div class="error-msg" id="addUserError"></div>
</div>
</div>
<script>
let currentUser = '';
let isAdmin = false;
// 初始化:获取用户信息和配置
async function init() {
// 获取当前用户信息(含角色)
try {
const resp = await fetch('/api/userinfo');
const data = await resp.json();
if (data.success) {
currentUser = data.username;
isAdmin = data.is_admin;
// 仅管理员显示用户管理区域
if (isAdmin) {
document.getElementById('adminSection').style.display = 'block';
loadUsers();
}
}
} catch (e) {
console.error('获取用户信息失败:', e);
}
// 加载配置
loadConfig();
}
// 加载当前配置
async function loadConfig() {
try {
const resp = await fetch('/api/settings/config');
const data = await resp.json();
if (data.success) {
document.getElementById('enableTuiToggle').checked = data.enable_tui !== false;
document.getElementById('tuiStatus').innerHTML =
data.enable_tui ? '<i class="fas fa-check-circle" style="color:var(--success-color,#22c55e)"></i> TUI 服务已启用' : '<i class="fas fa-stop-circle" style="color:var(--warning-color,#f59e0b)"></i> TUI 服务已禁用';
}
} catch (e) {
document.getElementById('tuiStatus').innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 无法加载配置';
}
}
// 加载 SSL 配置
try {
const sslResp = await fetch('/api/ssl/config');
const sslData = await sslResp.json();
if (sslData.success) {
document.getElementById('enableSslToggle').checked = sslData.ssl_enabled === true;
document.getElementById('ssl_cert_path').value = sslData.ssl_cert_path || '';
document.getElementById('ssl_key_path').value = sslData.ssl_key_path || '';
toggleSslFields(sslData.ssl_enabled === true);
}
} catch (e) {
// SSL 端点可能不支持
}
}
// 加载用户列表
async function loadUsers() {
try {
const resp = await fetch('/api/admin/users');
const data = await resp.json();
if (data.success) {
const tbody = document.getElementById('userTableBody');
tbody.innerHTML = '';
data.users.forEach(user => {
const tr = document.createElement('tr');
const isCurrentUser = user.username === currentUser;
const roleBadge = user.role === 'admin'
? '<span style="color:#ffaa44;font-weight:bold;">管理员</span>'
: '<span style="color:#8888cc;">用户</span>';
tr.innerHTML = `
<td class="${isCurrentUser ? 'current-user' : ''}">
${user.username} ${isCurrentUser ? '(当前)' : ''}
</td>
<td>${roleBadge}</td>
<td>${new Date(user.created_at).toLocaleString('zh-CN')}</td>
<td>
<button class="btn-small"
onclick="deleteUser(${user.id}, '${user.username}')"
${isCurrentUser ? 'disabled' : ''}>
删除
</button>
</td>
`;
tbody.appendChild(tr);
});
}
} catch (e) {
console.error('加载用户列表失败:', e);
}
}
// 删除用户
async function deleteUser(userId, username) {
if (!confirm(`确定要删除用户 "${username}" 吗?`)) {
return;
}
try {
const resp = await fetch(`/api/admin/users/${userId}`, {
method: 'DELETE'
});
const data = await resp.json();
if (data.success) {
showSuccess('用户已删除');
loadUsers();
} else {
showError(data.error || '删除失败');
}
} catch (e) {
showError('网络错误');
}
}
// 显示添加用户弹窗
document.getElementById('addUserBtn').addEventListener('click', () => {
document.getElementById('addUserModal').classList.add('show');
hideAddUserMessages();
});
// 隐藏弹窗
document.getElementById('cancelAddBtn').addEventListener('click', () => {
document.getElementById('addUserModal').classList.remove('show');
document.getElementById('addUserForm').reset();
});
// 添加用户
document.getElementById('addUserForm').addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('new_username').value.trim();
const password = document.getElementById('new_user_password').value;
if (!username || !password) {
showAddUserError('用户名和密码不能为空');
return;
}
if (password.length < 6) {
showAddUserError('密码长度至少 6 位');
return;
}
const btn = document.getElementById('confirmAddBtn');
btn.disabled = true;
btn.textContent = '添加中...';
hideAddUserMessages();
try {
const resp = await fetch('/api/admin/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const data = await resp.json();
if (data.success) {
showAddUserSuccess('<i class="fas fa-check-circle" style="color:var(--success-color,#22c55e)"></i> 用户添加成功');
setTimeout(() => {
document.getElementById('addUserModal').classList.remove('show');
document.getElementById('addUserForm').reset();
loadUsers();
}, 1500);
} else {
showAddUserError(data.error || '添加失败');
}
} catch (e) {
showAddUserError('网络错误');
} finally {
btn.disabled = false;
btn.textContent = '添加';
}
});
function showAddUserSuccess(msg) {
const el = document.getElementById('addUserSuccess');
el.innerHTML = msg;
el.classList.add('show');
document.getElementById('addUserError').classList.remove('show');
}
function showAddUserError(msg) {
const el = document.getElementById('addUserError');
el.textContent = msg;
el.classList.add('show');
document.getElementById('addUserSuccess').classList.remove('show');
}
function hideAddUserMessages() {
document.getElementById('addUserSuccess').classList.remove('show');
document.getElementById('addUserError').classList.remove('show');
}
init();
// API 配置加载与保存
async function loadApiConfig() {
try {
const resp = await fetch('/api/settings');
const data = await resp.json();
if (data.success && data.data) {
const cfg = data.data.api_config || {};
const limits = data.data.tool_limits || {};
// API 配置
document.getElementById('settings_api_key').value = cfg.api_key || '';
document.getElementById('settings_base_url').value = cfg.base_url || 'https://api.deepseek.com';
document.getElementById('settings_model').value = cfg.model || 'deepseek-chat';
document.getElementById('settings_timeout').value = cfg.message_timeout || 1200;
// 工具限制
document.getElementById('settings_persona_update').value = limits.persona_update_max || 1;
document.getElementById('settings_task_update').value = limits.task_update_max || 20;
document.getElementById('settings_task_query').value = limits.task_query_max || 30;
document.getElementById('settings_memory_query').value = limits.memory_query_max || 30;
document.getElementById('settings_memory_update').value = limits.memory_update_max || 15;
document.getElementById('apiConfigStatus').innerHTML = '<i class="fas fa-check-circle" style="color:var(--success-color,#22c55e)"></i> 配置已加载';
}
} catch (e) {
document.getElementById('apiConfigStatus').innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 无法加载配置';
}
}
// 保存 API 配置(含超时)
document.getElementById('apiConfigForm').addEventListener('submit', async (e) => {
e.preventDefault();
const btn = document.getElementById('saveApiConfigBtn');
btn.disabled = true;
btn.textContent = '保存中...';
const statusEl = document.getElementById('apiConfigStatus');
statusEl.innerHTML = '<i class="fas fa-spinner fa-spin" style="color:var(--primary-color,#4488ff)"></i> 保存中...';
try {
const resp = await fetch('/api/settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
api_config: {
api_key: document.getElementById('settings_api_key').value,
base_url: document.getElementById('settings_base_url').value,
model: document.getElementById('settings_model').value,
message_timeout: parseInt(document.getElementById('settings_timeout').value) || 1200
}
})
});
const data = await resp.json();
if (data.success) {
statusEl.innerHTML = '<i class="fas fa-check-circle" style="color:var(--success-color,#22c55e)"></i> API 配置已保存并生效';
setTimeout(() => { statusEl.textContent = ''; }, 3000);
} else {
statusEl.innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 保存失败: ' + (data.error || '未知错误');
}
} catch (e) {
statusEl.innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 网络错误';
} finally {
btn.disabled = false;
btn.textContent = '保 存 配 置';
}
});
// 保存工具调用限制
document.getElementById('toolLimitsForm').addEventListener('submit', async (e) => {
e.preventDefault();
const btn = document.getElementById('saveLimitsBtn');
btn.disabled = true;
btn.textContent = '保存中...';
const statusEl = document.getElementById('limitsStatus');
statusEl.innerHTML = '<i class="fas fa-spinner fa-spin" style="color:var(--primary-color,#4488ff)"></i> 保存中...';
try {
const resp = await fetch('/api/settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tool_limits: {
persona_update_max: parseInt(document.getElementById('settings_persona_update').value) || 1,
task_update_max: parseInt(document.getElementById('settings_task_update').value) || 20,
task_query_max: parseInt(document.getElementById('settings_task_query').value) || 30,
memory_query_max: parseInt(document.getElementById('settings_memory_query').value) || 30,
memory_update_max: parseInt(document.getElementById('settings_memory_update').value) || 15
}
})
});
const data = await resp.json();
if (data.success) {
statusEl.innerHTML = '<i class="fas fa-check-circle" style="color:var(--success-color,#22c55e)"></i> 工具限制已保存并生效';
setTimeout(() => { statusEl.textContent = ''; }, 3000);
} else {
statusEl.innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 保存失败: ' + (data.error || '未知错误');
}
} catch (e) {
statusEl.innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 网络错误';
} finally {
btn.disabled = false;
btn.textContent = '保 存 限 制';
}
});
// 补充 loadConfig 中调用 loadApiConfig
const origLoadConfig = loadConfig;
loadConfig = function() {
origLoadConfig();
loadApiConfig();
};
// SSL 开关 — 显示/隐藏证书路径
const sslToggle = document.getElementById('enableSslToggle');
const sslCertFields = document.getElementById('sslCertFields');
function toggleSslFields(enabled) {
sslCertFields.style.display = enabled ? '' : 'none';
}
sslToggle.addEventListener('change', function() {
toggleSslFields(this.checked);
});
// 保存 SSL 配置
document.getElementById('saveSslConfigBtn').addEventListener('click', async function() {
const enabled = sslToggle.checked;
const certPath = document.getElementById('ssl_cert_path').value.trim();
const keyPath = document.getElementById('ssl_key_path').value.trim();
if (enabled && (!certPath || !keyPath)) {
document.getElementById('sslStatus').innerHTML = '<i class="fas fa-exclamation-triangle" style="color:#ff6b6b"></i> 启用 HTTPS 必须填写证书和密钥路径';
return;
}
const btn = this;
btn.disabled = true;
btn.textContent = '保存中...';
document.getElementById('sslStatus').innerHTML = '<i class="fas fa-spinner fa-spin" style="color:#4488ff"></i> 保存中...';
try {
const resp = await fetch('/api/ssl/config', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
ssl_enabled: enabled,
ssl_cert_path: certPath,
ssl_key_path: keyPath
})
});
const data = await resp.json();
if (data.success) {
document.getElementById('sslStatus').innerHTML = '<i class="fas fa-check-circle" style="color:#22c55e"></i> ' + (data.message || '配置已保存');
showSuccess('HTTPS 配置已保存');
} else {
document.getElementById('sslStatus').innerHTML = '<i class="fas fa-exclamation-triangle" style="color:#ff6b6b"></i> ' + (data.error || '保存失败');
}
} catch (e) {
document.getElementById('sslStatus').innerHTML = '<i class="fas fa-exclamation-triangle" style="color:#ff6b6b"></i> 网络错误';
} finally {
btn.disabled = false;
btn.textContent = '保 存 HTTPS 配 置';
}
});
// TUI 开关
document.getElementById('enableTuiToggle').addEventListener('change', async function() {
const enable = this.checked;
document.getElementById('tuiStatus').innerHTML = '<i class="fas fa-spinner fa-spin" style="color:var(--primary-color,#4488ff)"></i> 更新中...';
try {
const resp = await fetch('/api/settings/config', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enable_tui: enable })
});
const data = await resp.json();
if (data.success) {
document.getElementById('tuiStatus').innerHTML =
enable ? '<i class="fas fa-check-circle" style="color:var(--success-color,#22c55e)"></i> TUI 服务已启用' : '<i class="fas fa-stop-circle" style="color:var(--warning-color,#f59e0b)"></i> TUI 服务已禁用';
showSuccess('设置已保存');
} else {
document.getElementById('tuiStatus').innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 更新失败';
this.checked = !enable;
}
} catch (e) {
document.getElementById('tuiStatus').innerHTML = '<i class="fas fa-exclamation-triangle" style="color:var(--warning-color,#f59e0b)"></i> 网络错误';
this.checked = !enable;
}
});
// 修改密码
document.getElementById('passwordForm').addEventListener('submit', async (e) => {
e.preventDefault();
const current = document.getElementById('current_password').value;
const newPwd = document.getElementById('new_password').value;
const confirm = document.getElementById('confirm_password').value;
if (newPwd !== confirm) {
showError('两次密码输入不一致');
return;
}
const btn = document.getElementById('changePwdBtn');
btn.disabled = true;
btn.textContent = '更新中...';
hideMessages();
try {
const resp = await fetch('/api/change-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
current_password: current,
new_password: newPwd,
confirm_password: confirm
})
});
const data = await resp.json();
if (data.success) {
showSuccess('<i class="fas fa-check-circle" style="color:var(--success-color,#22c55e)"></i> 密码已更新');
document.getElementById('current_password').value = '';
document.getElementById('new_password').value = '';
document.getElementById('confirm_password').value = '';
} else {
showError(data.error || '修改失败');
}
} catch (e) {
showError('网络错误');
} finally {
btn.disabled = false;
btn.textContent = '更 新 密 码';
}
});
function showSuccess(msg) {
const el = document.getElementById('successMsg');
el.innerHTML = msg;
el.classList.add('show');
document.getElementById('errorMsg').classList.remove('show');
setTimeout(() => el.classList.remove('show'), 3000);
}
function showError(msg) {
const el = document.getElementById('errorMsg');
el.textContent = msg;
el.classList.add('show');
document.getElementById('successMsg').classList.remove('show');
}
function hideMessages() {
document.getElementById('successMsg').classList.remove('show');
document.getElementById('errorMsg').classList.remove('show');
}
</script>
</body>
</html>