card-three.vue
9.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<template>
<div class="calendar-box">
<div class="calendar">
<a-card>
<template #title>
历史用电量详细
</template>
<!-- 切换日期 start -->
<template #extra>
<div class="calendar-operate">
<icon-caret-left size="16" @click="changeMonth('prev')" />
<div class="calendar-operate__title">{{ dateText }}</div>
<icon-caret-right size="16" @click="changeMonth('next')" />
</div>
</template>
<a-spin class="energy-box" :loading="loading" style="width: 100%" :tip="$t('global.loading')">
<!-- 切换日期 end -->
<!-- 日历 start -->
<div class="calendar-header">
<span v-for="(item, index) in weekMapZh" :key="index" class="calendar-header__item">{{ item }}</span>
</div>
<div class="calendar-content" :data-month="date.getMonth() + 1">
<template v-for="(item, index) in calendarTable" :key="index">
<div class="calendar-content__item"
:style="{ backgroundColor: item.backGroundColor ? item.backGroundColor : '#CEF3DC' }"
:class="[{ light: !item.isCurrentMonth }]" @click="lightClick(item.day, item.isCurrentMonth)">
<div :class="[{ active: isActive(item) }]">
<span>{{ item.day }}日</span>
</div>
<div class="calendar-content__battery">
{{ item.battery }}
</div>
</div>
</template>
</div>
<!-- 日历 end -->
<div class="illustrate-box">
<div class="title">
用电登记(度)
</div>
<div class="process">
<template v-for="(item, index) in ['#CEF3DC', '#E9F5F0', '#FBF1E6', '#F9DBC1', '#F7EBE9', '#F4C8CA']">
<div :style="{ backgroundColor: item }">{{ index * 250 }}</div>
</template>
</div>
</div>
</a-spin>
</a-card>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref, watchEffect } from 'vue';
import { CalendarItem, generateCalendar, isAllTrue, weekMapZh } from './calendar';
import { PowerModuleEunm, getChart } from '@/api/system/home-power';
import useLoading from '@/hooks/loading';
import dayjs from 'dayjs';
import { useIntervalFn } from '@vueuse/core';
//加载中
const { loading, setLoading } = useLoading(false);
//图表数据
const chartData = ref<any>({});
// 当前时间
const date = ref<Date>(new Date());
//日历
const calendarTable = ref<CalendarItem[]>([]);
//生成日历
calendarTable.value = generateCalendar(date.value)
//计算当前月份
const dateText = computed(() => {
return `${date.value.getFullYear()}年${date.value.getMonth() + 1}月`;
});
// 是否是今天
const isToday = computed(() => {
const current = new Date();
const validArr = [
date.value.getFullYear() === current.getFullYear(),
date.value.getMonth() === current.getMonth(),
date.value.getDay() === current.getDay(),
];
return isAllTrue(validArr);
});
/**
* 当天日期高亮显示, 兼容切换日期:
* 年月日都要对上才能高亮
* ps: 日历可能会显示下月/上月的同样日期, 仅当月日期高亮
*/
const isActive = (item: CalendarItem) => {
return isAllTrue([
item.day === date.value.getDate(),
item.isCurrentMonth,
item.month === new Date().getMonth(),
item.year === new Date().getFullYear(),
]);
};
// 切换到今天
const currentDate = () => {
date.value = new Date();
};
/**
* 切换月份, 上个月 or 下个月
* @param type
*/
const changeMonth = async (type: 'prev' | 'next') => {
let month: number;
let year: number;
if (type === 'prev') {
month = date.value.getMonth() === 0 ? 11 : date.value.getMonth() - 1;
year = month === 11 ? date.value.getFullYear() - 1 : date.value.getFullYear();
} else {
month = date.value.getMonth() === 11 ? 0 : date.value.getMonth() + 1;
year = month === 0 ? date.value.getFullYear() + 1 : date.value.getFullYear();
}
let day = 0;
if (month === new Date().getMonth()) {
day = new Date().getDate()
}
date.value.setDate(day);
date.value.setMonth(month);
date.value.setFullYear(year);
date.value = new Date(date.value);
console.log("calendarTable", calendarTable.value);
calendarTable.value = generateCalendar(date.value)
await fetchData();
};
/**
* 点击上个月或者下个月的 日期
* @param day
* @param isCurrentMonth
*/
const lightClick = (day: number, isCurrentMonth: boolean) => {
if (isCurrentMonth) return
if (day < 15) {
changeMonth('next')
}
if (day > 15) {
changeMonth('prev')
}
}
/**
* 左补0
* @param number
* @param targetLength
*/
const padLeftZero = (number: number, targetLength: number) => number.toString().padStart(targetLength, '0');
/**
* 获取日期数据
* @param param 时间参数
*/
const fetchData = async () => {
setLoading(true);
try {
let res = await getChart({ pageValue: PowerModuleEunm.electric_use_detail, "monthTime": `${date.value.getFullYear()}-${padLeftZero(date.value.getMonth() + 1, 2)}` });
if (res.code == 200) {
if (res.data.xAxis && res.data.yAxis && res.data.yAxis.length == 1 && res.data.yAxis[0].dataList && res.data.xAxis.length == res.data.yAxis[0].dataList.length) {
let map = new Map();
let data = res.data.yAxis[0].dataList;
res.data.xAxis.forEach((item: any, index: number) => {
map.set(item, data[index]);
})
calendarTable.value.forEach((item: any) => {
item.battery = null;
if (item.isCurrentMonth) {
item.battery = map.get(`${item.year}-${padLeftZero(item.month + 1, 2)}-${padLeftZero(item.day, 2)}`);
}
item.backGroundColor = item.battery >= 1250 ? '#F4C8CA'
: item.battery >= 1000 ? '#F7EBE9'
: item.battery >= 750 ? '#F9DBC1'
: item.battery >= 500 ? '#FBF1E6'
: item.battery >= 250 ? '#E9F5F0'
: '#CEF3DC';
})
console.log("历史用电量详细", calendarTable.value)
}
}
} catch (ex) {
console.error("历史用电量详细出错", ex)
} finally {
setLoading(false);
}
}
/**
* 5分钟定时-历史用电量详细
*
*/
const { resume: startInterval, pause: stopInterval } = useIntervalFn(async () => {
console.log("5分钟定时-历史用电量详细")
await fetchData();
}, 1000 * 60 * 5)
/**
* 组件加载完成
*/
onMounted(async () => {
await fetchData();
startInterval();
})
/**
* 组件卸载
*/
onUnmounted(() => {
stopInterval()
})
</script>
<style lang="less" scoped>
@gap: 8px;
@sub-active-color: #dbf0ff;
@active-color: rgb(var(--primary-5));
@gray: #979797;
.button {
height: 28px;
font-size: 12px;
background: #fff;
margin: 0;
padding: 0 16px;
border: 1px solid rgba(@gray, 0.4);
border-radius: 14px;
cursor: pointer;
&:hover {
color: @active-color;
}
&:active {
background-color: rgba(0, 0, 0, 6%);
}
&:disabled {
color: @gray;
background-color: rgba(0, 0, 0, 6.5%);
cursor: not-allowed;
}
}
.button-group {
.button:first-child {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.button:last-child {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
border-left: 0;
}
.button:not(:first-child, :last-child) {
border-radius: 0;
border-left: 0;
}
}
.calendar-operate {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 0;
font-size: 16px;
position: relative;
:hover {
cursor: pointer;
}
&__title {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
margin: 0 8px;
}
}
.calendar-header {
margin-top: 6px;
//padding: 8px 0;
font-size: 14px;
font-weight: bold;
display: flex;
&__item {
flex: 1;
text-align: center;
border-radius: 1px;
&.gray {
color: @gray;
font-weight: normal;
}
}
}
.calendar-content {
display: flex;
flex-wrap: wrap;
margin: 10px 0;
position: relative;
color: #333;
&__battery {
text-align: left;
color: red;
}
&__item {
min-width: 60px;
height: 45px;
display: flex;
flex-direction: column;
justify-content: space-between;
flex: calc(14.2% - @gap);
border-radius: 3px;
box-sizing: border-box;
text-align: right;
padding: 4px;
margin: 4px;
user-select: none;
transition: all 0.2s ease;
&:nth-child(7n) {
margin-right: 0;
}
.active {
text-align: right;
span {
display: inline-block;
background: white;
padding: 0 5px;
border-radius: 3px;
}
//border-bottom: 2px solid @active-color;
}
&.light {
color: rgba(@gray);
cursor: pointer;
}
&.light:hover {
background-color: rgba(@sub-active-color, 0.4);
}
}
}
.arco-card {
border-radius: 5px;
height: 460px;
}
.illustrate-box {
display: flex;
align-items: center;
justify-content: flex-start;
user-select: none;
.title {
width: 100px;
}
.process {
color: #1d2129;
width: calc(100% - 100px);
display: flex;
div {
width: 25%;
margin-right: 10px;
padding: 2px 0;
}
}
}
</style>