index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 { useCache } from '@/hooks/web/useCache'
  6. import { useTitle } from '@/hooks/web/useTitle'
  7. import { useNProgress } from '@/hooks/web/useNProgress'
  8. import { usePageLoading } from '@/hooks/web/usePageLoading'
  9. import { createRouter, createWebHashHistory } from 'vue-router'
  10. import { usePermissionStoreWithOut } from '@/store/modules/permission'
  11. import { useDictStoreWithOut } from '@/store/modules/dict'
  12. import { listSimpleDictDataApi } from '@/api/system/dict/dict.data'
  13. import { isRelogin } from '@/config/axios'
  14. const permissionStore = usePermissionStoreWithOut()
  15. const dictStore = useDictStoreWithOut()
  16. const { wsCache } = useCache()
  17. const { start, done } = useNProgress()
  18. const { loadStart, loadDone } = usePageLoading()
  19. // 创建路由实例
  20. const router = createRouter({
  21. history: createWebHashHistory(),
  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. if (!dictStore.getIsSetDict) {
  44. isRelogin.show = true
  45. // 获取所有字典
  46. const res = await listSimpleDictDataApi()
  47. dictStore.setDictMap(res)
  48. dictStore.setIsSetDict(true)
  49. }
  50. if (permissionStore.getIsAddRouters) {
  51. isRelogin.show = false
  52. next()
  53. return
  54. }
  55. // 开发者可根据实际情况进行修改
  56. const roleRouters = wsCache.get('roleRouters') || []
  57. await permissionStore.generateRoutes(roleRouters as AppCustomRouteRecordRaw[])
  58. permissionStore.getAddRouters.forEach((route) => {
  59. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  60. })
  61. const redirectPath = from.query.redirect || to.path
  62. const redirect = decodeURIComponent(redirectPath as string)
  63. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
  64. permissionStore.setIsAddRouters(true)
  65. next(nextData)
  66. }
  67. } else {
  68. if (whiteList.indexOf(to.path) !== -1) {
  69. next()
  70. } else {
  71. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  72. }
  73. }
  74. })
  75. router.afterEach((to) => {
  76. useTitle(to?.meta?.title as string)
  77. done() // 结束Progress
  78. loadDone()
  79. })
  80. export const resetRouter = (): void => {
  81. const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  82. router.getRoutes().forEach((route) => {
  83. const { name } = route
  84. if (name && !resetWhiteNameList.includes(name as string)) {
  85. router.hasRoute(name) && router.removeRoute(name)
  86. }
  87. })
  88. }
  89. export const setupRouter = (app: App<Element>) => {
  90. app.use(router)
  91. }
  92. export default router