diff --git a/src/App.vue b/src/App.vue index 6557431..1a12627 100644 --- a/src/App.vue +++ b/src/App.vue @@ -17,6 +17,14 @@ created () { this.versionReload() }, + mounted() { + // 监听浏览器关闭事件,自动退出登录 - rqrq + window.addEventListener('beforeunload', this.handleBeforeUnload) + }, + beforeDestroy() { + // 移除事件监听 - rqrq + window.removeEventListener('beforeunload', this.handleBeforeUnload) + }, methods: { versionReload(){ let version = this.version //版本号(每次上线前需要更新下版本号) @@ -28,6 +36,15 @@ this.version=versionLocal; location.reload(); } + }, + // 浏览器关闭时自动退出登录 - rqrq + handleBeforeUnload() { + const token = this.$cookie.get('token') + if (token && token.trim()) { + // 使用 sendBeacon 发送退出请求,可靠性高 - rqrq + const url = this.$http.adornUrl('/sys/logout') + navigator.sendBeacon(url, new Blob([JSON.stringify({ token })], { type: 'application/json' })) + } } } }