调整websocket响应
This commit is contained in:
parent
70004f842c
commit
cb258688c7
@ -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>
|
||||||
|
|||||||
@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user