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.

73 lines
1.8 KiB

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