feat: 添加三个 service 文件和 RoleGuard 组件

This commit is contained in:
simonkoson
2026-05-21 19:46:40 +08:00
parent 7cc0bf21ff
commit b7560a6cf6
6 changed files with 455 additions and 1 deletions
@@ -0,0 +1,20 @@
/**
* 角色路由守卫 — 拦截非授权角色访问受保护路由
* 用法: <Route path="/users" element={<RoleGuard roles={['zhipianren']}><UserManage /></RoleGuard>} />
*/
import { Navigate } from 'react-router-dom'
import useAuthStore from '../../stores/authStore'
function RoleGuard({ children, roles }) {
const { user } = useAuthStore()
if (!user) return null // AuthGuard 已处理未登录跳转
if (!roles.includes(user.role)) {
return <Navigate to="/dashboard" replace />
}
return children
}
export default RoleGuard