浏览代码

fix 问题

luogang 2 月之前
父节点
当前提交
ce771f4a29
共有 1 个文件被更改,包括 43 次插入31 次删除
  1. 43 31
      plus-ui-ts/src/views/index.vue

+ 43 - 31
plus-ui-ts/src/views/index.vue

@@ -621,8 +621,6 @@ const videoPlayer = ref(null);
 const curStep = ref(1);
 const eventVerifOptions = ref([]);
 const verifyInfo = ref('');
-const POLL_INTERVAL = 30000; // 30秒
-let pollTimer = null;
 onMounted(() => {
   try {
     const map = new BMapGL.Map('map'); // 创建地图实例
@@ -641,13 +639,10 @@ onMounted(() => {
   getList('init');
   getStat();
   getDeviceList();
-  // 设置轮询
-  pollTimer = setInterval(() => {
-    getList('init');
-  }, POLL_INTERVAL);
+  startMonitoring(30000);
 });
 onUnmounted(() => {
-  clearInterval(pollTimer);
+  stopMonitoring();
 });
 const getEventVerifOptions = () => {
   listData({
@@ -682,39 +677,51 @@ const getList = async (type) => {
     queryParams.value.params.endCreateTime = undefined;
   }
   const res = await listEvent(queryParams.value);
-  // 创建新列表(带格式化)
-  const newList = res.rows.map((item) => ({
+  eventList.value = res.rows.map((item) => ({
     ...item,
     status: item.status || '2',
     createTimeFormat: dateFormat(new Date(item.createTime), 'yyyy-MM-dd'),
     ext1: item.ext1 ? JSON.parse(item.ext1) : [],
     ext2: item.ext2 ? JSON.parse(item.ext2) : {}
   }));
-
-  // 检测新事件(只在已有数据时检测)
-  if (eventList.value.length > 0) {
-    const newEvents = detectNewEvents(eventList.value, newList);
-    if (newEvents.length > 0) {
-      showNewEventNotification(newEvents);
-    }
-  }
-  // 更新列表引用
-  eventList.value = newList;
   total.value = res.total;
 };
-const detectNewEvents = (oldList, newList) => {
-  const oldIds = new Set(oldList.map((item) => item.id));
-  return newList.filter((item) => !oldIds.has(item.id));
+// 响应式变量定义
+const timer = ref(null);
+const lastEventId = ref(null);
+// 定时监测新事件的方法
+const startMonitoring = (interval = 5000) => {
+  // 首次获取时设置监测起点
+  const initMonitoring = () => {
+    listEvent({}).then(({ code, rows }) => {
+      if (rows.length) {
+        lastEventId.value = rows[0].id;
+      }
+    });
+  };
+
+  // 启动时立即初始化
+  initMonitoring();
+  // 设置定时器
+  timer.value = setInterval(async () => {
+    const { rows } = await listEvent({});
+    if (rows.length > 0) {
+      if (rows[0].id !== lastEventId.value) {
+        ElNotification({
+          title: '提示',
+          message: '有新事件产生',
+          type: 'warning'
+        });
+        getList('init');
+      }
+    }
+  }, interval);
 };
-// 辅助函数:显示新事件通知
-const showNewEventNotification = (newEvents) => {
-  const count = newEvents.length;
-  const eventText = count > 1 ? `产生${count}个新事件` : '产生1个新事件';
-  ElNotification({
-    title: '提示',
-    message: eventText,
-    type: 'warning'
-  });
+const stopMonitoring = () => {
+  if (timer.value) {
+    clearInterval(timer.value);
+    timer.value = null;
+  }
 };
 const getStat = async () => {
   const monthRange = getTimeRange('month');
@@ -1448,8 +1455,10 @@ const delEventDepts = (index) => {
   display: flex;
   justify-content: center;
   padding: 20px 0;
+
   .step-item {
     position: relative;
+
     p {
       position: absolute;
       top: 25px;
@@ -1458,6 +1467,7 @@ const delEventDepts = (index) => {
       font-size: 12px;
       width: 100px;
     }
+
     span {
       width: 35px;
       height: 35px;
@@ -1471,6 +1481,7 @@ const delEventDepts = (index) => {
       border-radius: 50%;
       cursor: pointer;
     }
+
     &:not(:last-child) {
       display: flex;
       align-items: center;
@@ -1484,6 +1495,7 @@ const delEventDepts = (index) => {
       }
     }
   }
+
   // > div {
   //   // position: relative;
   //   // p {