Files
my-bigScreen/screen-vue/src/router/index.js

112 lines
2.2 KiB
JavaScript
Raw Normal View History

2026-02-24 23:26:41 +08:00
import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
2026-03-01 21:28:05 +08:00
import Page404 from '@/views/error/404.vue'
import Layout from '@/components/Layout/index.vue'
2026-02-24 23:26:41 +08:00
import Dashboard from '@/views/desktop/index.vue'
2026-03-01 21:28:05 +08:00
import bigScreen from '@/views/screen/index.vue'
// 扫描规则保持不变
const modules = import.meta.glob('../views/**/index.vue', {
eager: false,
import: 'default'
})
const generateRoutes = () => {
const routes = []
Object.entries(modules).forEach(([filePath, module]) => {
const excludePaths = [
'views/Login.vue',
'views/desktop/index.vue',
'views/error/',
'views/screen/',
]
if (excludePaths.some(path => filePath.includes(path))) {
return
}
const routePath = filePath
.replace('../views', '')
.replace('.vue', '')
.toLowerCase()
const routeName = routePath
.split('/')
.filter(Boolean)
.map(seg => seg.charAt(0).toUpperCase() + seg.slice(1))
.join('')
routes.push({
path: routePath,
name: routeName || 'SystemRoleIndex',
component: module
})
})
return routes
}
2026-02-24 23:26:41 +08:00
const routes = [
{
path: '/',
2026-03-01 21:28:05 +08:00
redirect: '/login'
2026-02-24 23:26:41 +08:00
},
{
path: '/login',
name: 'Login',
component: Login,
2026-03-01 21:28:05 +08:00
meta: { isPublic: true }
2026-02-24 23:26:41 +08:00
},
{
2026-03-01 21:28:05 +08:00
path: '/bigScreen',
name: 'bigScreen',
component: bigScreen,
meta: { requiresAuth: true }
},
{
path: '/',
component: Layout,
meta: { requiresAuth: true },
children: [
{
path: 'dashboard',
name: 'Dashboard',
component: Dashboard
},
...generateRoutes()
]
2026-02-24 23:26:41 +08:00
},
{
path: '/:pathMatch(.*)*',
2026-03-01 21:28:05 +08:00
name: 'Page404',
component: Page404
2026-02-24 23:26:41 +08:00
}
]
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL || '/'),
routes,
scrollBehavior() {
return { top: 0 }
}
})
router.beforeEach((to, from, next) => {
if (to.meta.isPublic) {
next()
return
}
try {
const token = localStorage.getItem('token')
if (token && token.trim() !== '') {
2026-02-27 13:59:38 +08:00
next();
2026-02-24 23:26:41 +08:00
} else {
2026-02-27 13:59:38 +08:00
next({ path: '/login', query: { redirect: to.fullPath } });
2026-02-24 23:26:41 +08:00
}
} catch (error) {
2026-02-27 13:59:38 +08:00
next('/login');
2026-02-24 23:26:41 +08:00
}
})
export default router