permission.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import { getToken } from '@/utils/auth' // get token from cookie
  7. import getPageTitle from '@/utils/get-page-title'
  8. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  9. const whiteList = ['/login', '/auth-redirect', '/401', '/404'] // no redirect whitelist
  10. router.beforeEach(async(to, from, next) => {
  11. // start progress bar
  12. NProgress.start()
  13. // set page title
  14. document.title = getPageTitle(to.meta.title)
  15. // if the query has token, need resave this token and replace the route
  16. if (to.query.token) {
  17. store.commit('user/SET_TOKEN', to.query.token)
  18. delete to.query.token
  19. next({ ...to })
  20. }
  21. // determine whether the user has logged in
  22. const hasToken = getToken()
  23. if (hasToken) {
  24. if (to.path === '/login') {
  25. // if is logged in, redirect to the home page
  26. next({ path: '/' })
  27. NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
  28. } else {
  29. // determine whether the user has obtained his permission roles through getInfo
  30. const hasRoles = store.getters.roles && store.getters.roles.length > 0
  31. if (hasRoles) {
  32. next()
  33. } else {
  34. try {
  35. // get user info
  36. // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
  37. const { menus } = await store.dispatch('user/getInfo')
  38. // // generate accessible routes map based on roles
  39. // const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
  40. // // dynamically add accessible routes
  41. // router.addRoutes(accessRoutes)
  42. // generate accessible routes map based on menus
  43. const accessRoutes = await store.dispatch('permission/generateRoutes', menus)
  44. // dynamically add accessible routes
  45. router.addRoutes(accessRoutes)
  46. // hack method to ensure that addRoutes is complete
  47. // set the replace: true, so the navigation will not leave a history record
  48. next({ ...to, replace: true })
  49. } catch (error) {
  50. // remove token and go to login page to re-login
  51. await store.dispatch('user/resetToken')
  52. Message.error(error || 'Has Error')
  53. next(`/login?redirect=${to.path}`)
  54. NProgress.done()
  55. }
  56. }
  57. }
  58. } else {
  59. /* has no token*/
  60. if (whiteList.indexOf(to.path) !== -1) {
  61. // in the free login whitelist, go directly
  62. next()
  63. } else {
  64. // other pages that do not have permission to access are redirected to the login page.
  65. next(`/login?redirect=${to.path}`)
  66. NProgress.done()
  67. }
  68. }
  69. })
  70. router.afterEach(() => {
  71. // finish progress bar
  72. NProgress.done()
  73. })