export const routes = { create: { nav: { no: '01', label: '开单' } }, 'price-locks': { nav: { no: '02', label: '待交付' } }, 'material-pools': { nav: { no: '03', label: '每日购料' } }, recoveries: { nav: { no: '04', label: '回收/置换' } }, orders: { nav: { no: '05', label: '订单' } }, stock: { nav: { no: '06', label: '库存' } }, report: { nav: { no: '07', label: '每日对账' } }, 'team-funds': { nav: { no: '08', label: '团队账目' }, redirect: { staff: 'report' } }, 'private-operations': { nav: { no: '09', label: '私域运营概况' }, redirect: { staff: 'report' } }, 'people-entry': { nav: { label: '登记人数' }, redirect: { owner: 'private-operations' } }, settings: { nav: { no: '10', label: '店铺与打印设置', staffLabel: '修改密码' } }, 'order-edit': { section: 'orders' }, 'price-lock-create': { section: 'create' }, login: {}, } as const; export type Route = keyof typeof routes; export type Section = { [K in Route]: (typeof routes)[K] extends { nav: NavItem } ? K : never }[Route]; type NavItem = { no?: string; label: string; staffLabel?: string }; interface Definition { section?: Route; nav?: NavItem; redirect?: Partial>; } const definitions: Record = routes; const entries = Object.entries(definitions) as [Route, Definition][]; const base = '/next'; export function routeOf(pathname: string): Route { const path = pathname.replace(/\/+$/, ''); return entries.find(([route]) => path === base + '/' + route)?.[0] || 'create'; } export function pathFor(route: Route) { return route === 'create' ? base + '/' : base + '/' + route; } export function sectionFor(route: Route) { const def: Definition = routes[route]; return def.section || route; } export function allowedRoute(route: Route, role: string): Route { const def: Definition = routes[route]; return def.redirect?.[role === 'owner' ? 'owner' : 'staff'] || route; } export function navItems(role: string) { return entries.flatMap(([key, def]) => def.nav && allowedRoute(key, role) === key ? [{ key, no: def.nav.no, label: (role !== 'owner' && def.nav.staffLabel) || def.nav.label }] : [], ); }