vue - 一定时间拦截到登录页

原理 - 每进行一次操作记录一次时间,定时器触发一定时间进行检查记录的时间与当前时间的间隔时间,判断间隔时间进行拦截到登录页

直接建立js文件进行编写逻辑,在main.js进行引入即可

webapp - 一定时间拦截到登录页

import router from '@/router'
import store from '@/store'

var lastTime = new Date().getTime()
var currentTime = new Date().getTime()
var timeOut = 5 * 60 * 1000 // 设置超时时间

window.onload = function() {  //页面加载进行添加监听事件,进行操作修改操作时间-lastTime
  window.document.onmousedown = function() {
    localStorage.setItem('lastTime', new Date().getTime())
  }
  window.document.tap = function() {
    localStorage.setItem('lastTime', new Date().getTime())
  }
  window.document.touchstart = function() {
    localStorage.setItem('lastTime', new Date().getTime())
  }
  window.document.touchmove = function() {
    localStorage.setItem('lastTime', new Date().getTime())
  }
  window.document.touchend = function() {
    localStorage.setItem('lastTime', new Date().getTime())
  }
  window.document.touchcancel = function() {
    localStorage.setItem('lastTime', new Date().getTime())
  }
}

//定时器触发检查时间间隔
function checkTimeout() {
  currentTime = new Date().getTime() // 更新当前时间
  lastTime = localStorage.getItem('lastTime')
  if (currentTime - lastTime > timeOut) { // 判断是否超时 ,在这里面写退出的一些操作
    if(router.history.current.fullPath !== '/login'){ //登录页长时间无操作不做处理
        localStorage.removeItem('lastTime');
        logout();
    }
  }
}


//退出方法
async function logout() {
  await store.dispatch('user/goLoginOut')
  router.push(`/login`)
}

//定时器检查时间间隔
window.setInterval(checkTimeout, 30000)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值