permission.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { useUserStore } from '@/pinia/modules/user'
  2. import { useRouterStore } from '@/pinia/modules/router'
  3. import getPageTitle from '@/utils/page'
  4. import router from '@/router'
  5. import Nprogress from 'nprogress'
  6. const whiteList = ['Login', 'Init']
  7. const getRouter = async(userStore) => {
  8. const routerStore = useRouterStore()
  9. await routerStore.SetAsyncRouter()
  10. await userStore.GetUserInfo()
  11. const asyncRouters = routerStore.asyncRouters
  12. asyncRouters.forEach(asyncRouter => {
  13. router.addRoute(asyncRouter)
  14. })
  15. }
  16. async function handleKeepAlive(to) {
  17. if (to.matched.some(item => item.meta.keepAlive)) {
  18. if (to.matched && to.matched.length > 2) {
  19. for (let i = 1; i < to.matched.length; i++) {
  20. const element = to.matched[i - 1]
  21. if (element.name === 'layout') {
  22. to.matched.splice(i, 1)
  23. await handleKeepAlive(to)
  24. }
  25. // 如果没有按需加载完成则等待加载
  26. if (typeof element.components.default === 'function') {
  27. await element.components.default()
  28. await handleKeepAlive(to)
  29. }
  30. }
  31. }
  32. }
  33. }
  34. router.beforeEach(async(to, from) => {
  35. const routerStore = useRouterStore()
  36. Nprogress.start()
  37. const userStore = useUserStore()
  38. to.meta.matched = [...to.matched]
  39. handleKeepAlive(to)
  40. const token = userStore.token
  41. // 在白名单中的判断情况
  42. document.title = getPageTitle(to.meta.title, to)
  43. if(to.meta.client) {
  44. return true
  45. }
  46. if (whiteList.indexOf(to.name) > -1) {
  47. if (token) {
  48. if (!routerStore.asyncRouterFlag && whiteList.indexOf(from.name) < 0) {
  49. await getRouter(userStore)
  50. }
  51. // token 可以解析但是却是不存在的用户 id 或角色 id 会导致无限调用
  52. if (userStore.userInfo?.authority?.defaultRouter != null) {
  53. if (router.hasRoute(userStore.userInfo.authority.defaultRouter)) {
  54. return { name: userStore.userInfo.authority.defaultRouter }
  55. } else {
  56. return { path: '/layout/404' }
  57. }
  58. } else {
  59. // 强制退出账号
  60. userStore.ClearStorage()
  61. return {
  62. name: 'Login',
  63. query: {
  64. redirect: document.location.hash
  65. }
  66. }
  67. }
  68. } else {
  69. return true
  70. }
  71. } else {
  72. // 不在白名单中并且已经登录的时候
  73. if (token) {
  74. // 添加flag防止多次获取动态路由和栈溢出
  75. if (!routerStore.asyncRouterFlag && whiteList.indexOf(from.name) < 0) {
  76. await getRouter(userStore)
  77. if (userStore.token) {
  78. if (router.hasRoute(userStore.userInfo.authority.defaultRouter)) {
  79. return { ...to, replace: true }
  80. } else {
  81. return { path: '/layout/404' }
  82. }
  83. } else {
  84. return {
  85. name: 'Login',
  86. query: { redirect: to.href }
  87. }
  88. }
  89. } else {
  90. if (to.matched.length) {
  91. return true
  92. } else {
  93. return { path: '/layout/404' }
  94. }
  95. }
  96. }
  97. // 不在白名单中并且未登录的时候
  98. if (!token) {
  99. return {
  100. name: 'Login',
  101. query: {
  102. redirect: document.location.hash
  103. }
  104. }
  105. }
  106. }
  107. })
  108. router.afterEach(() => {
  109. // 路由加载完成后关闭进度条
  110. document.getElementsByClassName('main-cont main-right')[0]?.scrollTo(0, 0)
  111. Nprogress.done()
  112. })
  113. router.onError(() => {
  114. // 路由发生错误后销毁进度条
  115. Nprogress.remove()
  116. })