index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import Vue from "vue";
  2. import Router from "vue-router";
  3. import http from "@/utils/request";
  4. Vue.use(Router);
  5. const routerPush = Router.prototype.push
  6. Router.prototype.push = function push(location, onResolve, onReject) {
  7. if (onResolve || onReject) return routerPush.call(this, location, onResolve, onReject)
  8. return routerPush.call(this, location).catch(err => err)
  9. }
  10. // 页面路由(独立页面)
  11. export const pageRoutes = [
  12. {
  13. path: "/404",
  14. component: () => import("@/views/pages/404"),
  15. name: "404",
  16. meta: { title: "404未找到" },
  17. beforeEnter(to, from, next) {
  18. // 拦截处理特殊业务场景
  19. // 如果, 重定向路由包含__双下划线, 为临时添加路由
  20. if (/__.*/.test(to.redirectedFrom)) {
  21. return next(to.redirectedFrom.replace(/__.*/, ""));
  22. }
  23. next();
  24. },
  25. },
  26. {
  27. path: "/login",
  28. component: () => import("@/views/pages/login"),
  29. name: "login",
  30. meta: { title: "登录" },
  31. },
  32. {
  33. path: "/infoWindow",
  34. component: () => import("@/views/modules/situational/components/infoWindow/index.vue"),
  35. name: "infoWindow",
  36. meta: { title: "弹窗" },
  37. },
  38. ];
  39. // 模块路由(基于主入口布局页面)
  40. export const moduleRoutes = {
  41. path: "/",
  42. component: () => import("@/views/main"),
  43. name: "main",
  44. redirect: { name: "home" },
  45. meta: { title: "主入口布局" },
  46. children: [
  47. { path: '/home', component: () => import('@/views/modules/home'), name: 'home', meta: { title: '主页', isTab: false } },
  48. //创衡智能体
  49. { path: '/agentDialog', component: () => import('@/views/modules/agent/dialog'), name: 'agentDialog', meta: { title: '创建新对话', isTab: false } },
  50. { path: '/agentDialogResult', component: () => import('@/views/modules/agent/dialogResult'), name: 'agentDialogResult', meta: { title: '对话结果', isTab: false } },
  51. { path: '/agentProgram', component: () => import('@/views/modules/agent/program'), name: 'agentProgram', meta: { title: '项目管理', isTab: false } },
  52. { path: '/agentProgramAddTeam', component: () => import('@/views/modules/agent/program/addTeam'), name: 'agentProgramAddTeam', meta: { title: '添加团队', isTab: false } },
  53. { path: '/agentKnowledge', component: () => import('@/views/modules/agent/knowledge'), name: 'agentKnowledge', meta: { title: '知识库管理', isTab: false } },
  54. { path: '/agentKnowledgeAdd', component: () => import('@/views/modules/agent/knowledge/add'), name: 'agentKnowledgeAdd', meta: { title: '创建知识库', isTab: false } },
  55. { path: '/agentKnowledgeDetail', component: () => import('@/views/modules/agent/knowledge/detail'), name: 'agentKnowledgeDetail', meta: { title: '知识库详情', isTab: false } },
  56. { path: '/agentKnowledgeCategory', component: () => import('@/views/modules/agent/knowledgeCategory'), name: 'agentKnowledgeCategory', meta: { title: '类目管理', isTab: false } },
  57. { path: '/agentUser', component: () => import('@/views/modules/agent/user'), name: 'agentUser', meta: { title: '用户管理', isTab: false } },
  58. { path: '/agentReport', component: () => import('@/views/modules/agent/report'), name: 'agentReport', meta: { title: '报告管理', isTab: false } },
  59. { path: '/agentQuestionnaire', component: () => import('@/views/modules/agent/questionnaire'), name: 'agentQuestionnaire', meta: { title: '问卷管理', isTab: false } },
  60. { path: '/agentQuestionnaireList', component: () => import('@/views/modules/agent/questionnaireList'), name: 'agentQuestionnaireList', meta: { title: '问卷列表', isTab: false } },
  61. { path: '/agentQuestionnaireSchedule', component: () => import('@/views/modules/agent/questionnaire/schedule'), name: 'agentQuestionnaireSchedule', meta: { title: '问卷作答进度', isTab: false } },
  62. { path: '/agentQuestionnaireReport', component: () => import('@/views/modules/agent/questionnaire/report'), name: 'agentQuestionnaireReport', meta: { title: '问卷报告', isTab: false } },
  63. { path: '/agentQuestionnaireDetail', component: () => import('@/views/modules/agent/questionnaire/detail'), name: 'agentQuestionnaireDetail', meta: { title: '问卷详情', isTab: false } },
  64. { path: '/agentQuestionnairePublish', component: () => import('@/views/modules/agent/questionnaire/publish'), name: 'agentQuestionnairePublish', meta: { title: '发布问卷', isTab: false } },
  65. ]
  66. }
  67. export function addDynamicRoute(routeParams, router) {
  68. // 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
  69. var routeName = routeParams.routeName;
  70. var dynamicRoute = window.SITE_CONFIG["dynamicRoutes"].filter(
  71. (item) => item.name === routeName
  72. )[0];
  73. if (dynamicRoute) {
  74. return router.push({ name: routeName, params: routeParams.params });
  75. }
  76. // 否则: 添加并全局变量保存, 再跳转
  77. dynamicRoute = {
  78. path: routeName,
  79. component: () => import(`@/views/modules/${routeParams.path}`),
  80. name: routeName,
  81. meta: {
  82. ...window.SITE_CONFIG["contentTabDefault"],
  83. menuId: routeParams.menuId,
  84. title: `${routeParams.title}`,
  85. },
  86. };
  87. router.addRoutes([
  88. {
  89. ...moduleRoutes,
  90. name: `main-dynamic__${dynamicRoute.name}`,
  91. children: [dynamicRoute],
  92. },
  93. ]);
  94. window.SITE_CONFIG["dynamicRoutes"].push(dynamicRoute);
  95. router.push({ name: dynamicRoute.name, params: routeParams.params });
  96. }
  97. const router = new Router({
  98. mode: "hash",
  99. scrollBehavior: () => ({ y: 0 }),
  100. routes: pageRoutes.concat(moduleRoutes),
  101. });
  102. router.beforeEach((to, from, next) => {
  103. if (to.path == "infoWindow") {
  104. next();
  105. } else {
  106. // 添加动态(菜单)路由
  107. // 已添加或者当前路由为页面路由, 可直接访问
  108. if (
  109. window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] ||
  110. fnCurrentRouteIsPageRoute(to, pageRoutes)
  111. ) {
  112. try {
  113. router.app.$options.store.state.sidebarMenuActiveName = [];
  114. var proute = window.SITE_CONFIG["menuList"].filter(
  115. (item) =>
  116. item.children.filter((child) => child.id == to.meta.menuId).length >
  117. 0
  118. );
  119. router.app.$options.store.state.sidebarMenuActiveName.push(
  120. proute.length > 0 ? proute[0].name : ""
  121. );
  122. router.app.$options.store.state.sidebarMenuActiveName.push(
  123. to.meta.title
  124. );
  125. } catch { }
  126. return next();
  127. }
  128. // 获取字典列表, 添加并全局变量保存
  129. http
  130. .get("/sys/dict/type/all")
  131. .then(({ data: res }) => {
  132. if (res.code !== 0) {
  133. return;
  134. }
  135. window.SITE_CONFIG["dictList"] = res.data;
  136. })
  137. .catch(() => { });
  138. // 获取菜单列表, 添加并全局变量保存
  139. http
  140. .get("/sys/menu/nav")
  141. .then(({ data: res }) => {
  142. if (res.code !== 0) {
  143. Vue.prototype.$message.error(res.msg);
  144. return next({ name: "login" });
  145. }
  146. window.SITE_CONFIG["menuList"] = res.data;
  147. fnAddDynamicMenuRoutes(window.SITE_CONFIG["menuList"]);
  148. next({ ...to, replace: true });
  149. })
  150. .catch(() => {
  151. next({ name: "login" });
  152. });
  153. }
  154. });
  155. router.onError((error) => {
  156. const pattern = /Loading chunk (.*?)+ failed/g;
  157. const isChunkLoadFailed = error.message.match(pattern);
  158. //const targetPath = router.history.pending.fullPath;
  159. if (isChunkLoadFailed) {
  160. //router.replace(targetPath);
  161. location.reload();
  162. }
  163. });
  164. /**
  165. * 判断当前路由是否为页面路由
  166. * @param {*} route 当前路由
  167. * @param {*} pageRoutes 页面路由
  168. */
  169. function fnCurrentRouteIsPageRoute(route, pageRoutes = []) {
  170. var temp = [];
  171. for (var i = 0; i < pageRoutes.length; i++) {
  172. if (route.path === pageRoutes[i].path) {
  173. return true;
  174. }
  175. if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
  176. temp = temp.concat(pageRoutes[i].children);
  177. }
  178. }
  179. return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false;
  180. }
  181. /**
  182. * 添加动态(菜单)路由
  183. * @param {*} menuList 菜单列表
  184. * @param {*} routes 递归创建的动态(菜单)路由
  185. */
  186. function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
  187. var temp = [];
  188. for (var i = 0; i < menuList.length; i++) {
  189. if (menuList[i].children && menuList[i].children.length >= 1) {
  190. temp = temp.concat(menuList[i].children);
  191. continue;
  192. }
  193. // 组装路由
  194. var route = {
  195. path: "",
  196. component: null,
  197. name: "",
  198. meta: {
  199. ...window.SITE_CONFIG["contentTabDefault"],
  200. menuId: menuList[i].id,
  201. title: menuList[i].name,
  202. },
  203. };
  204. let URL = (menuList[i].url || "").replace(/{{([^}}]+)?}}/g, (s1, s2) =>
  205. eval(s2)
  206. ); // URL支持{{ window.xxx }}占位符变量
  207. if (/^http[s]?:\/\/.*/.test(URL)) {
  208. route["path"] = route["name"] = `i-${menuList[i].id}`;
  209. route["meta"]["iframeURL"] = URL;
  210. } else {
  211. try {
  212. URL = URL.replace(/^\//, "").replace(/_/g, "-");
  213. route["path"] = route["name"] = URL.replace(/\//g, "-");
  214. route["component"] = () => import(`@/views/modules/${URL}`);
  215. } catch { }
  216. }
  217. routes.push(route);
  218. }
  219. if (temp.length >= 1) {
  220. return fnAddDynamicMenuRoutes(temp, routes);
  221. }
  222. // 添加路由
  223. router.addRoutes([
  224. {
  225. ...moduleRoutes,
  226. name: "main-dynamic-menu",
  227. children: routes,
  228. },
  229. { path: "*", redirect: { name: "404" } },
  230. ]);
  231. window.SITE_CONFIG["dynamicMenuRoutes"] = routes;
  232. window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] = true;
  233. }
  234. export default router;