lock.ts
944 Bytes
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
import type { LockInfo } from '/#/store';
import { defineStore } from 'pinia';
import { LOCK_INFO_KEY } from '/@/enums/cacheEnum';
import { Persistent } from '/@/utils/cache/persistent';
interface LockState {
lockInfo: Nullable<LockInfo>;
}
export const useLockStore = defineStore({
id: 'app-lock',
state: (): LockState => ({
lockInfo: Persistent.getLocal(LOCK_INFO_KEY),
}),
getters: {
getLockInfo(): Nullable<LockInfo> {
return this.lockInfo;
},
},
actions: {
setLockInfo(info: LockInfo) {
this.lockInfo = Object.assign({}, this.lockInfo, info);
Persistent.setLocal(LOCK_INFO_KEY, this.lockInfo, true);
},
resetLockInfo() {
Persistent.removeLocal(LOCK_INFO_KEY, true);
this.lockInfo = null;
},
// Unlock
async unLock(password?: string) {
if (this.lockInfo?.pwd === password) {
this.resetLockInfo();
return true;
}
},
},
});