cyywl_server/yudao-ui-admin-vue3/src/router/index.ts

100 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-07-18 19:06:37 +08:00
import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
2022-12-06 16:12:54 +08:00
import { createRouter, createWebHashHistory } from 'vue-router'
2022-07-18 19:06:37 +08:00
import remainingRouter from './modules/remaining'
2022-12-06 16:12:54 +08:00
import { isRelogin } from '@/config/axios/service'
import { getAccessToken } from '@/utils/auth'
2022-07-18 19:06:37 +08:00
import { useTitle } from '@/hooks/web/useTitle'
import { useNProgress } from '@/hooks/web/useNProgress'
import { usePageLoading } from '@/hooks/web/usePageLoading'
import { useDictStoreWithOut } from '@/store/modules/dict'
2022-08-03 12:38:58 +08:00
import { useUserStoreWithOut } from '@/store/modules/user'
2022-12-06 16:12:54 +08:00
import { usePermissionStoreWithOut } from '@/store/modules/permission'
2022-08-03 17:23:12 +08:00
import { getInfoApi } from '@/api/login'
2022-07-18 19:06:37 +08:00
const { start, done } = useNProgress()
const { loadStart, loadDone } = usePageLoading()
// 创建路由实例
const router = createRouter({
2022-08-03 16:36:52 +08:00
history: createWebHashHistory(), // createWebHashHistory URL带#createWebHistory URL不带#
2022-07-18 19:06:37 +08:00
strict: true,
routes: remainingRouter as RouteRecordRaw[],
scrollBehavior: () => ({ left: 0, top: 0 })
})
2022-07-19 23:03:10 +08:00
// 路由不重定向白名单
2022-07-18 19:06:37 +08:00
const whiteList = [
'/login',
'/social-login',
'/auth-redirect',
'/bind',
'/register',
'/oauthLogin/gitee'
]
// 路由加载前
router.beforeEach(async (to, from, next) => {
start()
loadStart()
if (getAccessToken()) {
if (to.path === '/login') {
next({ path: '/' })
} else {
2022-08-03 12:38:58 +08:00
// 获取所有字典
2022-08-03 16:01:06 +08:00
const dictStore = useDictStoreWithOut()
const userStore = useUserStoreWithOut()
const permissionStore = usePermissionStoreWithOut()
2023-01-05 14:52:14 +08:00
if (!dictStore.getIsSetDict) {
dictStore.setDictMap()
2022-08-03 12:55:27 +08:00
}
if (!userStore.getIsSetUser) {
2022-08-03 00:41:59 +08:00
isRelogin.show = true
2022-08-03 17:23:12 +08:00
const res = await getInfoApi()
await userStore.setUserInfoAction(res)
2022-08-03 00:41:59 +08:00
isRelogin.show = false
2022-08-03 12:38:58 +08:00
// 后端过滤菜单
await permissionStore.generateRoutes()
permissionStore.getAddRouters.forEach((route) => {
router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
})
const redirectPath = from.query.redirect || to.path
const redirect = decodeURIComponent(redirectPath as string)
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
next(nextData)
} else {
2022-07-18 19:06:37 +08:00
next()
}
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
2022-08-03 00:41:59 +08:00
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
2022-07-18 19:06:37 +08:00
}
}
})
router.afterEach((to) => {
useTitle(to?.meta?.title as string)
done() // 结束Progress
loadDone()
})
export const resetRouter = (): void => {
const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
router.getRoutes().forEach((route) => {
const { name } = route
if (name && !resetWhiteNameList.includes(name as string)) {
router.hasRoute(name) && router.removeRoute(name)
}
})
}
export const setupRouter = (app: App<Element>) => {
app.use(router)
}
export default router