路由权限添加
This commit is contained in:
@@ -100,7 +100,7 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
|||||||
startLoading();
|
startLoading();
|
||||||
|
|
||||||
const { data: loginToken, error } = await fetchLogin(userName, password);
|
const { data: loginToken, error } = await fetchLogin(userName, password);
|
||||||
|
|
||||||
if (!error) {
|
if (!error) {
|
||||||
const pass = await loginByToken(loginToken);
|
const pass = await loginByToken(loginToken);
|
||||||
|
|
||||||
@@ -130,8 +130,8 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
|||||||
|
|
||||||
async function loginByToken(loginToken: Api.Auth.LoginToken) {
|
async function loginByToken(loginToken: Api.Auth.LoginToken) {
|
||||||
// 1. stored in the localStorage, the later requests need it in headers
|
// 1. stored in the localStorage, the later requests need it in headers
|
||||||
|
localStg.set('userInfo', loginToken);
|
||||||
localStg.set('token', loginToken.token);
|
localStg.set('token', loginToken.token);
|
||||||
localStg.set('refreshToken', loginToken.refreshToken);
|
|
||||||
|
|
||||||
// 2. get user info
|
// 2. get user info
|
||||||
const pass = await getUserInfo();
|
const pass = await getUserInfo();
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { ROOT_ROUTE } from '@/router/routes/builtin';
|
|||||||
import { getRouteName, getRoutePath } from '@/router/elegant/transform';
|
import { getRouteName, getRoutePath } from '@/router/elegant/transform';
|
||||||
import { useAuthStore } from '../auth';
|
import { useAuthStore } from '../auth';
|
||||||
import { useTabStore } from '../tab';
|
import { useTabStore } from '../tab';
|
||||||
|
import { localStg } from '@/utils/storage';
|
||||||
import {
|
import {
|
||||||
filterAuthRoutesByRoles,
|
filterAuthRoutesByRoles,
|
||||||
getBreadcrumbsByRoute,
|
getBreadcrumbsByRoute,
|
||||||
@@ -190,14 +191,52 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
|||||||
tabStore.initHomeTab();
|
tabStore.initHomeTab();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据允许的路径列表,过滤路由树
|
||||||
|
* @param {Array} routes - 路由对象数组(可嵌套 children)
|
||||||
|
* @param {Array} allowedPaths - 允许的路径字符串数组
|
||||||
|
* @param {string} parentPath - 父级路径(用于拼接相对路径,本例中所有路径都是绝对路径,可忽略)
|
||||||
|
* @returns {Array} 过滤后的路由数组
|
||||||
|
*/
|
||||||
|
function filterRoutesByPaths(routes: ElegantConstRoute[], allowedPaths: string[], parentPath = '') {
|
||||||
|
const result = [];
|
||||||
|
const pathSet = new Set(allowedPaths);
|
||||||
|
|
||||||
|
for (const route of routes) {
|
||||||
|
// 计算完整路径(如果路径以 '/' 开头则为绝对路径,否则拼接父路径)
|
||||||
|
const fullPath = route.path.startsWith('/')
|
||||||
|
? route.path
|
||||||
|
: `${parentPath}/${route.path}`.replace(/\/+/g, '/');
|
||||||
|
|
||||||
|
// 检查当前路由是否在允许列表中
|
||||||
|
const isAllowed = pathSet.has(fullPath);
|
||||||
|
|
||||||
|
// 递归处理子路由
|
||||||
|
let filteredChildren = [];
|
||||||
|
if (route.children && route.children.length) {
|
||||||
|
filteredChildren = filterRoutesByPaths(route.children, allowedPaths, fullPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只要当前路由匹配,或有子路由匹配,则保留该路由
|
||||||
|
if (isAllowed || filteredChildren.length > 0) {
|
||||||
|
result.push({
|
||||||
|
...route,
|
||||||
|
path: fullPath, // 使用完整路径(可确保路径一致性)
|
||||||
|
children: filteredChildren.length ? filteredChildren : route.children
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
/** Init static auth route */
|
/** Init static auth route */
|
||||||
function initStaticAuthRoute() {
|
function initStaticAuthRoute() {
|
||||||
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
||||||
|
const filteredRoutes = filterRoutesByPaths(staticAuthRoutes, localStg.get('userInfo').perms);
|
||||||
if (authStore.isStaticSuper) {
|
if (authStore.isStaticSuper) {
|
||||||
addAuthRoutes(staticAuthRoutes);
|
addAuthRoutes(filteredRoutes);
|
||||||
} else {
|
} else {
|
||||||
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles);
|
const filteredAuthRoutes = filterAuthRoutesByRoles(filteredRoutes, authStore.userInfo.roles);
|
||||||
|
|
||||||
addAuthRoutes(filteredAuthRoutes);
|
addAuthRoutes(filteredAuthRoutes);
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+2
@@ -10,6 +10,8 @@ declare namespace StorageType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Local {
|
interface Local {
|
||||||
|
/** The user info */
|
||||||
|
userInfo: any;
|
||||||
/** The i18n language */
|
/** The i18n language */
|
||||||
lang: App.I18n.LangType;
|
lang: App.I18n.LangType;
|
||||||
/** The token */
|
/** The token */
|
||||||
|
|||||||
Reference in New Issue
Block a user