import React, { useState, useEffect } from 'react' import Home from './pages/Home' import Settings from './pages/Settings' import List from './pages/List' import Add from './pages/Add' import Stats from './pages/Stats' import News from './pages/News' import Info from './pages/Info' import Login from './pages/Login' import Detail from './pages/Detail' import Edit from './pages/Edit' import Admin from './pages/Admin' export default function App() { const [path, setPath] = useState(window.location.hash.slice(1) || '/') useEffect(() => { const handleHashChange = () => { setPath(window.location.hash.slice(1) || '/') } window.addEventListener('hashchange', handleHashChange) return () => window.removeEventListener('hashchange', handleHashChange) }, []) const handleNavigate = (newPath) => { window.location.hash = '#' + newPath setPath(newPath) } const getComponent = () => { const basePath = path.split('?')[0] if (basePath === '/') return if (basePath === '/news') return if (basePath === '/info') return if (basePath === '/stats') return if (basePath === '/list') return if (basePath === '/add') return if (basePath === '/login') return if (basePath === '/settings') return if (basePath === '/admin') return if (basePath.startsWith('/edit')) return if (basePath.startsWith('/detail')) return return } const token = localStorage.getItem('token') const userStr = localStorage.getItem('user') let user = null try { user = userStr ? JSON.parse(userStr) : null } catch (e) { console.error('Parse user error:', e) } const isAdmin = user && user.role === 'admin' const isEditor = user && user.role === 'editor' const canAccessInfo = isAdmin || isEditor // 登录页面独立渲染,不显示底部导航 if (!token) { // 强制刷新页面,确保状态同步 if (path !== '/login') { window.location.hash = '#/login' } return } // 如果已登录且在登录页,强制跳转到首页 if (path === '/login') { // 使用 href 强制刷新页面 window.location.href = window.location.origin + window.location.pathname + '#/' return null } return (
{getComponent()}
{[ { path: '/', icon: '🏠', label: '首页' }, { path: '/news', icon: '📰', label: '资讯' }, { path: '/stats', icon: '📊', label: '统计' }, { path: '/list', icon: '📚', label: '藏品' }, { path: '/add', icon: '🎯', label: '添加' }, ...(canAccessInfo ? [{ path: '/info', icon: '📝', label: '信息' }] : []), ...(isAdmin ? [{ path: '/admin', icon: '⚙️', label: '管理' }] : []) ].map(tab => (
handleNavigate(tab.path)} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', color: path === tab.path ? '#fbbf24' : '#94a3b8', cursor: 'pointer' }} >
{tab.icon}
{tab.label}
))}
) }