fix: 静态路由角色拦截限制
This commit is contained in:
2
.env
2
.env
@@ -35,5 +35,3 @@ VITE_SERVICE_EXPIRED_TOKEN_CODES=403
|
|||||||
|
|
||||||
VITE_SERVICE_SERVER_ERROR_CODE=500
|
VITE_SERVICE_SERVER_ERROR_CODE=500
|
||||||
|
|
||||||
# when the route mode is static, the defined super role
|
|
||||||
VITE_STATIC_SUPER_ROLE=R_SUPER
|
|
||||||
|
|||||||
@@ -5,6 +5,11 @@
|
|||||||
|
|
||||||
import type { GeneratedRoute } from '@elegant-router/types';
|
import type { GeneratedRoute } from '@elegant-router/types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* custom routes
|
||||||
|
*
|
||||||
|
* @link https://github.com/soybeanjs/elegant-router?tab=readme-ov-file#custom-route
|
||||||
|
*/
|
||||||
export const generatedRoutes: GeneratedRoute[] = [
|
export const generatedRoutes: GeneratedRoute[] = [
|
||||||
{
|
{
|
||||||
name: '403',
|
name: '403',
|
||||||
@@ -40,14 +45,38 @@ export const generatedRoutes: GeneratedRoute[] = [
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'about',
|
name: 'home',
|
||||||
path: '/about',
|
path: '/home',
|
||||||
component: 'layout.base$view.about',
|
component: 'layout.base$view.home',
|
||||||
meta: {
|
meta: {
|
||||||
title: 'about',
|
title: 'home',
|
||||||
i18nKey: 'route.about',
|
i18nKey: 'route.home',
|
||||||
icon: 'fluent:book-information-24-regular',
|
icon: 'mdi:monitor-dashboard',
|
||||||
order: 10
|
order: 0,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'login',
|
||||||
|
path: '/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?',
|
||||||
|
component: 'layout.blank$view._builtin_login',
|
||||||
|
props: true,
|
||||||
|
meta: {
|
||||||
|
title: 'login',
|
||||||
|
i18nKey: 'route.login',
|
||||||
|
constant: true,
|
||||||
|
hideInMenu: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'user-center',
|
||||||
|
path: '/user-center',
|
||||||
|
component: 'layout.base$view.user-center',
|
||||||
|
meta: {
|
||||||
|
title: 'user-center',
|
||||||
|
hideInMenu: true,
|
||||||
|
constant: true,
|
||||||
|
keepAlive: false,
|
||||||
|
i18nKey: 'route.user-center'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -58,7 +87,8 @@ export const generatedRoutes: GeneratedRoute[] = [
|
|||||||
title: 'function',
|
title: 'function',
|
||||||
i18nKey: 'route.function',
|
i18nKey: 'route.function',
|
||||||
icon: 'icon-park-outline:all-application',
|
icon: 'icon-park-outline:all-application',
|
||||||
order: 6
|
order: 6,
|
||||||
|
roles: ['admin'],
|
||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
@@ -141,7 +171,6 @@ export const generatedRoutes: GeneratedRoute[] = [
|
|||||||
i18nKey: 'route.function_super-page',
|
i18nKey: 'route.function_super-page',
|
||||||
icon: 'ic:round-supervisor-account',
|
icon: 'ic:round-supervisor-account',
|
||||||
order: 5,
|
order: 5,
|
||||||
roles: ['R_SUPER']
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -169,38 +198,15 @@ export const generatedRoutes: GeneratedRoute[] = [
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'home',
|
name: 'about',
|
||||||
path: '/home',
|
path: '/about',
|
||||||
component: 'layout.base$view.home',
|
component: 'layout.base$view.about',
|
||||||
meta: {
|
meta: {
|
||||||
title: 'home',
|
title: 'about',
|
||||||
i18nKey: 'route.home',
|
i18nKey: 'route.about',
|
||||||
icon: 'mdi:monitor-dashboard',
|
icon: 'fluent:book-information-24-regular',
|
||||||
order: 1
|
order: 100,
|
||||||
|
roles: ['admin']
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: 'login',
|
|
||||||
path: '/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?',
|
|
||||||
component: 'layout.blank$view._builtin_login',
|
|
||||||
props: true,
|
|
||||||
meta: {
|
|
||||||
title: 'login',
|
|
||||||
i18nKey: 'route.login',
|
|
||||||
constant: true,
|
|
||||||
hideInMenu: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'user-center',
|
|
||||||
path: '/user-center',
|
|
||||||
component: 'layout.base$view.user-center',
|
|
||||||
meta: {
|
|
||||||
title: 'user-center',
|
|
||||||
hideInMenu: true,
|
|
||||||
constant: true,
|
|
||||||
keepAlive: false,
|
|
||||||
i18nKey: 'route.user-center'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ export function createRouteGuard(router: Router) {
|
|||||||
const needLogin = !to.meta.constant;
|
const needLogin = !to.meta.constant;
|
||||||
const routeRoles = to.meta.roles || [];
|
const routeRoles = to.meta.roles || [];
|
||||||
|
|
||||||
// const hasRole = authStore.userInfo.roles?.some(role => routeRoles.includes(role));
|
const hasRole = authStore.userInfo.roles?.some(role => routeRoles.includes(role));
|
||||||
const hasAuth = authStore.isStaticSuper || !routeRoles.length;
|
const hasAuth = !routeRoles.length || hasRole
|
||||||
|
|
||||||
const routeSwitches: CommonType.StrategicPattern[] = [
|
const routeSwitches: CommonType.StrategicPattern[] = [
|
||||||
// if it is login route when logged in, then switch to the root page
|
// if it is login route when logged in, then switch to the root page
|
||||||
|
|||||||
@@ -1,57 +1,6 @@
|
|||||||
import type { ElegantConstRoute } from '@elegant-router/types';
|
import type {ElegantConstRoute} from '@elegant-router/types';
|
||||||
import { generatedRoutes } from '../elegant/routes';
|
import {generatedRoutes} from '../elegant/routes';
|
||||||
import { transformElegantRoutesToVueRoutes } from '../elegant/transform';
|
import {transformElegantRoutesToVueRoutes} from '../elegant/transform';
|
||||||
|
|
||||||
/**
|
|
||||||
* custom routes
|
|
||||||
*
|
|
||||||
* @link https://github.com/soybeanjs/elegant-router?tab=readme-ov-file#custom-route
|
|
||||||
*/
|
|
||||||
const customRoutes: any[] = [
|
|
||||||
{
|
|
||||||
name: 'exception',
|
|
||||||
path: '/exception',
|
|
||||||
component: 'layout.base',
|
|
||||||
meta: {
|
|
||||||
title: 'exception',
|
|
||||||
i18nKey: 'route.exception',
|
|
||||||
icon: 'ant-design:exception-outlined',
|
|
||||||
order: 7
|
|
||||||
},
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
name: 'exception_403',
|
|
||||||
path: '/exception/403',
|
|
||||||
component: 'view._builtin_403',
|
|
||||||
meta: {
|
|
||||||
title: 'exception_403',
|
|
||||||
i18nKey: 'route.exception_403',
|
|
||||||
icon: 'ic:baseline-block'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'exception_404',
|
|
||||||
path: '/exception/404',
|
|
||||||
component: 'view._builtin_404',
|
|
||||||
meta: {
|
|
||||||
title: 'exception_404',
|
|
||||||
i18nKey: 'route.exception_404',
|
|
||||||
icon: 'ic:baseline-web-asset-off'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'exception_500',
|
|
||||||
path: '/exception/500',
|
|
||||||
component: 'view._builtin_500',
|
|
||||||
meta: {
|
|
||||||
title: 'exception_500',
|
|
||||||
i18nKey: 'route.exception_500',
|
|
||||||
icon: 'ic:baseline-wifi-off'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
/** create routes when the auth route mode is static */
|
/** create routes when the auth route mode is static */
|
||||||
export function createStaticRoutes() {
|
export function createStaticRoutes() {
|
||||||
@@ -59,7 +8,7 @@ export function createStaticRoutes() {
|
|||||||
|
|
||||||
const authRoutes: any[] = [];
|
const authRoutes: any[] = [];
|
||||||
|
|
||||||
[...customRoutes, ...generatedRoutes].forEach(item => {
|
generatedRoutes.forEach(item => {
|
||||||
if (item.meta?.constant) {
|
if (item.meta?.constant) {
|
||||||
constantRoutes.push(item);
|
constantRoutes.push(item);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -31,13 +31,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/** is super role in static route */
|
|
||||||
const isStaticSuper = computed(() => {
|
|
||||||
const { VITE_AUTH_ROUTE_MODE, VITE_STATIC_SUPER_ROLE } = import.meta.env;
|
|
||||||
|
|
||||||
return VITE_AUTH_ROUTE_MODE === 'static' && userInfo.roles?.includes(VITE_STATIC_SUPER_ROLE);
|
|
||||||
});
|
|
||||||
|
|
||||||
/** Is login */
|
/** Is login */
|
||||||
const isLogin = computed(() => Boolean(token.value));
|
const isLogin = computed(() => Boolean(token.value));
|
||||||
|
|
||||||
@@ -160,7 +153,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
|||||||
return {
|
return {
|
||||||
token,
|
token,
|
||||||
userInfo,
|
userInfo,
|
||||||
isStaticSuper,
|
|
||||||
isLogin,
|
isLogin,
|
||||||
loginLoading,
|
loginLoading,
|
||||||
resetStore,
|
resetStore,
|
||||||
|
|||||||
@@ -165,21 +165,37 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
|||||||
|
|
||||||
/** Init auth route */
|
/** Init auth route */
|
||||||
async function initAuthRoute() {
|
async function initAuthRoute() {
|
||||||
await initStaticAuthRoute();
|
// await initStaticAuthRoute();
|
||||||
await initDynamicAuthRoute();
|
// await initDynamicAuthRoute();
|
||||||
|
|
||||||
|
|
||||||
|
const { data: routes, error } = await doGetUserRoutes();
|
||||||
|
if (!error) {
|
||||||
|
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
||||||
|
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
|
||||||
|
console.log(authStore.userInfo.roles,filteredAuthRoutes)
|
||||||
|
console.log(authStore.userInfo.roles,[ ...filteredAuthRoutes,...routes])
|
||||||
|
addAuthRoutes([ ...filteredAuthRoutes,...routes]);
|
||||||
|
|
||||||
|
handleAuthRoutes();
|
||||||
|
|
||||||
|
setRouteHome('home');
|
||||||
|
|
||||||
|
handleUpdateRootRouteRedirect('home');
|
||||||
|
|
||||||
|
setIsInitAuthRoute(true);
|
||||||
|
} else {
|
||||||
|
await authStore.resetStore();
|
||||||
|
}
|
||||||
tabStore.initHomeTab();
|
tabStore.initHomeTab();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Init static auth route */
|
/** Init static auth route */
|
||||||
async function initStaticAuthRoute() {
|
async function initStaticAuthRoute() {
|
||||||
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
||||||
if (authStore.isStaticSuper) {
|
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
|
||||||
addAuthRoutes(staticAuthRoutes);
|
|
||||||
} else {
|
|
||||||
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
|
|
||||||
|
|
||||||
addAuthRoutes(filteredAuthRoutes);
|
addAuthRoutes(filteredAuthRoutes);
|
||||||
}
|
|
||||||
|
|
||||||
handleAuthRoutes();
|
handleAuthRoutes();
|
||||||
|
|
||||||
@@ -196,7 +212,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
|||||||
|
|
||||||
setRouteHome('home');
|
setRouteHome('home');
|
||||||
|
|
||||||
handleUpdateRootRouteRedirect('manage_role');
|
handleUpdateRootRouteRedirect('home');
|
||||||
|
|
||||||
setIsInitAuthRoute(true);
|
setIsInitAuthRoute(true);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
3
src/typings/env.d.ts
vendored
3
src/typings/env.d.ts
vendored
@@ -55,8 +55,7 @@ declare namespace Env {
|
|||||||
* use "," to separate multiple codes
|
* use "," to separate multiple codes
|
||||||
*/
|
*/
|
||||||
readonly VITE_SERVICE_EXPIRED_TOKEN_CODES: string;
|
readonly VITE_SERVICE_EXPIRED_TOKEN_CODES: string;
|
||||||
/** when the route mode is static, the defined super role */
|
|
||||||
readonly VITE_STATIC_SUPER_ROLE: string;
|
|
||||||
/**
|
/**
|
||||||
* other backend service base url
|
* other backend service base url
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user