Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复路由没权限跳转报错问题 #235

Merged
merged 3 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/basic-layouts/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { IntlShape } from 'react-intl/lib/src/types';
import { ConfigProviderProps } from 'antd/es/config-provider'
import { ConfigProviderProps } from 'antd/es/config-provider'

export interface HandleMenuProps {
/**语言转换*/
Expand Down Expand Up @@ -31,6 +31,8 @@ export interface RouterMenu {
/**原来的path*/
oPath?: string;
element?: any;
id?: string;
parentId?: string;
}

export interface BasicLayoutsProps {
Expand Down Expand Up @@ -74,9 +76,9 @@ export interface BasicLayoutsProps {
*/
name?: string;
};
theme?:'dark' | 'light';
configProviderProps?:ConfigProviderProps;
className?:string;
theme?: 'dark' | 'light';
configProviderProps?: ConfigProviderProps;
className?: string;
}

export interface UseLayoutsProps extends Omit<BasicLayoutsProps, 'route'> {
Expand Down
100 changes: 62 additions & 38 deletions packages/basic-layouts/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IntlShape } from "react-intl/lib/src/types"


/**
*
*
* 1. 对路由权限进行判定
* 2. 保存每个子集对应的父级
* 3. 父级链
Expand Down Expand Up @@ -90,32 +90,26 @@ export class HandleMenu {
* 1. 权限处理
* 1. 子集全部没权限,父级也没有权限
* 2. 子集存在权限,父级也存在权限
* 3. 没有权限的自动把 path 转换成 403
* 3. 没有权限的自动把 path 转换成 403
* */
initAuth(routers: RouterMenu[], isParent = false) {
let child: RouterMenu[] = []
routers.sort(it => Number(it.order || 0)).forEach((item) => {
const { routes } = item
const newItem = { ...item }
if (Array.isArray(routes)) {
newItem.routes = this.initAuth(routes, true)
}
const check = this.checkAuth(item.path || "")
if (!check && item.path) {
const { element:Comp } = routers.find(item=>item.path === '/403') || {}
item.oPath = item.path
item.element = Comp || null
item.path = "/403"
} else {
child.push({ ...item })
}

if (!isParent) {
this.checkAuthMenus.push({ ...item })
}
})
return child
let child: RouterMenu[] = [];
routers
.sort((it) => Number(it.order || 0))
.forEach((item) => {
const { routes } = item;
const newItem = { ...item };
if (Array.isArray(routes)) {
newItem.routes = this.initAuth(routes, true);
}
if (!isParent) {
this.checkAuthMenus.push({ ...item });
}
child.push(newItem);
});
return child;
}

/**
* 2. 国际化翻译
* */
Expand Down Expand Up @@ -227,30 +221,60 @@ export class HandleMenu {

/**8. 获取父级path*/
getParentPath(path: string) {
const parentPath = this.childParent.get(path) || "/"
return parentPath
const parentPath = this.childParent.get(path) || '/';
return parentPath;
}

// 查询所有父节点和自己是否有权限
getCheckAuthAll(path: string) {
const node = this.flatAllMenu.find((item) => item.path === path);
if (!node) {
return false;
}
const pathList: string[] = [];
let currentNode: RouterMenu | undefined = node;
while (currentNode) {
pathList.push(currentNode.path || '');
currentNode = this.flatAllMenu.find((item) => item.id === currentNode?.parentId);
}
let hasPermission = true; // 假设有权限
// 从最上层父节点开始逐一判断权限
for (let i = pathList.length - 1; i >= 0; i--) {
const p = pathList[i];
if (!this.checkAuth(p)) {
hasPermission = false;
break;
}
}
return hasPermission;
}

/**9. 获取跳转地址*/
getToPath(path: string) {
// 1. 先判断是不是 side === true
const currentItem = this.flatAllMenu.find(item => item.path === path)
if (currentItem?.path === "/") {
this.preParentPath = ""
this.prePath = currentItem.redirectTo
return currentItem.redirectTo
const currentItem = this.flatAllMenu.find((item) => item.path === path);
if (currentItem?.path === '/') {
this.preParentPath = '';
this.prePath = currentItem.redirectTo;
return currentItem.redirectTo;
}
if (!currentItem) {
this.preParentPath = ""
if (this.prePath === "/404") {
this.preParentPath = ''
if (this.prePath === '/404') {
return false
}
this.prePath = "/404"
return "/404"
this.prePath = '/404'
return '/404'
}
// 查询所有父节点和自己是否有权限
if (!this.getCheckAuthAll(currentItem?.path || '')) {
this.prePath = '/403';
this.preParentPath = '';
return '/403';
}
if (!currentItem?.side) {
this.prePath = currentItem.path || ""
this.preParentPath = ""
this.prePath = currentItem.path || ''
this.preParentPath = ''
return false
}
if (this.preParentPath === currentItem.path) {
Expand All @@ -259,7 +283,7 @@ export class HandleMenu {
}
this.preParentPath = currentItem.path
const siderMenus = this.childMenu.get(path)
const findx = siderMenus?.find(item => item.index || item.redirectTo)
const findx = siderMenus?.find((item) => item.index || item.redirectTo)
const current = findx?.path || findx?.redirectTo
if (this.prePath === current) {
return false
Expand Down