Browse Source

试用平台计算时长

hbchen 1 year ago
parent
commit
eaf6f494a0
7 changed files with 49 additions and 51 deletions
  1. 2 2
      config/index.js
  2. 1 0
      src/api/http.js
  3. 1 0
      src/components/pwdDlg.vue
  4. 38 49
      src/utils/TimeOnPage.js
  5. 1 0
      src/views/Home.vue
  6. 5 0
      src/views/Login.vue
  7. 1 0
      src/views/resetpassword.vue

+ 2 - 2
config/index.js

@@ -37,8 +37,8 @@ module.exports = {
     assetsPublicPath: '/',
     proxyTable:{
 		'/adminapi': {
-      target: "http://8.136.199.33:7778",
-      // target: "http://192.168.20.49:8605",
+      // target: "http://8.136.199.33:7778",
+      target: "http://192.168.20.55:8606",
 			// secure:false,  // 如果是https接口,需要配置这个参数
 			changeOrigin:true, // 如果接口跨域,需要进行这个参数配置
 			pathRewrite:{

+ 1 - 0
src/api/http.js

@@ -52,6 +52,7 @@ function checkStatus(response) {
       bus.$message.error(res.Msg);
     } else if (res.Ret === 408) {
       localStorage.setItem("auth", "")
+      localStorage.setItem("loginTime", "")
       bus
         .$alert(res.Msg, "提示", {
           showClose: false,

+ 1 - 0
src/components/pwdDlg.vue

@@ -117,6 +117,7 @@ export default {
                 localStorage.setItem('AdminName',"");
                 localStorage.setItem("RoleIdentity", "");
                 localStorage.setItem("ManageType", "");
+                localStorage.setItem("loginTime", "");
                 this.$router.push({ path: "/login" });
               }, 1000);
             }

+ 38 - 49
src/utils/TimeOnPage.js

@@ -11,16 +11,13 @@ let stayTime = 0
 let intervalTimer=null
 
 export const openLoginTimer=()=>{
-  // 登录时间
-  localStorage.setItem("loginTime",new Date())
 
   clearInterval(intervalTimer)
   intervalTimer = setInterval(()=>{
-    // let inactiveTime = time || new Date()
     let ActiveTime=0
     if(localStorage.getItem('loginTime')){
       ActiveTime = (new Date()-new Date(localStorage.getItem('loginTime')))/1000
-      console.log("1分钟间隔记录时长",ActiveTime+'s');
+      console.log("间隔记录时长",ActiveTime+'s');
       recordActiveLogin({ActiveTime:Math.round(ActiveTime)}).then(res=>{
         if(res.Ret!==200) return
       })
@@ -39,7 +36,7 @@ export const recordActiveLoginFun=(time)=>{
     }).finally(()=>{
       // 清除工作
       clearInterval(intervalTimer)
-      localStorage.removeItem('loginTime')
+      // localStorage.removeItem('loginTime')
     })
   }
 }
@@ -49,6 +46,7 @@ let timer = null
 //打开计时器
 export const optionTimeCalc = ()=>{
   const IsActive = sessionStorage.getItem('IsActive')
+  // console.log(IsActive,'IsActive');
   if(Number(IsActive)===0){
     //console.log('打开计时器')
     sessionStorage.setItem('IsActive',1)
@@ -62,7 +60,6 @@ export const startCalc = ()=>{
   sessionStorage.setItem('preTitle',document.title)
   timer = setInterval(()=>{
     stayTime++
-    
     if(stayTime>=TimeInterval){
       endCalc('timeup',0)
     }
@@ -137,24 +134,25 @@ let rewriteHis = function(type){
 }
 
 export function init(){
-  const unloadT = sessionStorage.getItem('unloadT')
-  const IsActive = sessionStorage.getItem('IsActive')
-  if(unloadT){
-    //console.log('刷新该页呆了时长',unloadT)
-    sessionStorage.removeItem('unloadT')
-    sessionStorage.setItem('preTitle',document.title)
-    if(Number(IsActive)){
-      endCalc('unload',unloadT)
-    }
-  }
-  timeStr = new Date().getTime()
+  // const unloadT = sessionStorage.getItem('unloadT')
+  // console.log("init",unloadT,IsActive);
+  // if(unloadT){
+  //   //console.log('刷新该页呆了时长',unloadT)
+  //   sessionStorage.removeItem('unloadT')
+  //   sessionStorage.setItem('preTitle',document.title)
+  //   if(Number(IsActive)){
+  //     endCalc('unload',unloadT)
+  //   }
+  // }
+  // timeStr = new Date().getTime()
+  sessionStorage.setItem('IsActive',0)
   optionTimeCalc()
+  // const IsActive = sessionStorage.getItem('IsActive')
 
-  if((!localStorage.getItem("loginTime")) && localStorage.getItem('auth')){
-    // 初始化的时候 先重置最近登录时长
-    recordActiveLogin({ActiveTime:0}).then(res=>{
-      if(res.Ret!==200) return
-    })
+  // if(Number(IsActive)){
+  //   sessionStorage.setItem('unloadT',t)
+  // }
+  if(localStorage.getItem("loginTime") && localStorage.getItem('auth')){
     openLoginTimer()
   }
 }
@@ -195,10 +193,23 @@ export const doPageEventListener=function(){
   window.addEventListener('beforeunload',(e)=>{
     console.log('beforeunload')
     let t = new Date().getTime() - timeStr
+    // const unloadT = sessionStorage.getItem('unloadT')
     const IsActive = sessionStorage.getItem('IsActive')
-    if(Number(IsActive)){
-      sessionStorage.setItem('unloadT',t)
-    }
+    console.log("init",IsActive);
+    // if(unloadT){
+      //console.log('刷新该页呆了时长',unloadT)
+      // sessionStorage.removeItem('unloadT')
+      // sessionStorage.setItem('preTitle',document.title)
+      if(Number(IsActive)){
+        endCalc('unload',t)
+      }
+    // }
+    // timeStr = new Date().getTime()
+    // optionTimeCalc()
+    // const IsActive = sessionStorage.getItem('IsActive')
+    // if(Number(IsActive)){
+      // sessionStorage.setItem('unloadT',t)
+    // }
   })
 
   //刷新离开页面
@@ -207,7 +218,7 @@ export const doPageEventListener=function(){
     recordActiveLoginFun()
     // 猜测在unload这里 浏览器不会等待异步返回 清除工作在这进行
     clearInterval(intervalTimer)
-    localStorage.removeItem('loginTime')
+    // localStorage.removeItem('loginTime')
   })
 
   //popstate会在router.before/afterEach 之前触发,所以这里不对计时操作,操作放到before/afterEach
@@ -242,29 +253,6 @@ export const doPageEventListener=function(){
     optionTimeCalc()
   })
   
-  //页面监听
-  /* document.addEventListener('visibilitychange', function () {
-    // 用户离开了当前页面
-    if (document.visibilityState === 'hidden') {
-      console.log('hidden')
-      const IsActive = sessionStorage.getItem('IsActive')
-      if(Number(IsActive)){
-        let t = new Date().getTime() - timeStr
-        timeStr = new Date().getTime()
-        endCalc('hidden',t)
-      }
-    }
-  
-    // 用户打开或回到页面
-    if (document.visibilityState === 'visible') {
-      console.log('visible')
-      const IsActive = sessionStorage.getItem('IsActive')
-      if(IsActive==='0'){
-        timeStr = new Date().getTime()
-        optionTimeCalc()
-      }
-    }
-  }); */
   document.addEventListener('click',(e)=>{
 
     //console.log('click',e,e.composedPath())
@@ -274,6 +262,7 @@ export const doPageEventListener=function(){
     if(contentIndex===-1) return 
     //console.log('合法的click')
     const IsActive = sessionStorage.getItem('IsActive')
+    console.log(IsActive,'IsActive');
     if(Number(IsActive)===0){
       optionTimeCalc()
     }else{

+ 1 - 0
src/views/Home.vue

@@ -799,6 +799,7 @@ export default {
             localStorage.setItem("AdminName", "");
             localStorage.setItem("ManageType", "");
             localStorage.setItem("RoleIdentity", "");
+            localStorage.setItem("loginTime", "");
 
             that.$router.push("/login");
           }, 10);

+ 5 - 0
src/views/Login.vue

@@ -443,6 +443,11 @@ export default {
             localStorage.setItem("ManageType", res.Data.Authority);
             localStorage.setItem("AdminId", res.Data.AdminId);
             localStorage.setItem("AdminName", res.Data.AdminName);
+            localStorage.setItem("loginTime",new Date())
+            // 清除最近登录时长
+            recordActiveLogin({ActiveTime:0}).then(res=>{
+                if(res.Ret!==200) return
+            })
         },
         //根据角色判断应该进入系统的哪个页面,进入系统
         async loginSys(res){

+ 1 - 0
src/views/resetpassword.vue

@@ -183,6 +183,7 @@ export default {
                 localStorage.setItem("AdminName", "");
                 localStorage.setItem("RoleIdentity", "");
                 localStorage.setItem("ManageType", "");
+                localStorage.setItem("loginTime", "");
                 that.$router.push({ path: "/login" });
               }, 1000);
             }