useLockPage.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { computed, onUnmounted, unref, watchEffect } from 'vue';
  2. import { useThrottle } from '/@/hooks/core/useThrottle';
  3. import { useAppStore } from '/@/store/modules/app';
  4. import { useLockStore } from '/@/store/modules/lock';
  5. import { useUserStore } from '/@/store/modules/user';
  6. import { useRootSetting } from '../setting/useRootSetting';
  7. export function useLockPage() {
  8. const { getLockTime } = useRootSetting();
  9. const lockStore = useLockStore();
  10. const userStore = useUserStore();
  11. const appStore = useAppStore();
  12. let timeId: TimeoutHandle;
  13. function clear(): void {
  14. window.clearTimeout(timeId);
  15. }
  16. function resetCalcLockTimeout(): void {
  17. // not login
  18. if (!userStore.getToken) {
  19. clear();
  20. return;
  21. }
  22. const lockTime = appStore.getProjectConfig.lockTime;
  23. if (!lockTime || lockTime < 1) {
  24. clear();
  25. return;
  26. }
  27. clear();
  28. timeId = setTimeout(() => {
  29. lockPage();
  30. }, lockTime * 60 * 1000);
  31. }
  32. function lockPage(): void {
  33. lockStore.setLockInfo({
  34. isLock: true,
  35. pwd: undefined,
  36. });
  37. }
  38. watchEffect((onClean) => {
  39. if (userStore.getToken) {
  40. resetCalcLockTimeout();
  41. } else {
  42. clear();
  43. }
  44. onClean(() => {
  45. clear();
  46. });
  47. });
  48. onUnmounted(() => {
  49. clear();
  50. });
  51. const [keyupFn] = useThrottle(resetCalcLockTimeout, 2000);
  52. return computed(() => {
  53. if (unref(getLockTime)) {
  54. return { onKeyup: keyupFn, onMousemove: keyupFn };
  55. } else {
  56. clear();
  57. return {};
  58. }
  59. });
  60. }