permission.js 3.5 KB

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