全局通知

main
HUOJIN\92525 2024-09-18 09:46:12 +08:00
parent 0cc507fbc5
commit 17d2901584
3 changed files with 93 additions and 100 deletions

View File

@ -5,6 +5,93 @@
</template>
<script>
export default {
name: 'App'
name: 'App',
data() {
return {
notificationInstance: null, //
showNotification: false, //
}
},
mounted() {
this.initWebSocket();
}, methods: {
initWebSocket() {
const wsUri = (process.env.VUE_APP_WS_API === '/' ? '/' : (process.env.VUE_APP_WS_API + '/')) + 'webSocket/stock'
this.websock = new WebSocket(wsUri)
this.websock.onerror = this.webSocketOnError
this.websock.onmessage = this.webSocketOnMessage
this.pollMsg()
},
webSocketOnError(e) {
this.$notify({
title: 'WebSocket连接发生错误',
type: 'error',
duration: 0
})
}, webSocketOnMessage(e) {
const data = JSON.parse(e.data)
if (data.msgType === 'INFO') {
this.createOrUpdateNotification(data);
} else if (data.msgType === 'ERROR') {
this.$notify({
title: '',
message: data.msg,
dangerouslyUseHTMLString: true,
type: 'error',
duration: 0
})
}
},
webSocketSend(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,
onClose: this.onNotificationClose //
});
},
onNotificationClose() {
this.showNotification = false;
this.notificationInstance = null;
}
},
beforeDestroy() {
if (this.websock && this.websock.readyState === WebSocket.OPEN) {
this.websock.close();
}
if (this.notificationInstance) {
this.notificationInstance.close(); //
this.showNotification = false;
this.notificationInstance = null;
}
}
}
</script>

View File

@ -75,8 +75,8 @@ export function containerOut(data) {
export function stockMsg(data) {
return request({
url: 'api/stock/stockMsg',
method: 'post',
data
method: 'get',
params: {data}
})
}

View File

@ -36,107 +36,13 @@ export default {
},
data() {
return {
notificationInstance: null, //
showNotification: false, //
lineChartData: lineChartData.newVisitis
}
},
mounted() {
this.initWebSocket();
},
methods: {
handleSetLineChartData(type) {
this.lineChartData = lineChartData[type]
},
async pollMsg() {
//3
this.pollingTimer = setInterval(async () => {
const data = {msg: '友仓智慧物流,软件定义物流。', msgType: 'INFO'}
console.log(data)
this.createOrUpdateNotification(data)
}, 3000);
},
initWebSocket() {
stockUrl.stockMsg();
const wsUri = (process.env.VUE_APP_WS_API === '/' ? '/' : (process.env.VUE_APP_WS_API + '/')) + 'webSocket/stock'
this.websock = new WebSocket(wsUri)
this.websock.onerror = this.webSocketOnError
this.websock.onmessage = this.webSocketOnMessage
this.pollMsg()
},
webSocketOnError(e) {
this.$notify({
title: 'WebSocket连接发生错误',
type: 'error',
duration: 0
})
}, webSocketOnMessage(e) {
const data = JSON.parse(e.data)
if (data.msgType === 'INFO') {
this.createOrUpdateNotification(data);
} else if (data.msgType === 'ERROR') {
this.$notify({
title: '',
message: data.msg,
dangerouslyUseHTMLString: true,
type: 'error',
duration: 0
})
}
},
webSocketSend(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,
onClose: this.onNotificationClose //
});
},
onNotificationClose() {
this.showNotification = false;
this.notificationInstance = null;
}
},
beforeDestroy() {
if (this.pollingTimer) {
clearInterval(this.pollingTimer);
}
if (this.websock && this.websock.readyState === WebSocket.OPEN) {
this.websock.close();
}
if (this.notificationInstance) {
console.log(111)
this.notificationInstance.close(); //
this.showNotification = false;
this.notificationInstance = null;
}
}
}
</script>