app.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { ProjectConfig } from '/#/config';
  2. import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
  3. import store from '/@/store';
  4. import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';
  5. import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
  6. import { Persistent } from '/@/utils/cache/persistent';
  7. import { deepMerge } from '/@/utils';
  8. import { resetRouter } from '/@/router';
  9. export interface LockInfo {
  10. pwd: string | undefined;
  11. isLock: boolean;
  12. }
  13. let timeId: TimeoutHandle;
  14. const NAME = 'app';
  15. hotModuleUnregisterModule(NAME);
  16. @Module({ dynamic: true, namespaced: true, store, name: NAME })
  17. export default class App extends VuexModule {
  18. // Page loading status
  19. private pageLoadingState = false;
  20. // project config
  21. private projectConfigState: ProjectConfig | null = Persistent.getLocal(PROJ_CFG_KEY);
  22. // set main overflow hidden
  23. private lockMainScrollState = false;
  24. get getPageLoading() {
  25. return this.pageLoadingState;
  26. }
  27. get getLockMainScrollState() {
  28. return this.lockMainScrollState;
  29. }
  30. get getProjectConfig(): ProjectConfig {
  31. return this.projectConfigState || ({} as ProjectConfig);
  32. }
  33. @Mutation
  34. commitPageLoadingState(loading: boolean): void {
  35. this.pageLoadingState = loading;
  36. }
  37. @Mutation
  38. commitLockMainScrollState(lock: boolean): void {
  39. this.lockMainScrollState = lock;
  40. }
  41. @Mutation
  42. commitProjectConfigState(proCfg: DeepPartial<ProjectConfig>): void {
  43. this.projectConfigState = deepMerge(this.projectConfigState || {}, proCfg);
  44. Persistent.setLocal(PROJ_CFG_KEY, this.projectConfigState);
  45. }
  46. @Action
  47. async resumeAllState() {
  48. resetRouter();
  49. Persistent.clearAll();
  50. }
  51. @Action
  52. public async setPageLoadingAction(loading: boolean): Promise<void> {
  53. if (loading) {
  54. clearTimeout(timeId);
  55. // Prevent flicker
  56. timeId = setTimeout(() => {
  57. this.commitPageLoadingState(loading);
  58. }, 50);
  59. } else {
  60. this.commitPageLoadingState(loading);
  61. clearTimeout(timeId);
  62. }
  63. }
  64. }
  65. export const appStore = getModule<App>(App);