登录1h提示用户登录超时


import store from '@/store'
import { getToken } from "@/utils/auth";
import { MessageBox } from 'element-ui'

export default function () {
    let setInterval = null;//定时器
    let timeOut = 60 * 1000 * 60// 设置超时时间: 1小时
    // let timeOut = 10 * 1000 // 设置超时时间: 1小时
    // console.log("开始", new Date().getTime())
    // 初次向sessionStorage存入操作时间
    window.sessionStorage.setItem('lastTime', new Date().getTime())
    // 每次操作页面,更新sessionStorage存入的操作时间
    window.onload = function () {
        window.document.onmousedown = function () {
            console.log("开始")
            window.sessionStorage.setItem('lastTime', new Date().getTime())
        }
    }
    MessageBox.setDefaults({
        closeOnClickModal: false
    })
    function checkTimeout() {
        let currentTime = new Date().getTime() // 当前时间
        let lastTime = window.sessionStorage.getItem("lastTime")//上次操作的时间
        // 判断是否超时
        if(getToken()){
            if (currentTime - lastTime >= timeOut) {
                MessageBox(
                    '登录状态已过期,请重新登录',
                    '系统提示', {
                      confirmButtonText: '重新登录',
                      closeOnClickModal: false,
                      type: 'warning'
                    }
                  ).then(() => {
                    store.dispatch('LogOut').then(() => {
                      location.reload() // 为了重新实例化vue-router对象 避免bug
                    })
                    // 清除定时器
                    window.clearInterval(setInterval);
                    // 清除sessionStorage
                    window.sessionStorage.clear('lastTime')
                })
            }
        }
        
    }
    /* 定时器 间隔2秒检测是否长时间未操作页面 */
    setInterval = window.setInterval(checkTimeout, 30000)
    // setInterval = window.setInterval(checkTimeout, 10000)
}

在main.js中引用并使用

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值