index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * 全站路由配置
  3. * 代码中路由统一使用path属性跳转
  4. */
  5. import Vue from 'vue'
  6. import Router from 'vue-router'
  7. import {isURL} from '@/utils/validate'
  8. import {clearLoginInfo} from '@/utils'
  9. Vue.use(Router)
  10. const routerPush = Router.prototype.push
  11. Router.prototype.push = function push (location) {
  12. return routerPush.call(this, location).catch(error => error)
  13. }
  14. // 开发环境不使用懒加载
  15. const _import = require('./import-' + process.env.NODE_ENV)
  16. // 全局路由
  17. const globalRoutes = [
  18. {path: '/login', component: _import('modules/sys/login/login'), name: 'login', meta: {title: '登录'}},
  19. {path: '/casLogin', component: _import('common/CasLogin'), name: 'casLogin', meta: { title: 'CAS登录' }},
  20. {path: '/bigScreen', component: _import('modules/bigScreen/index'), name: 'bigScreen', meta: { title: '大屏' }},
  21. {path: '/bigScreen2', component: _import('modules/bigScreen2/index'), name: 'bigScreen2', meta: { title: '大屏' }}
  22. ]
  23. // 主入口路由
  24. const mainRoutes = {
  25. path: '/',
  26. component: _import('main'),
  27. name: 'main',
  28. redirect: {name: 'home'},
  29. meta: {title: '整体布局'},
  30. children: [
  31. {path: '/redirect/:path(.*)', component: _import('modules/redirect/index')},
  32. {path: '/home', redirect: '/sys/dashboard/analysis/index', name: 'home'},
  33. {path: '/visit', component: _import('modules/sys/dashboard/analysis/visitStatistics'), name: 'visit'},
  34. {path: '/flowable/task/TaskForm', component: _import('modules/flowable/task/TaskForm'), name: 'task-form', meta: {title: '流程表单'}},
  35. {path: '/flowable/task/TaskFormEdit', component: _import('modules/flowable/task/TaskFormEdit'), name: 'task-form-edit', meta: {title: '流程表单'}},
  36. {path: '/flowable/task/TaskFormDetail', component: _import('modules/flowable/task/TaskFormDetail'), name: 'task-form-detail', meta: {title: '流程表单详情'}},
  37. {path: '/form/generateList', component: _import('modules/form/GenerateList'), name: 'form-preview-list', meta: {title: '列表'}},
  38. {path: '/echarts/GenerateChart', component: _import('modules/echarts/GenerateChart'), name: 'echarts-generate', meta: {title: '预览图表'}},
  39. {path: '/ureport/designer', component: null, name: 'ureport-designer', meta: {title: '预览报表', type: 'iframe', menuId: 'ureport-designer'}},
  40. {path: '/ureport/preview', component: null, name: 'ureport-preview', meta: {title: '预览报表', type: 'iframe', menuId: 'ureport-preview'}},
  41. {path: '/form/explorer', component: null, name: 'form-explorer', meta: {title: '浏览器', type: 'iframe'}},
  42. {path: '/database/datatable/TableForm', component: _import('modules/database/datatable/TableForm'), name: 'table-form', meta: {title: '数据库表详情'}},
  43. {path: '/404', component: _import('common/404'), name: '404', meta: {title: '404未找到'}}
  44. ]
  45. }
  46. const router = new Router({
  47. mode: 'hash',
  48. scrollBehavior: () => ({y: 0}),
  49. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  50. routes: globalRoutes.concat(mainRoutes)
  51. })
  52. // 添加动态(菜单)路由
  53. router.beforeEach((to, from, next) => {
  54. let token = Vue.cookie.get('token')
  55. if (!token || !/\S/.test(token)) { // token为空,跳转到login登录
  56. clearLoginInfo()
  57. if (process.env.VUE_APP_SSO_LOGIN === 'true') { // 如果是单点登录
  58. if (to.name === 'casLogin') { // 单点登录跳转页面获取token
  59. next()
  60. } else {
  61. window.location.href = `${process.env.VUE_APP_CAS_SERVER}/login?service=${process.env.VUE_APP_CLIENT_LOGIN}`
  62. }
  63. } else {
  64. if (fnCurrentRouteType(to, globalRoutes) === 'global') {
  65. next()
  66. } else {
  67. next({name: 'login'})
  68. }
  69. }
  70. } else if (router.options.isAddDynamicMenuRoutes) { // 如果已经包含权限
  71. next()
  72. } else { // 请求权限
  73. let routerList = localStorage.getItem('routerList')
  74. if (routerList) {
  75. router.options.isAddDynamicMenuRoutes = true
  76. fnAddDynamicMenuRoutes(JSON.parse(routerList), [])
  77. next({...to, replace: true})
  78. } else {
  79. clearLoginInfo()
  80. next()
  81. console.log(`请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
  82. }
  83. }
  84. })
  85. /**
  86. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  87. * @param {*} route 当前路由
  88. */
  89. function fnCurrentRouteType (route, globalRoutes = []) {
  90. let temp = []
  91. for (let i = 0; i < globalRoutes.length; i++) {
  92. if (route.path === globalRoutes[i].path) {
  93. return 'global'
  94. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  95. temp = temp.concat(globalRoutes[i].children)
  96. }
  97. }
  98. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  99. }
  100. /**
  101. * 添加动态(菜单)路由
  102. * @param {*} menuList 菜单列表
  103. * @param {*} routes 递归创建的动态(菜单)路由
  104. */
  105. // eslint-disable-next-line no-unused-vars
  106. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  107. let temp = []
  108. for (let i = 0; i < menuList.length; i++) {
  109. if (menuList[i].children && menuList[i].children.length >= 1) {
  110. temp = temp.concat(menuList[i].children)
  111. }
  112. if (menuList[i].href && /\S/.test(menuList[i].href)) {
  113. menuList[i].href = menuList[i].href.replace(/[/]$/, '')
  114. const route = {
  115. path: menuList[i].href.split('?')[0],
  116. component: null,
  117. name: menuList[i].href.replace(/^\//g, '').replace(/[/]/g, '-').replace(/[?]/g, '-').replace(/&/g, '-').replace(/=/g, '-'),
  118. meta: {
  119. parentIds: menuList[i].parentIds,
  120. menuId: menuList[i].id,
  121. title: menuList[i].name,
  122. isDynamic: true,
  123. type: menuList[i].target,
  124. affix: menuList[i].affix === '1',
  125. iframeUrl: ''
  126. }
  127. }
  128. // url以http[s]://开头, 通过iframe展示
  129. if (menuList[i].target === 'iframe') {
  130. route.path = '/' + route.meta.menuId
  131. if (isURL(menuList[i].href)) {
  132. route['meta']['iframeUrl'] = menuList[i].href
  133. } else {
  134. route['meta']['iframeUrl'] = `${process.env.VUE_APP_SERVER_URL}${menuList[i].href}`
  135. }
  136. } else {
  137. try {
  138. if (menuList[i].href) {
  139. route['component'] = _import(`modules${menuList[i].href.split('?')[0]}`) || null
  140. }
  141. } catch (e) {
  142. console.log(e)
  143. }
  144. }
  145. let exist = routes.filter(r =>
  146. r.path === route.path
  147. ).length === 0 && mainRoutes.children.filter(c =>
  148. c.path === route.path
  149. ).length === 0
  150. if (exist) { // 如果路由不存在则添加
  151. routes.push(route)
  152. }
  153. }
  154. }
  155. if (temp.length >= 1) {
  156. fnAddDynamicMenuRoutes(temp, routes)
  157. } else {
  158. mainRoutes.name = 'main-dynamic'
  159. mainRoutes.children = routes
  160. router.addRoute(mainRoutes)
  161. router.addRoute({path: '*', redirect: {name: '404'}})
  162. localStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || []))
  163. }
  164. }
  165. export default router