From 503b33ba3b9a95d9f392324408b767217d224503 Mon Sep 17 00:00:00 2001 From: LiJianZhao Date: Fri, 9 May 2025 16:46:38 +0800 Subject: [PATCH] dev --- src/components/registration/CardDefault.vue | 9 +++++---- src/stores/wsStore.ts | 20 +++++++++++++++++++- src/utils/ws.ts | 13 +++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/components/registration/CardDefault.vue b/src/components/registration/CardDefault.vue index ea51565..aa93e1b 100644 --- a/src/components/registration/CardDefault.vue +++ b/src/components/registration/CardDefault.vue @@ -9,7 +9,6 @@ const wsStore = useWsStore(); const isReading = ref(false) const socialCard: any = defineModel(); const ReadSocialCard = async (readType: string) => { - socialCard.value.lastUse = "CardDefault" emit("changeLoading", true) isReading.value = true; let config_db: any = await post('common/config/getall'); @@ -35,9 +34,6 @@ const ReadSocialCard = async (readType: string) => { }; const reciceMessage = (response: Response) => { - if (socialCard.value.lastUse != "CardDefault") { - return; - } if (response.Code == 301) { let msg = response.Message; ElMessage({ @@ -63,6 +59,9 @@ const reciceMessage = (response: Response) => { } getInfoFor1101(params) } +const errorCallback = () => { + emit('changeLoading', false) +} const emit = defineEmits(['socialCardUpdate', 'close', 'changeLoading']) const getInfoFor1101 = (params: any) => { @@ -97,10 +96,12 @@ const getInfoFor1101 = (params: any) => { onMounted(async () => { wsStore.setMessageCallback(reciceMessage) + wsStore.setErrorCallback(errorCallback) }); onUnmounted(() => { emit('changeLoading', false) wsStore.removeAllMessageCallback() + wsStore.removeAllErrorCallback() }) const close = () => { diff --git a/src/stores/wsStore.ts b/src/stores/wsStore.ts index 2d42bda..5c45934 100644 --- a/src/stores/wsStore.ts +++ b/src/stores/wsStore.ts @@ -1,5 +1,15 @@ import { defineStore } from 'pinia'; -import { wsInit, wsSend, wsSetMessageCallback,wsRemoveMessageCallback,wsRemoveAllMessageCallback,wsClose, type Response, type Request } from '@/utils/ws'; +import { + wsInit, + wsSend, + wsSetMessageCallback, + wsRemoveMessageCallback, + wsRemoveAllMessageCallback, + wsClose, + type Response, + type Request, + wsSetErrorCallback, wsRemoveAllErrorCallback +} from '@/utils/ws'; export const useWsStore = defineStore('ws', { state: () => ({ @@ -20,6 +30,14 @@ export const useWsStore = defineStore('ws', { removeAllMessageCallback() { wsRemoveAllMessageCallback(); }, + setErrorCallback(onError: () => void){ + wsSetErrorCallback(onError); + }, + removeAllErrorCallback(){ + wsRemoveAllErrorCallback() ; + }, + + sendMessage(request: Request) { wsSend(request); diff --git a/src/utils/ws.ts b/src/utils/ws.ts index 6a00062..dfea6ff 100644 --- a/src/utils/ws.ts +++ b/src/utils/ws.ts @@ -4,6 +4,7 @@ let socket: WebSocket | null = null; let reconnectAttempts = 0; // 重连次数计数器 const reconnectInterval = 3000; // 重连间隔时间,单位毫秒 let onMessageList = [] as ((response: Response) => void)[]; +let onErrorList = [] as (() => void)[]; interface Response { Code: number; Message: string; @@ -32,6 +33,9 @@ const wsInit = (finish: () => void) => { // 监听错误事件 socket.addEventListener('error', (event) => { console.log('WebSocket连接失败'); + for(let i = 0; i < onErrorList.length; i++){ + onErrorList[i](); + } }); socket.addEventListener('message', (event) => { console.log('收到服务器消息:', event.data); @@ -73,6 +77,13 @@ const wsClose = () => { } }; +const wsSetErrorCallback=(onError: () => void)=>{ + onErrorList.push(onError) +} +const wsRemoveAllErrorCallback=()=>{ + onErrorList = []; +} + export { wsInit, wsSetMessageCallback, @@ -80,6 +91,8 @@ export { wsRemoveMessageCallback, wsSend, wsClose, + wsSetErrorCallback, + wsRemoveAllErrorCallback, type Response, type Request, }; \ No newline at end of file