vue中实现1小时不操作则退出登录功能

哈喽,大家好,本次鹏仔项目中需要实现后台管理端用户1小时不操作则退出登录功能,我第一时间想到的也就是本地存储时间判断,那么下面给大家分享一下。

首先,我们在vue项目中新建js文件 authService.js

javascript 复制代码
let logoutTimer = null;
const EXPIRE_TIME = 3600 * 1000 * 1; // 1小时(2小时乘2)
// const EXPIRE_TIME = 60 * 1000 * 1; // 1分钟(2分钟乘2)

export default {
  // 登录时启动定时器
  startLogoutTimer() {
    // 清除已有定时器,避免重复
    this.clearLogoutTimer();
    // 保存登录时间
    const loginTime = new Date().getTime();
    localStorage.setItem('loginTime', loginTime);
    // 设置1小时后自动退出
    logoutTimer = setTimeout(() => {
      this.logout();
    }, EXPIRE_TIME);
  },
  // 清除定时器
  clearLogoutTimer() {
    if (logoutTimer) {
      clearTimeout(logoutTimer);
      logoutTimer = null;
    }
  },
  // 重置定时器(用户操作时调用)
  resetLogoutTimer() {
    this.startLogoutTimer();
  },
  // 退出登录
  logout() {
    // 清除登录状态
    localStorage.clear();
    sessionStorage.clear();
    this.clearLogoutTimer();
   
    // 跳转到登录页
    // window.location.href = '/login';
   
    // 可在此处添加其他清理逻辑
    alert('登录已过期,请重新登录');
    window.location.reload();
  },
 
  // 检查登录是否过期(页面刷新时调用)
  checkExpired() {
    const loginTime = localStorage.getItem('loginTime');
    if (!loginTime) return true;
   
    const now = new Date().getTime();
    // 如果当前时间 - 登录时间 > 1小时,视为过期
    if (now - loginTime > EXPIRE_TIME) {
      this.logout();
      return true;
    }
   
    // 未过期则重新设置定时器
    this.startLogoutTimer();
    return false;
  }
};

接着,我们在 main.js 中引入,并执行监听用户操作时重启定时器。

javascript 复制代码
import authService from '@A/resource/authService'; // 自行引入路径
// 检查登录状态是否过期
authService.checkExpired();
// 监听用户活动,重置定时器
function handleUserActivity() {
  // 如果有登录状态才重置定时器
  if (localStorage.getItem('TOKEN')) {
    authService.resetLogoutTimer();
  }
}

// 监听常见的用户交互事件
window.addEventListener('click', handleUserActivity); // 监听点击事件
window.addEventListener('keypress', handleUserActivity); // 监听键盘按键事件
window.addEventListener('scroll', handleUserActivity); // 监听页面滚动事件
window.addEventListener('touchstart', handleUserActivity); // 监听触摸开始事件(移动端)

在后台页面用户输入账号密码登录成功时候,我们需要调用一次启动自动退出定时器。

javascript 复制代码
import authService from '@A/resource/authService'; // 自行引入路径
loginBtn(){
    localStorage.setItem('TOKEN', '存储TOKEN');
    console.log('登录成功');
    
    authService.startLogoutTimer(); // 启动自动退出定时器
}

当然,用户手动退出时,也不能放过,一定得清除一下定时器。

javascript 复制代码
import authService from '@A/resource/authService'; // 自行引入路径
// 退出
loginOut(){
    localStorage.clear();
    authService.clearLogoutTimer(); // 清除自动退出定时器
    this.$router.push({
        path:'/login'
    })
}

原文围观地址

https://www.sharedbk.com/post/290.htmlhttps://www.sharedbk.com/post/290.html

【鹏仔小扩展】

写好之后,发现在当前后台管理项目是没问题了,但是项目中有好几个模块是使用 iframe 嵌套的页面,在 iframe 中操作,并不触发当前的一些监听时间,所以导致原本在 iframe 页面操作中就自动退出了。

解决方法,iframe 嵌套的项目给主项目通知监听。

首先,我们在嵌套的子项目中进行监听用户操作,然后通知父级域名

javascript 复制代码
<script>
  // 监听 iframe 内的用户活动事件
  let { protocol, hostname, port } = window.location;
  let url = port ? `${protocol}//${hostname}:${port}` : `${protocol}//${hostname}`; // 获取当前页面的 url,带协议 域名 端口

  const activityEvents = ['click', 'keydown', 'scroll', 'touchstart']; // 触发事件
  activityEvents.forEach(event => {
    window.addEventListener(event, () => {
      // console.log(url);
      // console.log('用户活动事件');
      // 向父页面发送消息(需替换为父页面域名,* 表示允许所有域名,生产环境不推荐)
      window.parent.postMessage(
        { type: 'USER_ACTIVITY', message: 'iframe 内有用户操作' },
        url // 父页面域名
      );
    });
  });
</script>

接着,在我们的主项目 main.js 中监听 iframe 发送的消息,如下代码

javascript 复制代码
// 监听 iframe 发送的消息
window.addEventListener('message', (event) => {
  // 1. 验证消息来源(避免恶意网站伪造消息,需替换为实际 iframe 域名)
  // const trustedDomains = ['http://sharedbk.com', 'http://localhost:8080'];
  // if (!trustedDomains.includes(event.origin)) return;

  // 2. 若收到 iframe 发送的"用户活动"消息,重置定时器
  if (event.data.type === 'USER_ACTIVITY') {
    const token = localStorage.getItem('TOKEN');
    if (token) {
      authService.clearLogoutTimer(); // 清除旧定时器
      authService.startLogoutTimer(router); // 重新计时(1分钟)
    }
  }
});
相关推荐
GISer_Jing3 分钟前
深入解析 Three.js:从架构设计到 WebGPU 渲染革命
javascript·信息可视化·webgl
微祎_8 分钟前
写给新手的 triton-inference-server-ge-backend:昇腾Triton推理服务后端到底是啥?
前端·人工智能·cann
烂不烂问厨房12 分钟前
两张图片拼接在一起中间有条白线
前端
掘金安东尼15 分钟前
浏览器跨域窗口通信技术调研:window.open 与 postMessage
前端
Highcharts.js2 小时前
缺失数据可视化图表开发实战|Highcharts创建人员出生统计面积图表示例
开发语言·前端·javascript·信息可视化·highcharts·图表开发
LaughingZhu9 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫9 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux10 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水11 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger11 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude