单应用项目,可以创建很多独立工具类页面 ,不用登录 初始化的页面
zhangchen
2025-09-16 ef6b957561341b7301ba8d1e19a36b824af5c18f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { defineStore } from "pinia";
import { ref } from "vue";
import dayjs from "dayjs";
import { EventSourcePolyfill } from "event-source-polyfill";
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";
import type { DeviceLoginRecord } from './type/user.type';
import { logOutApi } from "@/api/user";
 
export const useBedsideAuxiliaryScreenStore = defineStore(
  "bedsideAuxiliaryScreen",
  () => {
    /** 设备编号 */
    const deviceCode = ref<string>(Local.get("devcieCode") || "");
 
    /** 副屏版本号 */
    const version = ref<string>(Local.get("version") || "0.0.0");
 
    /** 设备信息数据 */
    const deviceData = ref<BedsideAuxiliaryScreen>(defaultDeviceData());
 
    /** 任务列表 */
    const taskData = ref<Task[]>([]);
 
    /** 用户信息 */
    const userInfo = ref<DeviceLoginRecord | null>(null);
 
    /** 设置副屏版本号 */
    const setVersion = (val: string) => {
      version.value = val;
      Local.set("version", val);
    };
 
    /**
     * 设置设备编号
     * @param code
     */
    const setDeviceCode = (code: string) => {
      deviceCode.value = code;
      Local.set("devcieCode", code);
    };
 
    /**
     * 清除设备信息
     */
    const clearDevice = () => {
      deviceData.value = defaultDeviceData();
    };
 
    /**
     * 追加定时任务
     * @param taskItem
     */
    const pushTask = (taskItem: Task) => {
      taskData.value.push(taskItem);
    };
 
    /** 设置当前定时任务 */
    const setSyncTask = (taskItem: Task) => {
      taskData.value = [taskItem];
    };
 
    /** 清除当前定时任务 */
    const clearTask = () => {
      taskData.value = [];
    };
 
    /** 设置用户信息 */
    const setUserInfo = (user: DeviceLoginRecord | null) => {
      userInfo.value = user;
    };
 
 
    /** 退出登录 */
    const logout = async (deviceCodeStr: string) => {
      await logOutApi(deviceCodeStr);
      setUserInfo(null);
    };
 
    // SSE 相关状态
    const source = ref<EventSource | null>(null);
    const message = ref<string | null>(null);
    const isConnected = ref(false);
 
    // 重连控制
    let retryCount = 0;
    const maxRetryCount = 6000 * 60 * 24 * 30;
    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,
      });
 
      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) => {
        console.log("[SSE] 消息:", e.data);
        const msg = e.data;
        let dif = msg.indexOf("event:message");
        let beng = msg.indexOf("{");
        let end = msg.length - 1;
        if (beng !== -1 && end !== -1 && dif !== -1) {
          const datax = msg.slice(beng, end + 1);
          const dataBody = JSON.parse(datax) as SseMsgData;
          console.log("dataBody: ", dataBody);
          // 倒计时提示文本
          if (
            dataBody.倒计时?.提醒文本 &&
            Number(dataBody.倒计时?.设定提醒倒计时 > 0)
          ) {
            const serverTimeRaw = dataBody.倒计时?.当前服务器时间;
            const reminderMinutes = Number(
              dataBody.倒计时?.设定提醒倒计时 ?? 0
            );
            const serverTimeFormatted = serverTimeRaw.replace(" ", "T");
 
            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);
          // 当前登录的用户信息
          setUserInfo(dataBody.当前登录状态);
 
          // 判断本地的版本号与远程的版本号是否一致,如果不一致则执行刷新操作
          if (dataBody.服务端版本号 !== version.value) {
            refreshVersion(dataBody.服务端版本号);
          } else if (dataBody.是否需要立即刷新 === 1) {
            refreshVersion(dataBody.服务端版本号);
          }
        }
      };
    };
 
    /** 刷新副屏 */
    const refreshVersion = (val?: string) => {
      if (val) {
        setVersion(val);
      }
      ElMessage({
        type: "success",
        duration: 1000 * 3,
        message: "系统更新···",
        onClose: function () {
          window.location.reload();
        },
      });
    };
 
    /**
     * 关闭 SSE 连接
     */
    const close = () => {
      if (source.value) {
        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 {
      version,
      deviceCode,
      deviceData,
      setDeviceCode,
      source,
      message,
      isConnected,
      connect,
      close,
      refresh,
      taskData,
      pushTask,
      setSyncTask,
      clearTask,
      setVersion,
      refreshVersion,
      userInfo,
      setUserInfo,
      logout,
    };
  }
);