RequireSuperAdmin.tsx 801 Bytes
import { Navigate, useLocation } from 'react-router-dom';
import { Result } from 'antd';
import { useAppSelector } from '../store/hooks';
import { selectIsAuthenticated, selectUserInfo } from '../store/slices/authSlice';

export default function RequireSuperAdmin({ children }: { children: React.ReactNode }) {
  const isAuth = useAppSelector(selectIsAuthenticated);
  const userInfo = useAppSelector(selectUserInfo);
  const location = useLocation();

  if (!isAuth) {
    return <Navigate to="/login" replace state={{ from: location }} />;
  }
  if (userInfo?.userType !== 'SUPER_ADMIN') {
    return (
      <div data-testid="forbidden-result">
        <Result status="403" title="权限不足" subTitle="仅超级管理员可访问此页面" />
      </div>
    );
  }
  return <>{children}</>;
}