permission.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import router from './router'
  2. import store from './store'
  3. import storage from 'store'
  4. import NProgress from 'nprogress' // progress bar
  5. import '@/components/NProgress/nprogress.less' // progress bar custom style
  6. import notification from 'ant-design-vue/es/notification'
  7. import { setDocumentTitle, domTitle } from '@/utils/domUtil'
  8. import { ACCESS_TOKEN } from '@/store/mutation-types'
  9. import { i18nRender } from '@/locales'
  10. import { isEmpty } from '@/utils/util'
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. // 登录页
  13. const loginRoutePath = '/passport/login'
  14. // 白名单
  15. const whiteList = [loginRoutePath] // no redirect whitelist
  16. // 默认路由地址
  17. const defaultRoutePath = '/'
  18. router.beforeEach((to, from, next) => {
  19. NProgress.start() // start progress bar
  20. to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)}`))
  21. // 超管后台自动登录
  22. const { query } = to
  23. if (to.path === loginRoutePath && query['superLogin']) {
  24. store.dispatch('SuperLogin', {
  25. userId: query['userId'],
  26. token: query['token']
  27. })
  28. }
  29. /* has token */
  30. if (storage.get(ACCESS_TOKEN)) {
  31. // 已登录情况下访问登录页 - 跳转到默认页
  32. if (to.path === loginRoutePath) {
  33. next({ path: defaultRoutePath })
  34. NProgress.done()
  35. } else {
  36. // check login user.roles is null
  37. if (isEmpty(store.getters.roles)) {
  38. // 获取当前登录的用户信息
  39. store
  40. .dispatch('GetInfo')
  41. .then(({ roles }) => {
  42. // 根据roles权限生成可访问的路由表
  43. store.dispatch('GenerateRoutes', { roles })
  44. .then(routers => {
  45. // 动态添加可访问路由表
  46. // router.addRoutes(routers)
  47. for (let x of routers) {
  48. router.addRoute(x)
  49. }
  50. // 请求带有 redirect 重定向时,登录自动重定向到该地址
  51. const redirect = decodeURIComponent(from.query.redirect || to.path)
  52. if (to.path === redirect) {
  53. // set the replace: true so the navigation will not leave a history record
  54. next({ ...to, replace: true })
  55. } else {
  56. // 跳转到目的路由
  57. next({ path: redirect })
  58. }
  59. })
  60. })
  61. .catch(() => {
  62. notification.error({
  63. message: '错误',
  64. description: '请求用户信息失败,请重试'
  65. })
  66. // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
  67. store.dispatch('Logout').then(() => {
  68. next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  69. })
  70. })
  71. } else {
  72. next()
  73. }
  74. }
  75. } else {
  76. if (whiteList.includes(to.path)) {
  77. // 在免登录白名单,直接进入
  78. next()
  79. } else {
  80. next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  81. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  82. }
  83. }
  84. })
  85. router.afterEach(() => {
  86. NProgress.done() // finish progress bar
  87. })