|
|
|
@ -2,6 +2,7 @@ |
|
|
|
import Member from "@/api/member"; |
|
|
|
import { mapActions } from "vuex"; |
|
|
|
import app from "@/app.js" |
|
|
|
import Home from "@/api/home"; |
|
|
|
export default { |
|
|
|
onLaunch: function () { |
|
|
|
setInterval(() => { |
|
|
|
@ -26,6 +27,34 @@ export default { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
sendHeartbeat() { |
|
|
|
Home.online().then((response) => { |
|
|
|
console.log('心跳机制'); |
|
|
|
}).catch((err) => {}); |
|
|
|
}, |
|
|
|
startHeartbeat() { |
|
|
|
if (!this.heartbeatInterval && this.isLogin) { |
|
|
|
this.heartbeatInterval = setInterval(this.sendHeartbeat, 100000); // 每3秒发送一次心跳 |
|
|
|
console.log('心跳机制已启动'); |
|
|
|
} |
|
|
|
}, |
|
|
|
stopHeartbeat() { |
|
|
|
if (this.heartbeatInterval) { |
|
|
|
clearInterval(this.heartbeatInterval); |
|
|
|
this.heartbeatInterval = null; |
|
|
|
console.log('心跳机制已停止'); |
|
|
|
} |
|
|
|
}, |
|
|
|
handleVisibilityChange() { |
|
|
|
if (document.visibilityState === 'visible') { |
|
|
|
// 页面可见,启动心跳机制 |
|
|
|
this.startHeartbeat(); |
|
|
|
} else { |
|
|
|
// 页面不可见,停止心跳机制 |
|
|
|
this.stopHeartbeat(); |
|
|
|
} |
|
|
|
} |
|
|
|
// update(){ |
|
|
|
// var baseUrl=app.baseUrl + '/api/app/getNewestVersion' |
|
|
|
// console.log(baseUrl) |
|
|
|
@ -128,7 +157,23 @@ export default { |
|
|
|
uni.$emit("appShow"); |
|
|
|
this.$navFontColor(); |
|
|
|
}, |
|
|
|
computed:{ |
|
|
|
isLogin() { |
|
|
|
return Boolean(uni.getStorageSync("token")); |
|
|
|
}, |
|
|
|
}, |
|
|
|
beforeDestroy() { |
|
|
|
// 清理事件监听器和心跳请求 |
|
|
|
document.removeEventListener('visibilitychange', this.handleVisibilityChange); |
|
|
|
this.stopHeartbeat(); |
|
|
|
}, |
|
|
|
onHide: function () {}, |
|
|
|
mounted() { |
|
|
|
// 监听页面可见性变化 |
|
|
|
document.addEventListener('visibilitychange', this.handleVisibilityChange); |
|
|
|
// 启动心跳机制 |
|
|
|
this.startHeartbeat(); |
|
|
|
}, |
|
|
|
}; |
|
|
|
</script> |
|
|
|
|
|
|
|
|