AnCao/web/src/components/AdminRoute.tsx
yanlongqi c0a280132c 优化UI风格并添加管理员权限系统
主要更改:
- 新增管理员权限系统:添加 AdminAuth 中间件和 AdminRoute 组件,限制题库管理功能仅 yanlongqi 用户可访问
- UI 全面改版为白色毛玻璃风格(macOS 风格):应用毛玻璃效果、优化圆角和阴影、统一配色方案
- 登录页优化:将注册功能改为模态框形式,简化登录界面
- 首页优化:题库管理入口仅对管理员用户显示,优化响应式布局和卡片排列
- 移除底部导航栏:简化布局,改善用户体验

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-05 10:58:43 +08:00

48 lines
1.0 KiB
TypeScript

import React, { useEffect, useState } from 'react'
import { Navigate } from 'react-router-dom'
import { message } from 'antd'
interface AdminRouteProps {
children: React.ReactNode
}
const AdminRoute: React.FC<AdminRouteProps> = ({ children }) => {
const [isAdmin, setIsAdmin] = useState<boolean | null>(null)
useEffect(() => {
// 检查用户信息
const userStr = localStorage.getItem('user')
if (!userStr) {
setIsAdmin(false)
return
}
try {
const user = JSON.parse(userStr)
if (user.username === 'yanlongqi') {
setIsAdmin(true)
} else {
setIsAdmin(false)
message.error('无权限访问该页面')
}
} catch (e) {
setIsAdmin(false)
}
}, [])
// 正在检查权限时,不显示任何内容
if (isAdmin === null) {
return null
}
// 如果不是管理员,重定向到首页
if (!isAdmin) {
return <Navigate to="/" replace />
}
// 是管理员,显示子组件
return <>{children}</>
}
export default AdminRoute