|
|
@@ -145,24 +145,17 @@ export const hasMenuPermission = (menuId: number): boolean => {
|
|
|
return findMenu(menus, menuId);
|
|
|
};
|
|
|
|
|
|
-// 根据菜单路径检查是否有菜单权限
|
|
|
+// 根据菜单路径检查是否有菜单权限(会校验 visible)
|
|
|
export const hasMenuPathPermission = (path: string): boolean => {
|
|
|
const menus = getMenus();
|
|
|
- // 如果menus为null或空数组,返回false
|
|
|
- if (!menus || menus.length === 0) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- // 递归查找菜单
|
|
|
+ if (!menus || menus.length === 0) return false;
|
|
|
const findMenuByPath = (menuList: MenuItem[], menuPath: string): boolean => {
|
|
|
for (const menu of menuList) {
|
|
|
- // 匹配完整路径或部分路径
|
|
|
if (menu.path === menuPath || menu.path?.endsWith(menuPath)) {
|
|
|
return menu.visible !== false;
|
|
|
}
|
|
|
if (menu.children && menu.children.length > 0) {
|
|
|
- if (findMenuByPath(menu.children, menuPath)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
+ if (findMenuByPath(menu.children, menuPath)) return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
@@ -170,6 +163,20 @@ export const hasMenuPathPermission = (path: string): boolean => {
|
|
|
return findMenuByPath(menus, path);
|
|
|
};
|
|
|
|
|
|
+/** 只判断菜单树中是否包含该 path,不校验 visible */
|
|
|
+export const hasMenuPath = (path: string): boolean => {
|
|
|
+ const menus = getMenus();
|
|
|
+ if (!menus || menus.length === 0) return false;
|
|
|
+ const find = (list: MenuItem[], p: string): boolean => {
|
|
|
+ for (const m of list) {
|
|
|
+ if (m.path === p || m.path?.endsWith(p)) return true;
|
|
|
+ if (m.children && m.children.length > 0 && find(m.children, p)) return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ };
|
|
|
+ return find(menus, path);
|
|
|
+};
|
|
|
+
|
|
|
// 过滤菜单(根据权限)
|
|
|
export const filterMenusByPermission = (menus: MenuItem[]): MenuItem[] => {
|
|
|
return menus
|
|
|
@@ -268,3 +275,49 @@ export const mapMenuPathToKey = (path: string): string | null => {
|
|
|
return null;
|
|
|
};
|
|
|
|
|
|
+/**
|
|
|
+ * 从菜单树中按顺序(深度优先)找到第一个 path 有效且 visible 的菜单路径
|
|
|
+ */
|
|
|
+export const getFirstMenuPath = (menus: MenuItem[] | null): string | null => {
|
|
|
+ if (!menus || menus.length === 0) return null;
|
|
|
+ const find = (list: MenuItem[]): string | null => {
|
|
|
+ for (const m of list) {
|
|
|
+ if (m.visible === false) continue;
|
|
|
+ const p = (m.path || '').trim();
|
|
|
+ if (p && p !== '/') return p;
|
|
|
+ if (m.children && m.children.length > 0) {
|
|
|
+ const c = find(m.children);
|
|
|
+ if (c) return c;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ };
|
|
|
+ return find(menus);
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 根据 mapMenuPathToKey 的 key 映射为 { menu, subMenu },供 navigateToMenu 使用
|
|
|
+ */
|
|
|
+export const getMenuAndSubMenuFromKey = (key: string): { menu: string; subMenu: string } => {
|
|
|
+ const sys = ['menuManagement', 'departmentManagement', 'positionManagement', 'roleManagement', 'dictionaryManagement', 'cabinetManagement'];
|
|
|
+ if (key === 'systemConfig' || sys.includes(key)) return { menu: 'systemConfig', subMenu: sys.includes(key) ? key : 'menuManagement' };
|
|
|
+ if (key === 'userManagement' || key === 'userList' || key === 'notificationManagement') return { menu: 'userManagement', subMenu: key === 'userManagement' ? 'userList' : key };
|
|
|
+ const hw = ['cabinet', 'key', 'padlock', 'portable'];
|
|
|
+ if (key === 'hardwareManagement' || hw.includes(key)) return { menu: 'hardwareManagement', subMenu: hw.includes(key) ? key : 'cabinet' };
|
|
|
+ if (key === 'locationManagement') return { menu: 'locationManagement', subMenu: 'locationManagement' };
|
|
|
+ const iw = ['processDesign', 'sopManagement', 'workManagement', 'formManagement', 'processTemplate'];
|
|
|
+ if (key === 'isolationWork' || iw.includes(key)) return { menu: 'isolationWork', subMenu: iw.includes(key) ? key : 'processDesign' };
|
|
|
+ if (key === 'taskManagement') return { menu: 'taskManagement', subMenu: 'taskManagement' };
|
|
|
+ if (key === 'myTask') return { menu: 'isolationWork', subMenu: 'myTask' };
|
|
|
+ if (key === 'dashboard') return { menu: 'dashboard', subMenu: 'dashboard' };
|
|
|
+ return { menu: key, subMenu: key };
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 根据菜单 path 得到 { menu, subMenu },用于登录后跳转到非 /dashboard 的首个菜单时设置 navigateToMenu
|
|
|
+ */
|
|
|
+export const getMenuAndSubMenuFromPath = (path: string): { menu: string; subMenu: string } | null => {
|
|
|
+ const k = mapMenuPathToKey(path);
|
|
|
+ return k ? getMenuAndSubMenuFromKey(k) : null;
|
|
|
+};
|
|
|
+
|