5.8
pengwei
2025-05-08 b281791abab23d672922b7e9b7d1b51e348ed710
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
<template>
  <div class="AlarmReset">
    <div style="width: 100%; text-align: center; font-size: 5rem; color: #fff">
      报警复位
    </div>
    <div style="margin-top: 3rem">
      <el-table :data="tableData" style="width: 100%" height="800">
        <el-table-column prop="Id" label="序号" />
        <el-table-column prop="AlarmContent" label="报警内容" />
        <el-table-column prop="ResetStatus" label="复位状态" />
        <el-table-column prop="AlarmTime" label="报警时间">
          <template #default="scope">
            {{ formatTime(scope.row.AlarmTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="ResetTime" label="复位时间">
          <template #default="scope">
            {{ formatTime(scope.row.ResetTime) }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div
      style="
        width: 100%;
        display: flex;
        justify-content: center;
        margin-top: 2rem;
      "
    >
      <el-pagination
        size="large"
        :page-size="10"
        layout="prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="mt-4"
        background
      />
    </div>
    <div
      style="
        width: 100%;
        display: flex;
        justify-content: center;
        margin-top: 4rem;
      "
    >
      <el-button
        type="primary"
        style="width: 10rem; height: 3rem"
        @click="handleBecomeTrue"
        >报警复位</el-button
      >
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import { ElMessage } from "element-plus";
import { BecomeTrue } from "@/api/newapi/AlarmReset.js";
import { formatTime } from "@/utils/index.js";
 
const timer = ref(null);
const client = ref(null);
const tableData = ref([]);
const pageSize = ref(10); // 每页显示的条数
const currentPage = ref(0); // 当前页码
const total = ref(0); // 总条数
const createSocket = (url) => {
  clearInterval(timer.value);
  // 创建WebSocket连接
  //"ws://127.0.0.1:9295/admin"
  client.value = new WebSocket("ws://192.168.0.80:5173/");
  client.value.onopen = function () {
    console.log("WebSocket 连接成功");
  };
  client.value.onmessage = function (event) {
    let data = JSON.parse(event.data);
    tableData.value = data.Data.slice(
      currentPage.value * pageSize.value,
      (currentPage.value + 1) * pageSize.value
    );
    total.value = data.Data.length; // 更新总条数
    console.log("WebSocket 接收到消息", tableData.value);
  };
  client.value.onclose = function () {
    console.log("WebSocket 连接关闭");
    timer.value = setTimeout(createSocket, 1000);
  };
 
  client.value.onerror = function () {};
};
const handleCurrentChange = (val) => {
  currentPage.value = val - 1; // 更新当前页码
};
const account = JSON.parse(localStorage.getItem("user")).userName; // 获取账号
//复位按钮方法
const handleBecomeTrue = (val) => {
  BecomeTrue({
    account: account,
  }).then((res) => {
    if (res.code == 0) {
      ElMessage({
        message: "复位成功",
        type: "success",
      });
    } else {
      ElMessage({
        message: "复位失败",
        type: "error",
      });
    }
  });
};
 
onMounted(() => {
  createSocket();
});
 
onUnmounted(() => {
  clearInterval(timer.value);
  if (client.value) {
    client.value.close();
  }
});
</script>
<style lang="scss" scoped>
.AlarmReset {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: rgba(0, 0, 0, 0.2);
  box-sizing: border-box;
}
</style>