useLockPage.ts 1.7 KB

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