山大课题数据库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.6 KiB

4 years ago
<template>
<SearchBar @search="getData" />
4 years ago
<RealtimeData ref="childRef" class="mt-4" />
</template>
<script setup>
4 years ago
import SearchBar from 'components/search-bar.vue';
import RealtimeData from 'components/realtime-data.vue';
import { computed, ref, onUnmounted } from 'vue';
import { useStore } from 'vuex';
import dayjs from 'dayjs';
import { REALTIME_DATA_INTERVAL } from '@/config/config';
4 years ago
const childRef = ref(null);
let timer = null;
4 years ago
let apiTimer = null;
4 years ago
const store = useStore();
const token = computed(() => store.getters['user/token']);
const realtimeData = computed(() => store.state.statistics.realtimeData);
const currentDeviceId = computed(() => store.state.device.currentDeviceId); // 正在操作的设备的id
4 years ago
/**
* 实时数据统计
*/
const getData = async () => {
4 years ago
try {
if (token && token.value) {
const start = +dayjs().startOf('day').format('x');
const end = +dayjs().format('x');
4 years ago
const params = {
deviceId: currentDeviceId.value,
date: [start, end],
type: 0,
4 years ago
};
await store.dispatch('statistics/getRealtimeData', params);
timer && clearTimeout(timer);
timer = null;
childRef.value.changeDate(realtimeData.value.data);
// 定时轮询请求新数据
if (!apiTimer) {
apiTimer = setInterval(() => {
getData();
}, REALTIME_DATA_INTERVAL);
}
4 years ago
} else {
timer = setTimeout(() => {
getData();
}, 20);
4 years ago
}
} catch (error) {
console.log('error: ', error);
4 years ago
}
};
getData();
4 years ago
onUnmounted(() => {
apiTimer && clearInterval(apiTimer);
apiTimer = null;
});
4 years ago
</script>