index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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: '/agentProgramAddTeam', component: () => import('@/views/modules/agent/company/addTeam'), name: 'agentProgramAddTeam', meta: { title: '添加团队', isTab: false } },
  50. { path: '/agentKnowledgeAdd', component: () => import('@/views/modules/agent/knowledge/add'), name: 'agentKnowledgeAdd', meta: { title: '创建知识库', isTab: false } },
  51. { path: '/agentKnowledgeDetail', component: () => import('@/views/modules/agent/knowledge/detail'), name: 'agentKnowledgeDetail', meta: { title: '知识库详情', isTab: false } },
  52. { path: '/agentKnowledgeCategory', component: () => import('@/views/modules/agent/knowledgeCategory'), name: 'agentKnowledgeCategory', meta: { title: '类目管理', isTab: false } },
  53. { path: '/agentKnowledgeFile', component: () => import('@/views/modules/agent/knowledge/file'), name: 'agentKnowledgeFile', meta: { title: '知识库文件', isTab: false } },
  54. { path: '/agentQuestionnaireSchedule', component: () => import('@/views/modules/agent/questionnaire/schedule'), name: 'agentQuestionnaireSchedule', meta: { title: '问卷作答进度', isTab: false } },
  55. { path: '/agentQuestionnaireReport', component: () => import('@/views/modules/agent/questionnaire/report'), name: 'agentQuestionnaireReport', meta: { title: '问卷报告', isTab: false } },
  56. { path: '/agentQuestionnaireDetail', component: () => import('@/views/modules/agent/questionnaire/detail'), name: 'agentQuestionnaireDetail', meta: { title: '问卷详情', isTab: false } },
  57. { path: '/agentQuestionnairePublish', component: () => import('@/views/modules/agent/questionnaire/publish'), name: 'agentQuestionnairePublish', meta: { title: '发布问卷', isTab: false } },
  58. { path: '/agentUserInfo', component: () => import('@/views/modules/agent/userInfo'), name: 'agentUserInfo', meta: { title: '个人信息', isTab: false } },
  59. { path: '/agentTeamUser', component: () => import('@/views/modules/agent/company/teamUser'), name: 'agentTeamUser', meta: { title: '成员管理', isTab: false } },
  60. ]
  61. }
  62. export function addDynamicRoute(routeParams, router) {
  63. // 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
  64. var routeName = routeParams.routeName;
  65. var dynamicRoute = window.SITE_CONFIG["dynamicRoutes"].filter(
  66. (item) => item.name === routeName
  67. )[0];
  68. if (dynamicRoute) {
  69. return router.push({ name: routeName, params: routeParams.params });
  70. }
  71. // 否则: 添加并全局变量保存, 再跳转
  72. dynamicRoute = {
  73. path: routeName,
  74. component: () => import(`@/views/modules/${routeParams.path}`),
  75. name: routeName,
  76. meta: {
  77. ...window.SITE_CONFIG["contentTabDefault"],
  78. menuId: routeParams.menuId,
  79. title: `${routeParams.title}`,
  80. },
  81. };
  82. router.addRoutes([
  83. {
  84. ...moduleRoutes,
  85. name: `main-dynamic__${dynamicRoute.name}`,
  86. children: [dynamicRoute],
  87. },
  88. ]);
  89. window.SITE_CONFIG["dynamicRoutes"].push(dynamicRoute);
  90. router.push({ name: dynamicRoute.name, params: routeParams.params });
  91. }
  92. const router = new Router({
  93. mode: "hash",
  94. scrollBehavior: () => ({ y: 0 }),
  95. routes: pageRoutes.concat(moduleRoutes),
  96. });
  97. router.beforeEach((to, from, next) => {
  98. if (to.path == "infoWindow") {
  99. next();
  100. } else {
  101. // 添加动态(菜单)路由
  102. // 已添加或者当前路由为页面路由, 可直接访问
  103. if (
  104. window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] ||
  105. fnCurrentRouteIsPageRoute(to, pageRoutes)
  106. ) {
  107. try {
  108. router.app.$options.store.state.sidebarMenuActiveName = [];
  109. var proute = window.SITE_CONFIG["menuList"].filter(
  110. (item) =>
  111. item.children.filter((child) => child.id == to.meta.menuId).length >
  112. 0
  113. );
  114. router.app.$options.store.state.sidebarMenuActiveName.push(
  115. proute.length > 0 ? proute[0].name : ""
  116. );
  117. router.app.$options.store.state.sidebarMenuActiveName.push(
  118. to.meta.title
  119. );
  120. } catch { }
  121. return next();
  122. }
  123. // 获取字典列表, 添加并全局变量保存
  124. http
  125. .get("/sys/dict/type/all")
  126. .then(({ data: res }) => {
  127. if (res.code !== 0) {
  128. return;
  129. }
  130. window.SITE_CONFIG["dictList"] = res.data;
  131. })
  132. .catch(() => { });
  133. // 获取菜单列表, 添加并全局变量保存
  134. http
  135. .get("/sys/menu/nav")
  136. .then(({ data: res }) => {
  137. if (res.code !== 0) {
  138. Vue.prototype.$message.error(res.msg);
  139. return next({ name: "login" });
  140. }
  141. window.SITE_CONFIG["menuList"] = res.data;
  142. fnAddDynamicMenuRoutes(window.SITE_CONFIG["menuList"]);
  143. next({ ...to, replace: true });
  144. })
  145. .catch(() => {
  146. next({ name: "login" });
  147. });
  148. }
  149. });
  150. router.onError((error) => {
  151. const pattern = /Loading chunk (.*?)+ failed/g;
  152. const isChunkLoadFailed = error.message.match(pattern);
  153. //const targetPath = router.history.pending.fullPath;
  154. if (isChunkLoadFailed) {
  155. //router.replace(targetPath);
  156. location.reload();
  157. }
  158. });
  159. /**
  160. * 判断当前路由是否为页面路由
  161. * @param {*} route 当前路由
  162. * @param {*} pageRoutes 页面路由
  163. */
  164. function fnCurrentRouteIsPageRoute(route, pageRoutes = []) {
  165. var temp = [];
  166. for (var i = 0; i < pageRoutes.length; i++) {
  167. if (route.path === pageRoutes[i].path) {
  168. return true;
  169. }
  170. if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
  171. temp = temp.concat(pageRoutes[i].children);
  172. }
  173. }
  174. return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false;
  175. }
  176. /**
  177. * 添加动态(菜单)路由
  178. * @param {*} menuList 菜单列表
  179. * @param {*} routes 递归创建的动态(菜单)路由
  180. */
  181. function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
  182. var temp = [];
  183. for (var i = 0; i < menuList.length; i++) {
  184. if (menuList[i].children && menuList[i].children.length >= 1) {
  185. temp = temp.concat(menuList[i].children);
  186. continue;
  187. }
  188. // 组装路由
  189. var route = {
  190. path: "",
  191. component: null,
  192. name: "",
  193. meta: {
  194. ...window.SITE_CONFIG["contentTabDefault"],
  195. menuId: menuList[i].id,
  196. title: menuList[i].name,
  197. },
  198. };
  199. let URL = (menuList[i].url || "").replace(/{{([^}}]+)?}}/g, (s1, s2) =>
  200. eval(s2)
  201. ); // URL支持{{ window.xxx }}占位符变量
  202. if (/^http[s]?:\/\/.*/.test(URL)) {
  203. route["path"] = route["name"] = `i-${menuList[i].id}`;
  204. route["meta"]["iframeURL"] = URL;
  205. } else {
  206. try {
  207. URL = URL.replace(/^\//, "").replace(/_/g, "-");
  208. route["path"] = route["name"] = URL.replace(/\//g, "-");
  209. route["component"] = () => import(`@/views/modules/${URL}`);
  210. } catch { }
  211. }
  212. routes.push(route);
  213. }
  214. if (temp.length >= 1) {
  215. return fnAddDynamicMenuRoutes(temp, routes);
  216. }
  217. // 添加路由
  218. router.addRoutes([
  219. {
  220. ...moduleRoutes,
  221. name: "main-dynamic-menu",
  222. children: routes,
  223. },
  224. { path: "*", redirect: { name: "404" } },
  225. ]);
  226. window.SITE_CONFIG["dynamicMenuRoutes"] = routes;
  227. window.SITE_CONFIG["dynamicMenuRoutesHasAdded"] = true;
  228. }
  229. export default router;