permission.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 { setDocumentTitle, domTitle } from '@/utils/domUtil'
  7. import { ACCESS_TOKEN, ALL_APPS_MENU } from '@/store/mutation-types'
  8. import { Modal, notification } from 'ant-design-vue' // NProgress Configuration
  9. import { timeFix } from '@/utils/util'/// es/notification
  10. NProgress.configure({ showSpinner: false })
  11. const whiteList = ['login', 'register', 'registerResult'] // no redirect whitelist
  12. // 无默认首页的情况
  13. const defaultRoutePath = '/welcome'
  14. router.beforeEach((to, from, next) => {
  15. NProgress.start() // start progress bar
  16. to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
  17. if (Vue.ls.get(ACCESS_TOKEN)) {
  18. /* has token */
  19. if (to.path === '/user/login') {
  20. next({ path: defaultRoutePath })
  21. NProgress.done()
  22. } else {
  23. if (store.getters.roles.length === 0) {
  24. store
  25. .dispatch('GetInfo')
  26. .then(res => {
  27. if (res.menus.length < 1) {
  28. Modal.error({
  29. title: '提示:',
  30. content: '无菜单权限,请联系管理员',
  31. okText: '确定',
  32. onOk: () => {
  33. store.dispatch('Logout').then(() => {
  34. window.location.reload()
  35. })
  36. }
  37. })
  38. return
  39. }
  40. // eslint-disable-next-line camelcase
  41. const all_app_menu = Vue.ls.get(ALL_APPS_MENU)
  42. let antDesignmenus
  43. // eslint-disable-next-line camelcase
  44. if (all_app_menu == null) {
  45. const applocation = []
  46. res.apps.forEach(item => {
  47. const apps = { 'code': '', 'name': '', 'active': '', 'menu': '' }
  48. if (item.active) {
  49. apps.code = item.code
  50. apps.name = item.name
  51. apps.active = item.active
  52. apps.menu = res.menus
  53. antDesignmenus = res.menus
  54. } else {
  55. apps.code = item.code
  56. apps.name = item.name
  57. apps.active = item.active
  58. apps.menu = ''
  59. }
  60. applocation.push(apps)
  61. })
  62. Vue.ls.set(ALL_APPS_MENU, applocation, 7 * 24 * 60 * 60 * 1000)
  63. // 延迟 1 秒显示欢迎信息
  64. setTimeout(() => {
  65. notification.success({
  66. message: '欢迎',
  67. description: `${timeFix()},欢迎回来`
  68. })
  69. }, 1000)
  70. } else {
  71. antDesignmenus = Vue.ls.get(ALL_APPS_MENU)[0].menu
  72. }
  73. store.dispatch('GenerateRoutes', { antDesignmenus }).then(() => {
  74. // 动态添加可访问路由表
  75. router.addRoutes(store.getters.addRouters)
  76. // 请求带有 redirect 重定向时,登录自动重定向到该地址
  77. const redirect = decodeURIComponent(from.query.redirect || to.path)
  78. if (to.path === redirect) {
  79. next({ path: redirect })
  80. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  81. next({ ...to, replace: true })
  82. } else {
  83. // 跳转到目的路由
  84. next({ path: redirect })
  85. }
  86. })
  87. })
  88. .catch(() => {
  89. store.dispatch('Logout').then(() => {
  90. next({ path: '/user/login', query: { redirect: to.fullPath } })
  91. })
  92. })
  93. } else {
  94. next()
  95. }
  96. }
  97. } else {
  98. if (whiteList.includes(to.name)) {
  99. // 在免登录白名单,直接进入
  100. next()
  101. } else {
  102. next({ path: '/user/login', query: { redirect: to.fullPath } })
  103. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  104. }
  105. }
  106. })
  107. router.afterEach(() => {
  108. NProgress.done() // finish progress bar
  109. })