Commit 3a8cd9afe96ca0ea7697c0844a13b08d69d4cdf4
1 parent
81595f81
refactor: router
Showing
4 changed files
with
78 additions
and
72 deletions
yudao-ui-admin-vue3/src/main.ts
yudao-ui-admin-vue3/src/permission.ts
0 → 100644
| 1 | +import router from './router' | |
| 2 | +import type { RouteRecordRaw } from 'vue-router' | |
| 3 | +import { isRelogin } from '@/config/axios/service' | |
| 4 | +import { getAccessToken } from '@/utils/auth' | |
| 5 | +import { useTitle } from '@/hooks/web/useTitle' | |
| 6 | +import { useNProgress } from '@/hooks/web/useNProgress' | |
| 7 | +import { usePageLoading } from '@/hooks/web/usePageLoading' | |
| 8 | +import { useDictStoreWithOut } from '@/store/modules/dict' | |
| 9 | +import { useUserStoreWithOut } from '@/store/modules/user' | |
| 10 | +import { usePermissionStoreWithOut } from '@/store/modules/permission' | |
| 11 | + | |
| 12 | +const { start, done } = useNProgress() | |
| 13 | + | |
| 14 | +const { loadStart, loadDone } = usePageLoading() | |
| 15 | +// 路由不重定向白名单 | |
| 16 | +const whiteList = [ | |
| 17 | + '/login', | |
| 18 | + '/social-login', | |
| 19 | + '/auth-redirect', | |
| 20 | + '/bind', | |
| 21 | + '/register', | |
| 22 | + '/oauthLogin/gitee' | |
| 23 | +] | |
| 24 | + | |
| 25 | +// 路由加载前 | |
| 26 | +router.beforeEach(async (to, from, next) => { | |
| 27 | + start() | |
| 28 | + loadStart() | |
| 29 | + if (getAccessToken()) { | |
| 30 | + if (to.path === '/login') { | |
| 31 | + next({ path: '/' }) | |
| 32 | + } else { | |
| 33 | + // 获取所有字典 | |
| 34 | + const dictStore = useDictStoreWithOut() | |
| 35 | + const userStore = useUserStoreWithOut() | |
| 36 | + const permissionStore = usePermissionStoreWithOut() | |
| 37 | + if (!dictStore.getIsSetDict) { | |
| 38 | + dictStore.setDictMap() | |
| 39 | + } | |
| 40 | + if (!userStore.getIsSetUser) { | |
| 41 | + isRelogin.show = true | |
| 42 | + await userStore.setUserInfoAction() | |
| 43 | + isRelogin.show = false | |
| 44 | + // 后端过滤菜单 | |
| 45 | + await permissionStore.generateRoutes() | |
| 46 | + permissionStore.getAddRouters.forEach((route) => { | |
| 47 | + router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表 | |
| 48 | + }) | |
| 49 | + const redirectPath = from.query.redirect || to.path | |
| 50 | + const redirect = decodeURIComponent(redirectPath as string) | |
| 51 | + const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect } | |
| 52 | + next(nextData) | |
| 53 | + } else { | |
| 54 | + next() | |
| 55 | + } | |
| 56 | + } | |
| 57 | + } else { | |
| 58 | + if (whiteList.indexOf(to.path) !== -1) { | |
| 59 | + next() | |
| 60 | + } else { | |
| 61 | + next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 | |
| 62 | + } | |
| 63 | + } | |
| 64 | +}) | |
| 65 | + | |
| 66 | +router.afterEach((to) => { | |
| 67 | + useTitle(to?.meta?.title as string) | |
| 68 | + done() // 结束Progress | |
| 69 | + loadDone() | |
| 70 | +}) | ... | ... |
yudao-ui-admin-vue3/src/router/index.ts
| ... | ... | @@ -2,19 +2,6 @@ import type { App } from 'vue' |
| 2 | 2 | import type { RouteRecordRaw } from 'vue-router' |
| 3 | 3 | import { createRouter, createWebHashHistory } from 'vue-router' |
| 4 | 4 | import remainingRouter from './modules/remaining' |
| 5 | -import { isRelogin } from '@/config/axios/service' | |
| 6 | -import { getAccessToken } from '@/utils/auth' | |
| 7 | -import { useTitle } from '@/hooks/web/useTitle' | |
| 8 | -import { useNProgress } from '@/hooks/web/useNProgress' | |
| 9 | -import { usePageLoading } from '@/hooks/web/usePageLoading' | |
| 10 | -import { useDictStoreWithOut } from '@/store/modules/dict' | |
| 11 | -import { useUserStoreWithOut } from '@/store/modules/user' | |
| 12 | -import { usePermissionStoreWithOut } from '@/store/modules/permission' | |
| 13 | -import { getInfoApi } from '@/api/login' | |
| 14 | - | |
| 15 | -const { start, done } = useNProgress() | |
| 16 | - | |
| 17 | -const { loadStart, loadDone } = usePageLoading() | |
| 18 | 5 | |
| 19 | 6 | // 创建路由实例 |
| 20 | 7 | const router = createRouter({ |
| ... | ... | @@ -24,64 +11,6 @@ const router = createRouter({ |
| 24 | 11 | scrollBehavior: () => ({ left: 0, top: 0 }) |
| 25 | 12 | }) |
| 26 | 13 | |
| 27 | -// 路由不重定向白名单 | |
| 28 | -const whiteList = [ | |
| 29 | - '/login', | |
| 30 | - '/social-login', | |
| 31 | - '/auth-redirect', | |
| 32 | - '/bind', | |
| 33 | - '/register', | |
| 34 | - '/oauthLogin/gitee' | |
| 35 | -] | |
| 36 | - | |
| 37 | -// 路由加载前 | |
| 38 | -router.beforeEach(async (to, from, next) => { | |
| 39 | - start() | |
| 40 | - loadStart() | |
| 41 | - if (getAccessToken()) { | |
| 42 | - if (to.path === '/login') { | |
| 43 | - next({ path: '/' }) | |
| 44 | - } else { | |
| 45 | - // 获取所有字典 | |
| 46 | - const dictStore = useDictStoreWithOut() | |
| 47 | - const userStore = useUserStoreWithOut() | |
| 48 | - const permissionStore = usePermissionStoreWithOut() | |
| 49 | - if (!dictStore.getIsSetDict) { | |
| 50 | - dictStore.setDictMap() | |
| 51 | - } | |
| 52 | - if (!userStore.getIsSetUser) { | |
| 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 | - | |
| 79 | -router.afterEach((to) => { | |
| 80 | - useTitle(to?.meta?.title as string) | |
| 81 | - done() // 结束Progress | |
| 82 | - loadDone() | |
| 83 | -}) | |
| 84 | - | |
| 85 | 14 | export const resetRouter = (): void => { |
| 86 | 15 | const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root'] |
| 87 | 16 | router.getRoutes().forEach((route) => { | ... | ... |
yudao-ui-admin-vue3/src/store/modules/user.ts
| ... | ... | @@ -2,6 +2,7 @@ import { store } from '../index' |
| 2 | 2 | import { defineStore } from 'pinia' |
| 3 | 3 | import { getAccessToken, removeToken } from '@/utils/auth' |
| 4 | 4 | import { CACHE_KEY, useCache } from '@/hooks/web/useCache' |
| 5 | +import { getInfoApi } from '@/api/login' | |
| 5 | 6 | |
| 6 | 7 | const { wsCache } = useCache() |
| 7 | 8 | |
| ... | ... | @@ -43,11 +44,15 @@ export const useUserStore = defineStore('admin-user', { |
| 43 | 44 | } |
| 44 | 45 | }, |
| 45 | 46 | actions: { |
| 46 | - async setUserInfoAction(userInfo: UserInfoVO) { | |
| 47 | + async setUserInfoAction() { | |
| 47 | 48 | if (!getAccessToken()) { |
| 48 | 49 | this.resetState() |
| 49 | 50 | return null |
| 50 | 51 | } |
| 52 | + let userInfo = wsCache.get(CACHE_KEY.USER) | |
| 53 | + if (!userInfo) { | |
| 54 | + userInfo = await getInfoApi() | |
| 55 | + } | |
| 51 | 56 | this.permissions = userInfo.permissions |
| 52 | 57 | this.roles = userInfo.roles |
| 53 | 58 | this.user = userInfo.user | ... | ... |