Vben
authored
|
1
|
import type { LockInfo } from '/#/store';
|
Vben
authored
|
2
|
|
Vben
authored
|
3
|
import { defineStore } from 'pinia';
|
vben
authored
|
4
5
|
import { LOCK_INFO_KEY } from '/@/enums/cacheEnum';
|
Vben
authored
|
6
|
import { Persistent } from '/@/utils/cache/persistent';
|
Vben
authored
|
7
|
import { useUserStore } from './user';
|
vben
authored
|
8
|
|
Vben
authored
|
9
10
11
|
interface LockState {
lockInfo: Nullable<LockInfo>;
}
|
vben
authored
|
12
|
|
Vben
authored
|
13
14
15
16
17
18
|
export const useLockStore = defineStore({
id: 'app-lock',
state: (): LockState => ({
lockInfo: Persistent.getLocal(LOCK_INFO_KEY),
}),
getters: {
|
vben
authored
|
19
|
getLockInfo(): Nullable<LockInfo> {
|
Vben
authored
|
20
21
22
23
24
25
|
return this.lockInfo;
},
},
actions: {
setLockInfo(info: LockInfo) {
this.lockInfo = Object.assign({}, this.lockInfo, info);
|
|
26
|
Persistent.setLocal(LOCK_INFO_KEY, this.lockInfo, true);
|
Vben
authored
|
27
28
|
},
resetLockInfo() {
|
|
29
|
Persistent.removeLocal(LOCK_INFO_KEY, true);
|
Vben
authored
|
30
31
32
33
34
35
36
37
|
this.lockInfo = null;
},
// Unlock
async unLock(password?: string) {
const userStore = useUserStore();
if (this.lockInfo?.pwd === password) {
this.resetLockInfo();
return true;
|
vben
authored
|
38
|
}
|
Vben
authored
|
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
const tryLogin = async () => {
try {
const username = userStore.getUserInfo?.username;
const res = await userStore.login({
username,
password: password!,
goHome: false,
mode: 'none',
});
if (res) {
this.resetLockInfo();
}
return res;
} catch (error) {
return false;
}
};
return await tryLogin();
},
},
});
|