102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
/*
|
|
* @Author: RenXiaoDong
|
|
* @Date: 2025-06-23 22:13:30
|
|
*/
|
|
import { defineStore } from 'pinia';
|
|
import router from '@/router';
|
|
import type { RouteLocationNormalized } from 'vue-router';
|
|
import { MENU_LIST } from './constants';
|
|
import { useEnterpriseStore } from '@/stores/modules/enterprise';
|
|
import { getTaskUnread, patchTaskRead } from '@/api/all/common';
|
|
|
|
interface sidebarState {
|
|
activeMenuId: number | null;
|
|
menuList: any[];
|
|
allowAccessRoutes: any[];
|
|
unreadInfo: number[];
|
|
}
|
|
|
|
let unreadInfoTimer: number | null = null;
|
|
|
|
export const useSidebarStore = defineStore('sidebar', {
|
|
state: (): sidebarState => ({
|
|
activeMenuId: null, //
|
|
menuList: [], // 菜单信息
|
|
unreadInfo: [], // 未读消息
|
|
allowAccessRoutes: [], // 允许访问的路由列表
|
|
}),
|
|
actions: {
|
|
clearActiveMenuId() {
|
|
this.activeMenuId = null;
|
|
},
|
|
setActiveMenuId(id: number) {
|
|
this.activeMenuId = id;
|
|
},
|
|
clearUserNavbarMenuList() {
|
|
this.menuList = [];
|
|
},
|
|
// navbar菜单列表由企业对应权限决定
|
|
getUserNavbarMenuList() {
|
|
const enterpriseStore = useEnterpriseStore();
|
|
this.menuList = MENU_LIST.filter(
|
|
(item) => !item.permissionKey || enterpriseStore.enterpriseInfo?.permissions?.includes(item.permissionKey),
|
|
);
|
|
},
|
|
// 根据当前路由自动设置 activeMenuId
|
|
setActiveMenuIdByRoute(route: RouteLocationNormalized) {
|
|
const appRoutes = router.options?.routes ?? [];
|
|
|
|
const findMenuGroup = (routes: any[]): number | null => {
|
|
for (const routeItem of routes) {
|
|
if (routeItem.children?.length > 0) {
|
|
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;
|
|
}
|
|
} else {
|
|
if (routeItem.name === route.name) {
|
|
return routeItem.meta?.id || null;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const menuId = findMenuGroup(appRoutes as any);
|
|
if (menuId !== null) {
|
|
this.activeMenuId = menuId;
|
|
}
|
|
},
|
|
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 = [];
|
|
},
|
|
},
|
|
});
|