index.ts 3.0 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 { 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. const permissionStore = usePermissionStoreWithOut()
  14. const dictStore = useDictStoreWithOut()
  15. const { wsCache } = useCache()
  16. const { start, done } = useNProgress()
  17. const { loadStart, loadDone } = usePageLoading()
  18. // 创建路由实例
  19. const router = createRouter({
  20. history: createWebHashHistory(),
  21. strict: true,
  22. routes: remainingRouter as RouteRecordRaw[],
  23. scrollBehavior: () => ({ left: 0, top: 0 })
  24. })
  25. // 路由不重定向白名单
  26. const whiteList = [
  27. '/login',
  28. '/social-login',
  29. '/auth-redirect',
  30. '/bind',
  31. '/register',
  32. '/oauthLogin/gitee'
  33. ]
  34. // 路由加载前
  35. router.beforeEach(async (to, from, next) => {
  36. start()
  37. loadStart()
  38. if (getAccessToken()) {
  39. if (to.path === '/login') {
  40. next({ path: '/' })
  41. } else {
  42. if (!dictStore.getIsSetDict) {
  43. // 获取所有字典
  44. const res = await listSimpleDictDataApi()
  45. if (res) {
  46. dictStore.setDictMap(res)
  47. dictStore.setIsSetDict(true)
  48. }
  49. }
  50. if (permissionStore.getIsAddRouters) {
  51. next()
  52. return
  53. }
  54. // 开发者可根据实际情况进行修改
  55. const roleRouters = wsCache.get('roleRouters') || []
  56. await permissionStore.generateRoutes(roleRouters as AppCustomRouteRecordRaw[])
  57. permissionStore.getAddRouters.forEach((route) => {
  58. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  59. })
  60. const redirectPath = from.query.redirect || to.path
  61. const redirect = decodeURIComponent(redirectPath as string)
  62. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
  63. permissionStore.setIsAddRouters(true)
  64. next(nextData)
  65. }
  66. } else {
  67. if (whiteList.indexOf(to.path) !== -1) {
  68. next()
  69. } else {
  70. next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
  71. }
  72. }
  73. })
  74. router.afterEach((to) => {
  75. useTitle(to?.meta?.title as string)
  76. done() // 结束Progress
  77. loadDone()
  78. })
  79. export const resetRouter = (): void => {
  80. const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  81. router.getRoutes().forEach((route) => {
  82. const { name } = route
  83. if (name && !resetWhiteNameList.includes(name as string)) {
  84. router.hasRoute(name) && router.removeRoute(name)
  85. }
  86. })
  87. }
  88. export const setupRouter = (app: App<Element>) => {
  89. app.use(router)
  90. }
  91. export default router