大屏项目初始化
This commit is contained in:
@@ -1,33 +1,85 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import Login from '@/views/Login.vue'
|
||||
import Page404 from '@/views/error/404.vue'
|
||||
import Layout from '@/components/Layout/index.vue'
|
||||
import Dashboard from '@/views/desktop/index.vue'
|
||||
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
|
||||
}
|
||||
|
||||
// 路由规则
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
redirect: '/login' // 默认跳登录页
|
||||
redirect: '/login'
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'Login',
|
||||
component: Login,
|
||||
meta: {
|
||||
isPublic: true // 标记为公开页面,无需登录
|
||||
}
|
||||
meta: { isPublic: true }
|
||||
},
|
||||
{
|
||||
path: '/dashboard',
|
||||
name: 'Dashboard',
|
||||
component: Dashboard,
|
||||
meta: {
|
||||
requiresAuth: true // 需要登录验证
|
||||
}
|
||||
path: '/bigScreen',
|
||||
name: 'bigScreen',
|
||||
component: bigScreen,
|
||||
meta: { requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
component: Layout,
|
||||
meta: { requiresAuth: true },
|
||||
children: [
|
||||
{
|
||||
path: 'dashboard',
|
||||
name: 'Dashboard',
|
||||
component: Dashboard
|
||||
},
|
||||
...generateRoutes()
|
||||
]
|
||||
},
|
||||
// 关键补充:404兜底路由(匹配所有未定义的前端路由)
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
redirect: '/login' // 未知路径重定向到登录页
|
||||
name: 'Page404',
|
||||
component: Page404
|
||||
}
|
||||
]
|
||||
|
||||
@@ -39,15 +91,12 @@ const router = createRouter({
|
||||
}
|
||||
})
|
||||
|
||||
// 路由守卫:验证登录状态(优化版,增加容错)
|
||||
router.beforeEach((to, from, next) => {
|
||||
// 公开页面直接放行
|
||||
if (to.meta.isPublic) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// 非公开页面校验token
|
||||
try {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token && token.trim() !== '') {
|
||||
|
||||
Reference in New Issue
Block a user