<template>
	<!-- 这个组件不需要渲染任何内容 -->
</template>

<script setup>
	import { onMounted, onUnmounted } from "vue";
	import { useRouter } from "vue-router";
	import { useUserStore } from "@/store";
	import { removeToken } from "@/utils";

	const router = useRouter();
	const userStore = useUserStore();

	// 设置无操作超时时间(毫秒)
	let TIMEOUT = 3600 * 1000; // 从配置文件读取
	let timer;

	const resetTimer = () => {
		if (timer) clearTimeout(timer);
		timer = setTimeout(logout, TIMEOUT);
	};

	const logout = () => {
		userStore.logout(); // 调用store中的登出方法
		removeToken();
		router.push("/login"); // 跳转到登录页
	};

	onMounted(() => {
		window.addEventListener("mousemove", resetTimer);
		window.addEventListener("keypress", resetTimer);
		// 初始化定时器
		resetTimer();
	});

	onUnmounted(() => {
		window.removeEventListener("mousemove", resetTimer);
		window.removeEventListener("keypress", resetTimer);
		if (timer) clearTimeout(timer);
	});
</script>
