app.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import type { ProjectConfig } from '/#/config';
  2. import type { BeforeMiniState } from '/#/store';
  3. import { defineStore } from 'pinia';
  4. import { store } from '/@/store';
  5. import { ThemeEnum } from '/@/enums/appEnum';
  6. import { APP_DARK_MODE_KEY_, PROJ_CFG_KEY } from '/@/enums/cacheEnum';
  7. import { Persistent } from '/@/utils/cache/persistent';
  8. import { darkMode } from '/@/settings/designSetting';
  9. import { resetRouter } from '/@/router';
  10. import { deepMerge } from '/@/utils';
  11. import { getConfigInfo } from '/@/api/sys/general';
  12. interface AppState {
  13. darkMode?: ThemeEnum;
  14. // Page loading status
  15. pageLoading: boolean;
  16. // project config
  17. projectConfig: ProjectConfig | null;
  18. // When the window shrinks, remember some states, and restore these states when the window is restored
  19. beforeMiniInfo: BeforeMiniState;
  20. appTitle: string;
  21. appLogo: string;
  22. }
  23. let timeId: TimeoutHandle;
  24. export const useAppStore = defineStore({
  25. id: 'app',
  26. state: (): AppState => ({
  27. darkMode: undefined,
  28. pageLoading: false,
  29. projectConfig: Persistent.getLocal(PROJ_CFG_KEY),
  30. beforeMiniInfo: {},
  31. appTitle: 'admin',
  32. appLogo: '',
  33. }),
  34. getters: {
  35. getPageLoading() {
  36. return this.pageLoading;
  37. },
  38. getAppTitle() {
  39. return this.appTitle;
  40. },
  41. getAppLogo() {
  42. return this.appLogo;
  43. },
  44. getDarkMode(): 'light' | 'dark' | string {
  45. return this.darkMode || localStorage.getItem(APP_DARK_MODE_KEY_) || darkMode;
  46. },
  47. getBeforeMiniInfo() {
  48. return this.beforeMiniInfo;
  49. },
  50. getProjectConfig(): ProjectConfig {
  51. return this.projectConfig || ({} as ProjectConfig);
  52. },
  53. getHeaderSetting() {
  54. return this.getProjectConfig.headerSetting;
  55. },
  56. getMenuSetting() {
  57. return this.getProjectConfig.menuSetting;
  58. },
  59. getTransitionSetting() {
  60. return this.getProjectConfig.transitionSetting;
  61. },
  62. getMultiTabsSetting() {
  63. return this.getProjectConfig.multiTabsSetting;
  64. },
  65. },
  66. actions: {
  67. setPageLoading(loading: boolean): void {
  68. this.pageLoading = loading;
  69. },
  70. setAppTitle(appTitle: string) {
  71. this.appTitle = appTitle;
  72. },
  73. setAppLogo(appLogo: string) {
  74. this.appLogo = appLogo;
  75. },
  76. setDarkMode(mode: ThemeEnum): void {
  77. this.darkMode = mode;
  78. localStorage.setItem(APP_DARK_MODE_KEY_, mode);
  79. },
  80. setBeforeMiniInfo(state: BeforeMiniState): void {
  81. this.beforeMiniInfo = state;
  82. },
  83. setProjectConfig(config: DeepPartial<ProjectConfig>): void {
  84. this.projectConfig = deepMerge(this.projectConfig || {}, config);
  85. Persistent.setLocal(PROJ_CFG_KEY, this.projectConfig);
  86. },
  87. async resetAllState() {
  88. resetRouter();
  89. Persistent.clearAll();
  90. },
  91. async setPageLoadingAction(loading: boolean): Promise<void> {
  92. if (loading) {
  93. clearTimeout(timeId);
  94. // Prevent flicker
  95. timeId = setTimeout(() => {
  96. this.setPageLoading(loading);
  97. }, 50);
  98. } else {
  99. this.setPageLoading(loading);
  100. clearTimeout(timeId);
  101. }
  102. },
  103. async setAppInfoAction() {
  104. const res = await getConfigInfo();
  105. const config = res.row.basic.list;
  106. this.setAppTitle(config[0].value);
  107. this.setAppLogo(config[1].value);
  108. },
  109. },
  110. });
  111. // Need to be used outside the setup
  112. export function useAppStoreWidthOut() {
  113. return useAppStore(store);
  114. }