调整默认websocket处理

This commit is contained in:
ywyonui 2025-07-21 15:44:03 +08:00
parent ee7de58ba4
commit b0be4002d4
3 changed files with 27 additions and 17 deletions

View File

@ -13,7 +13,7 @@
</view> </view>
<view class="scrollable-content"> <view class="scrollable-content">
<web-view :src="payUrl" style="flex: 1;"></web-view> <web-view :src="payUrl"></web-view>
</view> </view>
<!-- 底部支付区域 --> <!-- 底部支付区域 -->
@ -30,7 +30,7 @@ import { onLoad } from "@dcloudio/uni-app";
import { jzGetQkExpiredTime, jzXkCancelApi } from "@/api/base/server"; import { jzGetQkExpiredTime, jzXkCancelApi } from "@/api/base/server";
import { useUserStore } from "@/store/modules/user"; import { useUserStore } from "@/store/modules/user";
import { useDataStore } from "@/store/modules/data"; import { useDataStore } from "@/store/modules/data";
const { getCurXs, getUser, setWsCallback } = useUserStore(); const { getCurXs, getUser, initWs, setWsCallback } = useUserStore();
const { getData } = useDataStore(); const { getData } = useDataStore();
const payUrl = ref(""); const payUrl = ref("");
@ -115,6 +115,7 @@ onLoad(async (options: any) => {
payUrl.value = decodeURIComponent(options.payUrl); payUrl.value = decodeURIComponent(options.payUrl);
const res = await jzGetQkExpiredTime({ xsId: getCurXs.id} ); const res = await jzGetQkExpiredTime({ xsId: getCurXs.id} );
seconds = res.result; seconds = res.result;
initWs();
startCountdown(); startCountdown();
} else { } else {
uni.showToast({ title: '缺少支付地址', icon: 'none' }) uni.showToast({ title: '缺少支付地址', icon: 'none' })
@ -144,7 +145,6 @@ onBeforeUnmount(() => {
// //
.scrollable-content { .scrollable-content {
flex: 1; flex: 1;
display: flex;
position: relative; position: relative;
} }

View File

@ -137,10 +137,14 @@ const payNow = async () => {
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);
const url = "https://www.baidu.com"; uni.showToast({
uni.redirectTo({ title: "发起支付失败",
url: `/pages/base/course-selection/pay-wait?payUrl=${encodeURIComponent(url)}` icon: "error",
}); });
// const url = "https://www.baidu.com";
// uni.redirectTo({
// url: `/pages/base/course-selection/pay-wait?payUrl=${encodeURIComponent(url)}`
// });
} }
}; };

View File

@ -4,6 +4,10 @@ import { AUTH_KEY } from "@/config";
import { imagUrl } from "@/utils"; import { imagUrl } from "@/utils";
import { useWebSocket } from '@/utils/webSocket/webSocket' import { useWebSocket } from '@/utils/webSocket/webSocket'
const defWsCallback = (type: string, data: any) => {
console.log('接收到 WebSocket 消息, 默认处理函数:', type, data);
}
interface UserState { interface UserState {
userdata: any; userdata: any;
curXs: any; curXs: any;
@ -25,7 +29,7 @@ export const useUserStore = defineStore({
//用户注册信息 //用户注册信息
auth: [], auth: [],
ws: null, ws: null,
wsCallback: (type: string, res: any) => {} wsCallback: defWsCallback
}), }),
getters: { getters: {
getToken(): string { getToken(): string {
@ -57,6 +61,17 @@ export const useUserStore = defineStore({
setWsCallback(callback: any) { setWsCallback(callback: any) {
this.wsCallback = callback; 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: * @description:
*/ */
@ -107,15 +122,6 @@ export const useUserStore = defineStore({
if (value[AUTH_KEY]) { if (value[AUTH_KEY]) {
this.setToken(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 }) => { authenticationApi({ userId: value.userId }).then(({ result }) => {
if (result) { if (result) {
this.setAuth(result) this.setAuth(result)
@ -134,7 +140,7 @@ export const useUserStore = defineStore({
this.ws.closeConnect(); this.ws.closeConnect();
this.ws = null; this.ws = null;
} }
this.wsCallback((type: string, res: any) => {}); this.wsCallback = defWsCallback;
}, },
}, },
persist: { persist: {