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

View File

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