|
@@ -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()
|