Files
lingji-work-fe/src/layouts/components/siderBar/index.vue

188 lines
5.7 KiB
Vue
Raw Normal View History

2025-06-16 14:42:26 +08:00
<script lang="tsx">
2025-08-18 17:22:11 +08:00
import { Dropdown, Menu } from 'ant-design-vue';
2025-06-16 14:42:26 +08:00
import type { RouteMeta, RouteRecordRaw } from 'vue-router';
2025-08-18 17:22:11 +08:00
import { useRoute } from 'vue-router';
import SvgIcon from '@/components/svg-icon/index.vue';
2025-06-16 14:42:26 +08:00
import { useAppStore } from '@/stores';
2025-06-23 23:59:08 -04:00
import { useSidebarStore } from '@/stores/modules/side-bar';
2025-08-18 17:22:11 +08:00
import { MENU_LIST } from './menu-list';
import type { typeMenuItem } from './menu-list';
import icon1 from '@/assets/img/agent/icon1.png';
2025-06-16 14:42:26 +08:00
export default defineComponent({
emit: ['collapse'],
setup() {
2025-08-18 17:22:11 +08:00
// const appStore = useAppStore();
2025-06-16 14:42:26 +08:00
const router = useRouter();
const route = useRoute();
2025-08-18 17:22:11 +08:00
2025-06-23 23:59:08 -04:00
const sidebarStore = useSidebarStore();
2025-08-18 17:22:11 +08:00
const currentMenuList = ref<typeMenuItem[]>([]);
const currentMenuModInfo = ref<typeMenuItem>({});
const currentRouteName = computed(() => route.name as string);
const currentRouteGroup = computed(() => route.meta?.group ?? 'GroupMain');
const getCollapseMenuKey = (routeName: string): string => {
let _key: string;
for (let i = 0; i < currentMenuList.value.length; i++) {
const menuItem = currentMenuList.value[i];
// 检查是否有list子级
if (menuItem.children?.length > 0) {
for (let j = 0; j < menuItem.children.length; j++) {
const subMenuItem = menuItem.children[j];
if (subMenuItem.activeMatch?.includes(routeName)) {
currentMenuModInfo.value = menuItem;
_key = menuItem.key;
break;
}
}
} else {
// 没有list子级直接检查当前项
if (menuItem.routeName === routeName) {
currentMenuModInfo.value = menuItem;
_key = menuItem.key;
break;
}
}
2025-06-16 14:42:26 +08:00
}
2025-08-18 17:22:11 +08:00
return _key;
2025-06-16 14:42:26 +08:00
};
2025-08-18 17:22:11 +08:00
const onClickItem = (name: string) => {
router.push({ name });
};
const renderMenuItem = (item: typeMenuItem, hideLabel = false) => {
const getMenuItemClass = () => {
2025-06-16 14:42:26 +08:00
if (item.children?.length) {
2025-08-18 17:22:11 +08:00
return getCollapseMenuKey(currentRouteName.value) === item.key ? 'active' : '';
2025-06-16 14:42:26 +08:00
}
2025-08-18 17:22:11 +08:00
return item.activeMatch?.includes(currentRouteName.value) ? 'active' : '';
2025-06-16 14:42:26 +08:00
};
2025-08-18 17:22:11 +08:00
return (
<Menu.Item class={`menu-item ${getMenuItemClass()}`} onClick={() => onClickItem(item.routeName)}>
<SvgIcon size="18" name={item.icon} alt="状态图标" class="color-#55585F flex-shrink-0" />
{!hideLabel && <span class="cts label">{item.label}</span>}
</Menu.Item>
);
};
const renderMenuList = () => {
return currentMenuList.value.map((item) => {
if (!item.children) {
return renderMenuItem(item, sidebarStore.menuCollapse);
}
return (
<Dropdown
overlayClassName="layout-sider-dropdown-xt"
placement="rightTop"
v-slots={{
overlay: () => {
return (
<div class="p-8px bg-#fff container">
{item.children.map((child) => {
return renderMenuItem(child);
})}
</div>
);
},
}}
>
{renderMenuItem(item, sidebarStore.menuCollapse)}
</Dropdown>
);
2025-06-16 14:42:26 +08:00
});
};
2025-06-23 23:59:08 -04:00
2025-08-18 17:22:11 +08:00
const initMenuList = () => {
const groupMenuList = MENU_LIST?.[currentRouteGroup.value as string];
currentMenuList.value = cloneDeep(groupMenuList);
2025-06-16 14:42:26 +08:00
};
2025-08-18 17:22:11 +08:00
const initCollapse = () => {
getCollapseMenuKey(currentRouteName.value);
2025-07-25 17:26:46 +08:00
2025-08-18 17:22:11 +08:00
if (currentMenuModInfo.value) {
sidebarStore.setActiveMenuKey(currentMenuModInfo.value?.key);
2025-06-16 14:42:26 +08:00
}
};
2025-08-18 17:22:11 +08:00
const init = () => {
// 初始化菜单数据
initMenuList();
// 初始化菜单展开项
initCollapse();
};
onMounted(() => {
init();
});
2025-06-16 14:42:26 +08:00
return () => (
2025-08-18 17:22:11 +08:00
<Menu class={`siderBar-wrap p-16px w-full h-full flex flex-col ${sidebarStore.menuCollapse ? 'menu-fold' : ''}`}>
<Menu.Item
class={`menu-item !mb-0 ${currentRouteName.value === 'Home' ? 'active' : ''}`}
onClick={() => onClickItem('Home')}
>
<img src={icon1} width={18} height={18} />
{!sidebarStore.menuCollapse && <span class="cts label">智能搜索</span>}
</Menu.Item>
<div class="line w-full h-1px bg-#211F24 my-12px"></div>
<div class="flex flex-col flex-1">
<div class="menu-list flex-1">{renderMenuList()}</div>
<div class="flex justify-end items-center">
<div
class="flex fold-btn items-center cursor-pointer"
onClick={() => {
sidebarStore.setMenuCollapse();
}}
>
{sidebarStore.menuCollapse ? (
<icon-menu-unfold size={16} class="color-#55585F icon mr-4px" />
) : (
<icon-menu-fold size={16} class="color-#55585F icon mr-4px" />
)}
{!sidebarStore.menuCollapse && <span class="cts !color-#55585F flex-shrink-0">展开</span>}
</div>
</div>
</div>
</Menu>
2025-06-16 14:42:26 +08:00
);
},
});
</script>
2025-07-01 16:00:35 +08:00
<style lang="scss" scoped>
2025-08-18 17:22:11 +08:00
@import './style.scss';
</style>
<style lang="scss">
@import './style.scss';
.layout-sider-dropdown-xt {
.container {
2025-07-01 16:00:35 +08:00
border-radius: 8px;
2025-08-18 17:22:11 +08:00
background: var(--BG-White, #fff);
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
.menu-item {
@include menu-item;
padding: 8px;
&:hover {
2025-08-18 18:09:45 +08:00
background-color: rgba(109, 76, 254, 0.08);
2025-08-18 17:22:11 +08:00
color: #6d4cfe;
.svg-icon {
color: #6d4cfe;
}
2025-07-01 16:00:35 +08:00
}
2025-08-18 17:22:11 +08:00
&.active {
2025-08-18 18:09:45 +08:00
background-color: #fff !important;
2025-08-18 17:22:11 +08:00
.label,
.svg-icon {
color: #6d4cfe;
}
2025-07-01 16:00:35 +08:00
}
}
}
2025-06-16 14:42:26 +08:00
}
</style>