permission.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. import { isRelogin } from '@/utils/request'
  8. NProgress.configure({ showSpinner: false })
  9. const whiteList = ['/login', '/register', '/test01', '/ehrentrance', '/contractBacklogEntry',
  10. '/business/ehr/ehrpm-entrance', '/canteenAddFood', '/canteenAddMenu', '/menuScreen',
  11. '/foodScreen', '/business/wms/historical-route', '/business/SupAtttachment',
  12. '/business/purchase/form/transferOrder/bipPull-entrance', '/contractDetail',
  13. '/spdAddQuestion', '/business/wms/ProductMarking',
  14. ]
  15. const whiteListName = ['asLogin', "afterSales", "Feedback", "Progress", "UpdateInfo"]
  16. router.beforeEach((to, from, next) => {
  17. NProgress.start()
  18. if (getToken()) {
  19. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  20. /* has token*/
  21. if (to.path === '/login') {
  22. next({ path: '/' })
  23. NProgress.done()
  24. } else {
  25. if (store.getters.roles.length === 0) {
  26. isRelogin.show = true
  27. // 判断当前用户是否已拉取完user_info信息
  28. store.dispatch('GetInfo').then(() => {
  29. isRelogin.show = false
  30. store.dispatch('GenerateRoutes').then(accessRoutes => {
  31. // 根据roles权限生成可访问的路由表
  32. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  33. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  34. })
  35. }).catch(err => {
  36. store.dispatch('LogOut').then(() => {
  37. Message.error(err)
  38. next({ path: '/' })
  39. })
  40. })
  41. } else {
  42. next()
  43. }
  44. }
  45. } else {
  46. // 没有token
  47. if (whiteList.indexOf(to.path) !== -1 || whiteListName.indexOf(to.name) !== -1) {
  48. // 在免登录白名单,直接进入
  49. next()
  50. } else {
  51. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  52. NProgress.done()
  53. }
  54. }
  55. })
  56. router.afterEach(() => {
  57. NProgress.done()
  58. })