permission.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /* has token*/
  13. if (to.path === '/login') {
  14. next({ path: '/' })
  15. NProgress.done()
  16. } else {
  17. if (store.getters.roles.length === 0) {
  18. // 判断当前用户是否已拉取完user_info信息
  19. store.dispatch('GetInfo').then(() => {
  20. store.dispatch('GenerateRoutes').then(accessRoutes => {
  21. // 根据roles权限生成可访问的路由表
  22. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  23. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  24. })
  25. }).catch(err => {
  26. store.dispatch('LogOut').then(() => {
  27. //Message.error(err)
  28. next({ path: '/' })
  29. })
  30. })
  31. store.dispatch('GetSipAccount').then(() => {
  32. }).catch(err => {
  33. });
  34. // store.dispatch('getQwUserInfo').then(() => {
  35. //
  36. // }).catch(err => {
  37. //
  38. // });
  39. } else {
  40. next()
  41. }
  42. }
  43. } else {
  44. // 没有token
  45. if (whiteList.indexOf(to.path) !== -1) {
  46. // 在免登录白名单,直接进入
  47. next()
  48. } else {
  49. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  50. NProgress.done()
  51. }
  52. }
  53. })
  54. router.afterEach(() => {
  55. NProgress.done()
  56. })