beforeunload和unload :刷新页面或者关闭卸载页面时都执行
ios要做单独做处理,使用pagehide
mounted(){
if (getUserAgent() === 'IOS') {
window.addEventListener('pagehide', e => this.unloadHandler(e))
} else {
window.addEventListener('unload', e => this.unloadHandler(e))
}
}
destroyed() {
if (getUserAgent() === 'IOS') {
window.removeEventListener('pagehide', e => this.unloadHandler(e))
} else {
window.removeEventListener('unload', e => this.unloadHandler(e))
}
}
unloadHandler(e){
this.commitTryWatchTime()
}
// 上报试看时长
async commitTryWatchTime() {
const params = {}
params.sign = getSignStr(params)
const formdata = new FormData()
for (const k in params) {
formdata.append(k, params[k])
}
const url = process.env.VUE_AP