fix(projects): 修复登录的重定向地址

This commit is contained in:
Soybean
2021-09-16 13:58:11 +08:00
parent c1cdc3a9ed
commit f97f226656
24 changed files with 261 additions and 174 deletions

View File

@ -9,7 +9,7 @@ const routes: Array<RouteRecordRaw> = [...customRoutes, ...constantRoutes];
/** 用于部署vercel托管服务 */
const isVercel = import.meta.env.VITE_HTTP_ENV === 'VERCEL';
const router = createRouter({
export const router = createRouter({
history: isVercel ? createWebHashHistory() : createWebHistory(),
routes
});

View File

@ -1,7 +1,7 @@
import type { Router } from 'vue-router';
import type { Router, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
import { useTitle } from '@vueuse/core';
import { getToken, getLoginRedirectUrl } from '@/utils';
import { RouteNameMap, whitelistRoutes } from './routes';
import { getToken } from '@/utils';
import { RouteNameMap } from './routes';
/**
* 路由守卫函数
@ -9,22 +9,10 @@ import { RouteNameMap, whitelistRoutes } from './routes';
*/
export default function createRouterGuide(router: Router) {
router.beforeEach((to, from, next) => {
// 开始 loadingBar
window.$loadingBar?.start();
const token = getToken();
if (whitelistRoutes.includes(to.name as string)) {
if (to.name === RouteNameMap.get('login') && token) {
next('/');
return;
}
next();
return;
}
if (token) {
next();
} else {
const redirectUrl = getLoginRedirectUrl();
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
}
// 页面跳转逻辑
handleRouterAction(to, from, next);
});
router.afterEach(to => {
// 设置document title
@ -33,3 +21,45 @@ export default function createRouterGuide(router: Router) {
window.$loadingBar?.finish();
});
}
function handleRouterAction(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {
const token = getToken();
const routerAction: [boolean, () => void][] = [
// 已登录状态跳转登录页,跳转至首页
[
to.name === RouteNameMap.get('login') && Boolean(token),
() => {
next({ name: RouteNameMap.get('root') });
}
],
// 不需要权限的页面直接通行
[
!to.meta?.requiresAuth,
() => {
next();
}
],
// 需要权限的页面
[
Boolean(to.meta?.requiresAuth),
() => {
if (token) {
// 有权限直接通行
next();
} else {
// 没有权限,跳转至登录页
const redirectUrl = window.location.href;
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
}
}
]
];
routerAction.some(item => {
const flag = item[0];
if (flag) {
item[1]();
}
return flag;
});
}

View File

@ -109,6 +109,7 @@ export const customRoutes: CustomRoute[] = [
path: EnumRoutePath['dashboard-analysis'],
component: () => import('@/views/dashboard/analysis/index.vue'),
meta: {
requiresAuth: true,
title: EnumRouteTitle['dashboard-analysis']
}
},
@ -117,6 +118,7 @@ export const customRoutes: CustomRoute[] = [
path: EnumRoutePath['dashboard-workbench'],
component: () => import('@/views/dashboard/workbench/index.vue'),
meta: {
requiresAuth: true,
title: EnumRouteTitle['dashboard-workbench']
}
}
@ -127,6 +129,7 @@ export const customRoutes: CustomRoute[] = [
path: EnumRoutePath.exception,
component: BasicLayout,
meta: {
requiresAuth: true,
title: EnumRouteTitle.exception,
icon: ExceptionOutlined
},
@ -136,6 +139,7 @@ export const customRoutes: CustomRoute[] = [
path: EnumRoutePath['exception-403'],
component: () => import('@/views/system/exception/403.vue'),
meta: {
requiresAuth: true,
title: EnumRouteTitle['exception-403'],
fullPage: true
}
@ -145,6 +149,7 @@ export const customRoutes: CustomRoute[] = [
path: EnumRoutePath['exception-404'],
component: () => import('@/views/system/exception/404.vue'),
meta: {
requiresAuth: true,
title: EnumRouteTitle['exception-404'],
fullPage: true
}
@ -154,6 +159,7 @@ export const customRoutes: CustomRoute[] = [
path: EnumRoutePath['exception-500'],
component: () => import('@/views/system/exception/500.vue'),
meta: {
requiresAuth: true,
title: EnumRouteTitle['exception-500'],
fullPage: true
}