permission.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // console.log(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. let firstMenu = null
  38. routes.forEach(item => {
  39. if(!firstMenu && item.children.length > 0) {
  40. item.children.forEach(child => {
  41. if(!firstMenu && !child.hidden) {
  42. firstMenu = item.path + '/' + child.path
  43. }
  44. })
  45. }
  46. })
  47. return firstMenu
  48. }
  49. router.beforeEach(async (to, from, next) => {
  50. // from.query = to.query
  51. // start progress bar
  52. NProgress.start()
  53. // set page title
  54. // document.title = getPageTitle(to.meta.title)
  55. document.title = getPageTitle()
  56. // determine whether the user has logged in
  57. const hasToken = getToken()
  58. if (hasToken) {
  59. if (to.path === '/login') {
  60. // 如果有tonken直接跳转到首页
  61. next({ path: '/' })
  62. NProgress.done()
  63. } else {
  64. const hasGetUserInfo = store.getters.name
  65. // 有名字 说明有用户信息 跳走
  66. if (hasGetUserInfo) {
  67. // const accessRoutes = await store.dispatch('permission/generateRoutes')
  68. // 动态添加可访问的路由
  69. // router.addRoutes(accessRoutes)
  70. next()
  71. } else {
  72. try {
  73. // 异步获取用户信息
  74. await store.dispatch('user/getInfo')
  75. // 请求接口 生成可访问路由
  76. const accessRoutes = await store.dispatch('permission/generateRoutes')
  77. // 动态添加可访问的路由
  78. router.addRoutes(accessRoutes)
  79. // 确保addroutes完整的hack方法
  80. // 设置replace:true,这样导航就不会留下历史记录。
  81. let firstMenu = getFirstMenu(accessRoutes)
  82. if(to.path == '/main/main') {
  83. next({ path: firstMenu, replace: true })
  84. } else {
  85. next({ ...to, replace: true })
  86. }
  87. } catch (error) {
  88. // remove token and go to login page to re-login
  89. await store.dispatch('user/resetToken')
  90. Message.error(error || 'Has Error')
  91. next(`/login`)
  92. NProgress.done()
  93. }
  94. }
  95. }
  96. } else {
  97. /* has no token*/
  98. if (whiteList.indexOf(to.path) !== -1) {
  99. // in the free login whitelist, go directly
  100. next()
  101. } else {
  102. // other pages that do not have permission to access are redirected to the login page.
  103. next(`/login`)
  104. NProgress.done()
  105. }
  106. }
  107. })
  108. router.afterEach(() => {
  109. // finish progress bar
  110. NProgress.done()
  111. })