use-global-store.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import type { GlobalState } from "./pinia.types";
  2. import { defineStore } from "pinia";
  3. import ls from "@/utils/store2";
  4. import { toRefs, reactive } from "vue";
  5. const useStore = defineStore("globalStore", () => {
  6. const state: GlobalState = reactive({
  7. globalLoading: false,
  8. routerLoading: false,
  9. requestLoading: false,
  10. fullScreenLoading: false,
  11. isPageSwitching: false,
  12. statusBarHeight: 0,
  13. mode: 'customer',
  14. theme: "light"
  15. });
  16. const setGlobalTheme = (payload: string) => {
  17. state.theme = payload;
  18. };
  19. const initMode = () => {
  20. const encryptedMode = ls.get('mode');
  21. state.mode = encryptedMode || 'customer';
  22. };
  23. const setMode = (payload: 'customer' | 'ib') => {
  24. ls.set('mode', payload)
  25. state.mode = payload;
  26. };
  27. const setBarHeight = (payload: number) => {
  28. state.statusBarHeight = payload;
  29. };
  30. const setGlobalLoading = (payload: boolean) => {
  31. state.globalLoading = payload;
  32. };
  33. const setRouterLoading = (payload: boolean) => {
  34. state.routerLoading = payload;
  35. };
  36. const setRequestLoading = (payload: boolean) => {
  37. state.requestLoading = payload;
  38. };
  39. const setFullScreenLoading = (payload: boolean) => {
  40. state.fullScreenLoading = payload;
  41. };
  42. initMode()
  43. return {
  44. ...toRefs(state),
  45. setGlobalLoading,
  46. setMode,
  47. setBarHeight,
  48. setGlobalTheme,
  49. setRouterLoading,
  50. setRequestLoading,
  51. setFullScreenLoading,
  52. };
  53. });
  54. export default useStore; // 导出