App.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script setup lang="ts">
  2. import { onBeforeUnmount, onMounted, watch } from "vue";
  3. import { useRoute } from "vue-router";
  4. import { motion } from "motion-v";
  5. import { deleteUserCache } from "@hooks/useCache";
  6. import { manualLogoutKey, reloginCancelKey } from "@/config/axios/service";
  7. import { removeToken, getAccessToken } from "@/utils/auth";
  8. import { useUserStore } from "@/stores/useUserStore";
  9. const route = useRoute();
  10. const userStore = useUserStore();
  11. const IDLE_TIMEOUT = 30 * 60 * 1000;
  12. const activityEvents = [
  13. "click",
  14. "mousemove",
  15. "keydown",
  16. "scroll",
  17. "touchstart",
  18. ];
  19. let idleTimer: number | null = null;
  20. const isLoginPage = () => route.path === "/login";
  21. const clearIdleTimer = () => {
  22. if (idleTimer) {
  23. window.clearTimeout(idleTimer);
  24. idleTimer = null;
  25. }
  26. };
  27. const logoutByIdle = () => {
  28. clearIdleTimer();
  29. if (!getAccessToken() || isLoginPage()) {
  30. return;
  31. }
  32. deleteUserCache();
  33. userStore.resetState();
  34. sessionStorage.setItem(manualLogoutKey, "true");
  35. sessionStorage.removeItem(reloginCancelKey);
  36. removeToken();
  37. window.location.href = "/login";
  38. };
  39. const resetIdleTimer = () => {
  40. clearIdleTimer();
  41. if (!getAccessToken() || isLoginPage()) {
  42. return;
  43. }
  44. idleTimer = window.setTimeout(() => {
  45. logoutByIdle();
  46. }, IDLE_TIMEOUT);
  47. };
  48. const handleUserActivity = () => {
  49. resetIdleTimer();
  50. };
  51. onMounted(() => {
  52. activityEvents.forEach((eventName) => {
  53. window.addEventListener(eventName, handleUserActivity, true);
  54. });
  55. watch(
  56. () => route.path,
  57. () => {
  58. resetIdleTimer();
  59. },
  60. { immediate: true },
  61. );
  62. });
  63. onBeforeUnmount(() => {
  64. clearIdleTimer();
  65. activityEvents.forEach((eventName) => {
  66. window.removeEventListener(eventName, handleUserActivity, true);
  67. });
  68. });
  69. </script>
  70. <template>
  71. <motion.div
  72. class="overflow-x-hidden font-sans"
  73. :key="$route.fullPath"
  74. :initial="{ opacity: 0 }"
  75. :animate="{ opacity: 1, transition: { duration: 0.5, ease: 'easeOut' } }"
  76. >
  77. <router-view></router-view>
  78. </motion.div>
  79. </template>
  80. <style scoped>
  81. /* 修改垂直滚动条 */
  82. ::-webkit-scrollbar {
  83. width: 5px; /* 修改宽度 */
  84. }
  85. ::-webkit-scrollbar-thumb {
  86. border-radius: 5px;
  87. }
  88. /* 修改滚动条轨道背景色 */
  89. ::-webkit-scrollbar-track {
  90. background-color: transparent;
  91. }
  92. /* 修改滚动条滑块颜色 */
  93. ::-webkit-scrollbar-thumb {
  94. background-color: #0a5f73;
  95. }
  96. /* 修改滚动条滑块悬停时的颜色 */
  97. ::-webkit-scrollbar-thumb:hover {
  98. background-color: #30459c;
  99. }
  100. </style>