最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue2/3?登錄后用戶無操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線(示例代碼)

 更新時(shí)間:2024年07月19日 10:18:17   作者:星?辰.  
這篇文章主要介紹了Vue2/3?登錄后用戶無操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

Vue2/3 登錄后用戶無操作半小時(shí)后自動(dòng)清除登錄信息退出登錄下線

1、打開App.vue 頁面,然后新增監(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)到登錄頁)
           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)用戶長時(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)
    // 跳到登陸頁
    // if (router.currentRouter.name == 'login') return // 當(dāng)前已經(jīng)是登陸頁時(shí)不做跳轉(zhuǎn)
    router.push({ name: 'login' })
  }
}
export default function() {
  /* 定時(shí)器 間隔30秒檢測(cè)是否長時(shí)間未操作頁面 */
  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.html

Vue.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è)頁面,各個(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);
          // 最后返回到登錄頁
          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)文章

  • element-ui 上傳圖片后清空?qǐng)D片顯示的實(shí)例

    element-ui 上傳圖片后清空?qǐng)D片顯示的實(shí)例

    今天小編就為大家分享一篇element-ui 上傳圖片后清空?qǐng)D片顯示的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue模擬實(shí)現(xiàn)購物車結(jié)算功能

    Vue模擬實(shí)現(xiàn)購物車結(jié)算功能

    這篇文章主要為大家詳細(xì)介紹了Vue模擬實(shí)現(xiàn)購物車結(jié)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Vue打包部署到Nginx時(shí),css樣式不生效的解決方式

    Vue打包部署到Nginx時(shí),css樣式不生效的解決方式

    這篇文章主要介紹了Vue打包部署到Nginx時(shí),css樣式不生效的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue3的ref,computed,reactive和toRefs你都了解嗎

    vue3的ref,computed,reactive和toRefs你都了解嗎

    這篇文章主要為大家詳細(xì)介紹了vue3的ref,computed,reactive和toRefs,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 淺談v-for 和 v-if 并用時(shí)篩選條件方法

    淺談v-for 和 v-if 并用時(shí)篩選條件方法

    今天小編就為大家分享一篇淺談v-for 和 v-if 并用時(shí)篩選條件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 詳解vue中router-link標(biāo)簽所必備了解的屬性

    詳解vue中router-link標(biāo)簽所必備了解的屬性

    這篇文章主要介紹了vue中router-link標(biāo)簽所必備了解的屬性,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Vue3+Vite環(huán)境變量與多環(huán)境配置詳解

    Vue3+Vite環(huán)境變量與多環(huán)境配置詳解

    本文介紹了在Vue3+Vite項(xiàng)目中配置和使用環(huán)境變量的完整方案,該方案實(shí)現(xiàn)了安全、靈活的環(huán)境變量管理,支持多環(huán)境開發(fā)和部署需求,感興趣的可以了解一下
    2026-05-05
  • 記錄一個(gè)van-list不斷onLoad加載的坑及解決

    記錄一個(gè)van-list不斷onLoad加載的坑及解決

    這篇文章主要介紹了記錄一個(gè)van-list不斷onLoad加載的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue子組件向父組件通信與父組件調(diào)用子組件中的方法

    Vue子組件向父組件通信與父組件調(diào)用子組件中的方法

    這篇文章主要介紹了Vue子組件向父組件通信與父組件調(diào)用子組件中的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • Vue.js實(shí)現(xiàn)表格動(dòng)態(tài)增加刪除的方法(附源碼下載)

    Vue.js實(shí)現(xiàn)表格動(dòng)態(tài)增加刪除的方法(附源碼下載)

    Vue.js通過簡(jiǎn)潔的API提供高效的數(shù)據(jù)綁定和靈活的組件系統(tǒng)。在前端紛繁復(fù)雜的生態(tài)中,Vue.js有幸受到一定程度的關(guān)注,下面這篇文章主要給介紹了Vue.js實(shí)現(xiàn)表格動(dòng)態(tài)增加刪除的方法實(shí)例,文末提供了源碼下載,需要的朋友可以參考借鑒。
    2017-01-01

最新評(píng)論

夏津县| 新宾| 泉州市| 南雄市| 营口市| 郸城县| 类乌齐县| 凉山| 北辰区| 延安市| 秭归县| 宁强县| 阜阳市| 油尖旺区| 蕉岭县| 襄汾县| 浦城县| 白沙| 梁平县| 秀山| 伊春市| 榕江县| 龙井市| 保靖县| 塔城市| 巴南区| 波密县| 陇川县| 凤山市| 塔河县| 广水市| 东乡族自治县| 乌什县| 嵊州市| 韶关市| 若尔盖县| 池州市| 永兴县| 安西县| 泊头市| 佛教|