import { createRouter, createWebHistory } from 'vue-router' import Layout from '@/layout/default/index.vue' import useUserStore from './user' import { screens } from './screens' const router = createRouter({ history: createWebHistory('/admin/'), routes: [ { path: '/login', component: () => import('@/views/account/login.vue') }, { path: '/', component: Layout, children: [ { path: '', name: 'live-home', component: () => import('./NoAccess.vue'), meta: { hideTab: true } }, { path: '/nav/:menuId', name: 'live-menu', component: () => import('./OnlinePage.vue') }, // Keep the reviewed hidden drill-down separate from the visible menu. { path: '/members/detail', name: 'live-member-detail', component: () => import('@/views/consumer/lists/detail.vue'), meta: { title: '用户详情', key: 'memberDetail' } }, ...screens .filter((s) => !s.hidden) .map((s) => ({ path: s.path, redirect: () => { const e = Object.values(useUserStore().entries).find( (e) => e.liveKey === s.key ) return e ? { name: 'live-menu', params: { menuId: e.id }, query: e.query || {} } : '/unavailable' } })), { path: '/unavailable', name: 'live-unavailable', component: () => import('./NoAccess.vue'), meta: { hideTab: true } } ] }, { path: '/:notfound(.*)*', redirect: '/' } ] }) router.beforeEach((to) => { const user = useUserStore() if (to.path === '/login') return user.ready ? '/' : true if (!user.ready) return '/login' if (to.name === 'live-home') { const first = user.firstId && user.entries[user.firstId] return first ? { name: 'live-menu', params: { menuId: first.id }, query: first.query || {} } : '/unavailable' } if (to.name === 'live-menu') { const id = String(to.params.menuId) if (!Object.hasOwn(user.entries, id)) return '/unavailable' const e = user.entries[id] to.meta.title = e.title to.meta.activeMenu = `/nav/${e.id}` } else if (to.meta.key) { if (!user.keys.includes(String(to.meta.key))) return '/unavailable' const parent = Object.values(user.entries).find((e) => e.liveKey === 'members') if (parent) to.meta.activeMenu = `/nav/${parent.id}` } return true }) export const getRoutePath = (perm: string) => perm === 'user.user/detail' && useUserStore().keys.includes('memberDetail') ? '/members/detail' : '' export default router