permission.js 1.7 KB

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