原理 - 每进行一次操作记录一次时间,定时器触发一定时间进行检查记录的时间与当前时间的间隔时间,判断间隔时间进行拦截到登录页
直接建立js文件进行编写逻辑,在main.js进行引入即可
webapp - 一定时间拦截到登录页
import router from '@/router'
import store from '@/store'
var lastTime = new Date().getTime()
var currentTime = new Date().getTime()
var timeOut = 5 * 60 * 1000 // 设置超时时间
window.onload = function() { //页面加载进行添加监听事件,进行操作修改操作时间-lastTime
window.document.onmousedown = function() {
localStorage.setItem('lastTime', new Date().getTime())
}
window.document.tap = function() {
localStorage.setItem('lastTime', new Date().getTime())
}
window.document.touchstart = function() {
localStorage.setItem('lastTime', new Date().getTime())
}
window.document.touchmove = function() {
localStorage.setItem('lastTime', new Date().getTime())
}
window.document.touchend = function() {
localStorage.setItem('lastTime', new Date().getTime())
}
window.document.touchcancel = function() {
localStorage.setItem('lastTime', new Date().getTime())
}
}
//定时器触发检查时间间隔
function checkTimeout() {
currentTime = new Date().getTime() // 更新当前时间
lastTime = localStorage.getItem('lastTime')
if (currentTime - lastTime > timeOut) { // 判断是否超时 ,在这里面写退出的一些操作
if(router.history.current.fullPath !== '/login'){ //登录页长时间无操作不做处理
localStorage.removeItem('lastTime');
logout();
}
}
}
//退出方法
async function logout() {
await store.dispatch('user/goLoginOut')
router.push(`/login`)
}
//定时器检查时间间隔
window.setInterval(checkTimeout, 30000)