feat(projects): support constant route without login status[支持未登录状态下访问自定义的固定路由]

This commit is contained in:
Soybean 2022-09-21 12:45:00 +08:00
parent 22c05674f8
commit a539112a0f
2 changed files with 20 additions and 3 deletions

View File

@ -18,7 +18,8 @@ export async function createDynamicRouteGuard(
if (!route.isInitAuthRoute) { if (!route.isInitAuthRoute) {
// 未登录情况下直接回到登录页,登录成功后再加载权限路由 // 未登录情况下直接回到登录页,登录成功后再加载权限路由
if (!isLogin) { if (!isLogin) {
if (to.name === routeName('login')) { const toName = to.name as AuthRoute.RouteKey;
if (route.isValidConstantRoute(toName) && !to.meta.requiresAuth) {
next(); next();
} else { } else {
const redirect = to.fullPath; const redirect = to.fullPath;

View File

@ -53,14 +53,30 @@ export const useRouteStore = defineStore('route-store', {
/** 重置路由数据,保留固定路由 */ /** 重置路由数据,保留固定路由 */
resetRoutes() { resetRoutes() {
const routes = router.getRoutes(); const routes = router.getRoutes();
const constantRouteNames = getConstantRouteNames(constantRoutes);
routes.forEach(route => { routes.forEach(route => {
const name: AuthRoute.RouteKey = (route.name || 'root') as AuthRoute.RouteKey; const name: AuthRoute.RouteKey = (route.name || 'root') as AuthRoute.RouteKey;
if (!constantRouteNames.includes(name)) { if (!this.isConstantRoute(name)) {
router.removeRoute(name); router.removeRoute(name);
} }
}); });
}, },
/**
*
* @param name
*/
isConstantRoute(name: AuthRoute.RouteKey) {
const constantRouteNames = getConstantRouteNames(constantRoutes);
return constantRouteNames.includes(name);
},
/**
*
* @param name
*/
isValidConstantRoute(name: AuthRoute.RouteKey) {
const NOT_FOUND_PAGE_NAME: AuthRoute.RouteKey = 'not-found-page';
const constantRouteNames = getConstantRouteNames(constantRoutes);
return constantRouteNames.includes(name) && name !== NOT_FOUND_PAGE_NAME;
},
/** /**
* *
* @param routes - * @param routes -