index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import type { App } from 'vue'
  2. import { getAccessToken } from '@/utils/auth'
  3. import type { RouteRecordRaw } from 'vue-router'
  4. import remainingRouter from './modules/remaining'
  5. import { useTitle } from '@/hooks/web/useTitle'
  6. import { useNProgress } from '@/hooks/web/useNProgress'
  7. import { usePageLoading } from '@/hooks/web/usePageLoading'
  8. import { createRouter, createWebHashHistory } from 'vue-router'
  9. import { usePermissionStoreWithOut } from '@/store/modules/permission'
  10. import { useDictStoreWithOut } from '@/store/modules/dict'
  11. import { useUserStoreWithOut } from '@/store/modules/user'
  12. import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'
  13. import { isRelogin } from '@/config/axios/service'
  14. import { getInfoApi } from '@/api/login'
  15. import { useCache } from '@/hooks/web/useCache'
  16. const { wsCache } = useCache('sessionStorage')
  17. const { start, done } = useNProgress()
  18. const { loadStart, loadDone } = usePageLoading()
  19. // 创建路由实例
  20. const router = createRouter({
  21. history: createWebHashHistory(), // createWebHashHistory URL带#,createWebHistory URL不带#
  22. strict: true,
  23. routes: remainingRouter as RouteRecordRaw[],
  24. scrollBehavior: () => ({ left: 0, top: 0 })
  25. })
  26. // 路由不重定向白名单
  27. const whiteList = [
  28. '/login',
  29. '/social-login',
  30. '/auth-redirect',
  31. '/bind',
  32. '/register',
  33. '/oauthLogin/gitee'
  34. ]
  35. // 路由加载前
  36. router.beforeEach(async (to, from, next) => {
  37. start()
  38. loadStart()
  39. if (getAccessToken()) {
  40. if (to.path === '/login') {
  41. next({ path: '/' })
  42. } else {
  43. // 获取所有字典
  44. const dictStore = useDictStoreWithOut()
  45. const userStore = useUserStoreWithOut()
  46. const permissionStore = usePermissionStoreWithOut()
  47. const dictMap = wsCache.get('dictCache')
  48. if (!dictMap) {
  49. const res = await listSimpleDictDataApi()
  50. dictStore.setDictMap(res)
  51. }
  52. if (userStore.getRoles.length === 0) {
  53. isRelogin.show = true
  54. const res = await getInfoApi()
  55. await userStore.setUserInfoAction(res)
  56. isRelogin.show = false
  57. // 后端过滤菜单
  58. await permissionStore.generateRoutes()
  59. permissionStore.getAddRouters.forEach((route) => {
  60. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  61. })
  62. const redirectPath = from.query.redirect || to.path
  63. const redirect = decodeURIComponent(redirectPath as string)
  64. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
  65. next(nextData)
  66. } else {
  67. next()
  68. }
  69. }
  70. } else {
  71. if (whiteList.indexOf(to.path) !== -1) {
  72. next()
  73. } else {
  74. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  75. }
  76. }
  77. })
  78. router.afterEach((to) => {
  79. useTitle(to?.meta?.title as string)
  80. done() // 结束Progress
  81. loadDone()
  82. })
  83. export const resetRouter = (): void => {
  84. const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  85. router.getRoutes().forEach((route) => {
  86. const { name } = route
  87. if (name && !resetWhiteNameList.includes(name as string)) {
  88. router.hasRoute(name) && router.removeRoute(name)
  89. }
  90. })
  91. }
  92. export const setupRouter = (app: App<Element>) => {
  93. app.use(router)
  94. }
  95. export default router