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

91 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-10-30 21:32:05 +08:00
import type { ProjectConfig } from '/@/types/config';
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-25 20:17:08 +08:00
import { setLocal, getLocal, clearSession, clearLocal } 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';
import { permissionStore } from './permission';
import { tabStore } from './tab';
2020-09-28 20:19:10 +08:00
import { userStore } from './user';
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 })
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
2020-09-28 20:19:10 +08:00
private projectConfigState: ProjectConfig | null = getLocal(PROJ_CFG_KEY);
2020-10-30 21:32:05 +08:00
// set main overflow hidden
2020-09-28 20:19:10 +08:00
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);
}
2020-11-23 00:35:15 +08:00
@Action
async resumeAllState() {
resetRouter();
clearSession();
clearLocal();
permissionStore.commitResetState();
tabStore.commitResetState();
userStore.commitResetState();
}
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);