<template>
<div id="app">
<router-view />
</div>
</template>
<script>
import { mapActions } from "vuex";
export default {
name: "App",
data() {
return {
lTime: new Date().getTime(),
cTime: new Date().getTime(),
tOut: 60 * 10 * 1000,
timer: null,
};
},
methods: {
tTime() {
this.cTime = new Date().getTime();
if (this.tOut && this.cTime - this.lTime > this.tOut) {
if (localStorage.getItem("token")) {
this.handleLogOut().then(() => {
this.$router.push({
name: "login",
});
});
this.$Message.error({
content: "登录超时,请重新登录",
duration: 3,
});
} else {
this.lTime = new Date().getTime();
}
}
},
...mapActions(["handleLogOut"]),
},
created() {
window.addEventListener(
"click",
() => {
this.lTime = new Date().getTime();
},
true
);
},
mounted() {
if (this.timer) {
clearInterval(this.timer);
}
this.timer = setInterval(this.tTime, 1000);
},
beforeDestroy() {
clearInterval(this.timer);
window.removeEventListener("click", () => {}, true);
},
};
</script>