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

102 lines
2.9 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-07-18 17:10:12 +08:00
import { getTaskUnread, patchTaskRead } from '@/api/all/common';
2025-06-23 23:59:08 -04:00
interface sidebarState {
activeMenuId: number | null;
2025-07-07 18:17:31 +08:00
menuList: any[];
2025-07-08 16:55:04 +08:00
allowAccessRoutes: any[];
2025-07-18 17:10:12 +08:00
unreadInfo: number[];
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-07-18 17:10:12 +08:00
activeMenuId: null, //
menuList: [], // 菜单信息
unreadInfo: [], // 未读消息
2025-07-08 16:55:04 +08:00
allowAccessRoutes: [], // 允许访问的路由列表
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-08 16:55:04 +08:00
clearUserNavbarMenuList() {
this.menuList = [];
},
2025-07-07 18:17:31 +08:00
// navbar菜单列表由企业对应权限决定
2025-07-08 16:55:04 +08:00
getUserNavbarMenuList() {
2025-07-07 18:17:31 +08:00
const enterpriseStore = useEnterpriseStore();
this.menuList = MENU_LIST.filter(
2025-07-08 16:55:04 +08:00
(item) => !item.permissionKey || enterpriseStore.enterpriseInfo?.permissions?.includes(item.permissionKey),
2025-07-07 18:17:31 +08:00
);
},
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;
};
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) {
this.activeMenuId = menuId;
}
},
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
},
});