import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router"; import { usePermissionStore } from "@/stores/permission"; import { getToken } from "@/utils/auth"; const routes: RouteRecordRaw[] = [ { path: "/login", name: "Login", component: () => import("@/views/login/index.vue"), meta: { requiresAuth: false, title: "登录" } }, { path: "/403", name: "Forbidden", component: () => import("@/views/403.vue"), meta: { requiresAuth: false, title: "无权访问" } }, { path: "/", name: "Root", component: () => import("@/layouts/index.vue"), redirect: "/dashboard", children: [ { path: "dashboard", name: "Dashboard", component: () => import("@/views/dashboard/index.vue"), meta: { requiresAuth: true, title: "仪表盘" } }, // 业务模块 { path: "customer/info", name: "CustomerInfo", component: () => import("@/views/customer/info/index.vue"), meta: { requiresAuth: true, title: "客户档案管理", permission: "customer:list" } }, { path: "customer/department", name: "CustomerDept", component: () => import("@/views/customer/department/index.vue"), meta: { requiresAuth: true, title: "客户部门", permission: "customer:department" } }, { path: "customer/account", name: "CustomerAccount", component: () => import("@/views/customer/account/index.vue"), meta: { requiresAuth: true, title: "客户小程序授权管理", permission: "customer:account" } }, { path: "customer/visit", name: "CustomerVisit", component: () => import("@/views/customer/visit/index.vue"), meta: { requiresAuth: true, title: "客户拜访", permission: "customer:visit" } }, { path: "customer/visit/create", name: "CustomerVisitCreate", component: () => import("@/views/customer/visit/form.vue"), meta: { requiresAuth: true, title: "新增客户拜访记录", permission: "customer:visit", parentTitle: "客户拜访", parentPath: "/customer/visit" } }, { path: "customer/visit/edit/:id", name: "CustomerVisitEdit", component: () => import("@/views/customer/visit/form.vue"), meta: { requiresAuth: true, title: "编辑客户拜访记录", permission: "customer:visit", parentTitle: "客户拜访", parentPath: "/customer/visit" } }, { path: "customer/topology", name: "CustomerTopology", component: () => import("@/views/customer/topology/index.vue"), meta: { requiresAuth: true, title: "空间拓扑与用电架构", permission: "customer:topology" } }, { path: "project", name: "Project", redirect: "/project/survey", meta: { requiresAuth: true, title: "项目管理", permission: "project:list" } }, { path: "project/survey", name: "ProjectSurvey", component: () => import("@/views/project/survey/index.vue"), meta: { requiresAuth: true, title: "项目勘察", permission: "project:survey" } }, { path: "project/plan", name: "ProjectPlan", component: () => import("@/views/project/plan/index.vue"), meta: { requiresAuth: true, title: "项目方案", permission: "project:plan" } }, { path: "project/contract", name: "ProjectContract", component: () => import("@/views/project/contract/index.vue"), meta: { requiresAuth: true, title: "合同管理", permission: "project:contract" } }, { path: "project/acceptance", name: "ProjectAcceptance", component: () => import("@/views/project/acceptance/index.vue"), meta: { requiresAuth: true, title: "项目验收", permission: "project:acceptance" } }, { path: "project/visit", name: "ProjectVisit", component: () => import("@/views/project/visit/index.vue"), meta: { requiresAuth: true, title: "回访记录", permission: "project:visit" } }, { path: "dispatch", name: "Dispatch", component: () => import("@/views/Placeholder.vue"), meta: { requiresAuth: true, title: "运营派单", permission: "dispatch:list" } }, { path: "device", name: "Device", component: () => import("@/views/Placeholder.vue"), meta: { requiresAuth: true, title: "设备监控", permission: "device:list" } }, { path: "report", name: "Report", component: () => import("@/views/Placeholder.vue"), meta: { requiresAuth: true, title: "统计报表", permission: "report:list" } }, // 系统管理 { path: "system/user", name: "SystemUser", component: () => import("@/views/system/user/index.vue"), meta: { requiresAuth: true, title: "用户管理", permission: "system:user" } }, { path: "system/role", name: "SystemRole", component: () => import("@/views/system/role/index.vue"), meta: { requiresAuth: true, title: "角色管理", permission: "system:role" } }, { path: "system/menu", name: "SystemMenu", component: () => import("@/views/system/menu/index.vue"), meta: { requiresAuth: true, title: "菜单管理", permission: "system:menu" } }, { path: "system/dict", name: "SystemDict", component: () => import("@/views/system/dict/index.vue"), meta: { requiresAuth: true, title: "数据字典", permission: "system:dict" } }, { path: "log/operation", name: "OperationLog", component: () => import("@/views/log/operation/index.vue"), meta: { requiresAuth: true, title: "操作日志", permission: "system:log" } } ] }, { path: "/:pathMatch(.*)*", redirect: "/403" } ]; const router = createRouter({ history: createWebHistory(), routes }); router.beforeEach(async (to, _from, next) => { document.title = to.meta.title ? `${to.meta.title} - 智能物联网设备管理系统` : "智能物联网设备管理系统"; const permissionStore = usePermissionStore(); const token = getToken(); if (to.meta.requiresAuth === false) { if (to.path === "/login" && token) { next({ path: "/" }); } else { next(); } return; } if (!token) { next({ path: "/login", query: { redirect: to.fullPath } }); return; } if (!permissionStore.userInfo) { try { const success = await permissionStore.fetchUserInfo(); if (!success) { next({ path: "/login", query: { redirect: to.fullPath } }); return; } } catch (error) { console.error('获取用户信息失败:', error); next({ path: "/login", query: { redirect: to.fullPath } }); return; } } if (to.meta.permission) { if (!permissionStore.hasPermission(to.meta.permission as string)) { next({ path: "/403" }); return; } } next(); }); export default router;