guanghulab/modules/m-channel/backup-混乱版/channel-router-backup.js
Guanghu Domestic Migration d1e47f4565
Some checks are pending
自动更新代码和重启 / update-and-restart (push) Waiting to run
CI检查 + 自动部署 / check (push) Waiting to run
CI检查 + 自动部署 / deploy (push) Blocked by required conditions
重启聊天服务 / restart (push) Waiting to run
chore: import sanitized domestic snapshot for REPO-002
Source snapshot: ca48d3ddf926d79aa138306164169baf764bb829
2026-07-17 15:54:41 +08:00

99 lines
2.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ================== 路由配置 ==================
const routes = {
'home': 'views/home.html',
'channel': 'views/channel.html',
'about': 'views/about.html'
};
// 获取当前 hash 中的路径(去掉 #/
function getHashPath() {
const hash = window.location.hash.slice(1) || '/';
const path = hash.startsWith('/') ? hash.slice(1) : hash;
return path || 'home';
}
// ================== 加载视图 ==================
async function loadView(path) {
const routerView = document.getElementById('router-view');
if (!routerView) return;
// 显示加载动画
routerView.innerHTML = '<div class="loader" style="margin: 2rem auto;"></div>';
try {
const viewFile = routes[path];
if (!viewFile) {
await load404(routerView);
return;
}
const response = await fetch(viewFile);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const html = await response.text();
routerView.innerHTML = html;
} catch (error) {
console.error('加载视图失败:', error);
routerView.innerHTML = `
<div class="error-message">
❌ 加载失败:${error.message}<br>
<small>请检查文件是否存在,或刷新重试</small>
</div>
`;
}
// 更新导航高亮和状态栏
updateActiveNav(path);
updateStatusBar(path);
}
// 加载 404 页面
async function load404(container) {
try {
const resp = await fetch('views/404.html');
if (resp.ok) {
container.innerHTML = await resp.text();
} else {
container.innerHTML = '<div class="error-message">⚠️ 404 - 页面未找到</div>';
}
} catch {
container.innerHTML = '<div class="error-message">⚠️ 404 - 页面未找到</div>';
}
}
// 更新导航高亮
function updateActiveNav(path) {
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.remove('active');
const linkPath = link.getAttribute('href').slice(2);
if (linkPath === path) {
link.classList.add('active');
}
});
}
// 更新状态栏
function updateStatusBar(path) {
const statusEl = document.getElementById('current-route');
if (statusEl) {
statusEl.textContent = `当前路由:/${path}`;
}
}
// 监听 hash 变化
window.addEventListener('hashchange', () => {
const path = getHashPath();
loadView(path);
});
// 首次加载
window.addEventListener('DOMContentLoaded', () => {
if (!window.location.hash) {
window.location.hash = '#/home';
} else {
const path = getHashPath();
loadView(path);
}
});