/* * @Author: wei.li * @Date: 2021-11-17 16:02:30 * @LastEditors: levi7754 levi7754@163.com * @LastEditTime: 2024-12-17 18:52:43 * @Description: file content */ import { store } from '../index'; import type { appType } from './types'; import { defineStore } from 'pinia'; import { storageLocal, deviceDetection } from '@utogether/utils'; import { responsiveStorageNameSpace, getConfig } from '../../config'; export const useAppStore = defineStore('udp-app', { state: (): appType => ({ sidebar: { opened: storageLocal.getItem(`${responsiveStorageNameSpace()}layout`)?.sidebarStatus ?? getConfig().SidebarStatus, withoutAnimation: false, isClickCollapse: false }, // 这里的layout用于监听容器拖拉后恢复对应的菜单布局 layout: storageLocal.getItem(`${responsiveStorageNameSpace()}layout`)?.layout ?? getConfig().Layout, device: deviceDetection() ? 'mobile' : 'desktop' }), getters: { getSidebarStatus() { return this.sidebar.opened; }, getDevice() { return this.device; } }, actions: { TOGGLE_SIDEBAR(opened?: boolean, resize?: string) { const layout = storageLocal.getItem(`${responsiveStorageNameSpace()}layout`); if (!layout) return; if (opened && resize) { this.sidebar.withoutAnimation = true; this.sidebar.opened = true; layout.sidebarStatus = true; } else if (!opened && resize) { this.sidebar.withoutAnimation = true; this.sidebar.opened = false; layout.sidebarStatus = false; } else if (!opened && !resize) { this.sidebar.withoutAnimation = false; this.sidebar.opened = !this.sidebar.opened; this.sidebar.isClickCollapse = !this.sidebar.opened; layout.sidebarStatus = this.sidebar.opened; } storageLocal.setItem(`${responsiveStorageNameSpace()}layout`, layout); }, async toggleSideBar(opened?: boolean, resize?: string) { await this.TOGGLE_SIDEBAR(opened, resize); }, toggleDevice(device: string) { this.device = device; }, setLayout(layout) { this.layout = layout; } } }); export function useAppStoreHook() { return useAppStore(store); }