permission.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* eslint-disable no-use-before-define */
  2. import router from './router'
  3. import store from './store'
  4. import {
  5. Message
  6. } from 'element-ui'
  7. import NProgress from 'nprogress' // progress bar
  8. import 'nprogress/nprogress.css' // progress bar style
  9. import {
  10. getUserId,
  11. getToken
  12. } from '@/utils/auth' // get token from cookie
  13. import {
  14. adminBuild
  15. } from '@/api/user.js'
  16. import { filterAsyncRouter } from '@/store/modules/permission.js'
  17. import getPageTitle from '@/utils/get-page-title'
  18. NProgress.configure({
  19. showSpinner: false
  20. }) // NProgress Configuration
  21. const whiteList = [ '/login' ] // no redirect whitelist
  22. router.beforeEach(async (to, from, next) => {
  23. // start progress bar
  24. NProgress.start()
  25. // set page title
  26. document.title = getPageTitle(to.meta.title)
  27. // determine whether the user has logged in
  28. const hasToken = getToken()
  29. if (hasToken) {
  30. if (to.path === '/login') {
  31. // if is logged in, redirect to the home page
  32. next({
  33. path: '/'
  34. })
  35. NProgress.done()
  36. } else {
  37. // if (store.getters.routers.length === 0) {}
  38. // const hasGetUserInfo = store.getters.name
  39. // if (hasGetUserInfo) {
  40. // console.log('hasGetUserInfo')
  41. // // console.log('hasGetUserInfo', hasGetUserInfo)
  42. // next()
  43. // }
  44. if (store.getters.routers.length === 0) { // 条件加载
  45. loadMenus(next, to)
  46. store.commit('SET_LOAD', true)
  47. } else if (!store.getters.hasLoad) { // 是否加载过动态路由
  48. // // 修改hasLoad为false,防止死循环
  49. store.commit('SET_LOAD', true)
  50. loadMenus(next, to)
  51. } else {
  52. try {
  53. next()
  54. } catch (error) {
  55. // remove token and go to login page to re-login
  56. await store.dispatch('user/resetToken')
  57. Message.error(error || 'Has Error')
  58. next(`/login?redirect=${to.path}`)
  59. NProgress.done()
  60. }
  61. }
  62. }
  63. } else {
  64. /* has no token*/
  65. if (whiteList.indexOf(to.path) !== -1) {
  66. // in the free login whitelist, go directly
  67. console.log('no token')
  68. next()
  69. } else {
  70. // other pages that do not have permission to access are redirected to the login page.
  71. next(`/login?redirect=${to.path}`)
  72. NProgress.done()
  73. }
  74. }
  75. })
  76. export const loadMenus = (next, to) => {
  77. adminBuild({ platformUserId: getUserId() }).then((res) => {
  78. // console.log(res.data) // 获取动态路由
  79. const asyncRouter = filterAsyncRouter(res.data)
  80. // 画布设置
  81. asyncRouter.forEach((item) => {
  82. if (item.path.indexOf('.html') !== -1) {
  83. item.path = item.path + '?' + getToken()
  84. }
  85. })
  86. // 异常跳转添加
  87. asyncRouter.push({ path: '*', redirect: '/404', hidden: true })
  88. store.dispatch('GenerateRoutes', asyncRouter).then(() => { // 存储路由
  89. router.addRoutes(asyncRouter) // 动态添加可访问路由表
  90. next({ ...to, replace: true })
  91. })
  92. })
  93. }
  94. router.afterEach(() => {
  95. // finish progress bar
  96. NProgress.done()
  97. })