use-global-store.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. applyStatus: false
  17. });
  18. const setGlobalTheme = (payload: string) => {
  19. state.theme = payload;
  20. };
  21. const initMode = () => {
  22. const encryptedMode = ls.get('mode');
  23. state.mode = encryptedMode || 'customer';
  24. };
  25. const setMode = (payload: 'customer' | 'ib') => {
  26. ls.set('mode', payload)
  27. state.mode = payload;
  28. };
  29. const setBarHeight = (payload: number) => {
  30. state.statusBarHeight = payload;
  31. };
  32. const setGlobalLoading = (payload: boolean) => {
  33. state.globalLoading = payload;
  34. };
  35. const setRouterLoading = (payload: boolean) => {
  36. state.routerLoading = payload;
  37. };
  38. const setRequestLoading = (payload: boolean) => {
  39. state.requestLoading = payload;
  40. };
  41. const setFullScreenLoading = (payload: boolean) => {
  42. state.fullScreenLoading = payload;
  43. };
  44. const setApplyStatus = (payload: boolean) => {
  45. state.applyStatus = payload;
  46. };
  47. initMode()
  48. return {
  49. ...toRefs(state),
  50. setGlobalLoading,
  51. setMode,
  52. setBarHeight,
  53. setGlobalTheme,
  54. setRouterLoading,
  55. setRequestLoading,
  56. setFullScreenLoading,
  57. setApplyStatus,
  58. };
  59. });
  60. export default useStore; // 导出