useLockPage.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { computed, onUnmounted, unref, watchEffect } from 'vue';
  2. import { useThrottleFn } from '@vueuse/core';
  3. import { useAppStore } from '@jeesite/core/store/modules/app';
  4. import { useLockStore } from '@jeesite/core/store/modules/lock';
  5. import { useUserStore } from '@jeesite/core/store/modules/case';
  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. if (userStore.getSessionTimeout) {
  20. clear();
  21. return;
  22. }
  23. const lockTime = appStore.getProjectConfig.lockTime;
  24. if (!lockTime || lockTime < 1 || lockTime > 99999) {
  25. clear();
  26. return;
  27. }
  28. clear();
  29. timeId = setTimeout(
  30. () => {
  31. lockPage();
  32. },
  33. lockTime * 60 * 1000,
  34. );
  35. }
  36. function lockPage(): void {
  37. lockStore.setLockInfo({
  38. isLock: true,
  39. pwd: undefined,
  40. });
  41. }
  42. watchEffect((onClean) => {
  43. // if (userStore.getToken) {
  44. if (!userStore.getSessionTimeout) {
  45. resetCalcLockTimeout();
  46. } else {
  47. clear();
  48. }
  49. onClean(() => {
  50. clear();
  51. });
  52. });
  53. onUnmounted(() => {
  54. clear();
  55. });
  56. const keyupFn = useThrottleFn(resetCalcLockTimeout, 2000);
  57. return computed(() => {
  58. if (unref(getLockTime)) {
  59. return { onKeyup: keyupFn, onMousemove: keyupFn };
  60. } else {
  61. clear();
  62. return {};
  63. }
  64. });
  65. }