permission.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  11. const allowList = ['login', 'applyLicense'] // no redirect allowList
  12. const loginRoutePath = '/user/login'
  13. const defaultRoutePath = '/index'
  14. router.beforeEach((to, from, next) => {
  15. NProgress.start() // start progress bar
  16. to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)} - ${domTitle}`))
  17. // 存储params参数到本地
  18. const paramsJson = JSON.stringify(to.params)
  19. if (paramsJson !== '{}') {
  20. localStorage.setItem('routerParams' + to.name, paramsJson)
  21. }
  22. /* has token */
  23. if (storage.get(ACCESS_TOKEN)) {
  24. if (to.path === loginRoutePath || to.path === '/') {
  25. next({ path: defaultRoutePath })
  26. NProgress.done()
  27. } else {
  28. // check login user.roles is null
  29. if (store.getters.roles.length === 0) {
  30. // request login userInfo
  31. store
  32. .dispatch('GetInfo')
  33. .then(res => {
  34. // const roles = res.result && res.result.role
  35. const roles = res.roles
  36. // generate dynamic router
  37. store.dispatch('GenerateRoutes', { roles }).then(() => {
  38. // 根据roles权限生成可访问的路由表
  39. // 动态添加可访问路由表
  40. router.addRoutes(store.getters.addRouters)
  41. // router.addRoutes(accessRoutes)
  42. // 请求带有 redirect 重定向时,登录自动重定向到该地址
  43. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  44. // const redirect = decodeURIComponent(from.query.redirect || to.path)
  45. // if (to.path === redirect) {
  46. // // set the replace: true so the navigation will not leave a history record
  47. // next({ ...to, replace: true })
  48. // } else {
  49. // // 跳转到目的路由
  50. // next({ path: redirect })
  51. // }
  52. })
  53. })
  54. .catch(() => {
  55. // notification.error({
  56. // message: '错误',
  57. // description: '请求用户信息失败,请重试'
  58. // })
  59. // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
  60. store.dispatch('Logout').then(() => {
  61. next({ path: '/' })
  62. })
  63. })
  64. } else {
  65. next()
  66. }
  67. }
  68. } else {
  69. if (allowList.includes(to.name)) {
  70. // 在免登录名单,直接进入
  71. next()
  72. } else {
  73. next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  74. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  75. }
  76. }
  77. })
  78. router.afterEach(() => {
  79. NProgress.done() // finish progress bar
  80. })