vue設(shè)置頁(yè)面超時(shí)15分鐘自動(dòng)退出登錄的方法詳解
需求:用戶登錄后,如果長(zhǎng)時(shí)間未操作頁(yè)面這個(gè)時(shí)候需要自動(dòng)退出登錄回到登錄頁(yè)面。
注意點(diǎn):這里我們?nèi)绻殉瑫r(shí)的時(shí)間放到瀏覽器里面存儲(chǔ),我們要放到本地存儲(chǔ)里面localStorage里面
Vue設(shè)置長(zhǎng)時(shí)間未操作登錄以后自動(dòng)到期返回登錄頁(yè)
方法一:
在app.vue中添加點(diǎn)擊事件,并且點(diǎn)擊時(shí)候添加點(diǎn)擊的時(shí)間,這里我們進(jìn)行判斷是否超時(shí)情況
Vue2里面的使用
<template>
<!-- 添加點(diǎn)擊事件 -->
<div id="app" style="height: 100%" @click="isTimeOut">
<router-view/>
</div>
</template>
<script>
export default {
name: 'App',
data () {
return {
lastTime: null, // 最后一次點(diǎn)擊的時(shí)間
currentTime: null, // 當(dāng)前點(diǎn)擊的時(shí)間
timeOut: 15 * 60 * 1000 // 設(shè)置超時(shí)時(shí)間:30分鐘
}
},
created () {
this.lastTime = new Date().getTime()
},
methods: {
isTimeOut () {
this.currentTime = new Date().getTime() // 記錄這次點(diǎn)擊的時(shí)間
if (this.currentTime - this.lastTime > this.timeOut) { // 判斷上次最后一次點(diǎn)擊的時(shí)間和這次點(diǎn)擊的時(shí)間間隔是否大于30分鐘
if (localStorage.getItem('loginInfo')) { // 如果是登錄狀態(tài)
localStorage.clear()
sessionStorage.clear();
this.$router.push({name: 'login'})
} else {
this.lastTime = new Date().getTime()
}
} else {
this.lastTime = new Date().getTime() // 如果在30分鐘內(nèi)點(diǎn)擊,則把這次點(diǎn)擊的時(shí)間記錄覆蓋掉之前存的最后一次點(diǎn)擊的時(shí)間
}
}
}
}
</script>方法二:
Vue3里面應(yīng)用:
在路由ruouter路由加載后進(jìn)行判斷是否有超時(shí)的邏輯

具體引用如下:
在utils里面創(chuàng)建點(diǎn)擊是否超時(shí)的組件如下activeCheck.ts
import { Local, Session } from "./storage"
import router from '/@/router';
const timeOut = 15 * 60 * 1000; //設(shè)置超時(shí)時(shí)間: 15分
function updateActiveTime() {
localStorage.setItem('lastActiveTime', new Date().getTime() + '');
}
window.onload = function () {
window.document.onmousedown = updateActiveTime;
};
let checkActiveInterval: any = null
export const startActiveCheck = () => {
if (checkActiveInterval !== null) return
updateActiveTime();
checkActiveInterval = setInterval(() => {
const currentTime = new Date().getTime();
const lastActiveTime = localStorage.getItem('lastActiveTime');
if (currentTime - Number(lastActiveTime) > timeOut) {
Local.clear();
Session.clear();
clearInterval(checkActiveInterval)
checkActiveInterval = null
router.push('/login');
}
}, 30000);
}
export const endActiveCheck = () => {
clearInterval(checkActiveInterval)
checkActiveInterval = null
}這里的storage.ts存儲(chǔ)組件如下:
import Cookies from 'js-cookie';
/**
* window.localStorage 瀏覽器永久緩存
* @method set 設(shè)置永久緩存
* @method get 獲取永久緩存
* @method remove 移除永久緩存
* @method clear 移除全部永久緩存
*/
export const Local = {
// 查看 v2.4.3版本更新日志
setKey(key: string) {
// @ts-ignore
return `${__NEXT_NAME__}:${key}`;
},
// 設(shè)置永久緩存
set<T>(key: string, val: T) {
window.localStorage.setItem(Local.setKey(key), JSON.stringify(val));
},
// 獲取永久緩存
get(key: string) {
let json = <string>window.localStorage.getItem(Local.setKey(key));
try{
return JSON.parse(json);
}catch{
return json
}
},
// 移除永久緩存
remove(key: string) {
window.localStorage.removeItem(Local.setKey(key));
},
// 移除全部永久緩存
clear() {
window.localStorage.clear();
},
};
/**
* window.sessionStorage 瀏覽器臨時(shí)緩存
* @method set 設(shè)置臨時(shí)緩存
* @method get 獲取臨時(shí)緩存
* @method remove 移除臨時(shí)緩存
* @method clear 移除全部臨時(shí)緩存
*/
export const Session = {
// 設(shè)置臨時(shí)緩存
set<T>(key: string, val: T) {
window.sessionStorage.setItem(Local.setKey(key), JSON.stringify(val));
},
// 獲取臨時(shí)緩存
get(key: string) {
let json = <string>window.sessionStorage.getItem(Local.setKey(key));
return JSON.parse(json);
},
// 移除臨時(shí)緩存
remove(key: string) {
window.sessionStorage.removeItem(Local.setKey(key));
},
// 移除全部臨時(shí)緩存
clear() {
window.sessionStorage.clear();
},
};import { endActiveCheck, startActiveCheck } from '../utils/activeCheck';
// 路由加載后
router.afterEach((to) => {
NProgress.done();
if (to.path === '/login') {
endActiveCheck()
} else {
startActiveCheck()
}
});到此這篇關(guān)于vue設(shè)置頁(yè)面超時(shí)15分鐘自動(dòng)退出登錄的方法詳解的文章就介紹到這了,更多相關(guān)vue頁(yè)面自動(dòng)推出登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用axios?post發(fā)送json數(shù)據(jù)跨域請(qǐng)求403的解決方案
這篇文章主要介紹了vue使用axios?post發(fā)送json數(shù)據(jù)跨域請(qǐng)求403的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
使用vue-cli4.0快速搭建一個(gè)項(xiàng)目的方法步驟
這篇文章主要介紹了使用vue-cli4.0快速搭建一個(gè)項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue-cli結(jié)合Element-ui基于cropper.js封裝vue實(shí)現(xiàn)圖片裁剪組件功能
這篇文章主要介紹了vue-cli結(jié)合Element-ui基于cropper.js封裝vue實(shí)現(xiàn)圖片裁剪組件功能,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-03-03
如何使用 vue-cli 創(chuàng)建模板項(xiàng)目
這篇文章主要介紹了如何使用 vue-cli 創(chuàng)建模板項(xiàng)目,幫助大家更好的理解和學(xué)習(xí)vue框架的知識(shí),感興趣的朋友可以了解下2020-11-11
關(guān)于怎么在vue項(xiàng)目里寫(xiě)react詳情
本篇文章是在vue項(xiàng)目里寫(xiě)tsx的一篇介紹。其實(shí)vue里面寫(xiě)jsx也挺有意思的,接下來(lái)小編九給大家詳細(xì)介紹吧,感興趣的小伙伴請(qǐng)參考下面的文章內(nèi)容2021-09-09
Vue樹(shù)表格分頁(yè)的實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Vue樹(shù)表格分頁(yè)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
利用Vue Router實(shí)現(xiàn)單頁(yè)面應(yīng)用(SPA)的代碼示例
在當(dāng)今前端開(kāi)發(fā)中,單頁(yè)面應(yīng)用(SPA)已成為一種主流的開(kāi)發(fā)模式,它通過(guò)在用戶與網(wǎng)頁(yè)之間提供更流暢的交互體驗(yàn),來(lái)改變傳統(tǒng)多頁(yè)面應(yīng)用的思維,本文將詳細(xì)介紹如何利用 Vue.js 中的 Vue Router 來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的單頁(yè)面應(yīng)用,需要的朋友可以參考下2025-01-01
詳解Nuxt內(nèi)導(dǎo)航欄的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了詳解Nuxt內(nèi)導(dǎo)航欄的兩種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

