permission.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if (store.getters.roles.length === 0) {
  35. store
  36. .dispatch('GetInfo')
  37. .then(res => {
  38. const menus = res.data.menuTrees
  39. store.dispatch('GenerateRoutes', { menus }).then(() => {
  40. // 根据roles权限生成可访问的路由表
  41. // 动态添加可访问路由表
  42. router.addRoutes(store.getters.addRouters)
  43. // 根据权限前往工作台
  44. let newPath = null
  45. if (to.path === '/toWorkplaceBacklog') {
  46. const role = res.data.roles.join()
  47. switch (true) {
  48. case role.includes('repair'):
  49. newPath = '/NewWorkplaceBacklog'
  50. break
  51. case role.includes('store'):
  52. newPath = '/StoreWorkplaceBacklog'
  53. break
  54. }
  55. } else {
  56. newPath = to.path
  57. }
  58. const redirect = decodeURIComponent(from.query.redirect || newPath)
  59. if (newPath === redirect) {
  60. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  61. next({ ...to, path: newPath, replace: true })
  62. } else {
  63. // 跳转到目的路由
  64. next({ path: redirect })
  65. }
  66. })
  67. })
  68. .catch(() => {
  69. notification.error({
  70. message: '错误',
  71. description: '请求用户信息失败,请重试'
  72. })
  73. store.dispatch('Logout').then(() => {
  74. next({ path: '/user/login', query: { redirect: to.fullPath } })
  75. })
  76. })
  77. } else {
  78. next()
  79. }
  80. }
  81. } else {
  82. if (whiteList.includes(to.name)) {
  83. // 在免登录白名单,直接进入
  84. next()
  85. } else {
  86. // next({ path: '/user/login', query: { redirect: to.fullPath } })
  87. next({ path: '/user/login', query: { } })
  88. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  89. }
  90. }
  91. })
  92. router.afterEach(() => {
  93. NProgress.done() // finish progress bar
  94. })