149 lines
3.8 KiB
TypeScript
149 lines
3.8 KiB
TypeScript
import { createRouter, createWebHashHistory } from 'vue-router'
|
||
import { useAuthStore } from '@/stores/auth'
|
||
import { ElMessage } from 'element-plus'
|
||
|
||
const router = createRouter({
|
||
history: createWebHashHistory(import.meta.env.BASE_URL),
|
||
routes: [
|
||
{
|
||
path: '/login',
|
||
name: 'login',
|
||
component: () => import('../views/Login.vue'),
|
||
meta: { requiresAuth: false },
|
||
},
|
||
{
|
||
path: '/',
|
||
component: () => import('../views/Layout.vue'),
|
||
meta: { requiresAuth: true },
|
||
children: [
|
||
{
|
||
path: '',
|
||
redirect: '/home',
|
||
},
|
||
{
|
||
path: '/home',
|
||
name: 'home',
|
||
component: () => import('../views/Home.vue'),
|
||
},
|
||
{
|
||
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'),
|
||
},
|
||
{
|
||
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'),
|
||
},
|
||
{
|
||
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'),
|
||
},
|
||
],
|
||
},
|
||
],
|
||
})
|
||
|
||
// 添加全局路由守卫
|
||
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
|
||
}
|
||
|
||
// 访问需要认证的页面,但未登录(无token)
|
||
if (to.meta.requiresAuth && !token) {
|
||
ElMessage.warning('请先登录')
|
||
next('/login')
|
||
return
|
||
}
|
||
|
||
// 如果访问根路径,重定向到首页
|
||
if (to.path === '/') {
|
||
next('/home')
|
||
return
|
||
}
|
||
|
||
// 已登录且访问合法页面,正常放行
|
||
next()
|
||
})
|
||
|
||
export default router
|