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: '/404', name: 'NoFound', component: () => import('@/pages/Error/404.vue'), meta: { hidden: true, title: '404', }, }, { path: '/file', component: () => import('@/pages/layout.vue'), name: 'FileManagement', meta: { canTo: true, hidden: false, noTagsView: false, icon: 'mage:file-2', title: '文件', }, children: [ { path: 'document', component: () => import('@/pages/fileDoc/index.vue'), name: 'FileDocument', meta: { icon: 'mage:file-2', title: '文档管理', }, }, ], }, { path: '/OnlineDocument', name: '/OnlineDocument', component: () => import('@/pages/fileDoc/index.vue'), meta: { hidden: false, icon: 'mage:file-records', 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('/file/document') // } // return // } if (to.path === '/OnlineDocument') { next('/file/document') return } if (to.path === '/') { next('/file/document') return } // if (to.path === '/login' || to.path === '/') { // next('/file/document') // 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() // } 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 } }