permission.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import NProgress from 'nprogress' // progress bar
  5. import '@/components/NProgress/nprogress.less' // progress bar custom style
  6. import notification from 'ant-design-vue/es/notification'
  7. import { setDocumentTitle, domTitle } from '@/utils/domUtil'
  8. import { ACCESS_TOKEN } from '@/store/mutation-types'
  9. import BaseTool from '@/utils/tool'
  10. import { GlobalConstant } from '@/constant'
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. const whiteList = ['login', 'register', 'test', 'registerResult', GlobalConstant.SINGLE_LOGIN_ROUTER_NAME] // no redirect whitelist
  13. router.beforeEach((to, from, next) => {
  14. if (to.path === GlobalConstant.PC_INDEX_PATH) {
  15. if (BaseTool.Util._isMobile()) {
  16. next({ path: GlobalConstant.MOBILE_INDEX_PATH, query: { } })
  17. NProgress.done()
  18. return
  19. }
  20. }
  21. // 单点登录
  22. if (to.name === GlobalConstant.SINGLE_LOGIN_ROUTER_NAME) {
  23. next()
  24. return
  25. }
  26. NProgress.start() // start progress bar
  27. to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
  28. if (Vue.ls.get(ACCESS_TOKEN)) {
  29. /* has token */
  30. if (to.path === '/user/login') {
  31. next({ path: GlobalConstant.PC_INDEX_PATH })
  32. NProgress.done()
  33. } else {
  34. console.log('to.path=================' + to.path)
  35. console.log('to.path=================2' + store.getters.roles.length)
  36. if (store.getters.roles.length === 0) {
  37. store
  38. .dispatch('GetInfo')
  39. .then(res => {
  40. const menus = res.data.menuTrees
  41. store.dispatch('GenerateRoutes', { menus }).then(() => {
  42. // 根据roles权限生成可访问的路由表
  43. // 动态添加可访问路由表
  44. router.addRoutes(store.getters.addRouters)
  45. // 根据权限前往工作台
  46. let newPath = null
  47. if (to.path === '/toWorkplaceBacklog') {
  48. const role = res.data.roles.join()
  49. console.log('to.path=================3' + role)
  50. switch (true) {
  51. case role.includes('repair'):
  52. newPath = '/NewWorkplaceBacklog'
  53. break
  54. case role.includes('store'):
  55. newPath = '/StoreWorkplaceBacklog'
  56. break
  57. default :
  58. newPath = '/WorkplaceBacklog'
  59. break
  60. }
  61. } else {
  62. newPath = to.path
  63. }
  64. const redirect = decodeURIComponent(from.query.redirect || newPath)
  65. if (newPath === redirect) {
  66. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  67. next({ ...to, path: newPath, replace: true })
  68. } else {
  69. // 跳转到目的路由
  70. next({ path: redirect })
  71. }
  72. })
  73. })
  74. .catch(() => {
  75. notification.error({
  76. message: '错误',
  77. description: '请求用户信息失败,请重试'
  78. })
  79. store.dispatch('Logout').then(() => {
  80. next({ path: '/user/login', query: { redirect: to.fullPath } })
  81. })
  82. })
  83. } else {
  84. next()
  85. }
  86. }
  87. } else {
  88. if (whiteList.includes(to.name)) {
  89. // 在免登录白名单,直接进入
  90. next()
  91. } else {
  92. // next({ path: '/user/login', query: { redirect: to.fullPath } })
  93. next({ path: '/user/login', query: { } })
  94. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  95. }
  96. }
  97. })
  98. router.afterEach(() => {
  99. NProgress.done() // finish progress bar
  100. })