| // src/components/RequireRole.jsx | |
| import React from 'react' | |
| import { Navigate } from 'react-router-dom' | |
| import { getUserInfo, isLoggedIn } from '../utils/auth' | |
| export function RequireRole({ role, children }) { | |
| if (!isLoggedIn()) { | |
| // 未登录 | |
| return <Navigate to="/login" replace /> | |
| } | |
| const user = getUserInfo() | |
| if (user.role !== role) { | |
| // 角色不匹配,回首页 | |
| return <Navigate to="/" replace /> | |
| } | |
| return children | |
| } |