index.js 8.7 KB

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