import { defineStore } from "pinia"; import Utils from "@/utils"; import { RoleInfo, UserSettingInfo, WidgetInfo, WidgetPosition } from "@/types"; import { flatten, indexOf, isNull, orderBy, pick, set } from "lodash"; import { BaseDict } from "@/types/Basics"; export interface IdefaultStore { menuVisible: boolean; userInfo: any; curRoleInfo: RoleInfo; userRoleList: Array; curUserSettingInfo: UserSettingInfo; curUserMenuList: Array; //当前角色菜单 curUserAppList: Array; //当前角色所有应用 /** 应用列表加载态(AppList 拉取时写入,Header 命令面板共享同一状态;2026-10-08) */ appListLoading: boolean; curRoleUid: string | null; //当前角色uid /** 未读消息数(msgCenter/summary 拉取 + WS newMessage 实时累加;Header 角标与 /msg 页左栏同源) */ unreadCount: number; // curUsedAppList: Array; //当前角色常用应用 } const defaultRoleInfo = () => ({ roleId: 0, roleName: "", schId: 0, schName: "", childId: 0, childName: "", }); const defaultUserSettingInfo = () => ({ wallpaperUrl: "", isWidgetShow: true, widgetAppIds: "", widgetAppList: [], desktopAppList: [], width: 380, height: 300, xaxis: 0, yaxis: 0, }); // currentYear: 0, // currentSemester: null, export const useDefaultStore = defineStore("defaultStore", { state: (): IdefaultStore => { return { menuVisible: true, userInfo: {}, curRoleInfo: defaultRoleInfo(), curUserSettingInfo: defaultUserSettingInfo(), userRoleList: [], curUserAppList: [], curUserMenuList: [], curRoleUid: null, unreadCount: 0, appListLoading: false, }; }, actions: { setCurRoleInfo(info: RoleInfo) { this.curRoleInfo = info; }, setUnreadCount(count: number) { this.unreadCount = Math.max(0, count || 0); }, incUnread() { this.unreadCount += 1; }, decUnread() { this.unreadCount = Math.max(0, this.unreadCount - 1); }, setCurUserSettingsInfo(info: UserSettingInfo) { this.curUserSettingInfo = info; }, setCurUserAppList(apps: any[]) { this.curUserAppList = apps; }, setAppListLoading(v: boolean) { this.appListLoading = v; }, setCurUserMenuList(menus: any[]) { this.curUserMenuList = menus; }, updateWidgetInfo(info: WidgetPosition) { Object.assign(this.curUserSettingInfo, info); }, }, getters: { getCurSchId(): number { return this.curRoleInfo?.schId || -1; }, getCurRoleId(): number { return this.curRoleInfo?.roleId || -1; }, getCurUserId(): number { if (this.getCurRoleId == 3) { if (!!this.curRoleInfo.childId) return this.curRoleInfo.childId; } return this.userInfo.id; }, getCurWallPaperUrl(): any { return this.curUserSettingInfo.wallpaperUrl; }, getFlattenAppList(): any[] { return flatten(this.curUserAppList.map(x => x.apps)); }, getIsAdmin() { // const user: any = this.userInfo; // if (user.type == 1) return true; // if (typeof user.roleIds == "undefined" || user.roleIds == null) return false; // let adminRoleList = user.roleList.filter((x: any) => [2, 5, 7].includes(x.roleId)); // return adminRoleList.findIndex((x: any) => x.schId == this.getCurSchId) > -1; const user: any = this.userInfo; if (user.type == 1) return true; let roleId: number = this.getCurRoleId; return [2, 5, 7].includes(roleId); }, getWidgetSel(): BaseDict[] { const { widgetAppList } = this.curUserSettingInfo; let arr = widgetAppList.map(x => { return { k: x.id, v: x.name, checked: this.getWidgetAppIds.includes(x.id) } as BaseDict; }); return arr; }, getWidgetAppIds() { const { widgetAppIds: appIds, widgetAppList: appList } = this.curUserSettingInfo; let checkIds: number[] = []; // getWidgetAppList 的比较器按 appIds 降序渲染(新添加置顶),故未配置态需预反转,才与 widget_catalog.sort 正序一致 if (appIds == null) checkIds = appList.map(x => x.id).reverse(); else if (appIds.length == 0) checkIds = []; else checkIds = appIds.split(",").map(x => parseInt(x)); return checkIds; }, getWidgetAppList() { const { widgetAppList: appList } = this.curUserSettingInfo; const appIds: number[] = this.getWidgetAppIds; let arr: any[] = appList .filter(x => appIds.includes(x.id)) .sort((a: any, b: any) => { return appIds.indexOf(b.id) - appIds.indexOf(a.id); }); return arr; }, getWidgetDragObj() { let keys: Array = ["isWidgetShow", "xaxis", "yaxis", "width", "height"]; let dragObj: WidgetInfo = pick(this.curUserSettingInfo, keys); let defaultObj = defaultUserSettingInfo(); if (dragObj.isWidgetShow == null) { dragObj = pick(defaultObj, keys); dragObj.isWidgetShow = true; } if (dragObj.xaxis === null || dragObj.yaxis == null) { dragObj.xaxis = defaultObj.xaxis; dragObj.yaxis = defaultObj.yaxis; dragObj.width = defaultObj.width; dragObj.height = defaultObj.height; } return dragObj; }, }, });