From d29fd40ceff7a78bb3914e1f27b1a5a2399f8ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=B8=E7=86=9F=E5=90=B4=E5=BD=A6=E7=A5=96?= Date: Fri, 27 Feb 2026 14:43:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(App):=20=E6=B7=BB=E5=8A=A0=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E5=85=B3=E9=97=AD=E6=97=B6=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=80=80=E5=87=BA=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 mounted 钩子中监听 beforeunload 事件 - 在 beforeDestroy 钩子中移除事件监听 - 实现 handleBeforeUnload 方法处理退出逻辑 - 使用 sendBeacon 发送可靠的安全退出请求 - 检查 token 存在时才执行退出操作 - 集成现有登出 API 接口 --- src/App.vue | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/App.vue b/src/App.vue index 25e4124..0906e4a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -22,6 +22,14 @@ created () { this.versionReload() }, + mounted() { + // 监听浏览器关闭事件,自动退出登录 - rqrq + window.addEventListener('beforeunload', this.handleBeforeUnload) + }, + beforeDestroy() { + // 移除事件监听 - rqrq + window.removeEventListener('beforeunload', this.handleBeforeUnload) + }, methods: { versionReload(){ let version = this.version //版本号(每次上线前需要更新下版本号) @@ -33,6 +41,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' })) + } } }, watch: {