window.Router = (function() { 'use strict'; const routes = { 'dashboard': { title: '仪表盘', render: 'DashboardPage.render', role: null }, 'companies': { title: '客户管理', render: 'CompaniesPage.render', role: null }, 'contacts': { title: '联系人管理', render: 'ContactsPage.render', role: null }, 'org-nodes': { title: '组织架构', render: 'OrgNodesPage.render', role: null }, 'decision-nodes': { title: '决策关系', render: 'DecisionPage.render', role: null }, 'follow-records': { title: '沟通留痕', render: 'FollowsPage.render', role: null }, 'users': { title: '用户管理', render: 'UsersPage.render', role: 'admin' }, }; function getCurrentHash() { const h = location.hash.replace(/^#\/?/, ''); return h || 'dashboard'; } function resolve(path) { const seg = path.split('/')[0]; return routes[seg] || null; } async function navigate(path) { const r = resolve(path); if (!r) { navigate('dashboard'); return; } if (r.role === 'admin' && !Auth.isAdmin) { UI.toast('无权访问此页面', 'error'); navigate('dashboard'); return; } document.getElementById('page-title').textContent = r.title; document.querySelectorAll('.nav-item').forEach(el => { el.classList.toggle('active', el.dataset.route === path || (el.dataset.route === 'dashboard' && path === 'dashboard')); }); const area = document.getElementById('content-area'); area.innerHTML = '
加载中...
'; try { const fn = eval(r.render); if (typeof fn === 'function') { const html = await fn(); area.innerHTML = html; } } catch (e) { Logger.error(`页面渲染失败: ${path} - ${e.message}`); area.innerHTML = '
页面加载失败
'; } } function start() { window.addEventListener('hashchange', () => navigate(getCurrentHash())); if (Auth.isLoggedIn) { navigate(getCurrentHash()); } } function buildNav() { const nav = document.getElementById('nav-menu'); const items = [ ['dashboard', '仪表盘'], ['companies', '客户管理'], ['contacts', '联系人'], ['org-nodes', '组织架构'], ['decision-nodes', '决策关系'], ['follow-records', '沟通留痕'], ]; if (Auth.isAdmin) items.push(['users', '用户管理']); nav.innerHTML = items.map(([k, label]) => `${label}` ).join(''); } return { start, navigate, getCurrentHash, buildNav, routes }; })();