2026-08-25 16:21:05 +08:00
|
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
|
import {
|
|
|
|
|
canAccessAdminRoute,
|
|
|
|
|
clearAdminBootstrapSession,
|
|
|
|
|
ensureAdminAuthBootstrap,
|
|
|
|
|
getAdminBootstrapSnapshot,
|
|
|
|
|
resolveAdminLandingPath,
|
|
|
|
|
} from '@/utils/admin-auth-bootstrap'
|
|
|
|
|
|
|
|
|
|
const pageNames = [
|
|
|
|
|
['', 'HomeView', 'admin', undefined], ['dashboard', 'DashboardView', 'admin-dashboard', 'dashboard:view'],
|
2026-08-26 21:57:15 +08:00
|
|
|
['users', 'UsersView', 'admin-users', 'user:view'], ['shops', 'ShopsView', 'admin-shops', 'shop:view'],
|
|
|
|
|
['profile', 'ProfileView', 'admin-profile', undefined]
|
2026-08-25 16:21:05 +08:00
|
|
|
] as const
|
|
|
|
|
|
|
|
|
|
const routes = [
|
|
|
|
|
{ path: '/admin/login', name: 'admin-login', component: () => import('../views/admin/AdminLoginView.vue'), meta: { requiresAuth: false, isAdmin: true, hideNav: true } },
|
|
|
|
|
{
|
|
|
|
|
path: '/admin', component: () => import('../views/admin/AdminLayout.vue'), meta: { requiresAuth: true, isAdmin: true, hideNav: true },
|
|
|
|
|
children: pageNames.map(([path, view, name, permissionCode]) => ({
|
|
|
|
|
path, name, component: () => import(`../views/admin/${view}.vue`), meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode }
|
|
|
|
|
}))
|
|
|
|
|
},
|
|
|
|
|
{ path: '/:pathMatch(.*)*', name: 'not-found', component: () => import('../views/NotFoundView.vue'), meta: { requiresAuth: false, isAdmin: true, hideNav: true } }
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes })
|
|
|
|
|
|
|
|
|
|
router.beforeEach(async to => {
|
|
|
|
|
if (!to.meta.isAdmin) return true
|
|
|
|
|
const snapshot = getAdminBootstrapSnapshot()
|
|
|
|
|
if (to.path === '/admin/login') {
|
|
|
|
|
if (!snapshot.token || !snapshot.userInfo) return true
|
|
|
|
|
return (await ensureAdminAuthBootstrap()) === 'ready' ? resolveAdminLandingPath() : true
|
|
|
|
|
}
|
2026-08-26 21:57:15 +08:00
|
|
|
if (!snapshot.isPlatformAdmin) {
|
|
|
|
|
clearAdminBootstrapSession()
|
|
|
|
|
return '/admin/login'
|
|
|
|
|
}
|
2026-08-25 16:21:05 +08:00
|
|
|
if (!snapshot.token || !snapshot.userInfo) {
|
|
|
|
|
clearAdminBootstrapSession()
|
|
|
|
|
return '/admin/login'
|
|
|
|
|
}
|
|
|
|
|
if (await ensureAdminAuthBootstrap() !== 'ready') return '/admin/login'
|
|
|
|
|
return canAccessAdminRoute(to.path, to.meta.permissionCode as string | undefined) ? true : resolveAdminLandingPath()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default router
|