pc端
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.

79 lines
1.9 KiB

4 years ago
<template>
4 years ago
<SearchBar @search="getDate" />
4 years ago
<RealtimeData ref="childRef" class="mt-4" />
</template>
<script setup>
4 years ago
import { computed, ref, onUnmounted } from 'vue';
4 years ago
import { useStore } from 'vuex';
4 years ago
import SearchBar from 'components/search-bar.vue';
import RealtimeData from 'components/realtime-data.vue';
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);
4 years ago
/**
* 格式化日期
* @param {Date} date 选择的日期
*/
const formatDate = date => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return `${year}-${month}-${day}`;
};
/**
* 实时数据统计
* @param {*} deviceId // 站点 设备id
* @param {*} date // 年月时间段
* @param {*} sort // 1 -> 按时间正序 -1->按时间倒序
* @param {*} page // 第几页
* @param {*} size // 每页多少条
* @param {*} type // 0查全部
*/
4 years ago
const getDate = async options => {
4 years ago
try {
if (token) {
const startTime = new Date(new Date(new Date().toLocaleDateString()).getTime());
const start = formatDate(startTime);
const end = formatDate(new Date());
const params = {
deviceId: options && options.deviceId ? options.deviceId : '',
date: [start, end],
sort: 1,
page: 1,
size: 10,
type: 0,
};
await store.dispatch('statistics/getRealtimeData', params);
timer && clearTimeout(timer);
timer = null;
childRef.value.changeDate(realtimeData.value);
} else {
timer = setTimeout(() => {
getDate();
});
}
} catch (error) {
console.log('error: ', error);
4 years ago
}
};
getDate();
4 years ago
apiTimer = setInterval(() => {
getDate();
}, 5000);
onUnmounted(() => {
apiTimer && clearInterval(apiTimer);
apiTimer = null;
});
4 years ago
</script>