From 9e0a7ac75e07ebbaacaf8db3fddd10c562b4bb8e 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:42:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=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 验证确保只对已登录用户执行退出操作 - 集成现有登出接口完成自动退出流程 --- src/App.vue | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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' })) + } } } }