Vue2/3?登錄后用戶無操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線(示例代碼)
Vue2/3 登錄后用戶無操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線
1、打開App.vue 頁(yè)面,然后新增監(jiān)聽全局點(diǎn)擊事件的方法
data() {
return {
lastTime: null,
timeOut: 10 * 100 * 60,
}
},
metaInfo() {
return {
title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
titleTemplate: title => {
return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
}
}
},
created() {
this.lastTime = new Date().getTime()
},
methods: {
//全局監(jiān)聽得點(diǎn)擊事件
isTimeOut() {
var currentTime = new Date().getTime();
// 判斷上次最后一次點(diǎn)擊的時(shí)間和這次點(diǎn)擊的時(shí)間間隔
if (currentTime - this.lastTime > this.timeOut) {
//這個(gè)是判斷 當(dāng)前用戶是否登錄,根據(jù)自己的情況寫
// if (null != sessionStorage.getItem('token')) {
this.$alert('身份驗(yàn)證已過期,請(qǐng)重新登錄', '提示', {
confirmButtonText: '確定',
callback: action => {
//這里寫路由(跳轉(zhuǎn)到登錄頁(yè))
this.$router.replace('/login')
}
});
// }
} else {
// 如果在期限內(nèi)點(diǎn)擊,則把這次點(diǎn)擊的時(shí)間覆蓋掉之前存的最后一次點(diǎn)擊的時(shí)間
this.lastTime = new Date().getTime()
}
}
}3步解決vue實(shí)現(xiàn)用戶長(zhǎng)時(shí)間不點(diǎn)擊操作,自動(dòng)退出登錄功能
1.在util文件夾下創(chuàng)建一個(gè)storage.js封裝localStorage方法,js文件內(nèi)容如下
export default {
setItem(key, value) {
value = JSON.stringify(value)
window.localStorage.setItem(key, value)
},
getItem(key, defaultValue) {
let value = window.localStorage.getItem(key)
try {
value = JSON.parse(value)
} catch {}
return value || defaultValue
},
removeItem(key) {
window.localStorage.removeItem(key)
},
clear() {
window.localStorage.clear()
},
}
=======================================================================
2.在util文件夾下創(chuàng)建一個(gè)astrict.js,js文件內(nèi)容如下,引入的文件根據(jù)自己實(shí)際路徑
// 引入路由和storage工具函數(shù)
import storage from '../utils/storage'
import router from '../router'
let lastTime = new Date().getTime()
let currentTime = new Date().getTime()
let timeOut = 1 * 60 * 1000 //設(shè)置超時(shí)時(shí)間: 30分鐘
window.onload = function() {
window.document.onmousedown = function() {
storage.setItem('lastTime', new Date().getTime())
}
}
function checkTimeout() {
currentTime = new Date().getTime() //更新當(dāng)前時(shí)間
lastTime = storage.getItem('lastTime')
console.log(lastTime)
if (currentTime - lastTime > timeOut) {
//判斷是否超時(shí)
// 清除storage的數(shù)據(jù)(登陸信息和token)
storage.clear()
console.log(router)
// 跳到登陸頁(yè)
// if (router.currentRouter.name == 'login') return // 當(dāng)前已經(jīng)是登陸頁(yè)時(shí)不做跳轉(zhuǎn)
router.push({ name: 'login' })
}
}
export default function() {
/* 定時(shí)器 間隔30秒檢測(cè)是否長(zhǎng)時(shí)間未操作頁(yè)面 */
window.setInterval(checkTimeout, 3000)
}
=======================================================
3.在main.js引入astrict.js
import Astrict from '../src/utils/astrict.js'
Vue.use(Astrict)
另附原文地址:https://www.uoften.com/article/186408.htmlVue.js框架:超出配置登出時(shí)間就會(huì)退出登陸(前端設(shè)置)
1、login.vue
在登陸時(shí)候記下點(diǎn)擊登陸的時(shí)間;
<strong>sessionStorage.setItem("lastClickTime", new Date().getTime());</strong>methods:{
login(){
this.$api.commn.login(this.loginForm.username, this.loginForm.password).then((result) => {
let res = result.data;
let token = "Bearer " + res.token;
this.$storage.token.set(token);
this.$storage.user.set(res);
//記錄點(diǎn)擊時(shí)間
sessionStorage.setItem("lastClickTime", new Date().getTime());
})
.catch((error) => {
console.log("error");
});
}
}2、home.vue
代碼精簡(jiǎn)了,html只剩下左邊菜單欄,展示定時(shí)器寫在哪個(gè)頁(yè)面,各個(gè)項(xiàng)目不同,對(duì)應(yīng)的不一定是home.vue,函數(shù)只寫了定時(shí)器的,僅供參考;
1. data先定義timer
2.鉤子函數(shù):methods里寫定時(shí)器實(shí)現(xiàn)函數(shù),created里捕獲記錄每次點(diǎn)擊的時(shí)間,mounted里執(zhí)行定時(shí)器,destroyed里銷毀定時(shí)器。
<template>
<div class="sidebar" style="background-color: rgb(50, 65, 87)">
<!--左側(cè)菜單 start *********************************************************************-->
<el-menu
class="sidebar-el-menu"
:collapse="collapse"
router=""
:default-active="onRoutes">
<template v-for="menu in menus">
<template v-if="menu.subs && menu.subs.length > 0">
<el-submenu :index="menu.id + ''">
<template slot="title">
<i :class="menu.icon"></i>
<span>{{ $t(menu.name) }}</span>
</template>
</el-submenu>
</template>
</el-menu>
<!--左側(cè)菜單 end *********************************************************************-->
</div>
<div class="content-box" :class="{ 'content-collapse': collapse }">
<v-tags></v-tags>
<!--內(nèi)容 start *********************************************************************-->
<!--內(nèi)容 end *********************************************************************-->
</div>
</template>
<script>
export default {
name: "home",
data() {
return {
timer: null,
};
},
methods: {
isTimeOut() {
// 使?定時(shí)器之前,要清除?下定時(shí)器
clearInterval(this.timer);
// 定時(shí)器
this.timer = setInterval(() => {
let times = 10 * 60 * 1000;//配置的是10min
let lastClickTime = sessionStorage.getItem("lastClickTime") * 1; // 把上次點(diǎn)擊時(shí)候的字符串時(shí)間轉(zhuǎn)換成數(shù)字時(shí)間
let nowTime = new Date().getTime(); // 獲取當(dāng)前時(shí)間
// 當(dāng)前時(shí)間減去上次點(diǎn)擊時(shí)間超出配置的登出時(shí)間,就提?登錄退出
if (nowTime - lastClickTime > times) {
// 提??下?戶
this.$message({ type: "warning", message: "超時(shí)了,已退出登錄" });
// 這?要清除定時(shí)器,結(jié)束任務(wù)
clearInterval(this.timer);
// 最后返回到登錄頁(yè)
this.$router.push({ path: "/login" });
}
}, 1000);
},
},
mounted() {
//在這執(zhí)行定時(shí)器
this.isTimeOut()
},
created() {
window.addEventListener("click",() => {
// 為了?便,我們把點(diǎn)擊事件的時(shí)間直接存到sessionStorage中去,這樣?便獲取?較
sessionStorage.setItem("lastClickTime", new Date().getTime());
},
//true 捕獲點(diǎn)擊事件
true
);
},
destroyed: function () {
clearInterval(this.timer);
window.removeEventListener("click", () => {}, true);
},
};
</script>到此這篇關(guān)于Vue2/3 登錄后用戶無操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線的文章就介紹到這了,更多相關(guān)vue自動(dòng)退出登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中選中多個(gè)選項(xiàng)并且改變選中的樣式的實(shí)例代碼
這篇文章主要介紹了vue中選中多個(gè)選項(xiàng)并且改變選中的樣式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
vue中實(shí)現(xiàn)子組件接收父組件方法并獲取返回值
這篇文章主要介紹了vue中實(shí)現(xiàn)子組件接收父組件方法并獲取返回值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue與原生app的對(duì)接交互的方法(混合開發(fā))
vue開發(fā)h5項(xiàng)目特別是移動(dòng)端的項(xiàng)目,很多都是打包后掛載在原生APP上的,這篇文章主要介紹了vue與原生app的對(duì)接交互的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11
vue如何監(jiān)聽元素橫向滾動(dòng)到最右側(cè)
這篇文章主要介紹了vue如何監(jiān)聽元素橫向滾動(dòng)到最右側(cè)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue基于iview實(shí)現(xiàn)登錄密碼的顯示與隱藏功能
這篇文章主要介紹了Vue基于iview實(shí)現(xiàn)登錄密碼的顯示與隱藏功能,本文通過截圖實(shí)例代碼說明給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
vue中對(duì)象的賦值Object.assign({}, row)方式
這篇文章主要介紹了vue中對(duì)象的賦值Object.assign({}, row)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

