permission.js 3.7 KB

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