lock.ts
1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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';
import { setLocal, getLocal, removeLocal } from '/@/utils/cache/persistent';
import { userStore } from './user';
export interface LockInfo {
pwd: string | undefined;
isLock: boolean;
}
const NAME = 'lock';
hotModuleUnregisterModule(NAME);
@Module({ dynamic: true, namespaced: true, store, name: NAME })
class Lock extends VuexModule {
// lock info
private lockInfoState: LockInfo | null = getLocal(LOCK_INFO_KEY);
get getLockInfo(): LockInfo {
return this.lockInfoState || ({} as LockInfo);
}
@Mutation
commitLockInfoState(info: LockInfo): void {
this.lockInfoState = Object.assign({}, this.lockInfoState, info);
setLocal(LOCK_INFO_KEY, this.lockInfoState);
}
@Mutation
resetLockInfo(): void {
removeLocal(LOCK_INFO_KEY);
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);