You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.4 KiB
95 lines
2.4 KiB
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
|
import { TOKEN_KEY } from '../api/http'
|
|
|
|
import AdminLayout from '../layouts/AdminLayout.vue'
|
|
|
|
const routes: RouteRecordRaw[] = [
|
|
{
|
|
path: '/login',
|
|
name: 'login',
|
|
component: () => import('../views/Login.vue'),
|
|
},
|
|
{
|
|
path: '/h5/verify/login',
|
|
name: 'h5-verify-login',
|
|
component: () => import('../views/h5/VerifyLogin.vue'),
|
|
},
|
|
{
|
|
path: '/h5/verify/scan',
|
|
name: 'h5-verify-scan',
|
|
component: () => import('../views/h5/VerifyScan.vue'),
|
|
},
|
|
{
|
|
path: '/m/verify/login',
|
|
name: 'm-verify-login',
|
|
component: () => import('../views/h5/VerifyLogin.vue'),
|
|
},
|
|
{
|
|
path: '/m/verify',
|
|
name: 'm-verify-scan',
|
|
component: () => import('../views/h5/VerifyScan.vue'),
|
|
},
|
|
{
|
|
path: '/',
|
|
name: 'admin-layout',
|
|
component: AdminLayout,
|
|
children: [],
|
|
},
|
|
]
|
|
|
|
export const router = createRouter({
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
routes,
|
|
})
|
|
|
|
function isMobileVerifyPath(path: string) {
|
|
return path.startsWith('/h5/verify') || path.startsWith('/m/verify')
|
|
}
|
|
|
|
router.beforeEach(async (to) => {
|
|
const token = localStorage.getItem(TOKEN_KEY)
|
|
|
|
if (to.path === '/login') {
|
|
if (token) {
|
|
const redirect = typeof to.query.redirect === 'string' ? to.query.redirect.trim() : ''
|
|
if (redirect && redirect.startsWith('/') && !redirect.startsWith('//')) {
|
|
return { path: redirect, replace: true }
|
|
}
|
|
const { getFirstMenuPath } = await import('./dynamicAdminRoutes')
|
|
return { path: await getFirstMenuPath(), replace: true }
|
|
}
|
|
return true
|
|
}
|
|
|
|
if (isMobileVerifyPath(to.path)) {
|
|
return true
|
|
}
|
|
|
|
if (!token) {
|
|
return '/login'
|
|
}
|
|
|
|
const dyn = await import('./dynamicAdminRoutes')
|
|
|
|
if (!dyn.isDynamicRoutesRegistered()) {
|
|
await dyn.registerDynamicAdminRoutes(router)
|
|
const p = to.path.replace(/\/$/, '') || '/'
|
|
if (p === '/' || p === '') {
|
|
return { path: await dyn.getFirstMenuPath(), replace: true }
|
|
}
|
|
return { path: to.fullPath, replace: true }
|
|
}
|
|
|
|
const allowed = dyn.getCachedAllowedPaths()
|
|
const p = to.path.replace(/\/$/, '') || '/'
|
|
if (allowed && allowed.size > 0 && !allowed.has(p)) {
|
|
return { path: dyn.pickDefaultPath(allowed), replace: true }
|
|
}
|
|
|
|
if (p === '/' || p === '') {
|
|
return { path: await dyn.getFirstMenuPath(), replace: true }
|
|
}
|
|
|
|
return true
|
|
})
|