Files
lingji-work-fe/src/stores/modules/side-bar/index.ts

71 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-06-23 23:59:08 -04:00
/*
* @Author: RenXiaoDong
* @Date: 2025-06-23 22:13:30
*/
import { defineStore } from 'pinia';
2025-07-07 18:17:31 +08:00
import router from '@/router';
2025-06-23 23:59:08 -04:00
import type { RouteLocationNormalized } from 'vue-router';
2025-07-07 18:17:31 +08:00
import { MENU_LIST } from './constants';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
2025-06-23 23:59:08 -04:00
interface sidebarState {
activeMenuId: number | null;
2025-07-07 18:17:31 +08:00
menuList: any[];
2025-06-23 23:59:08 -04:00
}
export const useSidebarStore = defineStore('sidebar', {
state: (): sidebarState => ({
activeMenuId: null,
2025-07-07 18:17:31 +08:00
menuList: [],
2025-06-23 23:59:08 -04:00
}),
actions: {
clearActiveMenuId() {
2025-06-24 01:35:52 -04:00
this.activeMenuId = null;
2025-06-23 23:59:08 -04:00
},
setActiveMenuId(id: number) {
this.activeMenuId = id;
},
2025-07-07 18:17:31 +08:00
// navbar菜单列表由企业对应权限决定
getNavbarMenuList() {
const enterpriseStore = useEnterpriseStore();
const enterpriseInfo = enterpriseStore.getEnterpriseInfo();
this.menuList = MENU_LIST.filter(
(item) => !item.permissionKey || enterpriseInfo?.permissions?.includes(item.permissionKey),
);
},
2025-06-23 23:59:08 -04:00
// 根据当前路由自动设置 activeMenuId
setActiveMenuIdByRoute(route: RouteLocationNormalized) {
2025-07-07 18:17:31 +08:00
const appRoutes = router.options?.routes ?? [];
2025-06-23 23:59:08 -04:00
// 查找当前路由所属的菜单组
const findMenuGroup = (routes: any[]): number | null => {
for (const routeItem of routes) {
// 检查子路由
2025-07-07 18:17:31 +08:00
if (routeItem.children?.length > 0) {
2025-06-23 23:59:08 -04:00
// 检查当前路由是否是这个父路由的子路由
const isChildRoute = routeItem.children.some((child: any) => child.name === route.name);
if (isChildRoute) {
return routeItem.meta?.id || null;
}
// 递归检查更深层的子路由
const childResult = findMenuGroup(routeItem.children);
if (childResult !== null) {
return routeItem.meta?.id || childResult;
}
2025-07-07 18:17:31 +08:00
} else {
if (routeItem.name === route.name) {
return routeItem.meta?.id || null;
}
2025-06-23 23:59:08 -04:00
}
}
return null;
};
const menuId = findMenuGroup(appRoutes);
if (menuId !== null) {
this.activeMenuId = menuId;
}
},
},
});