|
@@ -1,6 +1,6 @@
|
|
|
//计算账号累计活跃时长
|
|
|
-import global from '@/config/setting';
|
|
|
-import {timeOnPage ,recordActiveLogin} from '@/api/api.js';
|
|
|
+import setting from '@/mixins/theme.js'
|
|
|
+import {timeOnPage , recordActiveLogin} from '@/api/api.js';
|
|
|
//活跃时间间隔 (s)
|
|
|
const TimeInterval = 60*10
|
|
|
//页面停留时间 (s)
|
|
@@ -9,7 +9,6 @@ let stayTime = 0
|
|
|
|
|
|
//活跃时间记录计时器 1分钟记录一次
|
|
|
let intervalTimer=null
|
|
|
-let loginStayTime=0
|
|
|
|
|
|
export const openLoginTimer=()=>{
|
|
|
// 登录时间
|
|
@@ -40,7 +39,6 @@ export const recordActiveLoginFun=(time)=>{
|
|
|
}).finally(()=>{
|
|
|
// 清除工作
|
|
|
clearInterval(intervalTimer)
|
|
|
- loginStayTime=0
|
|
|
localStorage.removeItem('loginTime')
|
|
|
})
|
|
|
}
|
|
@@ -87,6 +85,15 @@ export const endCalc = (type,intervalT)=>{
|
|
|
sessionStorage.setItem('IsActive',0)
|
|
|
}
|
|
|
|
|
|
+// 退出登录时的结算
|
|
|
+export const loginEndCalc=()=>{
|
|
|
+ let t = new Date().getTime() - timeStr
|
|
|
+ //console.log('待了时长replacestate:'+ t)
|
|
|
+ const IsActive = sessionStorage.getItem('IsActive')
|
|
|
+ if(Number(IsActive)){
|
|
|
+ endCalc('logout',t)
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
//发送活跃时长数据
|
|
|
export const sendTOPInfo = (type,intervalT)=>{
|
|
@@ -102,7 +109,7 @@ export const sendTOPInfo = (type,intervalT)=>{
|
|
|
const Part = Title.split('-').length>1?Title.split('-')[1]:Title
|
|
|
const ActiveTime = type==='timeup'?stayTime:Number(intervalT)/1000
|
|
|
//发送数据
|
|
|
- if(!Part.length||Part==global.name) return
|
|
|
+ if(!Part.length||Part==setting.name) return
|
|
|
if(ActiveTime>TimeInterval*12) return
|
|
|
console.log("ActiveTime:",ActiveTime,"Part:",Part)
|
|
|
timeOnPage({
|
|
@@ -129,40 +136,61 @@ 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()
|
|
|
+ optionTimeCalc()
|
|
|
|
|
|
-export const doPageEventListener=function(){
|
|
|
-
|
|
|
- // 登录活跃时长 没有在记录 而且 有token 去打开计时器
|
|
|
if((!localStorage.getItem("loginTime")) && localStorage.getItem('auth')){
|
|
|
+ // 初始化的时候 先重置最近登录时长
|
|
|
+ recordActiveLogin({ActiveTime:0}).then(res=>{
|
|
|
+ if(res.Ret!==200) return
|
|
|
+ })
|
|
|
openLoginTimer()
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+export const doPageEventListener=function(){
|
|
|
+
|
|
|
+ // init() 放在router.afterEach中,不然一开始拿不到正确的页面标题
|
|
|
|
|
|
window.history.pushState = rewriteHis('pushState')
|
|
|
|
|
|
window.history.replaceState = rewriteHis('replaceState')
|
|
|
-
|
|
|
- //刷新加载完成
|
|
|
- window.addEventListener('load',(e)=>{
|
|
|
- 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)
|
|
|
- }
|
|
|
- }
|
|
|
- console.log('onload')
|
|
|
- timeStr = new Date().getTime()
|
|
|
- optionTimeCalc()
|
|
|
-
|
|
|
- // 登录活跃时长 没有在记录 而且 有token 去打开计时器
|
|
|
- if((!localStorage.getItem("loginTime")) && localStorage.getItem('auth')){
|
|
|
- openLoginTimer()
|
|
|
- }
|
|
|
- })
|
|
|
|
|
|
+ //刷新加载完成
|
|
|
+ // 因为监听改位置了,每次路由刷新时,由于需要等待接口返回,需要先判断是否有 权限才能监听,这时候onload已经执行了,根本监听不到
|
|
|
+ // window.addEventListener('load',(e)=>{
|
|
|
+ // 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()
|
|
|
+ // optionTimeCalc()
|
|
|
+
|
|
|
+ // if((!localStorage.getItem("loginTime")) && localStorage.getItem('auth')){
|
|
|
+ // // 初始化的时候 先重置最近登录时长
|
|
|
+ // recordActiveLogin({ActiveTime:0}).then(res=>{
|
|
|
+ // if(res.Ret!==200) return
|
|
|
+ // })
|
|
|
+ // openLoginTimer()
|
|
|
+ // }
|
|
|
+ // })
|
|
|
//刷新离开页面
|
|
|
window.addEventListener('beforeunload',(e)=>{
|
|
|
console.log('beforeunload')
|
|
@@ -179,7 +207,6 @@ export const doPageEventListener=function(){
|
|
|
recordActiveLoginFun()
|
|
|
// 猜测在unload这里 浏览器不会等待异步返回 清除工作在这进行
|
|
|
clearInterval(intervalTimer)
|
|
|
- loginStayTime=0
|
|
|
localStorage.removeItem('loginTime')
|
|
|
})
|
|
|
|
|
@@ -240,13 +267,6 @@ 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()
|