vben
2020-11-25 26b6109ca08a28c37355474bf8593f2e2b741ef6
提交 | 用户 | age
46e087 1 import { computed, onUnmounted, watchEffect } from 'vue';
2f6253 2 import { useThrottle } from '/@/hooks/core/useThrottle';
3
4 import { appStore } from '/@/store/modules/app';
5 import { userStore } from '/@/store/modules/user';
70fba7 6
2f6253 7 export function useLockPage() {
26b610 8   let timeId: TimeoutHandle;
2f6253 9
46e087 10   function clear(): void {
2f6253 11     window.clearTimeout(timeId);
12   }
46e087 13
V 14   function resetCalcLockTimeout(): void {
2f6253 15     // not login
16     if (!userStore.getTokenState) {
17       clear();
18       return;
19     }
20     const lockTime = appStore.getProjectConfig.lockTime;
21     if (!lockTime || lockTime < 1) {
22       clear();
23       return;
24     }
25     clear();
26
27     timeId = setTimeout(() => {
28       lockPage();
29     }, lockTime * 60 * 1000);
30   }
31
46e087 32   function lockPage(): void {
2f6253 33     appStore.commitLockInfoState({
34       isLock: true,
35       pwd: undefined,
36     });
37   }
38
46e087 39   watchEffect((onClean) => {
2f6253 40     if (userStore.getTokenState) {
41       resetCalcLockTimeout();
42     } else {
43       clear();
44     }
46e087 45     onClean(() => {
V 46       clear();
47     });
2f6253 48   });
46e087 49
2f6253 50   onUnmounted(() => {
51     clear();
52   });
46e087 53
2f6253 54   const [keyupFn] = useThrottle(resetCalcLockTimeout, 2000);
55
46e087 56   return computed(() => {
V 57     const openLockPage = appStore.getProjectConfig.lockTime;
58     if (openLockPage) {
59       return { onKeyup: keyupFn, onMousemove: keyupFn };
60     } else {
61       clear();
62       return {};
63     }
64   });
2f6253 65 }