在主界面使用混入(mixins)
export default {
data() {
return {
logoutCount: 0
}
},
computed: {
// 系統(tǒng)設(shè)置的待機(jī)時(shí)間
outTime() {
return this.$store.state.settings.outTime
}
},
mounted() {
// 監(jiān)聽鼠標(biāo)
document.onmousemove = event => {
this.logoutCount = 0
}
// 監(jiān)聽鍵盤
document.onkeydown = () => {
this.logoutCount = 0
}
// 監(jiān)聽Scroll
document.onscroll = () => {
this.logoutCount = 0
}
this.setTimer()
},
// 清除定時(shí)器
beforeDestroy() {
this.clearTimer()
},
methods: {
clearTimer() {
clearInterval(window.logoutTimer)
window.logoutTimer = null
},
setTimer() {
this.logoutCount = 0
if (!window.logoutTimer) {
window.logoutTimer = window.setInterval(this.logout, 1000)
}
},
async logout() {
const outTime = this.outTime * 60
// 判斷用戶N分鐘無(wú)操作就自動(dòng)登出
this.logoutCount++
if (outTime - this.logoutCount < 10 && outTime - this.logoutCount > 0) { // 鎖定錢包倒計(jì)時(shí)
this.$message.closeAll()
this.$message({
message: `系統(tǒng)已經(jīng)${this.outTime}分鐘無(wú)操作,將在${outTime - this.logoutCount}秒后鎖定錢包,如不想鎖定錢包,請(qǐng)任意操作鼠標(biāo)鍵盤`,
type: 'error'
})
} else if (this.logoutCount > outTime) { // 鎖定錢包
this.$message.closeAll()
this.$message({
message: `系統(tǒng)已經(jīng)${this.outTime}分鐘無(wú)操作,錢包自動(dòng)鎖定`,
type: 'error',
duration: 15000
})
await this.$store.dispatch('user/logout')
this.$router.push(`/login?redirect=${this.$route.path}`)
}
}
}
}