yanjinhui
10 天以前 c5de0d98241f8c8349fa38851b77efcfc61e4d26
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
<template>
  <div class="AlarmReset">
    <div
      :style="{
        width: '100%',
        textAlign: 'center',
        fontSize: isMin ? '5rem' : '2rem',
        color: '#fff',
      }"
    >
      报警复位
    </div>
    <div style="margin-top: 3rem">
      <el-table
        empty-text="暂无数据"
        :data="tableData"
        style="width: 100%"
        :height="isMin ? '800' : '450'"
      >
        <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',
        justifyContent: 'center',
        marginTop: isMin ? '4rem' : '2rem',
      }"
    >
      <el-button
        type="primary"
        :style="{
          width: isMin ? '16rem' : '10rem',
          height: isMin ? '5rem' : '2.5rem',
        }"
        @click="handleBecomeTrue"
        ><span :style="{ fontSize: isMin ? '3rem' : '1.5rem' }">
          报警复位</span
        ></el-button
      >
      <el-button
        type="primary"
        :style="{
          width: isMin ? '20rem' : '10rem',
          height: isMin ? '5rem' : '2.5rem',
        }"
        @click="handleClearAlarm"
        ><span :style="{ fontSize: isMin ? '3rem' : '1.5rem' }">
          清除报警信息</span
        ></el-button
      >
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import { ElMessage } from "element-plus";
import { BecomeTrue, ClearAlarm } 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 isStart = ref(false); // 是否开始
const positionvalue = ref({}); // 位置值
 
const createSocket = (url) => {
  clearInterval(timer.value);
  // 创建WebSocket连接
  //"ws://127.0.0.1:9295/admin"
  //192.168.1.103
  client.value = new WebSocket("ws://192.168.1.103:5173/");
  client.value.onopen = function () {
    console.log("WebSocket 连接成功");
  };
  client.value.onmessage = function (event) {
    let data = JSON.parse(event.data);
    if (data.Status) {
      tableData.value = data.Data.slice(
        currentPage.value * pageSize.value,
        (currentPage.value + 1) * pageSize.value
      );
      total.value = data.Data.length; // 更新总条数
    } else {
      positionvalue.value = data;
    }
 
    console.log("WebSocket 接收到消息", data);
  };
  client.value.onclose = function () {
    console.log("WebSocket 连接关闭");
    if (isStart.value) {
      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 = () => {
//     ElMessage({
//     message: "正在检查复位中,请稍等...",
//     type: "info",
//   });
//   BecomeTrue().then((res) => {
//     //显示加载中的提示
//     if (res.code == 0) {
//       ElMessage({
//         message: "复位成功",
//         type: "success",
//       });
//     } else {
//       ElMessage({
//         message: "复位失败",
//         type: "error",
//       });
//     }
//   });
// };
const handleBecomeTrue = async () => {
  // 先显示加载提示(不会自动关闭)
  const loadingMessage = ElMessage({
    message: "正在检查复位中,请稍等...",
    type: "info",
    duration: 4000, // 4 秒后自动关闭
  });
    const res = await BecomeTrue(); // 调用接口
    loadingMessage.close(); // 关闭加载提示
 
    if (res.code === 0) {
      ElMessage.success("复位成功");
    } else {
      ElMessage.error("复位失败");
    }
};
//清除报警信息方法
const handleClearAlarm = () => {
  ClearAlarm().then((res) => {
    if (res.code == 0) {
      ElMessage({
        message: "清除成功",
        type: "success",
      });
    }
  });
};
const isMin = ref(false); // 是否最小化
onMounted(() => {
  isMin.value = window.innerWidth <= 1080; // 判断是否最小化
  isStart.value = true; // 设置为开始状态
  createSocket();
});
 
onUnmounted(() => {
  isStart.value = false;
  clearInterval(timer.value);
  if (client.value) {
    client.value.close();
  }
  clearInterval(timer.value);
});
</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>