App.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. const ua = window.navigator.userAgent.toLowerCase();
  38. const logoutRedirect =
  39. ua.includes("dingtalk") || ua.includes("dingtalkwork") ? "/" : "/login";
  40. window.location.href = logoutRedirect;
  41. };
  42. const resetIdleTimer = () => {
  43. clearIdleTimer();
  44. if (!getAccessToken() || isLoginPage()) {
  45. return;
  46. }
  47. idleTimer = window.setTimeout(() => {
  48. logoutByIdle();
  49. }, IDLE_TIMEOUT);
  50. };
  51. const handleUserActivity = () => {
  52. resetIdleTimer();
  53. };
  54. onMounted(() => {
  55. activityEvents.forEach((eventName) => {
  56. window.addEventListener(eventName, handleUserActivity, true);
  57. });
  58. watch(
  59. () => route.path,
  60. () => {
  61. resetIdleTimer();
  62. },
  63. { immediate: true },
  64. );
  65. });
  66. onBeforeUnmount(() => {
  67. clearIdleTimer();
  68. activityEvents.forEach((eventName) => {
  69. window.removeEventListener(eventName, handleUserActivity, true);
  70. });
  71. });
  72. </script>
  73. <template>
  74. <motion.div
  75. class="overflow-x-hidden font-sans app-shell"
  76. :key="$route.fullPath"
  77. :initial="{ opacity: 0 }"
  78. :animate="{ opacity: 1, transition: { duration: 0.5, ease: 'easeOut' } }"
  79. >
  80. <router-view></router-view>
  81. </motion.div>
  82. </template>
  83. <style scoped>
  84. /* 修改垂直滚动条 */
  85. ::-webkit-scrollbar {
  86. width: 5px; /* 修改宽度 */
  87. }
  88. ::-webkit-scrollbar-thumb {
  89. border-radius: 5px;
  90. }
  91. /* 修改滚动条轨道背景色 */
  92. ::-webkit-scrollbar-track {
  93. background-color: transparent;
  94. }
  95. /* 修改滚动条滑块颜色 */
  96. ::-webkit-scrollbar-thumb {
  97. background-color: #0a5f73;
  98. }
  99. /* 修改滚动条滑块悬停时的颜色 */
  100. ::-webkit-scrollbar-thumb:hover {
  101. background-color: #30459c;
  102. }
  103. </style>