import type { UserRole } from '../hooks/useAuth'; type HasRole = (role: UserRole | UserRole[]) => boolean; type HasPermission = (permission: string | string[]) => boolean; interface ProfileAccess { roles: UserRole[]; permissions: string[]; } export function createAccessCheckers(profile: ProfileAccess) { const hasRole: HasRole = (role) => { if (Array.isArray(role)) return role.some((r) => profile.roles.includes(r)); return profile.roles.includes(role); }; const hasPermission: HasPermission = (permission) => { if (Array.isArray(permission)) { return permission.some((p) => profile.permissions.includes(p)); } return profile.permissions.includes(permission); }; return { hasRole, hasPermission }; } export function getDefaultHomePath(hasRole: HasRole, hasPermission: HasPermission): string { // 与后端统一登录入口 accountLoginService.resolveWebHomePath 保持一致: // 超管落地「系统设置」,而非数据看板 —— /dashboard 的路由守卫只放行 admin(roles: ['admin']), // 超管的 roles 只有 'super_admin'(不会被补成 admin),落过去会命中 App.tsx 的 // 「无权限 → 跳回默认首页」而自环。超管现有权限为 system_manage / branch_manage / data_import_export。 if (hasRole('super_admin')) return '/system/config'; if (hasRole('admin')) return '/dashboard'; if (hasRole('purchaser')) return '/purchaser/dashboard'; if (hasPermission('order_entry')) return '/customer-order'; if (hasPermission('order_manage')) return '/orders'; if (hasPermission('customer_manage')) return '/customers'; if (hasPermission('supplier_manage')) return '/suppliers'; if (hasPermission('preparation_manage')) return '/purchase/preparations'; if (hasPermission('purchase_manage')) return '/purchase'; if (hasPermission('picking_manage')) return '/picking'; if (hasPermission('inventory_manage')) return '/inventory'; if (hasPermission('delivery_manage')) return '/delivery'; if (hasPermission('finance_manage')) return '/finance'; if (hasPermission('inspection_manage')) return '/inspection'; if (hasPermission('processing_manage')) return '/inspection/processing'; if (hasPermission('market_monitor')) return '/market-monitor'; if (hasPermission(['system_manage', 'user_manage', 'role_manage', 'log_view'])) return '/system/config'; // 兜底必须是「不需要任何权限就能进」的页面。 // 原为 '/customer-order',但该路由守卫要求 order_entry, // 无角色/无权限的账号落过去会被 App.tsx「无权限 → 回默认首页」打回这里 → 自环。 // 见 src/pages/auth/NoAccess.tsx 顶部说明。 return '/no-access'; }