You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1 KiB

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