permission.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(data => {
  42. const roles = data.roles
  43. // 根据roles权限生成可访问的路由表
  44. store.dispatch('GenerateRoutes', { roles })
  45. .then(routers => {
  46. // 动态添加可访问路由表
  47. router.addRoutes(routers)
  48. // 请求带有 redirect 重定向时,登录自动重定向到该地址
  49. const redirect = decodeURIComponent(from.query.redirect || to.path)
  50. if (to.path === redirect) {
  51. // set the replace: true so the navigation will not leave a history record
  52. next({ ...to, replace: true })
  53. } else {
  54. // 跳转到目的路由
  55. next({ path: redirect })
  56. }
  57. })
  58. })
  59. .catch(() => {
  60. notification.error({
  61. message: '错误',
  62. description: '请求用户信息失败,请重试'
  63. })
  64. // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
  65. store.dispatch('Logout').then(() => {
  66. next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  67. })
  68. })
  69. } else {
  70. next()
  71. }
  72. }
  73. } else {
  74. if (whiteList.includes(to.path)) {
  75. // 在免登录白名单,直接进入
  76. next()
  77. } else {
  78. next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  79. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  80. }
  81. }
  82. })
  83. router.afterEach(() => {
  84. NProgress.done() // finish progress bar
  85. })