Files
rczn_center_control_web/rc_autoplc_front/src/router/index.ts

149 lines
3.8 KiB
TypeScript
Raw Normal View History

import { createRouter, createWebHashHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'
2026-04-30 15:10:28 +08:00
const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
2026-04-30 15:10:28 +08:00
routes: [
{
path: '/login',
name: 'login',
component: () => import('../views/Login.vue'),
meta: { requiresAuth: false },
},
2026-04-30 15:10:28 +08:00
{
path: '/',
component: () => import('../views/Layout.vue'),
meta: { requiresAuth: true },
2026-04-30 15:10:28 +08:00
children: [
{
path: '',
redirect: '/home',
},
{
path: '/home',
name: 'home',
component: () => import('../views/Home.vue'),
},
2026-04-30 15:10:28 +08:00
{
path: '/user',
name: 'user',
component: () => import('../views/user/index.vue'),
},
{
path: '/role',
name: 'role',
component: () => import('../views/role/index.vue'),
},
{
path: '/department',
name: 'department',
component: () => import('../views/department/index.vue'),
},
{
path: '/permission',
name: 'permission',
component: () => import('../views/permission/index.vue'),
},
2026-04-30 15:10:28 +08:00
{
path: '/position',
name: 'position',
component: () => import('../views/position/index.vue'),
},
{
path: '/manage-log',
name: 'manage-log',
component: () => import('../views/manage-log/index.vue'),
},
{
path: '/user-role',
name: 'user-role',
component: () => import('../views/user-role/index.vue'),
},
{
path: '/island-info',
name: 'island-info',
component: () => import('../views/islandInfo/index.vue'),
},
{
path: '/devparam',
name: 'devparam',
component: () => import('../views/devparam/index.vue'),
},
2026-04-30 15:10:28 +08:00
{
path: '/devinfo',
name: 'devinfo',
component: () => import('../views/devinfo/index.vue'),
},
{
path: '/plc-devinfo',
name: 'plc-devinfo',
component: () => import('../views/devinfo/plc.vue'),
},
{
path: '/goods-info',
name: 'goods-info',
component: () => import('../views/goods/index.vue'),
},
{
path: '/flow-info',
name: 'flow-info',
component: () => import('../views/flowinfo/index.vue'),
},
{
path: '/step-info',
name: 'step-info',
component: () => import('../views/stepinfo/index.vue'),
},
{
path: '/plc-device-control',
name: 'plc-device-control',
component: () => import('../views/plcdevicecontrol/index.vue'),
},
{
path: '/sample-injection',
name: 'sample-injection',
component: () => import('../views/sampleinjection/index.vue'),
},
2026-04-30 15:10:28 +08:00
],
},
],
})
// 添加全局路由守卫
router.beforeEach((to, _from, next) => {
const authStore = useAuthStore()
const token = authStore.getToken()
// 如果访问登录页
if (to.path === '/login') {
// 已登录状态下访问登录页,自动跳转到首页
if (token) {
next('/home')
ElMessage.success('已登录,自动跳转')
} else {
// 未登录,正常进入登录页
next()
}
return
}
2026-04-30 15:10:28 +08:00
// 访问需要认证的页面但未登录无token
if (to.meta.requiresAuth && !token) {
ElMessage.warning('请先登录')
next('/login')
return
}
// 如果访问根路径,重定向到首页
if (to.path === '/') {
next('/home')
return
}
// 已登录且访问合法页面,正常放行
next()
})
2026-04-30 15:10:28 +08:00
export default router