无感刷新通知消息内容
parent
96754b1500
commit
f998cbdaa2
|
|
@ -36,6 +36,8 @@ export default {
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
notificationInstance: null, // 用于存储当前的通知实例
|
||||||
|
showNotification: false, // 控制是否显示通知
|
||||||
lineChartData: lineChartData.newVisitis
|
lineChartData: lineChartData.newVisitis
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -52,6 +54,10 @@ export default {
|
||||||
this.websock = new WebSocket(wsUri)
|
this.websock = new WebSocket(wsUri)
|
||||||
this.websock.onerror = this.webSocketOnError
|
this.websock.onerror = this.webSocketOnError
|
||||||
this.websock.onmessage = this.webSocketOnMessage
|
this.websock.onmessage = this.webSocketOnMessage
|
||||||
|
//5秒轮询一次
|
||||||
|
this.pollingTimer = setInterval(() => {
|
||||||
|
stockUrl.stockMsg()
|
||||||
|
}, 5000);
|
||||||
},
|
},
|
||||||
webSocketOnError(e) {
|
webSocketOnError(e) {
|
||||||
this.$notify({
|
this.$notify({
|
||||||
|
|
@ -61,15 +67,8 @@ export default {
|
||||||
})
|
})
|
||||||
}, webSocketOnMessage(e) {
|
}, webSocketOnMessage(e) {
|
||||||
const data = JSON.parse(e.data)
|
const data = JSON.parse(e.data)
|
||||||
console.log(data)
|
|
||||||
if (data.msgType === 'INFO') {
|
if (data.msgType === 'INFO') {
|
||||||
this.$notify({
|
this.createOrUpdateNotification(data);
|
||||||
title: '点击关注公众号',
|
|
||||||
message: data.msg,
|
|
||||||
type: 'success',
|
|
||||||
offset: 100,
|
|
||||||
duration: 0
|
|
||||||
});
|
|
||||||
} else if (data.msgType === 'ERROR') {
|
} else if (data.msgType === 'ERROR') {
|
||||||
this.$notify({
|
this.$notify({
|
||||||
title: '',
|
title: '',
|
||||||
|
|
@ -83,7 +82,48 @@ export default {
|
||||||
webSocketSend(agentData) {
|
webSocketSend(agentData) {
|
||||||
this.websock.send(agentData)
|
this.websock.send(agentData)
|
||||||
},
|
},
|
||||||
|
createOrUpdateNotification(data) {
|
||||||
|
// 更新或创建通知
|
||||||
|
if (this.showNotification && this.notificationInstance) {
|
||||||
|
this.updateNotification(data);
|
||||||
|
} else {
|
||||||
|
this.createNotification(data);
|
||||||
|
}
|
||||||
|
this.showNotification = true;
|
||||||
|
},
|
||||||
|
|
||||||
|
updateNotification(data) {
|
||||||
|
// 尝试更新通知内容
|
||||||
|
try {
|
||||||
|
this.notificationInstance.title = '点击关注公众号';
|
||||||
|
this.notificationInstance.message = data.msg;
|
||||||
|
} catch (error) {
|
||||||
|
// 如果更新失败,关闭当前通知并重新创建
|
||||||
|
this.createNotification(data);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
createNotification(data) {
|
||||||
|
// 创建一个新的通知实例
|
||||||
|
this.notificationInstance = this.$notify({
|
||||||
|
title: '点击关注公众号',
|
||||||
|
message: data.msg,
|
||||||
|
type: 'success',
|
||||||
|
offset: 100,
|
||||||
|
duration: 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
beforeDestroy() {
|
||||||
|
if (this.pollingTimer) {
|
||||||
|
clearInterval(this.pollingTimer);
|
||||||
|
}
|
||||||
|
if (this.websock && this.websock.readyState === WebSocket.OPEN) {
|
||||||
|
this.websock.close();
|
||||||
|
}
|
||||||
|
if (this.notificationInstance) {
|
||||||
|
this.notificationInstance.close(); // 关闭通知
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue