Skip to content

Instantly share code, notes, and snippets.

@Alfxjx
Created February 24, 2021 09:01
Show Gist options
  • Save Alfxjx/6135832e3d8c0de4fb20a91cad5d5b54 to your computer and use it in GitHub Desktop.
Save Alfxjx/6135832e3d8c0de4fb20a91cad5d5b54 to your computer and use it in GitHub Desktop.
Vue mixin 检测登录之后未操作时间,超出阈值就退出。
<script>
// 需要在App.vue上面绑定一个点击事件用来刷新时间
import { logout } from "@/api/index";
export default {
data() {
return {
nowtime: null,
timeend: null,
interval: null
};
},
mounted() {
this.nowtime = Date.parse(new Date());
this.timeend = Date.parse(new Date());
console.log(`${this.nowtime}---${this.timeend}`);
this.interval = setInterval(() => {
this.checkLoginTime();
}, 10000);
},
beforeDestroy() {
this.interval = null;
},
methods: {
checkLoginTime() {
this.timeend = Date.parse(new Date());
console.log(`${this.timeend} is now check time`);
if (this.timeend - this.nowtime > 1800000) {
this.logout();
}
},
clickToReset() {
this.nowtime = Date.parse(new Date());
console.log(`reset nowtime to ${this.nowtime}`);
},
logout() {
logout().then(res => {
if (res.data) {
this.$router.push("/login");
} else {
console.error("退出失败请重试");
}
});
}
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment