调整websocket响应

This commit is contained in:
ywyonui 2025-07-01 20:54:38 +08:00
parent 70004f842c
commit cb258688c7
2 changed files with 10 additions and 5 deletions

View File

@ -11,13 +11,16 @@ import { useUserStore } from "@/store/modules/user";
const { getUser } = useUserStore();
const payUrl = ref("");
const ws = useWebSocket(`/zhxy/webSocket/${getUser.userId}`, (type: string, data: any) => {
console.log('收到WebSocket消息:', type, data);
if (type === 'pay') {
const ws = useWebSocket(`/zhxy/webSocket/${getUser.userId}`, (type: string, res: any) => {
console.log('收到WebSocket消息:', type, res.data);
// data
const dataObj = JSON.parse(res.data);
if (dataObj.action === 'pay') {
uni.showToast({
title: '支付成功',
icon: 'success',
})
});
ws.closeConnect();
//
setTimeout(() => {
uni.reLaunch({
@ -38,7 +41,7 @@ onLoad((options: any) => {
}
});
onUnmounted(() => {
onBeforeUnmount(() => {
ws.closeConnect();
});
</script>

View File

@ -70,6 +70,7 @@ export function useWebSocket(url: string, callback?: Function, OBJECT?: UniNames
}
function closeConnect() {
console.log('断开WebSocket连接');
// uni-app 提供的关闭方法(如果支持)
if (typeof uni.closeSocket === 'function') {
uni.closeSocket({
@ -82,6 +83,7 @@ export function useWebSocket(url: string, callback?: Function, OBJECT?: UniNames
//监听WebSocket接受到服务器的消息事件。
uni.onSocketMessage(function (res) {
console.log('收到服务器内容:' + res.data);
if (callback) {
callback('data', res)
}