feat(登录): 关闭浏览器之前执行登出

This commit is contained in:
fit2cloud-chenyw 2023-03-13 16:53:43 +08:00
parent 3232c2b934
commit d8f84426aa
3 changed files with 24 additions and 9 deletions

View File

@ -239,6 +239,9 @@ export default {
}, },
mounted() { mounted() {
window.addEventListener('beforeunload', (e) => this.beforeunloadHandler(e))
window.addEventListener('unload', (e) => this.unloadHandler(e))
this.initCurrentRoutes() this.initCurrentRoutes()
bus.$on('set-top-menu-info', this.setTopMenuInfo) bus.$on('set-top-menu-info', this.setTopMenuInfo)
bus.$on('set-top-menu-active-info', this.setTopMenuActiveInfo) bus.$on('set-top-menu-active-info', this.setTopMenuActiveInfo)
@ -251,6 +254,9 @@ export default {
}) })
}, },
beforeDestroy() { beforeDestroy() {
window.removeEventListener('beforeunload', (e) => this.beforeunloadHandler(e))
window.removeEventListener('unload', (e) => this.unloadHandler(e))
bus.$off('set-top-menu-info', this.setTopMenuInfo) bus.$off('set-top-menu-info', this.setTopMenuInfo)
bus.$off('set-top-menu-active-info', this.setTopMenuActiveInfo) bus.$off('set-top-menu-active-info', this.setTopMenuActiveInfo)
bus.$off('set-top-text-info', this.setTopTextInfo) bus.$off('set-top-text-info', this.setTopTextInfo)
@ -269,6 +275,16 @@ export default {
}) })
}, },
methods: { methods: {
beforeunloadHandler() {
this.beforeUnload_time = new Date().getTime()
},
unloadHandler(e) {
this.gap_time = new Date().getTime() - this.beforeUnload_time
if (this.gap_time <= 5) {
this.logout().then(res => {})
}
},
// store // store
initCurrentRoutes() { initCurrentRoutes() {
const { const {

View File

@ -1,10 +1,10 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
import i18n from '@/lang' import i18n from '@/lang'
import { $error, $confirm } from '@/utils/message' import { $error, $confirm } from '@/utils/message'
import {seizeLogin} from '@/api/user' import { seizeLogin } from '@/api/user'
import router from '@/router' import router from '@/router'
import store from "@/store"; import store from '@/store'
import { Loading } from 'element-ui'; import { Loading } from 'element-ui'
export function timeSection(date, type, labelFormat = 'yyyy-MM-dd') { export function timeSection(date, type, labelFormat = 'yyyy-MM-dd') {
if (!date) { if (!date) {
return null return null
@ -367,7 +367,7 @@ export const showMultiLoginMsg = () => {
const content = infos.map(info => buildMultiLoginErrorItem(info)).join('</br>') const content = infos.map(info => buildMultiLoginErrorItem(info)).join('</br>')
let msgContent = '<strong>' + i18n.t('multi_login_lang.title') + '</strong>' let msgContent = '<strong>' + i18n.t('multi_login_lang.title') + '</strong>'
msgContent += content + '<p>' + i18n.t('multi_login_lang.label') + '</p>' msgContent += content + '<p>' + i18n.t('multi_login_lang.label') + '</p>'
$error(msgContent, 10000, true); $error(msgContent, 10000, true)
} }
const multiLoginError2 = Cookies.get('MultiLoginError2') const multiLoginError2 = Cookies.get('MultiLoginError2')
if (multiLoginError2) { if (multiLoginError2) {
@ -378,11 +378,11 @@ export const showMultiLoginMsg = () => {
msgContent += content + '<p>' + i18n.t('multi_login_lang.confirm') + '</p>' msgContent += content + '<p>' + i18n.t('multi_login_lang.confirm') + '</p>'
$confirm(msgContent, () => seize(infos[0]), { $confirm(msgContent, () => seize(infos[0]), {
dangerouslyUseHTMLString: true dangerouslyUseHTMLString: true
}); })
} }
} }
const seize = model => { const seize = model => {
let loadingInstance = Loading.service({}); const loadingInstance = Loading.service({})
const token = model.token const token = model.token
const param = { const param = {
token token
@ -391,7 +391,7 @@ const seize = model => {
const resultToken = res.data.token const resultToken = res.data.token
store.dispatch('user/refreshToken', resultToken) store.dispatch('user/refreshToken', resultToken)
router.push('/') router.push('/')
loadingInstance.close(); loadingInstance.close()
}) })
} }
const buildMultiLoginErrorItem = (info) => { const buildMultiLoginErrorItem = (info) => {

View File

@ -30,8 +30,7 @@ module.exports = {
overlay: { overlay: {
warnings: false, warnings: false,
errors: true errors: true
}, }
before: require('./mock/mock-server.js')
}, },
pages: { pages: {