背景:前端通过链接跳转到中间页 然后通过中间页调用企微api打开会话,打开会话后,点击左上角返回按钮是回到了中间页,没有回到一开始点击链接的企微页面。前端用的vue。
一、在路由进入页面之前做下判断监听、回退的时候关闭页面
beforeRouteEnter(to, from, next) {
next(vm => {
vm.initAddEvent()
})
}
然后在methods中定义initAddEventh函数写对应的逻辑
initAddEvent() {
var hiddenProperty = 'hidden' in document ? 'hidden'
: 'webkitHidden' in document ? 'webkitHidden'
: 'mozHidden' in document ? 'mozHidden'
: null
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange')
var onVisibilityChange = function() {
if (!document[hiddenProperty]) {
const userAgent = navigator.userAgent
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
wx.closeWindow()
} else { // 安卓
wx.closeWindow()
}
console.log('页面---激活1')
} else {
console.log('页面---非激活')
}
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange)
}