70 lines
2.2 KiB
JavaScript
70 lines
2.2 KiB
JavaScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import { useAuth } from '@/store/auth'
|
|
import MainLayout from '@/layouts/MainLayout.vue'
|
|
import ScreenLayout from '@/layouts/ScreenLayout.vue'
|
|
|
|
const routes = [
|
|
{
|
|
path: '/login',
|
|
name: 'login',
|
|
component: () => import('@/views/Login.vue'),
|
|
meta: { public: true }
|
|
},
|
|
{
|
|
path: '/',
|
|
component: MainLayout,
|
|
redirect: '/materials',
|
|
children: [
|
|
{ path: 'users', name: 'users', component: () => import('@/views/UserManage.vue'), meta: { admin: true } },
|
|
{ path: 'factories', name: 'factories', component: () => import('@/views/FactoryManage.vue') },
|
|
{ path: 'factories/:id', name: 'factory-detail', component: () => import('@/views/FactoryDetail.vue') },
|
|
{ path: 'brands', name: 'brands', component: () => import('@/views/BrandManage.vue'), meta: { admin: true } },
|
|
{ path: 'dictionary', name: 'dictionary', component: () => import('@/views/DictionaryManage.vue'), meta: { admin: true } },
|
|
{ path: 'materials', name: 'materials', component: () => import('@/views/MaterialManage.vue') },
|
|
{ path: 'materials/:id', name: 'material-detail', component: () => import('@/views/MaterialDetail.vue') }
|
|
]
|
|
},
|
|
{
|
|
path: '/screen',
|
|
component: ScreenLayout,
|
|
meta: { admin: true },
|
|
redirect: '/screen/overview',
|
|
children: [
|
|
{ path: 'overview', name: 'screen-overview', component: () => import('@/views/screen/ScreenOverview.vue') },
|
|
{ path: 'materials', name: 'screen-materials', component: () => import('@/views/screen/ScreenMaterials.vue') },
|
|
{ path: 'factories', name: 'screen-factories', component: () => import('@/views/screen/ScreenFactories.vue') }
|
|
]
|
|
},
|
|
{
|
|
path: '/:pathMatch(.*)*',
|
|
name: 'notfound',
|
|
component: () => import('@/views/NotFound.vue'),
|
|
meta: { public: true }
|
|
}
|
|
]
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes
|
|
})
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
const { isAuthed, isAdmin } = useAuth()
|
|
|
|
if (to.meta.public) {
|
|
return next()
|
|
}
|
|
|
|
if (!isAuthed.value) {
|
|
return next('/login')
|
|
}
|
|
|
|
if (to.meta.admin && !isAdmin.value) {
|
|
return next('/materials')
|
|
}
|
|
|
|
return next()
|
|
})
|
|
|
|
export default router
|