vue-vben-admin/src/store/modules/app.ts

95 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-02-26 20:09:24 +08:00
import type { ProjectConfig } from '/#/config';
import type { BeforeMiniState } from '../types';
2020-10-30 21:32:05 +08:00
2020-09-28 20:19:10 +08:00
import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
2020-10-30 21:32:05 +08:00
import store from '/@/store';
2020-09-28 20:19:10 +08:00
import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';
2020-09-28 20:19:10 +08:00
2020-10-30 21:32:05 +08:00
import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
2021-02-27 19:55:30 +08:00
import { Persistent } from '/@/utils/cache/persistent';
2020-09-28 20:19:10 +08:00
import { deepMerge } from '/@/utils';
2020-10-30 21:32:05 +08:00
2020-11-23 00:35:15 +08:00
import { resetRouter } from '/@/router';
2020-09-28 20:19:10 +08:00
export interface LockInfo {
pwd: string | undefined;
isLock: boolean;
}
2020-11-25 21:26:10 +08:00
let timeId: TimeoutHandle;
2020-09-28 20:19:10 +08:00
const NAME = 'app';
hotModuleUnregisterModule(NAME);
@Module({ dynamic: true, namespaced: true, store, name: NAME })
2021-02-26 20:09:24 +08:00
export default class App extends VuexModule {
2020-10-30 21:32:05 +08:00
// Page loading status
2020-09-28 20:19:10 +08:00
private pageLoadingState = false;
2020-10-30 21:32:05 +08:00
// project config
2021-02-27 19:55:30 +08:00
private projectConfigState: ProjectConfig | null = Persistent.getLocal(PROJ_CFG_KEY);
2020-09-28 20:19:10 +08:00
2020-10-30 21:32:05 +08:00
// set main overflow hidden
2020-09-28 20:19:10 +08:00
private lockMainScrollState = false;
// When the window shrinks, remember some states, and restore these states when the window is restored
private beforeMiniState: BeforeMiniState = {};
2020-09-28 20:19:10 +08:00
get getPageLoading() {
return this.pageLoadingState;
}
get getBeforeMiniState() {
return this.beforeMiniState;
}
2020-09-28 20:19:10 +08:00
get getLockMainScrollState() {
return this.lockMainScrollState;
}
get getProjectConfig(): ProjectConfig {
return this.projectConfigState || ({} as ProjectConfig);
}
@Mutation
commitPageLoadingState(loading: boolean): void {
this.pageLoadingState = loading;
}
@Mutation
commitBeforeMiniState(state: BeforeMiniState): void {
this.beforeMiniState = state;
}
2020-09-28 20:19:10 +08:00
@Mutation
commitLockMainScrollState(lock: boolean): void {
this.lockMainScrollState = lock;
}
@Mutation
commitProjectConfigState(proCfg: DeepPartial<ProjectConfig>): void {
this.projectConfigState = deepMerge(this.projectConfigState || {}, proCfg);
2021-02-27 19:55:30 +08:00
Persistent.setLocal(PROJ_CFG_KEY, this.projectConfigState);
2020-09-28 20:19:10 +08:00
}
2020-11-23 00:35:15 +08:00
@Action
async resumeAllState() {
resetRouter();
2021-02-27 19:55:30 +08:00
Persistent.clearAll();
2020-11-23 00:35:15 +08:00
}
2020-09-28 20:19:10 +08:00
@Action
public async setPageLoadingAction(loading: boolean): Promise<void> {
if (loading) {
clearTimeout(timeId);
2020-11-21 22:47:10 +08:00
// Prevent flicker
2020-09-28 20:19:10 +08:00
timeId = setTimeout(() => {
this.commitPageLoadingState(loading);
2020-12-04 21:25:33 +08:00
}, 50);
2020-09-28 20:19:10 +08:00
} else {
this.commitPageLoadingState(loading);
clearTimeout(timeId);
}
}
}
export const appStore = getModule<App>(App);