From b0be4002d4f293e9ddb80c438c67abc531e8d9f1 Mon Sep 17 00:00:00 2001 From: ywyonui Date: Mon, 21 Jul 2025 15:44:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=BB=98=E8=AE=A4websocket?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/base/course-selection/pay-wait.vue | 6 ++--- src/pages/base/course-selection/payment.vue | 10 ++++--- src/store/modules/user.ts | 28 ++++++++++++-------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/pages/base/course-selection/pay-wait.vue b/src/pages/base/course-selection/pay-wait.vue index ead0a95..33080f9 100644 --- a/src/pages/base/course-selection/pay-wait.vue +++ b/src/pages/base/course-selection/pay-wait.vue @@ -13,7 +13,7 @@ - + @@ -30,7 +30,7 @@ import { onLoad } from "@dcloudio/uni-app"; import { jzGetQkExpiredTime, jzXkCancelApi } from "@/api/base/server"; import { useUserStore } from "@/store/modules/user"; import { useDataStore } from "@/store/modules/data"; -const { getCurXs, getUser, setWsCallback } = useUserStore(); +const { getCurXs, getUser, initWs, setWsCallback } = useUserStore(); const { getData } = useDataStore(); const payUrl = ref(""); @@ -115,6 +115,7 @@ onLoad(async (options: any) => { payUrl.value = decodeURIComponent(options.payUrl); const res = await jzGetQkExpiredTime({ xsId: getCurXs.id} ); seconds = res.result; + initWs(); startCountdown(); } else { uni.showToast({ title: '缺少支付地址', icon: 'none' }) @@ -144,7 +145,6 @@ onBeforeUnmount(() => { // 可滚动内容区域样式 .scrollable-content { flex: 1; - display: flex; position: relative; } diff --git a/src/pages/base/course-selection/payment.vue b/src/pages/base/course-selection/payment.vue index 19f976e..7c3d663 100644 --- a/src/pages/base/course-selection/payment.vue +++ b/src/pages/base/course-selection/payment.vue @@ -137,10 +137,14 @@ const payNow = async () => { } } catch (error) { console.log(error); - const url = "https://www.baidu.com"; - uni.redirectTo({ - url: `/pages/base/course-selection/pay-wait?payUrl=${encodeURIComponent(url)}` + uni.showToast({ + title: "发起支付失败", + icon: "error", }); + // const url = "https://www.baidu.com"; + // uni.redirectTo({ + // url: `/pages/base/course-selection/pay-wait?payUrl=${encodeURIComponent(url)}` + // }); } }; diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index ca9d3f6..90a1b0e 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -4,6 +4,10 @@ import { AUTH_KEY } from "@/config"; import { imagUrl } from "@/utils"; import { useWebSocket } from '@/utils/webSocket/webSocket' +const defWsCallback = (type: string, data: any) => { + console.log('接收到 WebSocket 消息, 默认处理函数:', type, data); +} + interface UserState { userdata: any; curXs: any; @@ -25,7 +29,7 @@ export const useUserStore = defineStore({ //用户注册信息 auth: [], ws: null, - wsCallback: (type: string, res: any) => {} + wsCallback: defWsCallback }), getters: { getToken(): string { @@ -57,6 +61,17 @@ export const useUserStore = defineStore({ setWsCallback(callback: any) { this.wsCallback = callback; }, + initWs() { + if (!this.ws) { + this.ws = useWebSocket(`/zhxy/webSocket/${this.userdata.userId}`, (type: string, res: any) => { + // 判断this.wsCallback是函数,调用 + if (typeof this.wsCallback === "function") { + this.wsCallback(type, res); + } + }); + this.ws.reconnect(); + } + }, /** * @description: 验证码登录 */ @@ -107,15 +122,6 @@ export const useUserStore = defineStore({ if (value[AUTH_KEY]) { this.setToken(value[AUTH_KEY]) } - if (!this.ws) { - this.ws = useWebSocket(`/zhxy/webSocket/${value.userId}`, (type: string, res: any) => { - // 判断this.wsCallback是函数,调用 - if (typeof this.wsCallback === "function") { - this.wsCallback(type, res); - } - }); - this.ws.reconnect(); - } authenticationApi({ userId: value.userId }).then(({ result }) => { if (result) { this.setAuth(result) @@ -134,7 +140,7 @@ export const useUserStore = defineStore({ this.ws.closeConnect(); this.ws = null; } - this.wsCallback((type: string, res: any) => {}); + this.wsCallback = defWsCallback; }, }, persist: {