vue設(shè)置頁面超時15分鐘自動退出登錄的方法詳解
需求:用戶登錄后,如果長時間未操作頁面這個時候需要自動退出登錄回到登錄頁面。
注意點:這里我們?nèi)绻殉瑫r的時間放到瀏覽器里面存儲,我們要放到本地存儲里面localStorage里面
Vue設(shè)置長時間未操作登錄以后自動到期返回登錄頁
方法一:
在app.vue中添加點擊事件,并且點擊時候添加點擊的時間,這里我們進(jìn)行判斷是否超時情況
Vue2里面的使用
<template> <!-- 添加點擊事件 --> <div id="app" style="height: 100%" @click="isTimeOut"> <router-view/> </div> </template> <script> export default { name: 'App', data () { return { lastTime: null, // 最后一次點擊的時間 currentTime: null, // 當(dāng)前點擊的時間 timeOut: 15 * 60 * 1000 // 設(shè)置超時時間:30分鐘 } }, created () { this.lastTime = new Date().getTime() }, methods: { isTimeOut () { this.currentTime = new Date().getTime() // 記錄這次點擊的時間 if (this.currentTime - this.lastTime > this.timeOut) { // 判斷上次最后一次點擊的時間和這次點擊的時間間隔是否大于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)點擊,則把這次點擊的時間記錄覆蓋掉之前存的最后一次點擊的時間 } } } } </script>
方法二:
Vue3里面應(yīng)用:
在路由ruouter路由加載后進(jìn)行判斷是否有超時的邏輯
具體引用如下:
在utils里面創(chuàng)建點擊是否超時的組件如下activeCheck.ts
import { Local, Session } from "./storage" import router from '/@/router'; const timeOut = 15 * 60 * 1000; //設(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存儲組件如下:
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 瀏覽器臨時緩存 * @method set 設(shè)置臨時緩存 * @method get 獲取臨時緩存 * @method remove 移除臨時緩存 * @method clear 移除全部臨時緩存 */ export const Session = { // 設(shè)置臨時緩存 set<T>(key: string, val: T) { window.sessionStorage.setItem(Local.setKey(key), JSON.stringify(val)); }, // 獲取臨時緩存 get(key: string) { let json = <string>window.sessionStorage.getItem(Local.setKey(key)); return JSON.parse(json); }, // 移除臨時緩存 remove(key: string) { window.sessionStorage.removeItem(Local.setKey(key)); }, // 移除全部臨時緩存 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è)置頁面超時15分鐘自動退出登錄的方法詳解的文章就介紹到這了,更多相關(guān)vue頁面自動推出登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用axios?post發(fā)送json數(shù)據(jù)跨域請求403的解決方案
這篇文章主要介紹了vue使用axios?post發(fā)送json數(shù)據(jù)跨域請求403的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12vue-cli結(jié)合Element-ui基于cropper.js封裝vue實現(xiàn)圖片裁剪組件功能
這篇文章主要介紹了vue-cli結(jié)合Element-ui基于cropper.js封裝vue實現(xiàn)圖片裁剪組件功能,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-03-03利用Vue Router實現(xiàn)單頁面應(yīng)用(SPA)的代碼示例
在當(dāng)今前端開發(fā)中,單頁面應(yīng)用(SPA)已成為一種主流的開發(fā)模式,它通過在用戶與網(wǎng)頁之間提供更流暢的交互體驗,來改變傳統(tǒng)多頁面應(yīng)用的思維,本文將詳細(xì)介紹如何利用 Vue.js 中的 Vue Router 來實現(xiàn)一個簡單的單頁面應(yīng)用,需要的朋友可以參考下2025-01-01詳解Nuxt內(nèi)導(dǎo)航欄的兩種實現(xiàn)方式
這篇文章主要介紹了詳解Nuxt內(nèi)導(dǎo)航欄的兩種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04