首先编写获取设置好的超时时间的方法
//查询超时时间设置 selectTime() { let that = this; if (localStorage.getItem("access-token")) { let params = {}; API.SetTimeFindOne(JSON.stringify(params)).then(res => { if (res.returnCode != "200") { layer.alert(res.msg, { icon: 2, //1 √ 2 × shade: 0.5 }); return; } if (res.returnData.hasOwnProperty('data')) { that.timeOut = parseInt(res.returnData.data.minutes) * 60 * 1000; } }); }else { this.$router.push({ name: "login" }); } }
created时候获取当前时间作为lasttime,并查询超时时间设置
created() { this.lastTime = new Date().getTime(); this.selectTime();
}
鼠标移动事件放在最顶级div @mousemove="isTimeOut"
isTimeOut() { this.currentTime = new Date().getTime(); // 记录这次点击的时间 if (this.currentTime - this.lastTime > this.timeOut) { // 判断上次最后一次点击的时间和这次点击的时间间隔是否大于30分钟 if (localStorage.getItem("access-token")) { // 如果是登录状态 localStorage.clear(); this.$notify({ title: "提示", message: "超时未操作,系统自动退出!" }); if (window.top != null && window.top.document.URL != document.URL) { window.top.location.href = `http://${window.location.host}/login` } else { this.$router.push({ name: "login" }); } } else { this.lastTime = new Date().getTime(); this.$router.push({ name: "login" }); } } else { this.lastTime = new Date().getTime(); // 如果在30分钟内点击,则把这次点击的时间记录覆盖掉之前存的最后一次点击的时间 } },