From 2a122106c080eb0a8b0ec82903e785087c6d3b49 Mon Sep 17 00:00:00 2001
From: zhangchen <1652267879@qq.com>
Date: 星期一, 04 八月 2025 16:40:15 +0800
Subject: [PATCH] ID1918-患者姓名根据配置脱敏

---
 src/store/bedsideAuxiliaryScreen.ts |  132 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 113 insertions(+), 19 deletions(-)

diff --git a/src/store/bedsideAuxiliaryScreen.ts b/src/store/bedsideAuxiliaryScreen.ts
index 799dd0d..63a739c 100644
--- a/src/store/bedsideAuxiliaryScreen.ts
+++ b/src/store/bedsideAuxiliaryScreen.ts
@@ -1,28 +1,28 @@
 import { defineStore } from "pinia";
 import { ref } from "vue";
-import cache from "../utils/cache";
+import dayjs from "dayjs";
 import { EventSourcePolyfill } from "event-source-polyfill";
-import type { BedsideAuxiliaryScreen } from './type/bedsideAuxiliaryScreen.type';
-import { defaultDeviceData, defaultconsumablesCollection, formatDeviceData } from './type/bedsideAuxiliaryScreen.type';
-
+import type { BedsideAuxiliaryScreen, SseMsgData } from "./type/bedsideAuxiliaryScreen.type";
+import type { Task } from "./type/task.type";
+import {
+  defaultDeviceData,
+  defaultconsumablesCollection,
+  formatDeviceData,
+} from "./type/bedsideAuxiliaryScreen.type";
+import { ElMessage } from "element-plus/es";
+import { Local } from "@/utils/storage";
 
 export const useBedsideAuxiliaryScreenStore = defineStore(
   "bedsideAuxiliaryScreen",
   () => {
     /** 设备编号 */
-    const deviceCode = ref<string>(cache.get("devcieCode") || "");
-
-    /** 床旁血压计数据 */
-    const bedsideBloodPressureMonitorData = ref({
-      date_time: '',
-      sbp: '',
-      pulseRate: '',
-      dbp: '',
-      zuihouTime: '',
-    });
+    const deviceCode = ref<string>(Local.get("devcieCode") || "");
 
     /** 设备信息数据 */
     const deviceData = ref<BedsideAuxiliaryScreen>(defaultDeviceData());
+
+    /** 任务列表 */
+    const taskData = ref<Task[]>([]);
 
     /**
      * 设置设备编号
@@ -30,7 +30,48 @@
      */
     const setDeviceCode = (code: string) => {
       deviceCode.value = code;
-      cache.set("devcieCode", code);
+      Local.set("devcieCode", code);
+    };
+
+    /**
+     * 清除设备信息
+     */
+    const clearDevice = () => {
+      deviceData.value = defaultDeviceData();
+    };
+
+    /**
+     * 追加定时任务
+     * @param taskItem
+     */
+    const pushTask = (taskItem: Task) => {
+      taskData.value.push(taskItem);
+    };
+
+    /**
+     * 是否将当前任务设置为已过期
+     * @param i
+     */
+    // const deleteTask = (i: number) => {
+    //   const task = taskData.value[i];
+    //   if (task) {
+    //     // 二次判断,判断任务时间是否早于或等于当前时间
+    //     const taskTime = dayjs(task.taskDate).second(0).millisecond(0);
+    //     const now = dayjs().second(0).millisecond(0); // 秒和毫秒都去掉
+    //     if (!taskTime.isAfter(now)) {
+    //       taskData.value[i].overdue = true
+    //     }
+    //   }
+    // };
+
+    /** 设置当前定时任务 */
+    const setSyncTask = (taskItem: Task) => {
+      taskData.value = [taskItem];
+    };
+
+    /** 清除当前定时任务 */
+    const clearTask = () => {
+      taskData.value = [];
     };
 
     // SSE 相关状态
@@ -38,12 +79,18 @@
     const message = ref<string | null>(null);
     const isConnected = ref(false);
 
+    // 重连控制
+    let retryCount = 0;
+    const maxRetryCount = 60;
+    const baseRetryDelay = 1000; // 1秒开始重连延迟
+
     /**
      * 连接 SSE 服务
      * @param url SSE 地址
      */
     const connect = (url: string) => {
       if (source.value) return; // 已连接,避免重复连接
+      ElMessage.success("正在连接设备,请稍候...");
 
       source.value = new EventSourcePolyfill(url, {
         heartbeatTimeout: 60000,
@@ -51,12 +98,29 @@
 
       source.value.onopen = () => {
         console.log("[SSE] 连接成功");
+        ElMessage.success("链接服务成功");
         isConnected.value = true;
+        retryCount = 0; // 成功连接后重置重试计数
       };
 
       source.value.onerror = (e) => {
         console.warn("[SSE] 错误,等待重连中", e);
         isConnected.value = false;
+        close(); // 关闭旧连接,避免残留
+        if (retryCount < maxRetryCount) {
+          const delay = baseRetryDelay * Math.pow(2, retryCount); // 指数退避
+          retryCount++;
+          console.log(`[SSE] 第${retryCount}次重连,延迟${baseRetryDelay}ms`);
+          ElMessage.warning(
+            `链接服务失败, 第${retryCount}次重连,请耐心等待重连。。`
+          );
+          setTimeout(() => {
+            connect(url);
+          }, delay);
+        } else {
+          console.error("[SSE] 重连次数达到上限,停止重连");
+          ElMessage.error("重连次数达到上限,请检查网络或设备状态");
+        }
       };
 
       source.value.onmessage = (e) => {
@@ -67,12 +131,29 @@
         let end = msg.length - 1;
         if (beng !== -1 && end !== -1 && dif !== -1) {
           const datax = msg.slice(beng, end + 1);
-          const dataBody = JSON.parse(datax);
-          console.log('dataBody: ', dataBody)
+          const dataBody = JSON.parse(datax) as SseMsgData;
+          console.log("dataBody: ", dataBody);
           // 倒计时提示文本
-          if (dataBody.倒计时?.提示文本) {}
+          if (dataBody.倒计时?.提醒文本 && Number(dataBody.倒计时?.设定提醒倒计时 > 0)) {
+            const serverTimeRaw = dataBody.倒计时?.当前服务器时间;
+            const reminderMinutes = Number(dataBody.倒计时?.设定提醒倒计时 ?? 0);
+            const serverTimeFormatted = serverTimeRaw.replace(' ', 'T');
 
-          deviceData.value = formatDeviceData(dataBody)
+            const taskTime = dayjs(serverTimeFormatted).add(reminderMinutes, 'second');
+            setSyncTask({
+              deviceCode: dataBody.IOT信息.设备唯一编号,
+              recordCode: dataBody.透析状态?.透析单编号,
+              taskDate: taskTime.format('YYYY-MM-DD HH:mm'),
+              taskName: dataBody.倒计时?.提醒文本,
+              overdue: false,
+              sync: true,
+              countdown: dataBody.倒计时?.设定提醒倒计时
+            })
+          } else {
+            clearTask();
+          }
+
+          deviceData.value = formatDeviceData(deviceData.value, dataBody);
         }
       };
     };
@@ -85,10 +166,18 @@
         source.value.close();
         source.value = null;
         isConnected.value = false;
+        clearDevice();
+        clearTask();
         console.log("[SSE] 连接已关闭");
       }
     };
 
+    /** 刷新 SSE 连接 */
+    const refresh = (url: string) => {
+      retryCount = 0;
+      close(); // 先关闭旧连接
+      connect(url); // 再重新连接
+    };
 
     return {
       deviceCode,
@@ -99,6 +188,11 @@
       isConnected,
       connect,
       close,
+      refresh,
+      taskData,
+      pushTask,
+      setSyncTask,
+      clearTask,
     };
   }
 );

--
Gitblit v1.8.0