permission.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import { Message } from 'element-ui'
  5. import NProgress from 'nprogress' // progress bar
  6. import 'nprogress/nprogress.css' // progress bar style
  7. import { getToken } from '@/utils/auth' // get token from cookie
  8. import getPageTitle from '@/utils/get-page-title'
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. const whiteList = ['/login'] // no redirect whitelist
  11. let isOpen = false
  12. router.onError((error) => {
  13. if (error instanceof Error) {
  14. const isChunkLoadFailed = error.name.indexOf('chunk')
  15. const targetPath = router.history.pending.fullPath;
  16. if (isChunkLoadFailed && !isOpen) {
  17. isOpen = true
  18. // (router.app.$confirm)
  19. // router.push({ path: "/403", query: { path: targetPath } })
  20. router.app.$confirm("网站有更新请点击确定刷新页面?", "更新提示", {
  21. confirmButtonText: "确定",
  22. cancelButtonText: "取消",
  23. type: "warning"
  24. })
  25. .then(() => {
  26. // router.replace(targetPath);
  27. location.hash = targetPath
  28. window.location.reload()
  29. })
  30. .catch(() => {
  31. return
  32. });
  33. }
  34. }
  35. });
  36. function getFirstMenu(routes) {
  37. console.log(routes)
  38. // let firstMenu = null
  39. // routes.forEach(item => {
  40. // if(!firstMenu && item.children?.length > 0) {
  41. // firstMenu = pathErgodic(item)
  42. // }
  43. // })
  44. // console.log('firstMenu',firstMenu)
  45. return routes[0].redirect
  46. }
  47. function pathErgodic(item) {
  48. let firstMenu = null
  49. item.children.forEach(i => {
  50. if(!firstMenu && i.children?.length > 0) {
  51. firstMenu = pathErgodic(i)
  52. } else {
  53. if(!firstMenu && checkPathUrl(i.path)) {
  54. firstMenu = i.path
  55. } else {
  56. if(!firstMenu && !i.hidden) {
  57. firstMenu = item.path + '/' + i.path
  58. }
  59. }
  60. }
  61. })
  62. return firstMenu
  63. }
  64. // 判断path有没有带/,并且是第一个位置
  65. function checkPathUrl(path) {
  66. return path.indexOf('/') === 0 ? true : false
  67. }
  68. router.beforeEach(async (to, from, next) => {
  69. // from.query = to.query
  70. // start progress bar
  71. NProgress.start()
  72. // set page title
  73. // document.title = getPageTitle(to.meta.title)
  74. document.title = getPageTitle()
  75. // determine whether the user has logged in
  76. const hasToken = getToken()
  77. if (hasToken) {
  78. if (to.path === '/login') {
  79. // 如果有tonken直接跳转到首页
  80. next({ path: '/' })
  81. NProgress.done()
  82. } else {
  83. const hasGetUserInfo = store.getters.name
  84. // 有名字 说明有用户信息 跳走
  85. if (hasGetUserInfo) {
  86. // const accessRoutes = await store.dispatch('permission/generateRoutes')
  87. // 动态添加可访问的路由
  88. // router.addRoutes(accessRoutes)
  89. next()
  90. } else {
  91. try {
  92. // 异步获取用户信息
  93. await store.dispatch('user/getInfo')
  94. // 请求接口 生成可访问路由
  95. const accessRoutes = await store.dispatch('permission/generateRoutes')
  96. accessRoutes.push({ path: '*', redirect: '/404', hidden: true })
  97. // 动态添加可访问的路由
  98. router.addRoutes(accessRoutes)
  99. // 确保addroutes完整的hack方法
  100. localStorage.removeItem('firstMenuUrl')
  101. // 设置replace:true,这样导航就不会留下历史记录。
  102. let firstMenu = getFirstMenu(accessRoutes)
  103. localStorage.setItem('firstMenuUrl', firstMenu)
  104. if(to.path == '/main/main') {
  105. next({ path: firstMenu, replace: true })
  106. } else {
  107. next({ ...to, replace: true })
  108. }
  109. } catch (error) {
  110. // remove token and go to login page to re-login
  111. await store.dispatch('user/resetToken')
  112. if(error.msg) {
  113. Message.error(error.msg)
  114. }
  115. next(`/login`)
  116. NProgress.done()
  117. }
  118. }
  119. }
  120. } else {
  121. /* has no token*/
  122. if (whiteList.indexOf(to.path) !== -1) {
  123. // in the free login whitelist, go directly
  124. next()
  125. } else {
  126. // other pages that do not have permission to access are redirected to the login page.
  127. next(`/login`)
  128. NProgress.done()
  129. }
  130. }
  131. })
  132. router.afterEach(() => {
  133. // finish progress bar
  134. NProgress.done()
  135. })