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

264 lines
8.0 KiB
Vue
Raw Normal View History

2025-06-16 14:42:26 +08:00
<script lang="tsx">
2025-09-11 11:26:51 +08:00
import { Dropdown, Layout, Menu } from 'ant-design-vue';
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
2025-09-11 11:26:51 +08:00
import { useAppStore, useUserStore } 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 type { typeMenuItem } from './menu-list';
2025-09-11 11:26:51 +08:00
import { MENU_LIST } from './menu-list';
import { handleUserHome } from '@/utils/user';
2025-08-18 17:22:11 +08:00
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();
const appStore = useAppStore();
2025-09-11 11:26:51 +08:00
const userStore = useUserStore();
2025-08-18 17:22:11 +08:00
2025-09-11 11:26:51 +08:00
// const currentMenuList = ref<typeMenuItem[]>([]);
2025-08-18 17:22:11 +08:00
const currentMenuModInfo = ref<typeMenuItem>({});
const currentRouteName = computed(() => route.name as string);
const currentRouteGroup = computed(() => route.meta?.group ?? 'GroupMain');
2025-08-29 12:03:04 +08:00
const isHomeRoute = computed(() => currentRouteName.value === 'Home');
const showAiSearch = computed(() => !route.meta?.hideAiSearch);
2025-09-11 11:26:51 +08:00
const currentMenuList = computed(() => sidebarStore.currentMenuList);
const hasOpenEnterprise = computed(() => userStore.isOpenEnterprise);
2025-08-18 17:22:11 +08:00
const collapsed = computed(() => {
return sidebarStore.menuCollapse;
});
const setCollapsed = (val) => {
appStore.updateSettings({ menuCollapse: val });
};
2025-08-18 17:22:11 +08:00
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-09-03 16:47:24 +08:00
const onClickItem = (item: typeMenuItem) => {
let targetRoute = item.routeName;
if (item.children?.length) {
targetRoute = item.children[0].routeName;
}
router.push({ name: targetRoute });
2025-08-18 17:22:11 +08:00
};
2025-09-10 14:29:18 +08:00
const renderMenuItem = ({
item,
hideLabel = false,
menuItemClass = '',
}: {
item: typeMenuItem;
hideLabel?: boolean;
menuItemClass?: string;
}) => {
2025-08-18 17:22:11 +08:00
const getMenuItemClass = () => {
2025-08-29 12:03:04 +08:00
const hasChildren = item.children?.length;
2025-09-10 14:29:18 +08:00
let target = '';
2025-08-29 12:03:04 +08:00
if (hasChildren) {
target += getCollapseMenuKey(currentRouteName.value) === item.key ? 'active' : '';
} else {
target += item.activeMatch?.includes(currentRouteName.value) ? 'active' : '';
2025-06-16 14:42:26 +08:00
}
2025-08-29 12:03:04 +08:00
return target;
2025-06-16 14:42:26 +08:00
};
2025-08-18 17:22:11 +08:00
return (
2025-09-10 14:29:18 +08:00
<Menu.Item class={`menu-item ${getMenuItemClass()} ${menuItemClass}`} onClick={() => onClickItem(item)}>
2025-08-29 12:03:04 +08:00
{(() => {
2025-09-10 14:29:18 +08:00
const isActive = getMenuItemClass().includes('active');
2025-08-29 12:03:04 +08:00
const iconName = Array.isArray(item.icon)
? isActive
? item.icon[1] ?? item.icon[0]
: item.icon[0]
: item.icon;
return <SvgIcon size="18" name={iconName as any} alt="状态图标" class="color-#55585F flex-shrink-0" />;
})()}
2025-08-18 17:22:11 +08:00
{!hideLabel && <span class="cts label">{item.label}</span>}
</Menu.Item>
);
};
const renderMenuList = () => {
return currentMenuList.value.map((item) => {
if (!item.children) {
2025-09-10 14:29:18 +08:00
return renderMenuItem({ item, hideLabel: collapsed.value });
2025-08-18 17:22:11 +08:00
}
return (
<Dropdown
overlayClassName="layout-sider-dropdown-xt"
placement="rightTop"
2025-09-01 10:15:44 +08:00
align={{ offset: [8, 0] }}
2025-08-18 17:22:11 +08:00
v-slots={{
overlay: () => {
return (
2025-09-01 10:38:33 +08:00
<div class="p-8px bg-#fff container w-139px">
2025-08-18 17:22:11 +08:00
{item.children.map((child) => {
2025-09-10 14:29:18 +08:00
return renderMenuItem({ item: child, menuItemClass: 'sub-menu-item' });
2025-08-18 17:22:11 +08:00
})}
</div>
);
},
}}
>
2025-09-10 14:29:18 +08:00
{renderMenuItem({ item, hideLabel: collapsed.value })}
2025-08-18 17:22:11 +08:00
</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 = () => {
2025-09-11 11:26:51 +08:00
let groupMenuList = MENU_LIST?.[currentRouteGroup.value as string] ?? [];
// 如果企业未开通,过滤掉 requireAuth 为 true 的菜单项
if (!hasOpenEnterprise.value) {
groupMenuList = groupMenuList.filter((item) => {
if (item.requireAuth === true) {
return false;
}
if (item.children && item.children.length > 0) {
const filteredChildren = item.children.filter((child) => !child.requireAuth);
if (filteredChildren.length === 0 && !item.routeName) {
return false;
}
item.children = filteredChildren;
}
return true;
});
}
sidebarStore.setCurrentMenuList(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();
};
2025-08-29 12:03:04 +08:00
watch(
() => currentRouteGroup.value,
() => {
init();
},
{ immediate: false, deep: true },
);
2025-08-18 17:22:11 +08:00
onMounted(() => {
init();
});
2025-06-16 14:42:26 +08:00
return () => (
<Layout.Sider
v-model={collapsed.value}
width={sidebarStore.sidebarWidth}
collapsible
trigger
onCollapse={setCollapsed}
>
2025-09-01 14:07:18 +08:00
<Menu class={`siderBar-wrap w-full flex flex-col px-16px pt-16px ${collapsed.value ? 'menu-fold' : ''}`}>
{showAiSearch.value && (
<>
<Menu.Item class={`menu-item !mb-0 ${isHomeRoute.value ? 'active' : ''}`} onClick={handleUserHome}>
<img src={icon1} width={18} height={18} />
2025-09-01 14:07:18 +08:00
{!collapsed.value && <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>
2025-08-18 17:22:11 +08:00
</div>
</Menu>
2025-09-01 14:07:18 +08:00
<div
class={`bg-#F6F5FC flex items-center absolute bottom-0 w-full pt-8px px-16px pb-16px right-0 ${
collapsed.value ? 'justify-center' : 'justify-end'
}`}
>
<div
class="flex fold-btn items-center cursor-pointer h-22px "
onClick={() => {
sidebarStore.setMenuCollapse();
}}
>
{collapsed.value ? (
<icon-menu-unfold size={16} class="color-#55585F icon mr-4px" />
) : (
<icon-menu-fold size={16} class="color-#55585F icon mr-4px" />
)}
{!collapsed.value && <span class="cts !color-#55585F flex-shrink-0">收起</span>}
</div>
</div>
</Layout.Sider>
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';
2025-09-11 11:26:51 +08:00
2025-08-18 17:22:11 +08:00
.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);
2025-09-11 11:26:51 +08:00
2025-08-18 17:22:11 +08:00
.menu-item {
@include menu-item;
padding: 8px;
2025-09-11 11:26:51 +08:00
2025-08-18 17:22:11 +08:00
&:hover {
2025-09-01 09:45:51 +08:00
background-color: rgba(109, 76, 254, 0.08) !important;
color: #6d4cfe !important;
2025-09-11 11:26:51 +08:00
2025-08-18 17:22:11 +08:00
.svg-icon {
2025-09-01 09:45:51 +08:00
color: #6d4cfe !important;
2025-08-18 17:22:11 +08:00
}
2025-07-01 16:00:35 +08:00
}
}
}
2025-06-16 14:42:26 +08:00
}
</style>