2024-11-18 19:40:44 +08:00
|
|
|
import { DeviceEnum, SIDEBAR_CLOSED, SIDEBAR_OPENED } from "@/constants/app-key"
|
2024-11-14 17:33:12 +08:00
|
|
|
import { pinia } from "@/store"
|
2023-06-01 09:24:37 +08:00
|
|
|
import { getSidebarStatus, setSidebarStatus } from "@/utils/cache/local-storage"
|
2024-11-18 19:40:44 +08:00
|
|
|
import { defineStore } from "pinia"
|
|
|
|
import { reactive, ref, watch } from "vue"
|
2022-04-21 18:20:39 +08:00
|
|
|
|
2023-05-21 09:51:41 +08:00
|
|
|
interface Sidebar {
|
2022-08-24 16:52:01 +08:00
|
|
|
opened: boolean
|
|
|
|
withoutAnimation: boolean
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
|
2023-05-24 18:24:06 +08:00
|
|
|
/** 设置侧边栏状态本地缓存 */
|
|
|
|
function handleSidebarStatus(opened: boolean) {
|
|
|
|
opened ? setSidebarStatus(SIDEBAR_OPENED) : setSidebarStatus(SIDEBAR_CLOSED)
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:52:01 +08:00
|
|
|
export const useAppStore = defineStore("app", () => {
|
2023-05-24 18:24:06 +08:00
|
|
|
/** 侧边栏状态 */
|
2023-05-21 09:51:41 +08:00
|
|
|
const sidebar: Sidebar = reactive({
|
2023-05-24 18:24:06 +08:00
|
|
|
opened: getSidebarStatus() !== SIDEBAR_CLOSED,
|
2022-08-24 16:52:01 +08:00
|
|
|
withoutAnimation: false
|
|
|
|
})
|
2023-05-24 18:24:06 +08:00
|
|
|
/** 设备类型 */
|
2023-05-21 10:47:42 +08:00
|
|
|
const device = ref<DeviceEnum>(DeviceEnum.Desktop)
|
2022-08-24 16:52:01 +08:00
|
|
|
|
2024-11-19 20:14:23 +08:00
|
|
|
// 监听侧边栏 opened 状态
|
2023-05-24 18:24:06 +08:00
|
|
|
watch(
|
|
|
|
() => sidebar.opened,
|
2024-11-18 19:40:44 +08:00
|
|
|
opened => handleSidebarStatus(opened)
|
2023-05-24 18:24:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
/** 切换侧边栏 */
|
2022-08-24 16:52:01 +08:00
|
|
|
const toggleSidebar = (withoutAnimation: boolean) => {
|
|
|
|
sidebar.opened = !sidebar.opened
|
|
|
|
sidebar.withoutAnimation = withoutAnimation
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
2023-05-24 18:24:06 +08:00
|
|
|
/** 关闭侧边栏 */
|
2022-08-24 16:52:01 +08:00
|
|
|
const closeSidebar = (withoutAnimation: boolean) => {
|
|
|
|
sidebar.opened = false
|
|
|
|
sidebar.withoutAnimation = withoutAnimation
|
|
|
|
}
|
2023-05-24 18:24:06 +08:00
|
|
|
/** 切换设备类型 */
|
2023-05-21 10:47:42 +08:00
|
|
|
const toggleDevice = (value: DeviceEnum) => {
|
2022-08-24 16:52:01 +08:00
|
|
|
device.value = value
|
|
|
|
}
|
|
|
|
|
2022-10-17 15:04:27 +08:00
|
|
|
return { device, sidebar, toggleSidebar, closeSidebar, toggleDevice }
|
2022-04-21 18:20:39 +08:00
|
|
|
})
|
2024-11-14 17:33:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
|
|
|
|
* 在 SSR 应用中可用于在 setup 外使用 store
|
|
|
|
*/
|
2024-11-21 20:41:48 +08:00
|
|
|
export function useAppStoreOutside() {
|
2024-11-14 17:33:12 +08:00
|
|
|
return useAppStore(pinia)
|
|
|
|
}
|