hbchen пре 1 година
родитељ
комит
115a8e9c03
5 измењених фајлова са 102 додато и 14 уклоњено
  1. 5 2
      src/api/api.js
  2. 7 1
      src/api/modules/oldApi.js
  3. 67 2
      src/utils/TimeOnPage.js
  4. 17 9
      src/views/Home.vue
  5. 6 0
      src/views/Login.vue

+ 5 - 2
src/api/api.js

@@ -186,7 +186,9 @@ const {
   reportMessageSend,
   weekReportValidAudio,
   getUserUuid,
-  getBusinessCode
+  getBusinessCode,
+  recordActiveLogin
+
 } = oldApis;
 
 export {
@@ -260,6 +262,7 @@ export {
   reportMessageSend,
   weekReportValidAudio,
   getUserUuid,
-  getBusinessCode
+  getBusinessCode,
+  recordActiveLogin
 };
 

+ 7 - 1
src/api/modules/oldApi.js

@@ -298,6 +298,11 @@ const getBusinessCode=params=>{
 	return http.get('/business_conf/code_encrypt',params)
 }
 
+// 获取商家code "ActiveTime":99
+const recordActiveLogin=params=>{
+	return http.post('/eta_trial/user/login_duration',params)
+}
+
 /**
  * 获取动态配配置 系统内部动态链接
  * @param {*} params 
@@ -378,5 +383,6 @@ export {
 	weekReportValidAudio,
 	getUserUuid,
 	getBusinessCode,
-	getPublicSettingsApi
+	getPublicSettingsApi,
+	recordActiveLogin
 }

+ 67 - 2
src/utils/TimeOnPage.js

@@ -1,12 +1,46 @@
 //计算账号累计活跃时长
 import global from '@/config/setting';
-import {timeOnPage} from '@/api/api.js';
+import {timeOnPage ,recordActiveLogin} from '@/api/api.js';
 //活跃时间间隔 (s)
 const TimeInterval = 60*10
 //页面停留时间 (s)
 let stayTime = 0
 
+//登录活跃时间间隔 30分钟
+const LOGIN_TIMEOUT = 30*60
 
+let loginTimer=null
+let loginStayTime=0
+
+export const openLoginTimer=()=>{
+  // 登录时间
+  localStorage.setItem("loginTime",new Date())
+  loginTimer = setInterval(()=>{
+    loginStayTime++
+    // console.log(loginStayTime);
+    if(loginStayTime>=LOGIN_TIMEOUT){
+      // 超时了,本次活跃计算结束
+      recordActiveLoginFun()
+    }
+  },1000)
+}
+
+export const recordActiveLoginFun=(time)=>{
+  let inactiveTime = time || new Date()
+  let ActiveTime=0
+  if(localStorage.getItem('loginTime')){
+    ActiveTime = (inactiveTime-new Date(localStorage.getItem('loginTime')))/1000
+    console.log("结束计算时长",ActiveTime+'s');
+    recordActiveLogin({ActiveTime:Math.round(ActiveTime)}).then(res=>{
+      if(res.Ret!==200) return
+    }).finally(()=>{
+      // 清除工作
+      loginTimer=null
+      loginStayTime=0
+      localStorage.removeItem('loginTime')
+    })
+  }
+}
 
 let timer = null
 
@@ -93,6 +127,12 @@ let rewriteHis = function(type){
 
 
 export const doPageEventListener=function(){
+
+  // 登录活跃时长 没有在记录 而且 有token 去打开计时器
+  if((!localStorage.getItem("loginTime")) && localStorage.getItem('auth')){
+    openLoginTimer()
+  }
+
   window.history.pushState = rewriteHis('pushState')
 
   window.history.replaceState = rewriteHis('replaceState')
@@ -112,16 +152,33 @@ export const doPageEventListener=function(){
     console.log('onload')
     timeStr = new Date().getTime()
     optionTimeCalc()
+
+    // 登录活跃时长 没有在记录 而且 有token 去打开计时器
+    if((!localStorage.getItem("loginTime")) && localStorage.getItem('auth')){
+      openLoginTimer()
+    }
   })
+
   //刷新离开页面
   window.addEventListener('beforeunload',(e)=>{
-    console.log('unload')
+    console.log('beforeunload')
     let t = new Date().getTime() - timeStr
     const IsActive = sessionStorage.getItem('IsActive')
     if(Number(IsActive)){
       sessionStorage.setItem('unloadT',t)
     }
   })
+
+  //刷新离开页面
+  window.addEventListener('unload',(e)=>{
+    console.log('unload')
+    recordActiveLoginFun()
+    // 猜测在unload这里 浏览器不会等待异步返回 清除工作在这进行
+    loginTimer=null
+    loginStayTime=0
+    localStorage.removeItem('loginTime')
+  })
+
   //popstate会在router.before/afterEach 之前触发,所以这里不对计时操作,操作放到before/afterEach
   window.addEventListener('popstate',(e)=>{
     //console.log('popState',e)
@@ -178,6 +235,14 @@ export const doPageEventListener=function(){
     }
   }); */
   document.addEventListener('click',(e)=>{
+
+    // 登录活跃时间
+    loginStayTime=0
+    // 超时之后的点击页面,重新计算登录活跃时间
+    if((!localStorage.getItem("loginTime")) && localStorage.getItem('auth')){
+      openLoginTimer()
+    }
+
     //console.log('click',e,e.composedPath())
     //只计算displayMain 的click
     const path = e.composedPath()

+ 17 - 9
src/views/Home.vue

@@ -317,6 +317,7 @@ import questionMsgDia from "@/components/questionMsgDia.vue";
 import questionnaireDia from "../components/questionnaireDia.vue";
 import EventBus from "@/api/bus.js";
 import global from '@/config/setting';
+import {recordActiveLoginFun} from "@/utils/TimeOnPage.js"
 
 export default {
   components: {
@@ -785,15 +786,22 @@ export default {
         type: "warning",
       })
         .then(() => {
-          localStorage.setItem("auth", "");
-          localStorage.setItem("userName", "");
-          localStorage.setItem("Role", "");
-          localStorage.setItem("RoleType", "");
-          localStorage.setItem("AdminId", "");
-          localStorage.setItem("AdminName", "");
-          localStorage.setItem("ManageType", "");
-          localStorage.setItem("RoleIdentity", "");
-          that.$router.push("/login");
+          if(this.isTrail){
+            // 结算登录计时
+            recordActiveLoginFun()
+          }
+          setTimeout(() => {
+            localStorage.setItem("auth", "");
+            localStorage.setItem("userName", "");
+            localStorage.setItem("Role", "");
+            localStorage.setItem("RoleType", "");
+            localStorage.setItem("AdminId", "");
+            localStorage.setItem("AdminName", "");
+            localStorage.setItem("ManageType", "");
+            localStorage.setItem("RoleIdentity", "");
+            that.$router.push("/login");
+          }, 0);
+
         })
         .catch(() => {});
     },

+ 6 - 0
src/views/Login.vue

@@ -73,6 +73,7 @@ import { userLogin, departInterence } from "@/api/api.js";
 import http from "@/api/http.js";
 import md5 from "@/utils/md5.js";
 import global from '@/config/setting'
+import {openLoginTimer} from "@/utils/TimeOnPage.js"
 export default {
   data() {
     return {
@@ -150,6 +151,11 @@ export default {
               localStorage.setItem("ManageType", res.Data.Authority);
               localStorage.setItem("AdminId", res.Data.AdminId);
               localStorage.setItem("AdminName", res.Data.AdminName);
+							// 打开登录计时
+							if(this.isTrail){
+								openLoginTimer()
+							}
+							
               if (this.checked) {
                 localStorage.setItem(
                   "account",