Initial commit: ToNav Personal Navigation Page

- Flask + SQLite 个人导航页系统
- 前台导航页(分类Tab、卡片展示)
- 管理后台(服务管理、分类管理、健康检测)
- 响应式设计
- Systemd 服务配置
This commit is contained in:
OpenClaw Agent
2026-02-12 21:57:15 +08:00
commit 872526505e
22 changed files with 3424 additions and 0 deletions

View File

@@ -0,0 +1,479 @@
{% extends "base.html" %}
{% block title %}分类管理 - ToNav{% endblock %}
{% block content %}
<div class="container admin-layout">
<!-- 顶部导航 -->
<div class="admin-header">
<div class="header-left">
<h1>📂 分类管理</h1>
<a href="/admin" class="back-link">← 返回首页</a>
</div>
<button class="btn btn-primary" onclick="showCreateModal()">+ 新建分类</button>
</div>
<!-- 分类列表 -->
<div class="categories-list">
<div class="loading">加载中...</div>
</div>
<!-- 创建/编辑弹窗 -->
<div class="modal" id="categoryModal">
<div class="modal-content">
<div class="modal-header">
<h2 id="modalTitle">新建分类</h2>
<button class="close-btn" onclick="closeModal()">×</button>
</div>
<form id="categoryForm" class="modal-body">
<input type="hidden" id="categoryId">
<div class="form-group">
<label>分类名称 *</label>
<input type="text" id="categoryName" class="input" required>
</div>
<div class="form-group">
<label>排序权重</label>
<input type="number" id="categorySort" class="input" value="0">
</div>
<div class="form-actions">
<button type="button" class="btn" onclick="closeModal()">取消</button>
<button type="submit" class="btn btn-primary">保存</button>
</div>
</form>
</div>
</div>
</div>
<style>
:root {
--main-red: #ff4d4f;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
}
.admin-header {
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
color: #fff;
padding: 25px 30px;
border-radius: 20px 20px 0 0;
display: flex;
justify-content: space-between;
align-items: center;
}
.header-left h1 {
font-size: 22px;
font-weight: 700;
}
.back-link {
color: #8c8c8c;
font-size: 13px;
text-decoration: none;
}
.back-link:hover {
color: #bfbfbf;
}
.categories-list {
background: #fff;
padding: 30px;
border-radius: 0 0 20px 20px;
}
.category-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
background: #fafafa;
border-radius: 12px;
margin-bottom: 10px;
transition: all 0.3s;
}
.category-item:hover {
background: #f0f0f0;
transform: translateX(5px);
}
.category-info {
display: flex;
align-items: center;
gap: 15px;
}
.category-icon {
font-size: 24px;
}
.category-name {
font-size: 16px;
font-weight: 600;
color: #262626;
}
.category-meta {
font-size: 12px;
color: #999;
margin-left: 10px;
}
.category-actions {
display: flex;
gap: 8px;
}
.action-btn-sm {
padding: 8px 14px;
border: 1px solid #d9d9d9;
background: #fff;
border-radius: 8px;
font-size: 13px;
cursor: pointer;
transition: all 0.3s;
}
.action-btn-sm:hover {
border-color: var(--main-red);
color: var(--main-red);
}
.action-btn-sm.danger:hover {
background: #fff2f0;
border-color: #ff4d4f;
color: #ff4d4f;
}
/* Modal */
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.6);
z-index: 1000;
align-items: center;
justify-content: center;
animation: fadeIn 0.3s ease;
}
.modal.active {
display: flex;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.modal-content {
background: #fff;
border-radius: 20px;
max-width: 500px;
width: 90%;
animation: slideUp 0.3s ease;
}
@keyframes slideUp {
from { transform: translateY(50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
.modal-header {
padding: 20px 25px;
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
color: #fff;
display: flex;
justify-content: space-between;
align-items: center;
border-radius: 20px 20px 0 0;
}
.modal-header h2 {
font-size: 18px;
font-weight: 600;
}
.close-btn {
background: none;
border: none;
color: #fff;
font-size: 28px;
cursor: pointer;
padding: 0;
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
}
.close-btn:hover {
background: rgba(255,255,255,0.2);
}
.modal-body {
padding: 25px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-size: 14px;
font-weight: 500;
color: #262626;
}
.input {
width: 100%;
padding: 10px 12px;
border: 1px solid #d9d9d9;
border-radius: 8px;
font-size: 14px;
transition: all 0.3s;
}
.input:focus {
outline: none;
border-color: var(--main-red);
box-shadow: 0 0 0 3px rgba(255, 77, 79, 0.1);
}
.form-actions {
display: flex;
justify-content: flex-end;
gap: 10px;
}
.btn {
padding: 10px 20px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: var(--main-red);
color: #fff;
box-shadow: 0 4px 15px rgba(255, 77, 79, 0.4);
}
.btn-primary:hover {
background: #ff7875;
}
.loading {
text-align: center;
padding: 40px;
color: #999;
}
.empty-state {
text-align: center;
padding: 40px;
color: #999;
}
</style>
{% endblock %}
{% block scripts %}
<script>
let allCategories = [];
let allServices = [];
// 初始化
document.addEventListener('DOMContentLoaded', function() {
loadData();
setupForm();
});
// 加载数据
async function loadData() {
try {
const [catResp, svcResp] = await Promise.all([
fetch('/api/admin/categories'),
fetch('/api/admin/services')
]);
if (!catResp.ok) {
window.location.href = '/admin/login';
return;
}
allCategories = await catResp.json();
allServices = await svcResp.json();
renderCategories();
} catch (err) {
console.error('加载失败:', err);
window.location.href = '/admin/login';
}
}
// 渲染分类列表
function renderCategories() {
const container = document.querySelector('.categories-list');
if (allCategories.length === 0) {
container.innerHTML = '<div class="empty-state">暂无分类</div>';
return;
}
let html = '';
allCategories.forEach(category => {
const serviceCount = allServices.filter(s => s.category === category.name).length;
html += `
<div class="category-item">
<div class="category-info">
<span class="category-icon">📂</span>
<div>
<span class="category-name">${category.name}</span>
<span class="category-meta">${serviceCount} 个服务 · 排序 ${category.sort_order}</span>
</div>
</div>
<div class="category-actions">
<button class="action-btn-sm" onclick="editCategory(${category.id})">编辑</button>
<button class="action-btn-sm danger" onclick="deleteCategory(${category.id}, '${category.name}')">删除</button>
</div>
</div>
`;
});
container.innerHTML = html;
}
// 设置表单
function setupForm() {
document.getElementById('categoryForm').addEventListener('submit', function(e) {
e.preventDefault();
saveCategory();
});
}
// 显示创建弹窗
function showCreateModal() {
document.getElementById('modalTitle').textContent = '新建分类';
document.getElementById('categoryId').value = '';
document.getElementById('categoryForm').reset();
document.getElementById('categorySort').value = '0';
document.getElementById('categoryModal').classList.add('active');
}
// 编辑分类
function editCategory(id) {
const category = allCategories.find(c => c.id === id);
if (!category) return;
document.getElementById('modalTitle').textContent = '编辑分类';
document.getElementById('categoryId').value = category.id;
document.getElementById('categoryName').value = category.name;
document.getElementById('categorySort').value = category.sort_order;
document.getElementById('categoryModal').classList.add('active');
}
// 保存分类
async function saveCategory() {
const id = document.getElementById('categoryId').value;
const data = {
name: document.getElementById('categoryName').value.trim(),
sort_order: parseInt(document.getElementById('categorySort').value) || 0
};
if (!data.name) {
alert('分类名称不能为空');
return;
}
try {
const url = id ? `/api/admin/categories/${id}` : '/api/admin/categories';
const method = id ? 'PUT' : 'POST';
const response = await fetch(url, {
method: method,
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
});
if (response.ok) {
closeModal();
loadData();
} else {
const result = await response.json();
alert(result.error || '保存失败');
}
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 删除分类
async function deleteCategory(id, name) {
const serviceCount = allServices.filter(s => s.category === name).length;
if (serviceCount > 0) {
alert(`该分类下有 ${serviceCount} 个服务,无法删除`);
return;
}
if (!confirm(`确定要删除分类"${name}"吗?此操作不可恢复。`)) return;
try {
const response = await fetch(`/api/admin/categories/${id}`, {
method: 'DELETE'
});
if (response.ok) {
loadData();
} else {
const result = await response.json();
alert(result.error || '删除失败');
}
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 关闭弹窗
function closeModal() {
document.getElementById('categoryModal').classList.remove('active');
}
// 点击外部关闭弹窗
document.getElementById('categoryModal').addEventListener('click', function(e) {
if (e.target === this) {
closeModal();
}
});
</script>
{% endblock %}

View File

@@ -0,0 +1,372 @@
{% extends "base.html" %}
{% block title %}ToNav 管理后台{% endblock %}
{% block content %}
<div class="container admin-layout">
<!-- 顶部导航 -->
<div class="admin-header">
<div class="header-left">
<h1>🧭 ToNav 管理后台</h1>
<span class="username" id="username">加载中...</span>
</div>
<button class="btn btn-primary" onclick="location.href='/admin/logout'">退出登录</button>
</div>
<!-- 主内容区 -->
<div class="main-content">
<!-- 统计卡片 -->
<div class="stats-grid">
<div class="stat-card">
<div class="stat-icon">📡</div>
<div class="stat-info">
<div class="stat-value" id="totalServices">0</div>
<div class="stat-label">总服务数</div>
</div>
</div>
<div class="stat-card">
<div class="stat-icon"></div>
<div class="stat-info">
<div class="stat-value" id="enabledServices">0</div>
<div class="stat-label">已启用</div>
</div>
</div>
<div class="stat-card">
<div class="stat-icon">📂</div>
<div class="stat-info">
<div class="stat-value" id="totalCategories">0</div>
<div class="stat-label">分类数</div>
</div>
</div>
</div>
<!-- 快捷操作 -->
<div class="quick-actions">
<button class="action-btn" onclick="location.href='/admin/services'">
<span class="action-icon">📡</span>
<span class="action-label">服务管理</span>
</button>
<button class="action-btn" onclick="location.href='/admin/categories'">
<span class="action-icon">📂</span>
<span class="action-label">分类管理</span>
</button>
<button class="action-btn" onclick="runHealthCheck()">
<span class="action-icon">🔍</span>
<span class="action-label">健康检测</span>
</button>
</div>
</div>
<!-- 修改密码 -->
<div class="settings-card">
<h2>修改密码</h2>
<form id="changePasswordForm">
<div class="form-row">
<input type="password" id="oldPassword" class="input" placeholder="旧密码" required>
<input type="password" id="newPassword" class="input" placeholder="新密码至少6位" required minlength="6">
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">修改密码</button>
</div>
</form>
</div>
</div>
<style>
.admin-layout {
max-width: 900px;
}
.admin-header {
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
color: #fff;
padding: 25px 30px;
border-radius: 20px 20px 0 0;
display: flex;
justify-content: space-between;
align-items: center;
}
.header-left h1 {
font-size: 22px;
font-weight: 700;
margin-bottom: 5px;
}
.username {
font-size: 13px;
color: #8c8c8c;
}
.admin-content {
background: #fff;
padding: 30px;
}
.main-content {
padding: 30px;
background: #fff;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
margin-bottom: 20px;
}
.stat-card {
display: flex;
align-items: center;
gap: 15px;
padding: 20px;
background: #fafafa;
border-radius: 15px;
transition: all 0.3s;
height: 100px;
}
.stat-card:hover {
background: #f0f0f0;
transform: translateY(-2px);
}
.stat-icon {
font-size: 36px;
}
.stat-info {
flex: 1;
}
.stat-value {
font-size: 32px;
font-weight: 800;
color: var(--main-red);
line-height: 1;
}
.stat-label {
font-size: 13px;
color: #8c8c8c;
margin-top: 5px;
}
.quick-actions {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
}
.action-btn {
background: #fff;
border: 2px solid #f0f0f0;
border-radius: 15px;
padding: 20px;
cursor: pointer;
transition: all 0.3s;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px;
height: 100px;
}
.action-btn:hover {
border-color: var(--main-red);
background: #fff2f0;
transform: translateY(-3px);
box-shadow: 0 4px 15px rgba(255, 77, 79, 0.2);
}
.action-icon {
font-size: 32px;
}
.action-label {
font-size: 14px;
font-weight: 600;
color: #262626;
}
.settings-card {
background: #fff;
padding: 30px;
border-radius: 0 0 20px 20px;
}
.settings-card h2 {
font-size: 18px;
font-weight: 600;
margin-bottom: 20px;
color: #262626;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
margin-bottom: 15px;
}
.input {
width: 100%;
padding: 12px 15px;
border: 1px solid #d9d9d9;
border-radius: 10px;
font-size: 14px;
transition: all 0.3s;
}
.input:focus {
outline: none;
border-color: var(--main-red);
box-shadow: 0 0 0 3px rgba(255, 77, 79, 0.1);
}
.form-actions {
display: flex;
gap: 10px;
}
.btn {
padding: 12px 24px;
border: none;
border-radius: 10px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: var(--main-red);
color: #fff;
box-shadow: 0 4px 15px rgba(255, 77, 79, 0.4);
}
.btn-primary:hover {
background: #ff7875;
}
@media (max-width: 768px) {
.stats-grid,
.quick-actions,
.form-row {
grid-template-columns: 1fr;
}
.admin-header {
flex-direction: column;
align-items: flex-start;
gap: 15px;
}
.stat-card,
.action-btn {
height: auto;
}
}
</style>
{% endblock %}
{% block scripts %}
<script>
// 初始化
document.addEventListener('DOMContentLoaded', function() {
loadStats();
loadUsername();
});
// 加载用户名
async function loadUsername() {
try {
const response = await fetch('/api/admin/login/status');
const data = await response.json();
if (data.logged_in) {
document.getElementById('username').textContent = data.username;
} else {
window.location.href = '/admin/login';
}
} catch (err) {
window.location.href = '/admin/login';
}
}
// 加载统计数据
async function loadStats() {
try {
const [servicesResp, categoriesResp] = await Promise.all([
fetch('/api/admin/services'),
fetch('/api/admin/categories')
]);
const services = await servicesResp.json();
const categories = await categoriesResp.json();
document.getElementById('totalServices').textContent = services.length;
document.getElementById('enabledServices').textContent =
services.filter(s => s.is_enabled).length;
document.getElementById('totalCategories').textContent = categories.length;
} catch (err) {
console.error('加载统计失败:', err);
}
}
// 健康检测
async function runHealthCheck() {
const btn = event.target.closest('.action-btn');
const icon = btn.querySelector('.action-icon');
const label = btn.querySelector('.action-label');
try {
icon.textContent = '⏳';
label.textContent = '检测中...';
const response = await fetch('/api/admin/health-check', {
method: 'POST'
});
const data = await response.json();
if (data.results) {
const online = data.results.filter(r => r.status === 'online').length;
const offline = data.results.filter(r => r.status === 'offline').length;
alert(`检测完成:\n在线: ${online}\n离线: ${offline}`);
}
} catch (err) {
alert('检测失败: ' + err.message);
} finally {
icon.textContent = '🔍';
label.textContent = '健康检测';
}
}
// 修改密码
document.getElementById('changePasswordForm').addEventListener('submit', async function(e) {
e.preventDefault();
const oldPassword = document.getElementById('oldPassword').value;
const newPassword = document.getElementById('newPassword').value;
try {
const response = await fetch('/api/admin/change-password', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({old_password: oldPassword, new_password: newPassword})
});
const data = await response.json();
if (response.ok) {
alert('密码修改成功,请重新登录');
this.reset();
} else {
alert(data.error || '修改失败');
}
} catch (err) {
alert('请求失败: ' + err.message);
}
});
</script>
{% endblock %}

140
templates/admin/login.html Normal file
View File

@@ -0,0 +1,140 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ToNav 管理后台</title>
<style>
:root {
--main-red: #ff4d4f;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "PingFang SC", -apple-system, BlinkMacSystemFont, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.login-card {
background: #fff;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
max-width: 400px;
width: 90%;
padding: 40px 30px;
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header h1 {
font-size: 28px;
font-weight: 700;
color: #262626;
margin-bottom: 5px;
}
.header p {
font-size: 14px;
color: #8c8c8c;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-size: 14px;
font-weight: 500;
color: #262626;
}
.form-group input {
width: 100%;
padding: 12px 15px;
border: 1px solid #d9d9d9;
border-radius: 10px;
font-size: 14px;
transition: all 0.3s;
}
.form-group input:focus {
outline: none;
border-color: var(--main-red);
box-shadow: 0 0 0 3px rgba(255, 77, 79, 0.1);
}
.btn {
width: 100%;
padding: 12px;
border: none;
border-radius: 10px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: var(--main-red);
color: #fff;
box-shadow: 0 4px 15px rgba(255, 77, 79, 0.4);
}
.btn-primary:hover {
background: #ff7875;
box-shadow: 0 6px 20px rgba(255, 77, 79, 0.5);
}
.error {
background: #fff2f0;
border: 1px solid #ffccc7;
color: #ff4d4f;
padding: 10px 15px;
border-radius: 8px;
font-size: 14px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="login-card">
<div class="header">
<h1>🧭 ToNav</h1>
<p>管理后台登录</p>
</div>
{% if error %}
<div class="error">{{ error }}</div>
{% endif %}
<form method="POST">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" id="username" name="username" required autofocus>
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">登 录</button>
</form>
</div>
</body>
</html>

View File

@@ -0,0 +1,642 @@
{% extends "base.html" %}
{% block title %}服务管理 - ToNav{% endblock %}
{% block content %}
<div class="container admin-layout">
<!-- 顶部导航 -->
<div class="admin-header">
<div class="header-left">
<h1>📡 服务管理</h1>
<a href="/admin" class="back-link">← 返回首页</a>
</div>
<button class="btn btn-primary" onclick="showCreateModal()">+ 新建服务</button>
</div>
<!-- 服务列表 -->
<div class="services-list">
<div class="loading">加载中...</div>
</div>
<!-- 创建/编辑弹窗 -->
<div class="modal" id="serviceModal">
<div class="modal-content">
<div class="modal-header">
<h2 id="modalTitle">新建服务</h2>
<button class="close-btn" onclick="closeModal()">×</button>
</div>
<form id="serviceForm" class="modal-body">
<input type="hidden" id="serviceId">
<div class="form-group">
<label>服务名称 *</label>
<input type="text" id="serviceName" class="input" required>
</div>
<div class="form-group">
<label>访问URL *</label>
<input type="url" id="serviceUrl" class="input" required placeholder="http://">
</div>
<div class="form-group">
<label>描述</label>
<input type="text" id="serviceDesc" class="input" placeholder="简短描述">
</div>
<div class="form-row">
<div class="form-group">
<label>图标emoji</label>
<input type="text" id="serviceIcon" class="input" placeholder="📡">
</div>
<div class="form-group">
<label>分类</label>
<select id="serviceCategory" class="input">
<option value="默认">默认</option>
<option value="内网服务">内网服务</option>
<option value="开发工具">开发工具</option>
<option value="测试环境">测试环境</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group">
<label>排序权重</label>
<input type="number" id="serviceSort" class="input" value="0">
</div>
<div class="form-group">
<label>状态</label>
<label class="checkbox-label">
<input type="checkbox" id="serviceEnabled" checked>
<span>启用</span>
</label>
</div>
</div>
<div class="form-divider">健康检测设置</div>
<div class="form-group">
<label class="checkbox-label">
<input type="checkbox" id="healthCheckEnabled">
<span>启用健康检测</span>
</label>
</div>
<div class="form-group" id="healthUrlGroup" style="display: none;">
<label>检测URL</label>
<input type="url" id="healthCheckUrl" class="input" placeholder="留空则使用主URL">
</div>
<div class="form-actions">
<button type="button" class="btn" onclick="closeModal()">取消</button>
<button type="submit" class="btn btn-primary">保存</button>
</div>
</form>
</div>
</div>
</div>
<style>
:root {
--main-red: #ff4d4f;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1000px;
margin: 0 auto;
}
.admin-header {
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
color: #fff;
padding: 25px 30px;
border-radius: 20px 20px 0 0;
display: flex;
justify-content: space-between;
align-items: center;
}
.header-left h1 {
font-size: 22px;
font-weight: 700;
}
.back-link {
color: #8c8c8c;
font-size: 13px;
text-decoration: none;
}
.back-link:hover {
color: #bfbfbf;
}
.services-list {
background: #fff;
padding: 30px;
border-radius: 0 0 20px 20px;
min-height: 400px;
}
.service-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background: #fafafa;
border-radius: 12px;
margin-bottom: 15px;
transition: all 0.3s;
}
.service-item:hover {
background: #f0f0f0;
transform: translateX(5px);
}
.service-info {
display: flex;
align-items: center;
gap: 15px;
flex: 1;
}
.service-icon-large {
font-size: 36px;
}
.service-details {
flex: 1;
}
.service-name {
font-size: 18px;
font-weight: 600;
color: #262626;
margin-bottom: 5px;
}
.service-url {
font-size: 13px;
color: #8c8c8c;
}
.service-meta {
display: flex;
gap: 10px;
font-size: 12px;
color: #999;
}
.tag {
padding: 2px 8px;
border-radius: 4px;
background: #e6f7ff;
color: #1890ff;
}
.service-actions {
display: flex;
gap: 8px;
}
.action-btn-sm {
padding: 8px 14px;
border: 1px solid #d9d9d9;
background: #fff;
border-radius: 8px;
font-size: 13px;
cursor: pointer;
transition: all 0.3s;
}
.action-btn-sm:hover {
border-color: var(--main-red);
color: var(--main-red);
}
.action-btn-sm.danger:hover {
background: #fff2f0;
border-color: #ff4d4f;
color: #ff4d4f;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #d9d9d9;
}
.status-dot.enabled {
background: #52c41a;
}
/* Modal */
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.6);
z-index: 1000;
align-items: center;
justify-content: center;
animation: fadeIn 0.3s ease;
}
.modal.active {
display: flex;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.modal-content {
background: #fff;
border-radius: 20px;
max-width: 600px;
width: 90%;
max-height: 85vh;
overflow-y: auto;
animation: slideUp 0.3s ease;
}
@keyframes slideUp {
from { transform: translateY(50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
.modal-header {
padding: 20px 25px;
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
color: #fff;
display: flex;
justify-content: space-between;
align-items: center;
}
.modal-header h2 {
font-size: 18px;
font-weight: 600;
}
.close-btn {
background: none;
border: none;
color: #fff;
font-size: 28px;
cursor: pointer;
padding: 0;
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
}
.close-btn:hover {
background: rgba(255,255,255,0.2);
}
.modal-body {
padding: 25px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-size: 14px;
font-weight: 500;
color: #262626;
}
.input, .input[type="text"], .input[type="url"], .input[type="number"], select.input {
width: 100%;
padding: 10px 12px;
border: 1px solid #d9d9d9;
border-radius: 8px;
font-size: 14px;
transition: all 0.3s;
}
.input:focus {
outline: none;
border-color: var(--main-red);
box-shadow: 0 0 0 3px rgba(255, 77, 79, 0.1);
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
}
.form-divider {
margin: 25px 0;
padding-top: 20px;
border-top: 1px solid #f0f0f0;
font-size: 14px;
font-weight: 600;
color: #8c8c8c;
}
.checkbox-label {
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
}
.checkbox-label input[type="checkbox"] {
width: 18px;
height: 18px;
cursor: pointer;
}
.form-actions {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #f0f0f0;
}
.btn {
padding: 10px 20px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: var(--main-red);
color: #fff;
box-shadow: 0 4px 15px rgba(255, 77, 79, 0.4);
}
.btn-primary:hover {
background: #ff7875;
}
.loading {
text-align: center;
padding: 60px;
color: #999;
}
.empty-state {
text-align: center;
padding: 60px;
color: #999;
}
@media (max-width: 768px) {
.service-item {
flex-direction: column;
align-items: flex-start;
gap: 15px;
}
.service-actions {
width: 100%;
justify-content: flex-end;
}
.form-row {
grid-template-columns: 1fr;
}
}
</style>
{% endblock %}
{% block scripts %}
<script>
let allServices = [];
// 初始化
document.addEventListener('DOMContentLoaded', function() {
loadServices();
setupForm();
});
// 加载服务列表
async function loadServices() {
try {
const response = await fetch('/api/admin/services');
if (!response.ok) {
window.location.href = '/admin/login';
return;
}
allServices = await response.json();
renderServices();
} catch (err) {
console.error('加载服务失败:', err);
window.location.href = '/admin/login';
}
}
// 渲染服务列表
function renderServices() {
const container = document.querySelector('.services-list');
if (allServices.length === 0) {
container.innerHTML = '<div class="empty-state">暂无服务</div>';
return;
}
let html = '';
allServices.forEach(service => {
html += `
<div class="service-item">
<div class="service-info">
<span class="service-icon-large">${service.icon || '📡'}</span>
<div class="service-details">
<div class="service-name">
${service.name}
<span class="status-dot ${service.is_enabled ? 'enabled' : ''}"></span>
</div>
<div class="service-url">${service.url}</div>
<div class="service-meta">
<span class="tag">${service.category}</span>
<span>排序: ${service.sort_order}</span>
${service.health_check_enabled ? '<span>🔍 检测中</span>' : ''}
</div>
</div>
</div>
<div class="service-actions">
<button class="action-btn-sm" onclick="editService(${service.id})">编辑</button>
<button class="action-btn-sm" onclick="toggleService(${service.id})">
${service.is_enabled ? '禁用' : '启用'}
</button>
<button class="action-btn-sm danger" onclick="deleteService(${service.id}, '${service.name}')">删除</button>
</div>
</div>
`;
});
container.innerHTML = html;
}
// 设置表单
function setupForm() {
// 健康检测URL显示/隐藏
document.getElementById('healthCheckEnabled').addEventListener('change', function() {
document.getElementById('healthUrlGroup').style.display = this.checked ? 'block' : 'none';
});
// 表单提交
document.getElementById('serviceForm').addEventListener('submit', function(e) {
e.preventDefault();
saveService();
});
}
// 显示创建弹窗
function showCreateModal() {
document.getElementById('modalTitle').textContent = '新建服务';
document.getElementById('serviceId').value = '';
document.getElementById('serviceForm').reset();
document.getElementById('serviceEnabled').checked = true;
document.getElementById('serviceSort').value = '0';
document.getElementById('healthUrlGroup').style.display = 'none';
document.getElementById('serviceModal').classList.add('active');
}
// 编辑服务
function editService(id) {
const service = allServices.find(s => s.id === id);
if (!service) return;
document.getElementById('modalTitle').textContent = '编辑服务';
document.getElementById('serviceId').value = service.id;
document.getElementById('serviceName').value = service.name;
document.getElementById('serviceUrl').value = service.url;
document.getElementById('serviceDesc').value = service.description || '';
document.getElementById('serviceIcon').value = service.icon || '';
document.getElementById('serviceCategory').value = service.category;
document.getElementById('serviceSort').value = service.sort_order;
document.getElementById('serviceEnabled').checked = service.is_enabled === 1;
document.getElementById('healthCheckEnabled').checked = service.health_check_enabled === 1;
document.getElementById('healthCheckUrl').value = service.health_check_url || '';
document.getElementById('healthUrlGroup').style.display = service.health_check_enabled ? 'block' : 'none';
document.getElementById('serviceModal').classList.add('active');
}
// 保存服务
async function saveService() {
const id = document.getElementById('serviceId').value;
const data = {
name: document.getElementById('serviceName').value,
url: document.getElementById('serviceUrl').value,
description: document.getElementById('serviceDesc').value,
icon: document.getElementById('serviceIcon').value,
category: document.getElementById('serviceCategory').value,
sort_order: parseInt(document.getElementById('serviceSort').value) || 0,
is_enabled: document.getElementById('serviceEnabled').checked ? 1 : 0,
health_check_enabled: document.getElementById('healthCheckEnabled').checked ? 1 : 0,
health_check_url: document.getElementById('healthCheckUrl').value
};
try {
const url = id ? `/api/admin/services/${id}` : '/api/admin/services';
const method = id ? 'PUT' : 'POST';
const response = await fetch(url, {
method: method,
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
});
if (response.ok) {
closeModal();
loadServices();
} else {
const result = await response.json();
alert(result.error || '保存失败');
}
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 切换服务状态
async function toggleService(id) {
if (!confirm('确定要切换服务状态吗?')) return;
try {
const response = await fetch(`/api/admin/services/${id}/toggle`, {
method: 'POST'
});
if (response.ok) {
loadServices();
} else {
alert('操作失败');
}
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 删除服务
async function deleteService(id, name) {
if (!confirm(`确定要删除服务"${name}"吗?此操作不可恢复。`)) return;
try {
const response = await fetch(`/api/admin/services/${id}`, {
method: 'DELETE'
});
if (response.ok) {
loadServices();
} else {
alert('删除失败');
}
} catch (err) {
alert('请求失败: ' + err.message);
}
}
// 关闭弹窗
function closeModal() {
document.getElementById('serviceModal').classList.remove('active');
}
// 点击外部关闭弹窗
document.getElementById('serviceModal').addEventListener('click', function(e) {
if (e.target === this) {
closeModal();
}
});
</script>
{% endblock %}

51
templates/base.html Normal file
View File

@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}ToNav - 个人导航页{% endblock %}</title>
<style>
:root {
--main-red: #ff4d4f;
--gold: #faad14;
--silver: #76838b;
--bronze: #d48806;
--primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--header-gradient: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "PingFang SC", -apple-system, BlinkMacSystemFont, sans-serif;
background: var(--primary-gradient);
min-height: 100vh;
padding: 20px;
}
a {
text-decoration: none;
color: inherit;
}
.container {
max-width: 800px;
margin: 0 auto;
}
</style>
</head>
<body>
{% block content %}{% endblock %}
<script>
// 全局状态
window.currentTab = 'all';
</script>
{% block scripts %}{% endblock %}
</body>
</html>

335
templates/index.html Normal file
View File

@@ -0,0 +1,335 @@
{% extends "base.html" %}
{% block title %}ToNav - 个人导航页{% endblock %}
{% block content %}
<div class="container">
<!-- 头部 -->
<div class="header">
<h1>🧭 ToNav</h1>
<div class="subtitle">个人导航站</div>
<div class="status-bar" id="statusBar">
<span id="lastCheckTime">检测中...</span>
</div>
</div>
<!-- 分类 Tabs -->
<div class="tabs" id="categoryTabs">
<button class="tab-btn active" data-category="all">全部</button>
<button class="tab-btn" data-category="内网服务">内网服务</button>
<button class="tab-btn" data-category="开发工具">开发工具</button>
<button class="tab-btn" data-category="测试环境">测试环境</button>
</div>
<!-- 服务卡片网格 -->
<div class="services-grid" id="servicesGrid">
<div class="loading">加载中...</div>
</div>
<!-- 底部 -->
<div class="footer">
<div>© 2026 ToNav - <a href="/admin">管理后台</a></div>
</div>
</div>
<style>
.header {
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
color: #fff;
padding: 25px 20px;
border-radius: 20px 20px 0 0;
text-align: center;
}
.header h1 {
font-size: 24px;
font-weight: 700;
margin-bottom: 5px;
}
.subtitle {
font-size: 13px;
color: #8c8c8c;
margin-bottom: 10px;
}
.status-bar {
font-size: 12px;
color: #595959;
}
.tabs {
display: flex;
background: #262626;
padding: 8px;
gap: 5px;
}
.tab-btn {
flex: 1;
padding: 12px 10px;
color: #8c8c8c;
border: none;
background: none;
cursor: pointer;
transition: all 0.3s ease;
font-size: 14px;
font-weight: 600;
border-radius: 10px;
}
.tab-btn:hover {
color: #bfbfbf;
background: rgba(255,255,255,0.05);
}
.tab-btn.active {
color: #fff;
background: var(--main-red);
box-shadow: 0 4px 15px rgba(255, 77, 79, 0.4);
}
.services-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 15px;
padding: 20px 0;
min-height: 300px;
}
.service-card {
background: #fff;
border-radius: 15px;
padding: 20px;
transition: all 0.3s ease;
cursor: pointer;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
animation: fadeInUp 0.5s ease backwards;
display: flex;
flex-direction: column;
gap: 8px;
}
.service-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.card-icon {
font-size: 32px;
}
.card-status {
width: 12px;
height: 12px;
border-radius: 50%;
background: #d9d9d9;
}
.card-status.online {
background: #52c41a;
box-shadow: 0 0 8px rgba(82, 196, 26, 0.5);
}
.card-status.offline {
background: #ff4d4f;
box-shadow: 0 0 8px rgba(255, 77, 79, 0.5);
}
.card-name {
font-size: 18px;
font-weight: 600;
color: #262626;
}
.card-desc {
font-size: 13px;
color: #8c8c8c;
flex: 1;
}
.card-footer {
font-size: 11px;
color: #bfbfbf;
}
.loading {
grid-column: 1 / -1;
text-align: center;
padding: 60px;
color: #999;
}
.empty-state {
grid-column: 1 / -1;
text-align: center;
padding: 60px;
color: #999;
}
.footer {
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
color: #8c8c8c;
padding: 20px;
border-radius: 0 0 20px 20px;
text-align: center;
font-size: 13px;
}
.footer a {
color: var(--main-red);
transition: opacity 0.3s;
}
.footer a:hover {
opacity: 0.8;
}
@media (max-width: 480px) {
.services-grid {
grid-template-columns: 1fr;
}
.tab-btn {
font-size: 13px;
padding: 10px 8px;
}
}
</style>
{% endblock %}
{% block scripts %}
<script>
let allServices = [];
let allCategories = [];
let healthStatus = {}; // 存储健康状态
// 初始化
document.addEventListener('DOMContentLoaded', function() {
loadCategories();
loadServices();
setupTabs();
});
// 加载分类
async function loadCategories() {
try {
const response = await fetch('/api/categories');
allCategories = await response.json();
renderTabs();
} catch (err) {
console.error('加载分类失败:', err);
}
}
// 加载服务
async function loadServices() {
try {
const response = await fetch('/api/services');
allServices = await response.json();
renderServices(window.currentTab || 'all');
updateLastCheckTime();
} catch (err) {
console.error('加载服务失败:', err);
document.getElementById('servicesGrid').innerHTML =
'<div class="loading">加载失败,请刷新页面</div>';
}
}
// 渲染分类 Tabs
function renderTabs() {
const tabsContainer = document.getElementById('categoryTabs');
let html = '<button class="tab-btn active" data-category="all">全部</button>';
allCategories.forEach(cat => {
html += `<button class="tab-btn" data-category="${cat.name}">${cat.name}</button>`;
});
tabsContainer.innerHTML = html;
setupTabs();
}
// 设置 Tab 点击事件
function setupTabs() {
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', function() {
const category = this.dataset.category;
window.currentTab = category;
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
this.classList.add('active');
renderServices(category);
});
});
}
// 渲染服务卡片
function renderServices(category) {
const container = document.getElementById('servicesGrid');
const filteredServices = category === 'all'
? allServices
: allServices.filter(s => s.category === category);
if (filteredServices.length === 0) {
container.innerHTML = '<div class="empty-state">暂无服务</div>';
return;
}
let html = '';
filteredServices.forEach((service, index) => {
const status = healthStatus[service.id] || 'unknown';
const statusClass = status === 'online' ? 'online' : (status === 'offline' ? 'offline' : '');
html += `
<a href="${service.url}" target="_blank" class="service-card" style="animation-delay: ${index * 0.05}s">
<div class="card-header">
<span class="card-icon">${service.icon || '📡'}</span>
<span class="card-status ${statusClass}"></span>
</div>
<div class="card-name">${service.name}</div>
<div class="card-desc">${service.description || ''}</div>
<div class="card-footer">${service.category}</div>
</a>
`;
});
container.innerHTML = html;
}
// 更新最后检测时间
function updateLastCheckTime() {
const now = new Date();
const timeStr = now.toLocaleDateString('zh-CN', {
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
document.getElementById('lastCheckTime').textContent = `最后更新: ${timeStr}`;
}
// 定时刷新每30秒
setInterval(() => {
loadServices();
}, 30000);
</script>
{% endblock %}