stdproject/frontend/src/router/index.ts

161 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-05-30 13:43:31 +08:00
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/user'
// 路由配置
2025-06-07 09:22:52 +08:00
const routes:any = [
2025-05-30 13:43:31 +08:00
{
path: '/',
redirect: '/dashboard'
},
2025-06-07 09:22:52 +08:00
// 登录页面路由配置
2025-05-30 13:43:31 +08:00
{
path: '/login',
name: 'Login',
2025-06-07 09:22:52 +08:00
component: () => import('@/data-public/Login.vue'), // 使用标准组件路径
2025-05-30 13:43:31 +08:00
meta: {
title: '登录',
requiresAuth: false
}
},
{
path: '/dashboard',
name: 'Dashboard',
2025-06-07 09:22:52 +08:00
component: () => import('@/data-public/Dashboard.vue'),
2025-05-30 13:43:31 +08:00
meta: {
title: '仪表板',
requiresAuth: true
}
},
{
path: '/users',
name: 'Users',
2025-06-07 09:22:52 +08:00
component: () => import('@/data-public/Users.vue'),
2025-05-30 13:43:31 +08:00
meta: {
title: '用户管理',
requiresAuth: true
}
},
{
path: '/roles',
name: 'Roles',
2025-06-07 09:22:52 +08:00
component: () => import('@/data-public/Roles.vue'),
2025-05-30 13:43:31 +08:00
meta: {
title: '角色管理',
requiresAuth: true
}
},
{
path: '/organizations',
name: 'Organizations',
2025-06-07 09:22:52 +08:00
component: () => import('@/data-public/Organizations.vue'),
2025-05-30 13:43:31 +08:00
meta: {
title: '组织管理',
requiresAuth: true
}
},
{
path: '/dictionaries',
name: 'Dictionaries',
2025-06-07 09:22:52 +08:00
component: () => import('@/data-public/Dictionaries.vue'),
2025-05-30 13:43:31 +08:00
meta: {
title: '字典管理',
requiresAuth: true
}
},
2025-06-17 15:09:51 +08:00
// {
// path: '/FormCreateDesigner',
// name: 'FormCreateDesigner',
// component: () => import('@/data-public/FormCreateDesigner.vue'),
// meta: {
// title: '字典管理',
// requiresAuth: true
// }
// }
2025-05-30 16:20:02 +08:00
{
2025-06-17 15:09:51 +08:00
path: '/FormCreate',
name: 'FormCreate',
component: () => import('@/views/BuiltInPage/FormCreate.vue'),
2025-05-30 16:20:02 +08:00
meta: {
2025-06-17 15:09:51 +08:00
title: '表单管理',
2025-05-30 16:20:02 +08:00
requiresAuth: true
}
2025-06-17 15:09:51 +08:00
}
,{
path: '/visualization',
name: 'visualization',
component: () => import('@/data-visualization/index.vue'),
meta: {
title: '字典管理',
requiresAuth: true
}
2025-05-30 16:20:02 +08:00
},
2025-05-30 13:43:31 +08:00
{
path: '/:pathMatch(.*)*',
2025-05-30 14:40:25 +08:00
name: '404',
2025-06-07 09:22:52 +08:00
component: () => import('@/data-public/error-page/404.vue'),
2025-05-30 13:43:31 +08:00
meta: {
title: '页面未找到',
requiresAuth: false
}
2025-06-18 11:14:16 +08:00
},
{
path: '/Permission',
name: 'Permission',
2025-06-18 15:50:20 +08:00
component: () => import('@/views/system/manage/index.vue'),
2025-06-18 11:14:16 +08:00
meta: {
title: '权限管理',
requiresAuth: true
}
2025-06-19 11:21:06 +08:00
},
{
path: '/UserLogin',
name: 'UserLogin',
component: () => import('@/views/system/userlogin/login_container.vue'),
meta: {
title: '登录',
2025-06-19 18:31:46 +08:00
requiresAuth: false
2025-06-19 11:21:06 +08:00
}
},
{
path: '/UserNavbar',
name: 'UserNavbar',
component: () => import('@/views/system/userlogin/appframe_container.vue'),
meta: {
title: '导航',
requiresAuth: true
}
2025-05-30 13:43:31 +08:00
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
const userStore = useUserStore()
// 设置页面标题
document.title = to.meta.title ? `${to.meta.title} - StdProject` : 'StdProject'
2025-06-20 10:00:15 +08:00
const token = localStorage.getItem('token') || ''
2025-06-19 18:31:46 +08:00
console.log(userStore.isAuthenticated)
2025-05-30 13:43:31 +08:00
// 检查是否需要认证
if (to.meta.requiresAuth) {
2025-06-20 10:00:15 +08:00
if (!!token) {
2025-06-19 18:31:46 +08:00
next()
} else {
next('/UserLogin') // 未认证重定向到登录页
}
2025-05-30 13:43:31 +08:00
} else {
// 如果已登录且访问登录页,重定向到仪表板
2025-06-19 18:31:46 +08:00
if (to.path === '/UserLogin' && userStore.isAuthenticated) {
2025-05-30 13:43:31 +08:00
next('/dashboard')
} else {
next()
}
}
})
export default router