Browse Source

feat(auth): 添加浏览器关闭时自动退出登录功能

- 在 mounted 钩子中添加 beforeunload 事件监听
- 在 beforeDestroy 钩子中移除事件监听避免内存泄漏
- 实现 handleBeforeUnload 方法检测登录状态
- 使用 sendBeacon 发送可靠的退出登录请求
- 添加 token 验证确保只对已登录用户执行退出操作
- 集成现有登出接口完成自动退出流程
master
常熟吴彦祖 3 days ago
parent
commit
9e0a7ac75e
  1. 17
      src/App.vue

17
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' }))
}
}
}
}

Loading…
Cancel
Save