app.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import type { MainAppProps } from '#/main';
  2. import type { ProjectConfig, HeaderSetting, MenuSetting, TransitionSetting, MultiTabsSetting } from '/#/config';
  3. import type { BeforeMiniState } from '/#/store';
  4. import { defineStore } from 'pinia';
  5. import { store } from '/@/store';
  6. import { ThemeEnum } from '/@/enums/appEnum';
  7. import { APP_DARK_MODE_KEY_, PROJ_CFG_KEY } from '/@/enums/cacheEnum';
  8. import { Persistent } from '/@/utils/cache/persistent';
  9. import { darkMode } from '/@/settings/designSetting';
  10. import { resetRouter } from '/@/router';
  11. import { deepMerge } from '/@/utils';
  12. import { getHideLayoutTypes } from '/@/utils/env';
  13. import setting from '/@/settings/projectSetting';
  14. interface AppState {
  15. darkMode?: ThemeEnum;
  16. // Page loading status
  17. pageLoading: boolean;
  18. // project config
  19. projectConfig: ProjectConfig | null;
  20. // When the window shrinks, remember some states, and restore these states when the window is restored
  21. beforeMiniInfo: BeforeMiniState;
  22. // 页面跳转临时参数存储
  23. messageHrefParams: any;
  24. // 应用参数
  25. mainAppProps: MainAppProps;
  26. // 地图选择了的参数,存储于此供各页面使用
  27. simpleMapParams: any;
  28. widthScale: number;
  29. heightScale: number;
  30. }
  31. let timeId: TimeoutHandle;
  32. export const useAppStore = defineStore({
  33. id: 'app',
  34. state: (): AppState => ({
  35. darkMode: undefined,
  36. pageLoading: false,
  37. projectConfig: Persistent.getLocal(PROJ_CFG_KEY),
  38. beforeMiniInfo: {},
  39. messageHrefParams: {},
  40. mainAppProps: {},
  41. simpleMapParams: {},
  42. widthScale: 1,
  43. heightScale: 1,
  44. }),
  45. getters: {
  46. getPageLoading(): boolean {
  47. return this.pageLoading;
  48. },
  49. getDarkMode(): 'light' | 'dark' | string {
  50. // liaozhiyang---date:20250411---for:【QQYUN-11956】修复projectSetting中配置主题模式不生效
  51. const getSettingTheme = () => {
  52. const theme = setting.themeMode;
  53. if (theme) {
  54. if (theme == ThemeEnum.DARK) {
  55. // 为了index.html页面loading时是暗黑
  56. localStorage.setItem(APP_DARK_MODE_KEY_, theme);
  57. }
  58. return theme;
  59. }
  60. return '';
  61. };
  62. // liaozhiyang---date:20250411---for:【QQYUN-11956】修复projectSetting中配置主题模式不生效
  63. return this.darkMode || localStorage.getItem(APP_DARK_MODE_KEY_) || getSettingTheme() || darkMode;
  64. },
  65. getBeforeMiniInfo(): BeforeMiniState {
  66. return this.beforeMiniInfo;
  67. },
  68. getProjectConfig(): ProjectConfig {
  69. return this.projectConfig || ({} as ProjectConfig);
  70. },
  71. getHeaderSetting(): HeaderSetting {
  72. return this.getProjectConfig.headerSetting;
  73. },
  74. getMenuSetting(): MenuSetting {
  75. return this.getProjectConfig.menuSetting;
  76. },
  77. getTransitionSetting(): TransitionSetting {
  78. return this.getProjectConfig.transitionSetting;
  79. },
  80. getMultiTabsSetting(): MultiTabsSetting {
  81. return this.getProjectConfig.multiTabsSetting;
  82. },
  83. getMessageHrefParams(): any {
  84. return this.messageHrefParams;
  85. },
  86. getMainAppProps(): MainAppProps {
  87. return this.mainAppProps;
  88. },
  89. getSimpleMapParams(): any {
  90. return this.simpleMapParams;
  91. },
  92. getLayoutHideSider(): boolean {
  93. const hideLayoutTypes = getHideLayoutTypes();
  94. if (hideLayoutTypes.includes('sider')) {
  95. return true;
  96. }
  97. return !!this.mainAppProps.hideSider;
  98. },
  99. getLayoutHideHeader(): boolean {
  100. const hideLayoutTypes = getHideLayoutTypes();
  101. if (hideLayoutTypes.includes('header')) {
  102. return true;
  103. }
  104. return !!this.mainAppProps.hideHeader;
  105. },
  106. getLayoutHideMultiTabs(): boolean {
  107. const hideLayoutTypes = getHideLayoutTypes();
  108. if (hideLayoutTypes.includes('multi-tabs')) {
  109. return true;
  110. }
  111. return !!this.mainAppProps.hideMultiTabs;
  112. },
  113. getWidthScale(): number {
  114. return this.widthScale;
  115. },
  116. getHeightScale(): number {
  117. return this.heightScale;
  118. },
  119. },
  120. actions: {
  121. setPageLoading(loading: boolean): void {
  122. this.pageLoading = loading;
  123. },
  124. setDarkMode(mode: ThemeEnum): void {
  125. this.darkMode = mode;
  126. localStorage.setItem(APP_DARK_MODE_KEY_, mode);
  127. this.setProjectConfig({ themeMode: mode });
  128. },
  129. setBeforeMiniInfo(state: BeforeMiniState): void {
  130. this.beforeMiniInfo = state;
  131. },
  132. setProjectConfig(config: DeepPartial<ProjectConfig>): void {
  133. this.projectConfig = deepMerge(this.projectConfig || {}, config);
  134. // 代码逻辑说明: 【QQYUN-8922】设置导航栏模式没存本地,刷新就还原了
  135. Persistent.setLocal(PROJ_CFG_KEY, this.projectConfig, true);
  136. },
  137. async resetAllState() {
  138. resetRouter();
  139. Persistent.clearAll();
  140. },
  141. async setPageLoadingAction(loading: boolean): Promise<void> {
  142. if (loading) {
  143. clearTimeout(timeId);
  144. // Prevent flicker
  145. timeId = setTimeout(() => {
  146. this.setPageLoading(loading);
  147. }, 50);
  148. } else {
  149. this.setPageLoading(loading);
  150. clearTimeout(timeId);
  151. }
  152. },
  153. setMessageHrefParams(params: any): void {
  154. this.messageHrefParams = params;
  155. },
  156. // 设置主应用参数
  157. setMainAppProps(args: MainAppProps) {
  158. this.mainAppProps.hideHeader = args.hideHeader ?? false;
  159. this.mainAppProps.hideSider = args.hideSider ?? false;
  160. this.mainAppProps.hideMultiTabs = args.hideMultiTabs ?? false;
  161. },
  162. setSimpleMapParams(params: any): void {
  163. this.simpleMapParams = params;
  164. },
  165. setWidthScale(scale: number) {
  166. this.widthScale = scale;
  167. },
  168. setHeightScale(scale: number) {
  169. this.heightScale = scale;
  170. },
  171. },
  172. });
  173. // Need to be used outside the setup
  174. export function useAppStoreWithOut() {
  175. return useAppStore(store);
  176. }