import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router' import type { RouteRecordRaw } from 'vue-router' import { useUserStore } from '../stores/user' import { usePermissionStore } from '@/stores/permission' import { useDictStore } from '@/stores/dict' import { getToken } from '@/utils/auth' export const defaultRoute = [ { path: '/login', component: () => import('@/pages/system/login/index.vue'), name: 'Login', meta: { hidden: true, title: '登录', noTagsView: true, }, }, { path: '/404', name: 'NoFound', component: () => import('@/pages/Error/404.vue'), meta: { hidden: true, title: '404', }, }, { path: '/user', component: () => import('@/pages/layout.vue'), name: 'UserInfo', meta: { hidden: true, }, children: [ { path: 'profile', component: () => import('@/pages/Profile/index.vue'), name: 'Profile', meta: { canTo: true, hidden: true, noTagsView: false, icon: 'ep:user', title: '个人中心', }, }, ], }, { path: '/file', component: () => import('@/pages/layout.vue'), name: 'FileManagement', meta: { canTo: true, hidden: false, noTagsView: false, icon: 'ep:user', title: '文件', }, children: [ { path: 'document', component: () => import('@/pages/fileDoc/index.vue'), name: 'FileDocument', meta: { icon: 'ep:user', title: '文档管理', }, }, { path:'test', component:()=>import('@/pages/fileDoc/test.vue'), name:'Test', meta:{ icon:'ep:user', title:'测试' } } ], }, // { // path: '/device', // component: () => import('@/pages/layout.vue'), // name: 'UserInfo', // meta: { // canTo: true, // hidden: true, // noTagsView: false, // icon: 'ep:user', // title: '系统管理', // }, // children: [ // { // path: 'deviceOperation', // component: () => import('@/pages/deviceInfo/info.vue'), // name: 'deviceOperation', // meta: { // icon: 'ep:user', // hidden: true, // title: '设备xx', // }, // }, // ], // }, // { // path: '/user', // component: () => import('@/pages/layout.vue'), // name: 'UserInfo', // meta: { // canTo: true, // hidden: false, // noTagsView: false, // icon: 'ep:user', // title: '系统管理', // }, // children: [ // { // path: 'deviceField', // component: () => import('@/pages/system/deviceField/index.vue'), // name: 'DeviceField', // meta: { // icon: 'ep:user', // title: '字段管理', // }, // }, // { // path: 'device', // component: () => import('@/pages/deviceInfo/index.vue'), // name: 'Device', // meta: { // icon: 'ep:user', // title: '设备管理', // }, // }, // }, // { // path: 'manufacturer', // component: () => import('@/pages/system/manufacturer/index.vue'), // name: 'Manufacturer', // meta: { // icon: 'ep:user', // title: '厂商管理', // }, // }, // { // path: 'deviceTemplate', // component: () => import('@/pages/deviceTest/testPlan/index.vue'), // name: 'DeviceTemplate', // meta: { // icon: 'ep:user', // title: '模板管理', // }, // } // ], // }, ] const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: defaultRoute, }) const whiteList = ['/login'] router.beforeEach(async (to, from, next) => { if (!getToken()) { if (whiteList.indexOf(to.path) !== -1) { next() } else { next('/login') } return } if (to.path === '/login' || to.path === '/') { next('/home') return } const userStore = useUserStore() const permissionStore = usePermissionStore() const dictStore = useDictStore() if (!dictStore.getIsSetDict) { await dictStore.setDictMap() } if (!userStore.getIsSetUser) { await userStore.setUserInfoAction() // 后端过滤菜单 await permissionStore.generateRoutes() permissionStore.getAddRouters.forEach(route => { router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表 }) const redirectPath = from.query.redirect || to.path // 修复跳转时不带参数的问题 const redirect = decodeURIComponent(redirectPath as string) const { basePath, paramsObject: query } = parseURL(redirect) const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query } next(nextData) } else { next() } }) export default router const parseURL = ( url: string | null | undefined ): { basePath: string; paramsObject: { [key: string]: string } } => { // 如果输入为 null 或 undefined,返回空字符串和空对象 if (url == null) { return { basePath: '', paramsObject: {} } } // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数 const questionMarkIndex = url.indexOf('?') let basePath = url const paramsObject: { [key: string]: string } = {} // 如果找到了问号,说明有查询参数 if (questionMarkIndex !== -1) { // 获取 basePath basePath = url.substring(0, questionMarkIndex) // 从 URL 中获取查询字符串部分 const queryString = url.substring(questionMarkIndex + 1) // 使用 URLSearchParams 遍历参数 const searchParams = new URLSearchParams(queryString) searchParams.forEach((value, key) => { // 封装进 paramsObject 对象 paramsObject[key] = value }) } // 返回 basePath 和 paramsObject return { basePath, paramsObject } }