IndexChart.vue
5.8 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
<template>
<div class="p-4">
<ChartGroupCard class="enter-y" :loading="loading" type="chart" :statistics="statistics" />
<SaleTabCard class="!my-4 enter-y" :loading="loading" :barData="barData" :buttonStats="buttonStats" />
<a-row>
<a-col :span="24">
<a-card :loading="loading" :bordered="false" title="最近一周访问量统计">
<div class="infoArea">
<HeadInfo title="今日IP" :iconColor="ipColor" :content="loginfo.todayIp" icon="environment" />
<HeadInfo title="今日访问" :iconColor="visitColor" :content="loginfo.todayVisitCount" icon="team" />
<HeadInfo title="总访问量" :iconColor="seriesColor" :content="loginfo.totalVisitCount" icon="rise" />
</div>
<!-- <LineMulti :chartData="lineMultiData" height="33vh" type="line" :option="{ legend: { top: 'bottom' } }" />-->
</a-card>
</a-col>
</a-row>
</div>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref, watch } from 'vue';
import ChartGroupCard from '../components/ChartGroupCard.vue';
import SaleTabCard from '../components/SaleTabCard.vue';
import HeadInfo from '/@/components/chart/HeadInfo.vue';
import { getLoginfo, getStatistics, getVisitInfo } from '../api';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
const loading = ref(true);
const { getThemeColor } = useRootSetting();
// 使用实时数据对象
const statistics = ref({
todayCount: 0,
yesterdayCount: 0,
growthRate: 0,
rejectedCount: 0,
totalCount: 0,
averageCount: 0,
buttonStats: [] as { name: string; total: number }[],
monthlyData: [] as { month: string; count: number }[],
});
// 实时数据获取函数
async function fetchStatistics() {
try {
const res = await getStatistics();
console.log('接收到的统计数据:', res); // 添加日志输出
console.log('res是否有success属性:', 'success' in res);
// 直接使用响应数据,不检查 success 属性
statistics.value = {
todayCount: res.todayCount || res.data?.todayCount || 0,
yesterdayCount: res.yesterdayCount || 0,
growthRate: res.growthRate || 0,
rejectedCount: res.rejectedCount || res.data?.rejectedCount || 0,
totalCount: res.totalCount || res.data?.totalCount || 0,
averageCount: res.averageCount || 0,
buttonStats: res.buttonStats || [],
monthlyData: res.monthlyData || res.data?.monthlyData || generateMonthlyData(),
};
console.log('更新后的 statistics:', statistics.value);
} catch (e) {
console.error('获取统计数据失败', e);
}
}
// 生成模拟月度数据(当后端未提供时)
function generateMonthlyData() {
return Array.from({ length: 12 }, (_, i) => ({
name: `${i + 1}月`,
value: Math.floor(Math.random() * 1000) + 200,
}));
}
// 转换按钮统计数据格式
const buttonStats = computed(() => {
return statistics.value.buttonStats.map((item) => ({
name: item.name,
total: item.total,
}));
});
// 在 setup 中添加转换函数
const barData = computed(() => {
if (!statistics.value.monthlyData) return [];
return statistics.value.monthlyData.map((item) => {
// 提取月份数字 (格式如 "2025-05" -> 5)
const monthNum = parseInt(item.month.split('-')[1]);
return {
name: `${monthNum}月`,
value: item.count,
};
});
});
onMounted(() => {
fetchStatistics();
});
setTimeout(() => {
loading.value = false;
}, 500);
const loginfo = ref({});
const lineMultiData = ref([]);
function initLogInfo() {
getLoginfo(null).then((res) => {
if (res.success) {
Object.keys(res.result).forEach((key) => {
res.result[key] = res.result[key] + '';
});
loginfo.value = res.result;
}
});
getVisitInfo(null).then((res) => {
if (res.success) {
lineMultiData.value = [];
res.result.forEach((item) => {
lineMultiData.value.push({ name: item.type, type: 'ip', value: item.ip, color: ipColor.value });
lineMultiData.value.push({ name: item.type, type: 'visit', value: item.visit, color: visitColor.value });
});
}
});
}
const ipColor = ref();
const visitColor = ref();
const seriesColor = ref();
watch(
() => getThemeColor.value,
() => {
seriesColor.value = getThemeColor.value;
visitColor.value = '#67B962';
ipColor.value = getThemeColor.value;
initLogInfo();
},
{ immediate: true }
);
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
<style lang="less" scoped>
.infoArea {
display: flex;
justify-content: space-between;
padding: 0 10%;
.head-info.center {
padding: 0;
}
.head-info {
min-width: 0;
}
}
.circle-cust {
position: relative;
top: 28px;
left: -100%;
}
.extra-wrapper {
line-height: 55px;
padding-right: 24px;
.extra-item {
display: inline-block;
margin-right: 24px;
a {
margin-left: 24px;
}
}
}
/* 首页访问量统计 */
.head-info {
position: relative;
text-align: left;
padding: 0 32px 0 0;
min-width: 125px;
&.center {
text-align: center;
padding: 0 32px;
}
span {
color: rgba(0, 0, 0, 0.45);
display: inline-block;
font-size: 0.95rem;
line-height: 42px;
margin-bottom: 4px;
}
p {
line-height: 42px;
margin: 0;
a {
font-weight: 600;
font-size: 1rem;
}
}
}
.ant-card {
::v-deep(.ant-card-head-title) {
font-weight: normal;
}
}
</style>