import Vue from "vue"; import Router from "vue-router"; import http from "@/utils/request"; Vue.use(Router); const routerPush = Router.prototype.push Router.prototype.push = function push(location, onResolve, onReject) { if (onResolve || onReject) return routerPush.call(this, location, onResolve, onReject) return routerPush.call(this, location).catch(err => err) } // 页面路由(独立页面) export const pageRoutes = [ { path: "/404", component: () => import("@/views/pages/404"), name: "404", meta: { title: "404未找到" }, beforeEnter(to, from, next) { // 拦截处理特殊业务场景 // 如果, 重定向路由包含__双下划线, 为临时添加路由 if (/__.*/.test(to.redirectedFrom)) { return next(to.redirectedFrom.replace(/__.*/, "")); } next(); }, }, { path: "/login", component: () => import("@/views/pages/login"), name: "login", meta: { title: "登录" }, }, { path: "/infoWindow", component: () => import("@/views/modules/situational/components/infoWindow/index.vue"), name: "infoWindow", meta: { title: "弹窗" }, }, ]; // 模块路由(基于主入口布局页面) export const moduleRoutes = { path: "/", component: () => import("@/views/main"), name: "main", redirect: { name: "home" }, meta: { title: "主入口布局" }, children: [ { path: '/home', component: () => import('@/views/modules/home'), name: 'home', meta: { title: '主页', isTab: false } }, //创衡智能体 { path: '/agentDialog', component: () => import('@/views/modules/agent/dialog'), name: 'agentDialog', meta: { title: '创建新对话', isTab: false } }, { path: '/agentDialogResult', component: () => import('@/views/modules/agent/dialogResult'), name: 'agentDialogResult', meta: { title: '对话结果', isTab: false } }, { path: '/agentProgram', component: () => import('@/views/modules/agent/program'), name: 'agentProgram', meta: { title: '项目管理', isTab: false } }, { path: '/agentProgramAddTeam', component: () => import('@/views/modules/agent/program/addTeam'), name: 'agentProgramAddTeam', meta: { title: '添加团队', isTab: false } }, { path: '/agentKnowledge', component: () => import('@/views/modules/agent/knowledge'), name: 'agentKnowledge', meta: { title: '知识库管理', isTab: false } }, { path: '/agentKnowledgeAdd', component: () => import('@/views/modules/agent/knowledge/add'), name: 'agentKnowledgeAdd', meta: { title: '创建知识库', isTab: false } }, { path: '/agentKnowledgeDetail', component: () => import('@/views/modules/agent/knowledge/detail'), name: 'agentKnowledgeDetail', meta: { title: '知识库详情', isTab: false } }, { path: '/agentKnowledgeCategory', component: () => import('@/views/modules/agent/knowledgeCategory'), name: 'agentKnowledgeCategory', meta: { title: '类目管理', isTab: false } }, { path: '/agentUser', component: () => import('@/views/modules/agent/user'), name: 'agentUser', meta: { title: '用户管理', isTab: false } }, { path: '/agentReport', component: () => import('@/views/modules/agent/report'), name: 'agentReport', meta: { title: '报告管理', isTab: false } }, { path: '/agentQuestionnaire', component: () => import('@/views/modules/agent/questionnaire'), name: 'agentQuestionnaire', meta: { title: '问卷管理', isTab: false } }, { path: '/agentQuestionnaireList', component: () => import('@/views/modules/agent/questionnaireList'), name: 'agentQuestionnaireList', meta: { title: '问卷列表', isTab: false } }, { path: '/agentQuestionnaireSchedule', component: () => import('@/views/modules/agent/questionnaire/schedule'), name: 'agentQuestionnaireSchedule', meta: { title: '问卷作答进度', isTab: false } }, { path: '/agentQuestionnaireReport', component: () => import('@/views/modules/agent/questionnaire/report'), name: 'agentQuestionnaireReport', meta: { title: '问卷报告', isTab: false } }, { path: '/agentQuestionnaireDetail', component: () => import('@/views/modules/agent/questionnaire/detail'), name: 'agentQuestionnaireDetail', meta: { title: '问卷详情', isTab: false } }, { path: '/agentQuestionnairePublish', component: () => import('@/views/modules/agent/questionnaire/publish'), name: 'agentQuestionnairePublish', meta: { title: '发布问卷', isTab: false } }, ] } export function addDynamicRoute(routeParams, router) { // 组装路由名称, 并判断是否已添加, 如是: 则直接跳转 var routeName = routeParams.routeName; var dynamicRoute = window.SITE_CONFIG["dynamicRoutes"].filter( (item) => item.name === routeName )[0]; if (dynamicRoute) { return router.push({ name: routeName, params: routeParams.params }); } // 否则: 添加并全局变量保存, 再跳转 dynamicRoute = { path: routeName, component: () => import(`@/views/modules/${routeParams.path}`), name: routeName, meta: { ...window.SITE_CONFIG["contentTabDefault"], menuId: routeParams.menuId, title: `${routeParams.title}`, }, }; router.addRoutes([ { ...moduleRoutes, name: `main-dynamic__${dynamicRoute.name}`, children: [dynamicRoute], }, ]); window.SITE_CONFIG["dynamicRoutes"].push(dynamicRoute); router.push({ name: dynamicRoute.name, params: routeParams.params }); } const router = new Router({ mode: "hash", scrollBehavior: () => ({ y: 0 }), routes: pageRoutes.concat(moduleRoutes), }); router.beforeEach((to, from, next) => { if (to.path == "infoWindow") { next(); } else { // 添加动态(菜单)路由 // 已添加或者当前路由为页面路由, 可直接访问 if ( window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] || fnCurrentRouteIsPageRoute(to, pageRoutes) ) { try { router.app.$options.store.state.sidebarMenuActiveName = []; var proute = window.SITE_CONFIG["menuList"].filter( (item) => item.children.filter((child) => child.id == to.meta.menuId).length > 0 ); router.app.$options.store.state.sidebarMenuActiveName.push( proute.length > 0 ? proute[0].name : "" ); router.app.$options.store.state.sidebarMenuActiveName.push( to.meta.title ); } catch { } return next(); } // 获取字典列表, 添加并全局变量保存 http .get("/sys/dict/type/all") .then(({ data: res }) => { if (res.code !== 0) { return; } window.SITE_CONFIG["dictList"] = res.data; }) .catch(() => { }); // 获取菜单列表, 添加并全局变量保存 http .get("/sys/menu/nav") .then(({ data: res }) => { if (res.code !== 0) { Vue.prototype.$message.error(res.msg); return next({ name: "login" }); } window.SITE_CONFIG["menuList"] = res.data; fnAddDynamicMenuRoutes(window.SITE_CONFIG["menuList"]); next({ ...to, replace: true }); }) .catch(() => { next({ name: "login" }); }); } }); router.onError((error) => { const pattern = /Loading chunk (.*?)+ failed/g; const isChunkLoadFailed = error.message.match(pattern); //const targetPath = router.history.pending.fullPath; if (isChunkLoadFailed) { //router.replace(targetPath); location.reload(); } }); /** * 判断当前路由是否为页面路由 * @param {*} route 当前路由 * @param {*} pageRoutes 页面路由 */ function fnCurrentRouteIsPageRoute(route, pageRoutes = []) { var temp = []; for (var i = 0; i < pageRoutes.length; i++) { if (route.path === pageRoutes[i].path) { return true; } if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) { temp = temp.concat(pageRoutes[i].children); } } return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false; } /** * 添加动态(菜单)路由 * @param {*} menuList 菜单列表 * @param {*} routes 递归创建的动态(菜单)路由 */ function fnAddDynamicMenuRoutes(menuList = [], routes = []) { var temp = []; for (var i = 0; i < menuList.length; i++) { if (menuList[i].children && menuList[i].children.length >= 1) { temp = temp.concat(menuList[i].children); continue; } // 组装路由 var route = { path: "", component: null, name: "", meta: { ...window.SITE_CONFIG["contentTabDefault"], menuId: menuList[i].id, title: menuList[i].name, }, }; let URL = (menuList[i].url || "").replace(/{{([^}}]+)?}}/g, (s1, s2) => eval(s2) ); // URL支持{{ window.xxx }}占位符变量 if (/^http[s]?:\/\/.*/.test(URL)) { route["path"] = route["name"] = `i-${menuList[i].id}`; route["meta"]["iframeURL"] = URL; } else { try { URL = URL.replace(/^\//, "").replace(/_/g, "-"); route["path"] = route["name"] = URL.replace(/\//g, "-"); route["component"] = () => import(`@/views/modules/${URL}`); } catch { } } routes.push(route); } if (temp.length >= 1) { return fnAddDynamicMenuRoutes(temp, routes); } // 添加路由 router.addRoutes([ { ...moduleRoutes, name: "main-dynamic-menu", children: routes, }, { path: "*", redirect: { name: "404" } }, ]); window.SITE_CONFIG["dynamicMenuRoutes"] = routes; window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] = true; } export default router;