Files
ops2/frontend/ops_vue_js/src/router/index.js
T
2026-04-28 19:18:55 +08:00

190 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
routes: [
// ── 需要登录的页面(带 DefaultLayout ──
{
path: '/',
component: () => import('@/layouts/DefaultLayout.vue'),
children: [
{
path: '',
name: 'home',
component: () => import('@/views/HomeView.vue'),
},
{
path: 'settings/account',
name: 'settings-account',
component: () => import('@/views/settings/AccountView.vue'),
},
{
path: 'settings/contact',
name: 'settings-contact',
component: () => import('@/views/settings/ContactView.vue'),
},
{
path: 'settings/security',
name: 'settings-security',
component: () => import('@/views/settings/SecurityView.vue'),
},
{
path: 'schedule',
name: 'schedule',
component: () => import('@/views/ScheduleView.vue'),
},
{
path: 'purchase',
name: 'purchase',
component: () => import('@/views/purchase/PurchaseList.vue'),
},
{
path: 'purchase/addorder',
name: 'purchase-add',
component: () => import('@/views/purchase/addorder.vue'),
},
{
path: 'purchase/showorder/:id',
name: 'purchase-show',
component: () => import('@/views/purchase/ShowOrder.vue'),
},
{
path: 'purchase/editorder/:id',
name: 'purchase-edit',
component: () => import('@/views/purchase/editorder.vue'),
},
{
path: 'work_order',
name: 'work-order-list',
component: () => import('@/views/work_order/WorkOrderList.vue'),
},
{
path: 'work_order/add',
name: 'work-order-add',
component: () => import('@/views/work_order/AddEditWorkOrder.vue'),
},
{
path: 'work_order/edit/:id',
name: 'work-order-edit',
component: () => import('@/views/work_order/AddEditWorkOrder.vue'),
},
{
path: 'work_order/show/:id',
name: 'work-order-show',
component: () => import('@/views/work_order/ShowWorkOrder.vue'),
},
{
path: 'warehouse',
redirect: '/warehouse/container',
},
{
path: 'warehouse/container',
name: 'warehouse',
component: () => import('@/views/warehouse/WarehouseOverview.vue'),
},
{
path: 'warehouse/container/:id',
name: 'warehouse-container-detail',
component: () => import('@/views/warehouse/WarehouseContainerDetail.vue'),
},
{
path: 'warehouse/container/:id/add-item',
name: 'warehouse-add-item',
component: () => import('@/views/warehouse/WarehouseAddItem.vue'),
},
{
path: 'warehouse/item/:id',
name: 'warehouse-item-detail',
component: () => import('@/views/warehouse/WarehouseItemDetail.vue'),
},
{
path: 'warehouse/item/edit/:id',
name: 'warehouse-item-edit',
component: () => import('@/views/warehouse/WarehouseItemEdit.vue'),
},
{
path: 'admin',
name: 'admin',
component: () => import('@/views/AdminView.vue'),
},
{
path: 'sysadmin',
name: 'sysadmin',
component: () => import('@/views/SysAdminView.vue'),
meta: { requireSysAdmin: true },
},
],
},
// ── 认证页面(AuthLayout,全屏居中) ──
{
path: '/login',
component: () => import('@/layouts/AuthLayout.vue'),
children: [
{
path: '',
name: 'login',
component: () => import('@/views/LoginView.vue'),
},
],
},
{
path: '/register',
component: () => import('@/layouts/AuthLayout.vue'),
children: [
{
path: '',
name: 'register',
component: () => import('@/views/RegisterView.vue'),
},
],
},
{
path: '/forgot_password',
component: () => import('@/layouts/AuthLayout.vue'),
children: [
{
path: '',
name: 'forgot-password',
component: () => import('@/views/ForgotPasswordView.vue'),
},
],
},
// ── 404 ──
{
path: '/404',
name: '404',
component: () => import('@/views/NotFoundView.vue'),
},
{
path: '/:pathMatch(.*)*',
redirect: '/404',
},
],
})
// ── 全局前置守卫 ──
router.beforeEach((to) => {
const userStore = useUserStore()
// 不需要登录的页面
const publicPages = ['/', '/login', '/register', '/forgot_password', '/schedule', '/404']
if (publicPages.includes(to.path)) return true
// 未登录 → 跳转登录
if (!userStore.isLoggedIn) {
return { name: 'login', query: { redirect: to.fullPath } }
}
// 需要系统管理员权限
if (to.meta.requireSysAdmin && !userStore.isSysAdmin) {
return { name: 'home' }
}
return true
})
export default router