index.vue
3.66 KB
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
<template>
<a-row class="content">
<a-col flex="50px">
<a-space>
<a-button type="primary" status="success" :disabled="status" @click="handleReception">接收</a-button>
<a-button type="primary" status="warning" :disabled="!status" @click="handleStop">停止</a-button>
<a-button type="primary" status="danger" @click="handleClear">清空</a-button>
</a-space>
</a-col>
<a-col flex="auto" class="content-view">
<TransitionGroup name="list" tag="ul">
<li v-for="item in codeData" :key="item">
<p>{{item.time}}</p>
{{ item.value }}
</li>
</TransitionGroup>
</a-col>
</a-row>
</template>
<script setup lang="ts">
import {onMounted, ref, inject, watch, onBeforeUnmount} from "vue";
import {useRouter} from 'vue-router';
import {SocketData} from "@/api/websocketService";
//路由
const router = useRouter();
const channelSn = ref<any>(router.currentRoute.value.query.channelSn);
const codeData = ref<any>([]);
const status = ref<any>(true);
let timer: ReturnType<typeof setTimeout> | null = null;
// WebSocket 数据
const {data, sendData}: SocketData = inject('webSocketData', {
data: {value: 0},
sendData: () => {
}
});
// 订阅
const subscribe = () => {
sendData({
action: 'subscribe',
bizSn: channelSn.value
});
}
// 取消订阅
const onSubscribe = () => {
sendData({
action: 'unsubscribe',
bizSn: channelSn.value
});
}
//接收
const handleReception = () => {
status.value = !status.value;
subscribe();
timerStart();
}
// 计时器开始
const timerStart = () => {
// 设置定时器,在一分钟之后取消请求
timer = setTimeout(() => {
handleStop();
}, 60 * 1000)
}
// 停止
const handleStop = () => {
status.value = !status.value;
clearTimeout(timer!);
onSubscribe();
}
// 清空
const handleClear = () => {
codeData.value = [];
}
function splitStringIntoPairs(str: any) {
return str.match(/.{1,2}/g).join(' ');
}
watch(data, async (newVal: any) => {
if (newVal.key == channelSn.value) {
newVal.value = splitStringIntoPairs(newVal.value)
codeData.value.unshift(newVal);
}
});
onMounted(async () => {
subscribe();
timerStart();
})
onBeforeUnmount(() => {
onSubscribe();
clearTimeout(timer!);
});
</script>
<style lang="less" scoped>
.content {
width: 100%;
padding: 20px;
display: flex;
box-sizing: border-box;
flex-direction: column;
background: var(--color-bg-1);
.content-view {
width: 100%;
height: 74vh;
overflow-y: hidden;
box-sizing: border-box;
color: var(--color-neutral-8);
outline: none;
box-shadow: none; /* 移除可能的阴影效果 */
border: none;
padding: 10px;
background: var(--color-bg-3);
ul {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
list-style: none;
overflow-y: scroll;
&::-webkit-scrollbar {
display: none;
}
li {
line-height: 20px;
margin-bottom: 20px;
transition: all 0.1s ease-out;
p{
margin: 0;
color: rgb(var(--red-7));
//font-weight: bold;
font-size: 14px;
margin-left: 4px;
}
}
}
.textarea {
position: absolute;
width: 100%;
outline: none;
box-shadow: none; /* 移除可能的阴影效果 */
border: none;
padding: 10px;
overflow-y: hidden;
box-sizing: border-box;
color: var(--color-neutral-8);
background: var(--color-neutral-2);
}
}
}
.list-enter-active,
.list-leave-active {
//transition: all 0.2s ease;
}
.list-enter-from,
.list-leave-to {
opacity: 0;
transform: translateX(30px);
}
</style>