Created
February 24, 2021 09:01
-
-
Save Alfxjx/6135832e3d8c0de4fb20a91cad5d5b54 to your computer and use it in GitHub Desktop.
Vue mixin 检测登录之后未操作时间,超出阈值就退出。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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