useLockPage.ts 1.2 KB
Newer Older
陈文彬 已提交
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
import { onUnmounted, watchEffect } from 'vue';
import { useThrottle } from '/@/hooks/core/useThrottle';

import { appStore } from '/@/store/modules/app';
import { userStore } from '/@/store/modules/user';
export function useLockPage() {
  let timeId: ReturnType<typeof setTimeout>;

  function clear() {
    window.clearTimeout(timeId);
  }
  function resetCalcLockTimeout() {
    // not login
    if (!userStore.getTokenState) {
      clear();
      return;
    }
    const lockTime = appStore.getProjectConfig.lockTime;
    if (!lockTime || lockTime < 1) {
      clear();
      return;
    }
    clear();

    timeId = setTimeout(() => {
      lockPage();
    }, lockTime * 60 * 1000);
  }

  function lockPage() {
    appStore.commitLockInfoState({
      isLock: true,
      pwd: undefined,
    });
  }

  watchEffect(() => {
    if (userStore.getTokenState) {
      resetCalcLockTimeout();
    } else {
      clear();
    }
  });
  onUnmounted(() => {
    clear();
  });
  const [keyupFn] = useThrottle(resetCalcLockTimeout, 2000);

  return {
    registerGlobOnKeyup: keyupFn,
    registerGlobOnMouseMove: keyupFn,
    on: {
      onKeyup: keyupFn,
      onMousemove: keyupFn,
    },
  };
}