app.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { store } from "/@/store";
  2. import { appType } from "./types";
  3. import { defineStore } from "pinia";
  4. import { getConfig } from "/@/config";
  5. import type { StorageConfigs } from "/#/index";
  6. import { deviceDetection, storageLocal } from "@pureadmin/utils";
  7. export const useAppStore = defineStore({
  8. id: "pure-app",
  9. state: (): appType => ({
  10. sidebar: {
  11. opened:
  12. storageLocal.getItem<StorageConfigs>("responsive-layout")
  13. ?.sidebarStatus ?? getConfig().SidebarStatus,
  14. withoutAnimation: false,
  15. isClickHamburger: false
  16. },
  17. // 这里的layout用于监听容器拖拉后恢复对应的导航模式
  18. layout:
  19. storageLocal.getItem<StorageConfigs>("responsive-layout")?.layout ??
  20. getConfig().Layout,
  21. device: deviceDetection() ? "mobile" : "desktop"
  22. }),
  23. getters: {
  24. getSidebarStatus() {
  25. return this.sidebar.opened;
  26. },
  27. getDevice() {
  28. return this.device;
  29. }
  30. },
  31. actions: {
  32. TOGGLE_SIDEBAR(opened?: boolean, resize?: string) {
  33. const layout = storageLocal.getItem<StorageConfigs>("responsive-layout");
  34. if (opened && resize) {
  35. this.sidebar.withoutAnimation = true;
  36. this.sidebar.opened = true;
  37. layout.sidebarStatus = true;
  38. } else if (!opened && resize) {
  39. this.sidebar.withoutAnimation = true;
  40. this.sidebar.opened = false;
  41. layout.sidebarStatus = false;
  42. } else if (!opened && !resize) {
  43. this.sidebar.withoutAnimation = false;
  44. this.sidebar.opened = !this.sidebar.opened;
  45. this.sidebar.isClickHamburger = !this.sidebar.opened;
  46. layout.sidebarStatus = this.sidebar.opened;
  47. }
  48. storageLocal.setItem("responsive-layout", layout);
  49. },
  50. async toggleSideBar(opened?: boolean, resize?: string) {
  51. await this.TOGGLE_SIDEBAR(opened, resize);
  52. },
  53. toggleDevice(device: string) {
  54. this.device = device;
  55. },
  56. setLayout(layout) {
  57. this.layout = layout;
  58. }
  59. }
  60. });
  61. export function useAppStoreHook() {
  62. return useAppStore(store);
  63. }