app.ts 2.3 KB
Newer Older
V
vben 已提交
1 2
import type { ProjectConfig } from '/@/types/config';

陈文彬 已提交
3
import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
V
vben 已提交
4
import store from '/@/store';
陈文彬 已提交
5

V
vben 已提交
6
import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';
陈文彬 已提交
7

V
vben 已提交
8
import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
V
Vben 已提交
9
import { setLocal, getLocal, clearSession, clearLocal } from '/@/utils/cache/persistent';
陈文彬 已提交
10
import { deepMerge } from '/@/utils';
V
vben 已提交
11

V
vben 已提交
12 13 14 15
import { resetRouter } from '/@/router';
import { permissionStore } from './permission';
import { tabStore } from './tab';

陈文彬 已提交
16 17 18 19 20 21 22
import { userStore } from './user';

export interface LockInfo {
  pwd: string | undefined;
  isLock: boolean;
}

V
vben 已提交
23
let timeId: TimeoutHandle;
陈文彬 已提交
24 25 26 27
const NAME = 'app';
hotModuleUnregisterModule(NAME);
@Module({ dynamic: true, namespaced: true, store, name: NAME })
class App extends VuexModule {
V
vben 已提交
28
  // Page loading status
陈文彬 已提交
29 30
  private pageLoadingState = false;

V
vben 已提交
31
  // project config
陈文彬 已提交
32 33
  private projectConfigState: ProjectConfig | null = getLocal(PROJ_CFG_KEY);

V
vben 已提交
34
  // set main overflow hidden
陈文彬 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  private lockMainScrollState = false;

  get getPageLoading() {
    return this.pageLoadingState;
  }

  get getLockMainScrollState() {
    return this.lockMainScrollState;
  }

  get getProjectConfig(): ProjectConfig {
    return this.projectConfigState || ({} as ProjectConfig);
  }

  @Mutation
  commitPageLoadingState(loading: boolean): void {
    this.pageLoadingState = loading;
  }

  @Mutation
  commitLockMainScrollState(lock: boolean): void {
    this.lockMainScrollState = lock;
  }

  @Mutation
  commitProjectConfigState(proCfg: DeepPartial<ProjectConfig>): void {
    this.projectConfigState = deepMerge(this.projectConfigState || {}, proCfg);
    setLocal(PROJ_CFG_KEY, this.projectConfigState);
  }

V
vben 已提交
65 66 67 68 69 70 71 72 73 74 75
  @Action
  async resumeAllState() {
    resetRouter();
    clearSession();
    clearLocal();

    permissionStore.commitResetState();
    tabStore.commitResetState();
    userStore.commitResetState();
  }

陈文彬 已提交
76 77 78 79
  @Action
  public async setPageLoadingAction(loading: boolean): Promise<void> {
    if (loading) {
      clearTimeout(timeId);
V
vben 已提交
80
      // Prevent flicker
陈文彬 已提交
81 82
      timeId = setTimeout(() => {
        this.commitPageLoadingState(loading);
V
vben 已提交
83
      }, 50);
陈文彬 已提交
84 85 86 87 88 89 90
    } else {
      this.commitPageLoadingState(loading);
      clearTimeout(timeId);
    }
  }
}
export const appStore = getModule<App>(App);