调整默认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 class="scrollable-content">
<web-view :src="payUrl" style="flex: 1;"></web-view>
<web-view :src="payUrl"></web-view>
</view>
<!-- 底部支付区域 -->
@ -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;
}

View File

@ -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)}`
// });
}
};

View File

@ -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: {