ソースを参照

长时间不操作(鼠标点击,光标移动,敲击键盘)退出登录,时长在全局配置setting中设置和修改(不设置默认不退出)

shenqilong 6 ヶ月 前
コミット
1d012a17a4
3 ファイル変更59 行追加0 行削除
  1. 2 0
      src/main.js
  2. 43 0
      src/utils/astrict.js
  3. 14 0
      src/utils/storage.js

+ 2 - 0
src/main.js

@@ -14,6 +14,8 @@ import dataV from '@jiaminghi/data-view'
 // less的css编码语法
 import less from 'less'
 
+import Astrict from '@/utils/astrict'
+Vue.use(Astrict)
 // 表格自适应高度
 // import Plugin from 'v-fit-columns'
 // import Blob from './excel/Blob'

+ 43 - 0
src/utils/astrict.js

@@ -0,0 +1,43 @@
+// 引入路由和storage工具函数
+import storage from '@/utils/storage'
+import router from '../router'
+import store from '@/store'
+let lastTime = new Date().getTime()
+let currentTime = new Date().getTime()
+// let timeOut = 30 * 60 * 1000 //设置超时时间: 30分钟
+
+window.onload = function () {
+  // 鼠标点击事件
+  window.document.onmousedown = function () {
+    storage.setItem('lastTime', new Date().getTime())
+  }
+  // 鼠标移动事件
+  window.document.onmousemove = function () {
+    storage.setItem('lastTime', new Date().getTime())
+  }
+  // 键盘敲击事件
+  window.document.onkeydown = function () {
+    storage.setItem('lastTime', new Date().getTime())
+  }
+}
+
+function checkTimeout() {
+  currentTime = new Date().getTime() //更新当前时间
+  lastTime = storage.getItem('lastTime')
+  if (
+    store.getters?.setting?.timeout?.time &&
+    currentTime - lastTime > store.getters?.setting?.timeout?.time
+  ) {
+    //判断是否超时
+    //清除storage的数据(登陆信息和token)
+    storage.clear()
+    // 跳到登陆页
+    router.push({ path: '/login' })
+  }
+}
+
+export default function () {
+  /* 定时器 间隔30秒检测是否长时间未操作页面 */
+  // window.setInterval(checkTimeout, 60 * 1000)
+  window.setInterval(checkTimeout, 1 * 1000)
+}

+ 14 - 0
src/utils/storage.js

@@ -67,6 +67,20 @@ const storage = {
   clear: () => {
     window.sessionStorage.clear()
     window.localStorage.clear()
+  },
+  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)
   }
 }