修复服务器重启时端口被占用和数据保存问题

This commit is contained in:
Alex Yang
2025-11-28 19:38:21 +08:00
parent 16bc615a52
commit 24b8cf19aa
20 changed files with 820873 additions and 109 deletions

View File

@@ -2708,77 +2708,105 @@ function handlePageSwitch() {
}
}
// 处理hash变化
function handleHashChange() {
let hash = window.location.hash;
// 如果没有hash默认设置为#dashboard
if (!hash) {
hash = '#dashboard';
window.location.hash = hash;
return;
}
const targetId = hash.substring(1);
// 查找对应的菜单项
let targetMenuItem = null;
menuItems.forEach(item => {
if (item.getAttribute('href') === hash) {
targetMenuItem = item;
}
});
// 如果找到了对应的菜单项,切换页面
if (targetMenuItem) {
switchPage(targetId, targetMenuItem);
} else {
// 如果没有找到对应的菜单项,尝试显示对应的内容
const contentElement = document.getElementById(`${targetId}-content`);
if (contentElement) {
// 隐藏所有内容
document.querySelectorAll('[id$="-content"]').forEach(content => {
content.classList.add('hidden');
});
// 显示目标内容
contentElement.classList.remove('hidden');
// 查找对应的菜单项并更新活动状态
menuItems.forEach(item => {
item.classList.remove('sidebar-item-active');
if (item.getAttribute('href') === hash) {
item.classList.add('sidebar-item-active');
// 更新页面标题
document.getElementById('page-title').textContent = item.querySelector('span').textContent;
}
});
} else {
// 如果没有找到对应的内容默认显示dashboard
window.location.hash = '#dashboard';
}
}
}
// 初始化hash路由
function initHashRoute() {
handleHashChange();
}
// 监听hash变化事件
window.addEventListener('hashchange', handleHashChange);
menuItems.forEach(item => {
item.addEventListener('click', (e) => {
// 允许默认的hash变化
// 页面切换会由hashchange事件处理
});
});
// 初始化hash路由
initHashRoute();
}
// 处理hash变化 - 全局函数,确保在页面加载时就能被调用
function handleHashChange() {
let hash = window.location.hash;
// 如果没有hash默认设置为#dashboard
if (!hash) {
hash = '#dashboard';
window.location.hash = hash;
return;
}
const targetId = hash.substring(1);
const menuItems = document.querySelectorAll('nav a');
// 首先检查是否存在对应的内容元素
const contentElement = document.getElementById(`${targetId}-content`);
// 查找对应的菜单项
let targetMenuItem = null;
menuItems.forEach(item => {
if (item.getAttribute('href') === hash) {
targetMenuItem = item;
}
});
// 如果找到了对应的内容元素,直接显示
if (contentElement) {
// 隐藏所有内容
document.querySelectorAll('[id$="-content"]').forEach(content => {
content.classList.add('hidden');
});
// 显示目标内容
contentElement.classList.remove('hidden');
// 更新活动菜单项和页面标题
menuItems.forEach(item => {
item.classList.remove('sidebar-item-active');
});
if (targetMenuItem) {
targetMenuItem.classList.add('sidebar-item-active');
// 更新页面标题
const pageTitle = targetMenuItem.querySelector('span').textContent;
document.getElementById('page-title').textContent = pageTitle;
} else {
// 如果没有找到对应的菜单项尝试根据hash更新页面标题
const titleElement = document.getElementById(`${targetId}-title`);
if (titleElement) {
document.getElementById('page-title').textContent = titleElement.textContent;
}
}
} else if (targetMenuItem) {
// 隐藏所有内容
document.querySelectorAll('[id$="-content"]').forEach(content => {
content.classList.add('hidden');
});
// 显示目标内容
document.getElementById(`${targetId}-content`).classList.remove('hidden');
// 更新页面标题
document.getElementById('page-title').textContent = targetMenuItem.querySelector('span').textContent;
// 更新活动菜单项
menuItems.forEach(item => {
item.classList.remove('sidebar-item-active');
});
targetMenuItem.classList.add('sidebar-item-active');
// 侧边栏切换(移动端)
if (window.innerWidth < 1024) {
toggleSidebar();
}
} else {
// 如果既没有找到内容元素也没有找到菜单项默认显示dashboard
window.location.hash = '#dashboard';
}
}
// 初始化hash路由
function initHashRoute() {
handleHashChange();
}
// 监听hash变化事件 - 全局事件监听器
window.addEventListener('hashchange', handleHashChange);
// 初始化hash路由 - 确保在页面加载时就能被调用
initHashRoute();
// 侧边栏切换
function toggleSidebar() {
const sidebar = document.getElementById('sidebar');