app.ts 2.3 KB

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