permission.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, getCompanyId } 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. // debugger
  12. if (getToken()) {
  13. /* has token*/
  14. if (to.path === '/login') {
  15. if (!store.getters.userId) {
  16. store.dispatch('GetInfo')
  17. }
  18. next()
  19. NProgress.done()
  20. } else {
  21. if (getCompanyId()) { // 判断是否有公司ID 没有去选择公司 不能进入
  22. if (store.getters.isInit) {
  23. next()
  24. } else {
  25. // 初始化路由
  26. store.dispatch('GenerateRoutes').then(accessRoutes => {
  27. store.commit('INIT')
  28. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  29. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  30. })
  31. }
  32. } else {
  33. next(`/login?redirect=${to.fullPath}`)
  34. }
  35. }
  36. } else {
  37. // 没有token
  38. if (store.getters.companyList.length > 0) {
  39. store.commit("SET_COMPANYLIST", [])
  40. }
  41. if (whiteList.indexOf(to.path) !== -1) {
  42. // 在免登录白名单,直接进入
  43. next()
  44. } else {
  45. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  46. NProgress.done()
  47. }
  48. }
  49. })
  50. router.afterEach(() => {
  51. NProgress.done()
  52. })