移动端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 rivejä
1.5 KiB

  1. import router from './router'
  2. import store from './store'
  3. import NProgress from 'nprogress'
  4. import 'nprogress/nprogress.css'
  5. import { getToken } from '@/utils/auth'
  6. NProgress.configure({ showSpinner: false })
  7. const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
  8. router.beforeEach((to, from, next) => {
  9. NProgress.start()
  10. if (getToken()) {
  11. /* has token*/
  12. if (to.path === '/login') {
  13. next({ path: '/' })
  14. NProgress.done()
  15. } else {
  16. if (store.getters.roles.length === 0) {
  17. next()
  18. // 判断当前用户是否已拉取完user_info信息
  19. store.dispatch('GetInfo').then(res => {
  20. // 拉取user_info
  21. const roles = res.roles
  22. // store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
  23. // // 根据roles权限生成可访问的路由表
  24. // router.addRoutes(accessRoutes) // 动态添加可访问路由表
  25. // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  26. // })
  27. next()
  28. }).catch(err => {
  29. store.dispatch('LogOut').then(() => {
  30. // Message.error(err)
  31. next({ path: '/' })
  32. })
  33. })
  34. } else {
  35. next()
  36. }
  37. }
  38. } else {
  39. // 没有token
  40. if (whiteList.indexOf(to.path) !== -1) {
  41. // 在免登录白名单,直接进入
  42. next()
  43. } else {
  44. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  45. NProgress.done()
  46. }
  47. }
  48. })
  49. router.afterEach(() => {
  50. NProgress.done()
  51. })