index.vue
3.28 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
<!--
* 功能:状态图
* 作者:闫李壮
* 日期:2024-06-04
-->
<template>
<!-- 加载动画,加载完成后显示状态图或空状态 -->
<a-spin :loading="loading">
<a-card :title="chartInfo.cardName" v-if="chartInfo.statusInfo.icon">
<component :is="chartInfo.statusInfo.icon" :style="iconStyle"/>
</a-card>
<a-empty v-else/>
</a-spin>
</template>
<script lang="ts" setup>
import {computed, inject, onBeforeUnmount, onMounted, ref, watch} from "vue";
import useLoading from "@/hooks/loading";
import {SocketData} from "@/api/websocketService";
import {useIntervalFn} from "@vueuse/core";
import {getChartInfo} from "@/api/dashboard/api";
// 加载状态
const {loading, setLoading} = useLoading(false);
// 定义组件属性
const props = defineProps({
public: {
type: Object,
default: () => ({})
},
// 设备编号
deviceSn: {
type: String,
default: ''
}
});
// 图表信息
const chartInfo = ref({
cardName: '',
statusInfo: {
icon: '',
status: 1,
varSn: ''
}
});
// 图标样式
const iconStyle = computed(() => {
return {
fontSize: '50px',
color: chartInfo.value.statusInfo.status ? '#52c41a' : '#f5222d'
}
});
// webSocket 数据
const {data, sendData}: SocketData = inject('webSocketData', {
data: {
value: 0
},
sendData: () => {
}
});
// 监听 webSocket 数据
watch(data, (newVal: any) => {
if (newVal && newVal.key && newVal.key === chartInfo.value.statusInfo?.varSn) {
chartInfo.value.statusInfo.status = newVal.value.status === 1 ? 0 : 1;
}
});
// 5分钟轮询
const {resume: startInterval, pause: stopInterval} = useIntervalFn(async () => {
await fetchData();
}, 1000 * 60 * 5);
// 获取数据
const fetchData = async () => {
try {
setLoading(true);
const params = {
configId: props.public.dashboardConfigId,
cardKey: props.public.cardKey,
deviceSn: props.deviceSn
};
const res: any = await getChartInfo(params);
if (res.code === 200 && res.data) {
chartInfo.value = res.data;
} else {
chartInfo.value = {
cardName: '',
statusInfo: {
icon: '',
status: 0,
varSn: ''
}
}
}
} catch (e) {
console.error('获取数据失败:', e);
} finally {
setLoading(false);
}
};
// 组件挂载时获取数据并订阅
onMounted(async () => {
await fetchData();
startInterval();
if (chartInfo.value.statusInfo?.varSn) {
sendData({
action: 'subscribe',
bizSn: chartInfo.value.statusInfo.varSn
});
}
});
// 组件卸载时取消订阅
onBeforeUnmount(() => {
stopInterval();
if (chartInfo.value.statusInfo?.varSn) {
sendData({
action: 'unsubscribe',
bizSn: chartInfo.value.statusInfo.varSn
});
}
});
// 对外暴露的接口
defineExpose({
fetchData
});
</script>
<style lang="less" scoped>
.arco-spin {
width: 100%;
height: 100%;
border-radius: 4px;
border: 1px solid var(--color-border-2);
background: var(--color-bg-2);
}
.arco-card {
height: 100%;
:deep(.arco-card-body) {
height: calc(100% - 46px);
display: flex;
align-items: center;
justify-content: center;
}
}
.arco-empty {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
</style>