2025-06-23 23:59:08 -04:00
|
|
|
/*
|
|
|
|
|
* @Author: RenXiaoDong
|
|
|
|
|
* @Date: 2025-06-23 22:13:30
|
|
|
|
|
*/
|
2025-08-29 16:44:27 +08:00
|
|
|
// import { useRoute } from 'vue-router';
|
2025-06-23 23:59:08 -04:00
|
|
|
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-08-22 09:51:32 +08:00
|
|
|
// import { MENU_LIST } from './constants';
|
2025-07-07 18:17:31 +08:00
|
|
|
import { useEnterpriseStore } from '@/stores/modules/enterprise';
|
2025-07-18 17:10:12 +08:00
|
|
|
import { getTaskUnread, patchTaskRead } from '@/api/all/common';
|
2025-06-23 23:59:08 -04:00
|
|
|
|
|
|
|
|
interface sidebarState {
|
2025-08-18 17:22:11 +08:00
|
|
|
activeMenuKey: string | number | null;
|
2025-08-29 16:44:27 +08:00
|
|
|
currentMenuList: any[];
|
2025-07-18 17:10:12 +08:00
|
|
|
unreadInfo: number[];
|
2025-08-18 17:22:11 +08:00
|
|
|
menuCollapse: boolean;
|
2025-06-23 23:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-18 17:10:12 +08:00
|
|
|
let unreadInfoTimer: number | null = null;
|
|
|
|
|
|
2025-06-23 23:59:08 -04:00
|
|
|
export const useSidebarStore = defineStore('sidebar', {
|
|
|
|
|
state: (): sidebarState => ({
|
2025-08-18 17:22:11 +08:00
|
|
|
activeMenuKey: null, // 激活的菜单id
|
2025-08-29 16:44:27 +08:00
|
|
|
currentMenuList: [], // 菜单信息
|
2025-07-18 17:10:12 +08:00
|
|
|
unreadInfo: [], // 未读消息
|
2025-08-18 17:22:11 +08:00
|
|
|
menuCollapse: false, // 菜单是否折叠
|
2025-06-23 23:59:08 -04:00
|
|
|
}),
|
2025-08-18 17:38:14 +08:00
|
|
|
getters: {
|
2025-08-29 16:44:27 +08:00
|
|
|
showSider(): boolean {
|
|
|
|
|
const route = router.currentRoute.value;
|
|
|
|
|
return !route.meta?.hideSidebar && this.currentMenuList.length > 0;
|
|
|
|
|
},
|
2025-08-18 17:38:14 +08:00
|
|
|
sidebarWidth(): number {
|
2025-08-29 16:44:27 +08:00
|
|
|
if (!this.showSider) return 0;
|
2025-08-18 17:38:14 +08:00
|
|
|
return this.menuCollapse ? 74 : 138;
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-06-23 23:59:08 -04:00
|
|
|
actions: {
|
2025-08-18 17:22:11 +08:00
|
|
|
clearActiveMenuKey() {
|
|
|
|
|
this.activeMenuKey = null;
|
2025-06-23 23:59:08 -04:00
|
|
|
},
|
2025-08-18 17:22:11 +08:00
|
|
|
setActiveMenuKey(key: string | number) {
|
|
|
|
|
this.activeMenuKey = key;
|
|
|
|
|
},
|
|
|
|
|
setMenuCollapse() {
|
|
|
|
|
this.menuCollapse = !this.menuCollapse;
|
|
|
|
|
},
|
|
|
|
|
clearMenuCollapse() {
|
|
|
|
|
this.menuCollapse = false;
|
2025-06-23 23:59:08 -04:00
|
|
|
},
|
2025-08-29 16:44:27 +08:00
|
|
|
setCurrentMenuList(val: any[]) {
|
|
|
|
|
this.currentMenuList = val;
|
|
|
|
|
},
|
2025-07-07 18:17:31 +08:00
|
|
|
// navbar菜单列表由企业对应权限决定
|
2025-08-22 09:51:32 +08:00
|
|
|
// getUserNavbarMenuList() {
|
|
|
|
|
// const enterpriseStore = useEnterpriseStore();
|
|
|
|
|
// this.menuList = MENU_LIST.filter(
|
|
|
|
|
// (item) => !item.permissionKey || enterpriseStore.enterpriseInfo?.permissions?.includes(item.permissionKey),
|
|
|
|
|
// );
|
|
|
|
|
// },
|
2025-08-18 17:22:11 +08:00
|
|
|
// 根据当前路由自动设置 activeMenuKey
|
|
|
|
|
setActiveMenuKeyByRoute(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;
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-08 16:55:04 +08:00
|
|
|
const menuId = findMenuGroup(appRoutes as any);
|
2025-06-23 23:59:08 -04:00
|
|
|
if (menuId !== null) {
|
2025-08-18 17:22:11 +08:00
|
|
|
this.activeMenuKey = menuId;
|
2025-06-23 23:59:08 -04:00
|
|
|
}
|
|
|
|
|
},
|
2025-07-18 17:10:12 +08:00
|
|
|
async getTaskUnreadInfo() {
|
|
|
|
|
const { code, data } = await getTaskUnread();
|
|
|
|
|
if (code === 200) {
|
|
|
|
|
this.unreadInfo = data;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 查询未读信息
|
|
|
|
|
startUnreadInfoPolling() {
|
|
|
|
|
if (unreadInfoTimer) return;
|
|
|
|
|
this.getTaskUnreadInfo();
|
|
|
|
|
unreadInfoTimer = window.setInterval(() => {
|
|
|
|
|
this.getTaskUnreadInfo();
|
|
|
|
|
}, 30000);
|
|
|
|
|
},
|
|
|
|
|
stopUnreadInfoPolling() {
|
|
|
|
|
this.unreadInfo = [];
|
|
|
|
|
if (unreadInfoTimer) {
|
|
|
|
|
clearInterval(unreadInfoTimer);
|
|
|
|
|
unreadInfoTimer = null;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
async removeTaskUnreadInfo() {
|
|
|
|
|
patchTaskRead({ ids: this.unreadInfo });
|
|
|
|
|
this.unreadInfo = [];
|
|
|
|
|
},
|
2025-06-23 23:59:08 -04:00
|
|
|
},
|
|
|
|
|
});
|