flat: 添加30分钟退出功能

This commit is contained in:
Apcallover
2024-05-10 16:45:20 +08:00
parent 5f1c2f1a7d
commit c839003715
3 changed files with 37 additions and 2 deletions

30
src/util/backTime.js Normal file
View File

@@ -0,0 +1,30 @@
const MAX_IDLE_TIME = 30 * 60 * 1000;
let lastActionTime = new Date();
import store from "@/store";
import router from "@/router/router";
function updateLastActionTime() {
lastActionTime = new Date();
}
// 定义一个计时器
let timeoutId = setTimeout(() => {
console.log('用户已经很长时间没有操作了!');
backLogOut()
}, MAX_IDLE_TIME);
// 监听页面上任何一个元素的点击事件
document.addEventListener('click', () => {
updateLastActionTime();
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
console.log('用户已经很长时间没有操作了!');
backLogOut()
}, MAX_IDLE_TIME);
});
function backLogOut() {
store.dispatch("LogOut").then(() => {
router.push({path: "/login"});
});
}