permission.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import Vue from 'vue'
  2. import router from './router'
  3. import store 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. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. const whiteList = ['login'] // no redirect whitelist
  11. const defaultRoutePath = '/home'
  12. router.beforeEach((to, from, next) => {
  13. NProgress.start() // start progress bar
  14. to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
  15. let token = store.state.user.token || sessionStorage.getItem(ACCESS_TOKEN)
  16. token = token != 'undefined' ? token : ''
  17. console.log(token == 'null')
  18. // 已登录
  19. if (token) {
  20. /* has token */
  21. if (to.path === '/user/login') {
  22. next({ path: defaultRoutePath })
  23. NProgress.done()
  24. } else {
  25. const userInfo = Vue.ls.get('userName')
  26. console.log(userInfo)
  27. if (userInfo) {
  28. store.dispatch('SetInfo', userInfo)
  29. if (store.getters.roles.permissionList.length === 0 && (token != 'null')) {
  30. store
  31. .dispatch('GetInfo', token)
  32. .then(res => {
  33. const roles = res
  34. console.log(roles, 'roles')
  35. if (roles.permissionList.length == 0 || !roles) {
  36. store.dispatch('Logout').then(() => {
  37. store.dispatch('ResetRoutes').then(() => {
  38. next({ path: '/user/login', query: { redirect: to.fullPath } })
  39. })
  40. })
  41. } else {
  42. store.dispatch('GenerateRoutes', { roles }).then(() => {
  43. // 根据roles权限生成可访问的路由表
  44. // 动态添加可访问路由表
  45. // router.addRoutes(store.getters.addRouters)
  46. const redirect = decodeURIComponent(from.query.redirect || to.path)
  47. if (to.path === redirect) {
  48. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  49. next({ ...to, replace: true })
  50. } else {
  51. // 跳转到目的路由
  52. next({ path: redirect })
  53. }
  54. })
  55. }
  56. })
  57. .catch(() => {
  58. notification.error({
  59. message: '错误',
  60. description: '请求用户信息失败,请重试'
  61. })
  62. store.dispatch('Logout').then(() => {
  63. next({ path: '/user/login', query: { redirect: to.fullPath } })
  64. })
  65. })
  66. } else {
  67. next()
  68. }
  69. } else {
  70. store.dispatch('Logout').then(() => {
  71. next({ path: '/user/login', query: { redirect: to.fullPath } })
  72. })
  73. }
  74. }
  75. } else {
  76. // 未登录
  77. if (whiteList.includes(to.name)) {
  78. // 在免登录白名单,直接进入
  79. next()
  80. } else {
  81. next({ path: '/user/login', query: { redirect: to.fullPath } })
  82. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  83. }
  84. }
  85. })
  86. router.afterEach(() => {
  87. NProgress.done() // finish progress bar
  88. })