index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. // 进度条
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import store from '@/store/index.js'
  7. import util from '@/utils/util.js'
  8. import setting from '@/setting.js'
  9. import i18n from '@/utils/i18n.js'
  10. // 验权
  11. import { getToken, getRefreshToken } from '@/utils/auth'
  12. // 路由数据
  13. import routes from './routes'
  14. // fix vue-router NavigationDuplicated
  15. const VueRouterPush = VueRouter.prototype.push
  16. VueRouter.prototype.push = function push(location) {
  17. return VueRouterPush.call(this, location).catch(err => err)
  18. }
  19. const VueRouterReplace = VueRouter.prototype.replace
  20. VueRouter.prototype.replace = function replace(location) {
  21. return VueRouterReplace.call(this, location).catch(err => err)
  22. }
  23. Vue.use(VueRouter)
  24. // 导出路由 在 main.js 里使用
  25. const router = new VueRouter({
  26. routes
  27. })
  28. /**
  29. * 路由拦截
  30. * 权限验证
  31. */
  32. router.beforeEach(async(to, from, next) => {
  33. // 确认已经加载多标签页数据
  34. await store.dispatch('ibps/page/isLoaded')
  35. // 确认已经加载组件尺寸设置
  36. await store.dispatch('ibps/size/isLoaded')
  37. // 关闭搜索面板
  38. store.commit('ibps/search/set', false)
  39. // 进度条
  40. NProgress.start()
  41. const locking = util.cookies.get('locking')
  42. if (locking === 'locked' && to.name !== 'locking') { // 判断当前是否是锁定状态
  43. next({
  44. replace: true,
  45. name: 'locking'
  46. })
  47. } else if (locking === 'unlock' && to.name === 'locking') {
  48. next(false)
  49. } else {
  50. // 这里将cookie里是否存有token作为验证是否登录的条件
  51. const hasToken = getToken()
  52. if (hasToken && hasToken !== 'undefined') { // 从cookie 获取用户token
  53. // 登录 锁定 401没权限 403禁止访问
  54. if (to.name === 'locking' || to.name === 'login' || to.name === 'error401' || to.name === 'error403' || to.name === 'nomenu') {
  55. next()
  56. } else {
  57. // 判断是否有子系统和是否有菜单
  58. if (util.isEmpty(store.getters.system) || util.isEmpty(store.getters.menus)) {
  59. store.dispatch('ibps/user/load').then(res => { // 拉取用户信息,避免刷新用户丢失
  60. const system = store.getters.system
  61. if ((util.isEmpty(system) || util.isEmpty(system.id))) {
  62. if (util.isEmpty(store.getters.tenants)) {
  63. if (to.name === 'systemSelect') return next()
  64. return next({ path: '/systemSelect', replace: true })
  65. }
  66. // 租户模式下跳转
  67. if (util.isNotEmpty(store.getters.tenants)) {
  68. if (to.name === 'tenantSelect') return next()
  69. if (to.name === 'systemSelect') return next()
  70. return next({ path: '/tenantSelect', replace: true })
  71. }
  72. }
  73. store.dispatch('ibps/menu/init', { systemId: system.id }).then(() => { // 根据用户菜单权限生成可访问的路由表
  74. if (util.isEmpty(store.getters.menus)) {
  75. next()
  76. } else {
  77. const addRouters = store.getters.routers
  78. if (addRouters && addRouters.length > 0) {
  79. // 动态添加可访问路由表
  80. router.addRoutes(addRouters)
  81. // 初始化加载或用户刷新页面后从数据库加载一系列的设置
  82. store.dispatch('ibps/account/loadInfo', {
  83. addRouters,
  84. menus: store.getters.menus
  85. })
  86. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  87. } else {
  88. next({ path: '/403', replace: true, query: { noGoBack: true }})
  89. }
  90. }
  91. }).catch((e) => {
  92. console.error(e)
  93. NProgress.done() // 结束Progress
  94. })
  95. }).catch((e) => {
  96. NProgress.done() // 结束Progress
  97. // 前台登出
  98. store.dispatch('ibps/account/fedLogout').then(() => {
  99. next({ name: 'login' })
  100. }).catch((err) => {
  101. console.error(err)
  102. })
  103. })
  104. } else { // 动态改变权限,判断是否有菜单权限,或者刷新页面
  105. if (to.matched.length === 0) { // 不能匹配的路由
  106. return next({ path: '401', replace: true, query: { noGoBack: true }})
  107. } else {
  108. next()
  109. }
  110. }
  111. // end
  112. }
  113. } else { /* has no token */
  114. // 判断refresh tonken是否过期
  115. const refreshToken = getRefreshToken()
  116. if (util.isNotEmpty(refreshToken)) {
  117. // 刷新tonken
  118. await store.dispatch('ibps/account/refreshToken').then(() => {
  119. next()
  120. }).catch((err) => {
  121. console.error(err)
  122. })
  123. } else {
  124. // 在免登录白名单,直接进入
  125. if (setting.whiteRouterList.indexOf(to.path) !== -1) {
  126. next()
  127. } else {
  128. // 没有登录的时候跳转到登录界面
  129. // 携带上登陆成功之后需要跳转的页面完整路径
  130. next({
  131. name: 'login',
  132. query: {
  133. redirect: to.fullPath
  134. }
  135. })
  136. NProgress.done()
  137. }
  138. }
  139. }
  140. }
  141. })
  142. router.afterEach(to => {
  143. // 进度条
  144. NProgress.done()
  145. // 多页控制 打开新的页面
  146. /* store.dispatch('ibps/page/open', to) */
  147. // 更改标题
  148. i18n.setTitle(to.meta.name || to.name, to.meta.title)
  149. })
  150. export default router