Browse Source

格式化日期

master
song 4 years ago
parent
commit
d4acdb86ea
  1. 22
      src/components/data-search-bar.vue
  2. 6
      src/components/realtime-data.vue
  3. 6
      src/components/search-bar.vue
  4. 49
      src/views/statistical-history.vue
  5. 79
      src/views/statistical-realtime.vue

22
src/components/data-search-bar.vue

@ -6,7 +6,7 @@
<el-option :label="item.address" :value="item.deviceId" v-for="item in devices" :key="item.deviceId"></el-option> <el-option :label="item.address" :value="item.deviceId" v-for="item in devices" :key="item.deviceId"></el-option>
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="选择日期" class="ml-6"> <el-form-item label="选择日期">
<el-date-picker <el-date-picker
v-model="searchDevice.value1" v-model="searchDevice.value1"
type="monthrange" type="monthrange"
@ -35,13 +35,15 @@ const searchDeviceForm = ref(null); // form
const store = useStore(); const store = useStore();
const devices = computed(() => store.state.device.devices); const devices = computed(() => store.state.device.devices);
// /**
const timeChange = time => { * 格式化日期
const d = new Date(time); * @param {Date} date 选择的日期
const y = d.getFullYear(); */
let m = d.getMonth() + 1; const formatDate = date => {
m = m < 10 ? `0${m}` : m; const year = date.getFullYear();
return `${y}-${m}`; const month = date.getMonth() + 1;
const day = date.getDate();
return `${year}-${month}-${day}`;
}; };
const onSubmit = () => { const onSubmit = () => {
@ -50,8 +52,8 @@ const onSubmit = () => {
const options = { ...searchDevice }; const options = { ...searchDevice };
let date = []; let date = [];
if (options.value1) { if (options.value1) {
const start = timeChange([...options.value1][0]); const start = formatDate([...options.value1][0]);
const end = timeChange([...options.value1][1]); const end = formatDate([...options.value1][1]);
date = [start, end]; date = [start, end];
} }
const params = { deviceId: options.device, date }; const params = { deviceId: options.device, date };

6
src/components/realtime-data.vue

@ -22,9 +22,13 @@ onMounted(() => {
// //
const setDate = data => { const setDate = data => {
noData.value = false; noData.value = false;
if (bar) { if (bar) {
bar.destroy(); console.log('重新加载');
bar.changeData(data);
return;
} }
bar = new Line('realtimeContainer', { bar = new Line('realtimeContainer', {
data, data,
padding: 'auto', padding: 'auto',

6
src/components/search-bar.vue

@ -13,9 +13,11 @@
</template> </template>
<script setup> <script setup>
import { reactive, ref, computed } from 'vue'; import { reactive, ref, computed, defineEmits } from 'vue';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
const emit = defineEmits(['getDate']);
const searchDevice = reactive({ device: '' }); const searchDevice = reactive({ device: '' });
const searchDeviceForm = ref(null); // form const searchDeviceForm = ref(null); // form
@ -26,6 +28,8 @@ const devices = computed(() => store.state.device.devices);
const onSubmit = () => { const onSubmit = () => {
searchDeviceForm.value.validate(valid => { searchDeviceForm.value.validate(valid => {
console.log(valid, { ...searchDevice }); console.log(valid, { ...searchDevice });
const { device } = searchDevice;
emit('search', { deviceId: device });
}); });
}; };
</script> </script>

49
src/views/statistical-history.vue

@ -8,28 +8,55 @@ import SearchBar from 'components/search-bar-data.vue';
import HistoryData from 'components/history-data.vue'; import HistoryData from 'components/history-data.vue';
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
import { getHistories } from 'apis/index';
const childRef = ref(null); const childRef = ref(null);
let timer = null; let timer = null;
const page = ref({ page: 2, size: 10 });
const data = ref(null);
const search = ref({});
const store = useStore(); const store = useStore();
const token = computed(() => store.getters['user/token']); const token = computed(() => store.getters['user/token']);
const realtimeData = computed(() => store.state.statistics.realtimeData);
// /**
const getDate = async () => { * 格式化日期
* @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
*/
const getDate = async options => {
try { try {
if (token) { if (token) {
const params = { search: { ...search.value }, page: { page: page.value.page, size: page.value.size } }; let date = [];
const resData = await getHistories(params); const startTime = new Date(new Date(new Date().toLocaleDateString()).getTime());
data.value = resData.data; const start = formatDate(startTime);
page.value = resData.page; const end = formatDate(new Date());
date = [start, end];
const params = {
deviceId: options && options.deviceId ? options.deviceId : '',
date: options && options.date && options.date.length ? options.date : date,
sort: 1,
page: 1,
size: 10,
type: 0,
};
await store.dispatch('statistics/getRealtimeData', params);
timer && clearTimeout(timer); timer && clearTimeout(timer);
timer = null; timer = null;
childRef.value.changeDate(data.value); childRef.value.changeDate(realtimeData.value);
} else { } else {
timer = setTimeout(() => { timer = setTimeout(() => {
getDate(); getDate();

79
src/views/statistical-realtime.vue

@ -1,41 +1,78 @@
<template> <template>
<DataSearchBar @getDate="getDate" /> <SearchBar @search="getDate" />
<RealtimeData ref="childRef" class="mt-4" /> <RealtimeData ref="childRef" class="mt-4" />
</template> </template>
<script setup> <script setup>
import DataSearchBar from 'components/data-search-bar.vue'; import { computed, ref, onUnmounted } from 'vue';
import RealtimeData from 'components/realtime-data.vue';
import { computed, ref } from 'vue';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
import SearchBar from 'components/search-bar.vue';
import RealtimeData from 'components/realtime-data.vue';
const childRef = ref(null); const childRef = ref(null);
let timer = null; let timer = null;
let apiTimer = null;
const store = useStore(); const store = useStore();
const token = computed(() => store.getters['user/token']); const token = computed(() => store.getters['user/token']);
const realtimeData = computed(() => store.state.statistics.realtimeData); const realtimeData = computed(() => store.state.statistics.realtimeData);
// /**
* 格式化日期
* @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
*/
const getDate = async options => { const getDate = async options => {
if (token) { try {
const params = { if (token) {
deviceId: options && options.deviceId ? options.deviceId : '', // id const startTime = new Date(new Date(new Date().toLocaleDateString()).getTime());
date: options && options.date && options.date.length ? options.date : [], // const start = formatDate(startTime);
sort: 1, // 1 -> -1-> const end = formatDate(new Date());
page: 1, //
size: 10, // const params = {
}; deviceId: options && options.deviceId ? options.deviceId : '',
await store.dispatch('statistics/getRealtimeData', params); date: [start, end],
timer && clearTimeout(timer); sort: 1,
timer = null; page: 1,
childRef.value.changeDate(realtimeData.value); size: 10,
} else { type: 0,
timer = setTimeout(() => { };
getDate(); 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);
} }
}; };
getDate(); getDate();
apiTimer = setInterval(() => {
getDate();
}, 5000);
onUnmounted(() => {
apiTimer && clearInterval(apiTimer);
apiTimer = null;
});
</script> </script>

Loading…
Cancel
Save