permission.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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','/noPermission'] // 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.phone
  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. // console.log(accessRoutes, 'accessRoutes')
  97. const isMenu = accessRoutes && accessRoutes.length > 0 ? true : false
  98. accessRoutes.push({ path: '*', redirect: '/404', hidden: true })
  99. console.log(accessRoutes,'accessRoutes')
  100. // 动态添加可访问的路由
  101. router.addRoutes(accessRoutes)
  102. // 确保addroutes完整的hack方法
  103. localStorage.removeItem('firstMenuUrl')
  104. // 设置replace:true,这样导航就不会留下历史记录。
  105. let firstMenu = getFirstMenu(accessRoutes)
  106. localStorage.setItem('firstMenuUrl', firstMenu)
  107. // console.log(firstMenu, 'firstMenu', isMenu)
  108. // 判断是否有菜单
  109. if(isMenu) {
  110. if(to.path == '/workbench') {
  111. next({ path: firstMenu, replace: true })
  112. } else {
  113. next({ ...to, replace: true })
  114. }
  115. } else {
  116. next({ path: '/noPermission', replace: true })
  117. }
  118. } catch (error) {
  119. // remove token and go to login page to re-login
  120. await store.dispatch('user/resetToken')
  121. if(error.msg) {
  122. Message.error(error.msg)
  123. }
  124. next(`/login`)
  125. NProgress.done()
  126. }
  127. }
  128. }
  129. } else {
  130. /* has no token*/
  131. console.log(to.path,'to.path')
  132. if (whiteList.indexOf(to.path) !== -1) {
  133. // in the free login whitelist, go directly
  134. next()
  135. } else {
  136. // other pages that do not have permission to access are redirected to the login page.
  137. next(`/login`)
  138. NProgress.done()
  139. }
  140. }
  141. })
  142. router.afterEach(() => {
  143. // finish progress bar
  144. NProgress.done()
  145. })