当前位置: 首页 > article >正文

Vue3 Typescript 前端页面5min后无操作自动退出至登录页面

效果图:
在这里插入图片描述

前端页面5min无操作,弹出弹窗提示用户系统将在30s后退出,30s后系统自动退出至登录页面。

<template>
    <div>
        <div class="f-z-20-px">
            {{ remainTime }}秒后无操作将退出
        </div>
        <el-dialog
            title="您长时间未操作"
            v-model="dialogVisible"
            :close-on-click-modal="false"
            :show-close="false"
            @before-close="handleLogout()"
            width="600px">
            <p class="f-z-20-px text-center m-b-5">
                为保障您的隐私,系统将
                <b class="text-primary">{{ logoutTime }}</b>
                秒后自动退出。
            </p>
            <template #footer>
            <span>
                <el-button @click="handleLogout()">手动退出</el-button>
                <el-button type="primary" @click="continueAdk()">继续问诊</el-button>
            </span>
            </template>
        </el-dialog>
        
    </div>
</template>

<script setup lang="ts">

const router = useRouter();
const currentRoute = useRoute();


let lastTime = new Date().getTime();
let currentTime = new Date().getTime();
let timeOut = 1000 * 60 * 0.1; //设置超时时间: 5分钟
const remainTime = ref(300);//剩余时间
function checkTimeout() {
  currentTime = new Date().getTime(); //更新当前时间
  lastTime = localStorage.getItem("lastTime");
  remainTime.value = parseInt((timeOut - (currentTime - lastTime)) / 1000);
  console.log("currentTime", currentTime);
  if (remainTime.value < 1) {
    //超时清空lastTime缓存,退出至登录页
    localStorage.removeItem("lastTime");
    console.log("超时登出", lastTime);
    let userInfo = localStorage.getItem("userInfoStorage");
    if (router.currentRoute.value.path.includes("/login")) return; // 当前已经是登陆页时不做跳转
    router.push("/login");
    stopTimer();
  }
}
let intervalId: NodeJS.Timeout | null = null;
function startTimer() {
  if (intervalId !== null) return; // 如果已经有一个定时器在运行,则不启动新的  
  intervalId = setInterval(() => {
    checkTimeout();
  }, 1000);
}
function stopTimer() {
   console.log('停止计时器全局倒计时');
    if (intervalId) {
        clearInterval(intervalId);
        intervalId = null; 
       
    }
}
//提示用户系统将退出的弹窗上的倒计时
let logoutTimerId: NodeJS.Timeout | null = null;
const logoutTime = ref(30);//自动关闭倒计时
function startLogoutTimer() {
    if (logoutTimerId !== null) return; // 如果已经有一个定时器在运行,则不启动新的  
    logoutTimerId = setInterval(() => {
    logoutTime.value--;
    console.log('当前计数:', logoutTime.value);
    if(logoutTime.value < 1 ){
        handleLogout();
    }
  }, 1000);
}
function stopLogoutTimer() {
    logoutTime.value = 30;
    if (logoutTimerId) {
        clearInterval(logoutTimerId);
        logoutTimerId = null; 
    }
}
const dialogVisible = ref(false);
function continueAdk(){
    stopLogoutTimer();
    stopTimer();
    startTimer();
    dialogVisible.value = false;
}
function handleLogout() {
    stopTimer();
    stopLogoutTimer();
    dialogVisible.value = false;
    router.push("/login");
}

onMounted(() => {
    localStorage.setItem("lastTime", new Date().getTime());
    startTimer();
    window.document.onmousedown = function () {
    	//监听点击事件,用户点击从头开始计时
        localStorage.setItem("lastTime", new Date().getTime());
    };
});
onBeforeUnmount(() => {
  stopTimer();
});
</script>

http://www.kler.cn/news/327319.html

相关文章:

  • Windows上面搭建Flutter Android运行环境
  • cmd下的管理员权限闪退 原理分析
  • 【Rockchip系列】官方函数:drm_buf_alloc
  • 【Kotlin基于selenium实现自动化测试】初识selenium以及搭建项目基本骨架(1)
  • 华为OD机试 - 超级玛丽通过吊桥的走法 - 动态规划(Python/JS/C/C++ 2024 E卷 200分)
  • 数据结构——计数、桶、基数排序
  • 深入探索 PyTorch 在机器学习中的应用
  • 观测云对接 SkyWalking 最佳实践
  • Springboot中yml文件不生效原因分析及解决
  • 【C++篇】启航——初识C++(下篇)
  • 滚雪球学Oracle[7.1讲]:Oracle云数据库
  • 如何从 Windows 11/10/8.1/8/7 中恢复已删除的视频
  • 前端导出页面PDF
  • rust的nutyp验证和validator验证数据的方法
  • MySQL | group by 用法
  • 牛客周赛 Round 62
  • 828华为云征文|部署个人文档管理系统 Docspell
  • Kali Linux安全工具
  • 实战OpenCV之形态学操作
  • 网络带宽对于服务器的影响
  • 云原生之运维监控实践-使用Prometheus与Grafana实现对MySQL和Redis服务的监测
  • Drf认证组件
  • Feign 主要负责简化 HTTP API 的调用逻辑; Eureka 负责服务实例的注册和服务发现; Ribbon 则负责实现客户端的负载均衡。
  • UE4_Niagara基础实例—7、如何让粒子照亮周边环境
  • 制造企业各部门如何参与生产成本控制与管理?
  • Leetcode Hot 100 | 543.二叉树的直径 | 递归+优化
  • 【人人保-注册安全分析报告-无验证方式导致安全隐患】
  • 项目:微服务即时通讯系统客户端(基于C++QT)]四,中间界面搭建和逻辑准备
  • git使用“保姆级”教程3——添加暂存区及提交本地库
  • 苹果手机如何录屏?IOS 自带工具与嗨格式录屏大师 APP 详解