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
<template>
  <div class="AlarmReset">
    <div style="width: 100%; text-align: center; font-size: 5rem; color: blue"> 报警复位 </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'
 
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 createSocket = (url) => {
  clearInterval(timer.value)
  // 创建WebSocket连接
  //"ws://127.0.0.1:9295/admin"
  client.value = new WebSocket('ws://192.168.1.103:5174/')
  client.value.onopen = function () {
    console.log('WebSocket 连接成功')
  }
  client.value.onmessage = function (event) {
    const 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 连接关闭')
    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')).userInfo.userName // 获取账号
//复位按钮方法
const handleBecomeTrue = (val) => {
   const loadingMessage = ElMessage({
    message: "正在检查复位中,请稍等...",
    type: "info",
    duration: 4000, // 4 秒后自动关闭
  });
  BecomeTrue({
    account: account
  }).then((res) => {
    if (res.status == true) {
      if (res.code == 0) {
        ElMessage({
          message: '复位成功',
          type: 'success'
        })
      } else {
        ElMessage({
          message: '复位失败',
          type: 'error'
        })
      }
    } else {
      ElMessage({
        message: res.message,
        type: 'warning'
      })
    }
  })
}
 
onMounted(() => {
  isStart.value = true
  createSocket()
})
//切换横屏/竖屏
// onMounted(() => {
//   // 统一判断函数
//   const checkIsMobile = () => {
//     isMobile.value = window.innerWidth <= 1200; // 假设<=1200是移动端
//   };
  
//   // 初始化检查
//   checkIsMobile();
  
//   // 监听窗口变化
//   window.addEventListener('resize', checkIsMobile);
  
//   // 记得在onUnmounted中移除监听器
//   onUnmounted(() => {
//     window.removeEventListener('resize', checkIsMobile);
//   });
// });
 
onUnmounted(() => {
  console.log('WebSocket 连接关闭')
  isStart.value = false
  clearInterval(timer.value)
  if (client.value) {
    client.value.close()
  }
})
</script>
<style lang="scss" scoped>
.AlarmReset {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}
</style>