vue typescript 页面30分钟未操作,用户退出登录

需求

vue typescript项目,当页面30分钟未操作,用户退出登录

ts代码

app.vue页面

// 在组件中监听页面的onload事件
window.onload = function () {
	window.document.onmousedown = () => {
		const lastTime_onload = new Date().getTime()
		localStorage.setItem('lastTime', lastTime_onload.toString())
	}
}

utils文件夹 astrict.ts

import store from '@/store'
import { ElMessageBox } from 'element-plus/es'

const timeout = 1000 * 60 * 30 // 设置超时时间 10 分钟

const checkTimeOut = () => {
	const nowTime = new Date().getTime()
	const lastTime = parseInt(localStorage.getItem('lastTime') as string, 10)
	if (nowTime - lastTime > timeout) {
		clearLoginStatus()
	}
}

// 清除用户的登录状态函数
function clearLoginStatus() {
	// 当用户存在token时,退出登录
	if (store.userStore.token) {
		store.userStore.logoutAction().then(() => {
			// 刷新页面
			// 刷新页面
			location.reload()
			ElMessageBox.confirm('长时间未操作页面,请重新登录', '提示', {
				showCancelButton: false,
				closeOnClickModal: false,
				showClose: false,
				confirmButtonText: '重新登录',
				type: 'warning'
			})
		})
	}
}

export default function () {
	/* 定时器 间隔5秒检测是否长时间未操作页面 */
	window.setInterval(checkTimeOut, 1000 * 5)
}

main.ts

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import App from './App.vue'
import Astrict from '@/utils/astrict'
const app = createApp(App)
app.use(Astrict)
app.mount('#app')
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值