mirror of
https://gitcode.com/JianFeeeee/TrulyMEM-TrueHumanMEM.git
synced 2026-10-04 00:14:26 +00:00
多用户系统 + Admin 角色权限 + Web/TUI 同步 + 构建集成
本轮实现功能: 1. 多用户隔离:每个用户独立 config.json + graph.db 2. TUI 登录页 + 旧版自动迁移(core/migrate.py) 3. Admin/User 角色体系(core/embedded_db.py) 4. Web 后台管理 API(userinfo + admin CRUD) 5. Web 设置页用户管理区(仅 admin 可见) 6. TUI 侧栏配置区权限同步(非 admin 隐藏 Web 服务设置) 7. Web 服务打包为独立二进制(trulymem-web) 8. 双入口 PyInstaller 构建脚本(TUI + Web) 9. 活动记录器(core/activity_recorder.py) 10. 静态页面模板(登录/设置/首次引导)
This commit is contained in:
544
templates/settings.html
Normal file
544
templates/settings.html
Normal file
@ -0,0 +1,544 @@
|
||||
<!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>
|
||||
<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: #6666aa;
|
||||
text-decoration: none; font-size: 13px; transition: color 0.3s;
|
||||
}
|
||||
.back-link:hover { color: #4488ff; }
|
||||
.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">⚙ 设置</h1>
|
||||
|
||||
<!-- 修改密码 -->
|
||||
<div class="section-title">🔑 修改密码</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>
|
||||
|
||||
<!-- TUI 服务控制 -->
|
||||
<div class="section-title">🖥 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">👥 用户管理</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">← 返回星图</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').textContent =
|
||||
data.enable_tui ? '✅ TUI 服务已启用' : '⏹️ TUI 服务已禁用';
|
||||
}
|
||||
} catch (e) {
|
||||
document.getElementById('tuiStatus').textContent = '⚠️ 无法加载配置';
|
||||
}
|
||||
}
|
||||
|
||||
// 加载用户列表
|
||||
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('✅ 用户添加成功');
|
||||
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.textContent = 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();
|
||||
|
||||
// TUI 开关
|
||||
document.getElementById('enableTuiToggle').addEventListener('change', async function() {
|
||||
const enable = this.checked;
|
||||
document.getElementById('tuiStatus').textContent = '🔄 更新中...';
|
||||
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').textContent =
|
||||
enable ? '✅ TUI 服务已启用' : '⏹️ TUI 服务已禁用';
|
||||
showSuccess('设置已保存');
|
||||
} else {
|
||||
document.getElementById('tuiStatus').textContent = '⚠️ 更新失败';
|
||||
this.checked = !enable;
|
||||
}
|
||||
} catch (e) {
|
||||
document.getElementById('tuiStatus').textContent = '⚠️ 网络错误';
|
||||
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('✅ 密码已更新');
|
||||
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.textContent = 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>
|
||||
Reference in New Issue
Block a user