vben
authored
5 years ago
1
2
3
4
5
6
import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
import store from '/@/store';
import { LOCK_INFO_KEY } from '/@/enums/cacheEnum';
import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
Vben
authored
4 years ago
7
import { Persistent } from '/@/utils/cache/persistent';
vben
authored
5 years ago
8
9
10
11
12
13
14
15
import { userStore } from './user';
export interface LockInfo {
pwd: string | undefined;
isLock: boolean;
}
Vben
authored
4 years ago
16
const NAME = 'app-lock';
vben
authored
5 years ago
17
18
19
20
hotModuleUnregisterModule(NAME);
@Module({ dynamic: true, namespaced: true, store, name: NAME })
class Lock extends VuexModule {
// lock info
Vben
authored
4 years ago
21
private lockInfoState: LockInfo | null = Persistent.getLocal(LOCK_INFO_KEY);
vben
authored
5 years ago
22
23
24
25
26
27
28
29
get getLockInfo(): LockInfo {
return this.lockInfoState || ({} as LockInfo);
}
@Mutation
commitLockInfoState(info: LockInfo): void {
this.lockInfoState = Object.assign({}, this.lockInfoState, info);
Vben
authored
4 years ago
30
Persistent.setLocal(LOCK_INFO_KEY, this.lockInfoState);
vben
authored
5 years ago
31
32
33
34
}
@Mutation
resetLockInfo(): void {
Vben
authored
4 years ago
35
Persistent.removeLocal(LOCK_INFO_KEY);
vben
authored
5 years ago
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
this.lockInfoState = null;
}
/**
* @description: unlock page
*/
@Action
public async unLockAction({ password }: { password: string }) {
const tryLogin = async () => {
try {
const username = userStore.getUserInfoState.username;
const res = await userStore.login({ username, password, goHome: false, mode: 'none' });
if (res) {
this.resetLockInfo();
}
return res;
} catch (error) {
return false;
}
};
if (this.getLockInfo?.pwd === password) {
this.resetLockInfo();
return true;
}
return await tryLogin();
}
}
export const lockStore = getModule<Lock>(Lock);