仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ templates/tdesign/spa/src/user/views/stats/SiteStatsView.vue 8.3 KB · 314 行 原始文件
Z zfhsh first commit 2 个月前
1<template>
2 <div class="td-page">
3 <div class="td-page-head">
4 <div>
5 <h3 class="td-page-title"><i class="mdi mdi-chart-line"></i>站点统计</h3>
6 <p class="td-page-subtitle">查看访问量、流量等站点统计信息</p>
7 </div>
8 <div class="td-head-actions">
9 <t-select v-model="range" style="width: 130px" @change="onRangeChange">
10 <t-option value="today" label="今日" />
11 <t-option value="yesterday" label="昨日" />
12 <t-option value="7days" label="近7天" />
13 <t-option value="30days" label="近30天" />
14 </t-select>
15 <t-button theme="default" variant="outline" @click="loadAll">
16 <i class="mdi mdi-refresh"></i> 刷新
17 </t-button>
18 </div>
19 </div>
20
21 <!-- 数字卡片汇总 -->
22 <section class="stat-row" v-if="overview">
23 <div class="stat-card" v-for="card in summaryCards" :key="card.label">
24 <div class="stat-card-body">
25 <div class="stat-label">{{ card.label }}</div>
26 <div class="stat-num" :style="{ color: card.color }">{{ card.value }}</div>
27 <div class="stat-foot">
28 <i class="mdi" :class="card.footIcon"></i>
29 {{ card.foot }}
30 </div>
31 </div>
32 <div class="stat-card-icon" :style="{ background: card.bg, color: card.color }">
33 <i class="mdi" :class="card.icon"></i>
34 </div>
35 </div>
36 </section>
37
38 <t-loading v-if="overviewLoading && !overview" text="加载概览中…" size="large" />
39
40 <!-- 明细列表 -->
41 <div class="td-table-wrap">
42 <div class="td-toolbar">
43 <t-radio-group v-model="detailTab" variant="default-filled" size="small" @change="onTabChange">
44 <t-radio-button value="uri_rank">访问路径</t-radio-button>
45 <t-radio-button value="ip_rank">IP 排行</t-radio-button>
46 <t-radio-button value="errors">错误日志</t-radio-button>
47 </t-radio-group>
48 <div class="td-toolbar-spacer"></div>
49 <t-button theme="default" variant="text" @click="loadDetail">
50 <i class="mdi mdi-refresh"></i>
51 </t-button>
52 </div>
53
54 <t-table
55 row-key="id"
56 :data="rows"
57 :columns="columns"
58 :loading="detailLoading"
59 :pagination="pagination"
60 table-layout="auto"
61 stripe
62 bordered
63 @page-change="onPageChange"
64 >
65 <template #empty>
66 <div class="td-empty">
67 <i class="mdi mdi-database-search-outline"></i>
68 暂无统计数据
69 </div>
70 </template>
71 </t-table>
72 </div>
73 </div>
74</template>
75
76<script setup>
77import { ref, reactive, computed, onMounted } from 'vue'
78import { MessagePlugin } from 'tdesign-vue-next'
79import { getSiteStats } from '@/user/api/stats'
80
81const overview = ref(null)
82const overviewLoading = ref(false)
83const detailLoading = ref(false)
84const rows = ref([])
85const range = ref('today')
86const detailTab = ref('uri_rank')
87
88const pagination = reactive({
89 current: 1,
90 pageSize: 15,
91 total: 0,
92 showJumper: true,
93})
94
95const columns = computed(() => {
96 if (detailTab.value === 'ip_rank') {
97 return [
98 { colKey: 'ip', title: 'IP 地址', minWidth: 140 },
99 { colKey: 'count', title: '请求数', width: 120 },
100 { colKey: 'traffic', title: '流量', width: 120 },
101 ]
102 }
103 if (detailTab.value === 'errors') {
104 return [
105 { colKey: 'time', title: '时间', width: 160 },
106 { colKey: 'status', title: '状态码', width: 90 },
107 { colKey: 'uri', title: '路径', minWidth: 200, ellipsis: true },
108 { colKey: 'ip', title: 'IP', width: 130 },
109 ]
110 }
111 // uri_rank 默认
112 return [
113 { colKey: 'uri', title: '访问路径', minWidth: 200, ellipsis: true },
114 { colKey: 'count', title: '请求数', width: 120 },
115 { colKey: 'traffic', title: '流量', width: 120 },
116 ]
117})
118
119const summaryCards = computed(() => {
120 const d = overview.value
121 if (!d) return []
122 const totalReq = Number(d.total_requests || d.requests || d.count || 0)
123 const totalIp = Number(d.unique_ips || d.ips || 0)
124 const totalTraffic = Number(d.total_traffic || d.traffic || 0)
125 const totalErr = Number(d.error_count || d.errors || 0)
126 return [
127 {
128 label: '总请求数',
129 value: totalReq.toLocaleString(),
130 icon: 'mdi-file-multiple-outline',
131 color: '#0052d9',
132 bg: '#e8f3ff',
133 footIcon: 'mdi-chart-bell-curve',
134 foot: rangeText(),
135 },
136 {
137 label: '独立 IP',
138 value: totalIp.toLocaleString(),
139 icon: 'mdi-account-multiple-outline',
140 color: '#2ba471',
141 bg: '#e8f8f0',
142 footIcon: 'mdi-eye-outline',
143 foot: 'IP 访问',
144 },
145 {
146 label: '总流量',
147 value: fmtFlow(totalTraffic),
148 icon: 'mdi-cloud-download-outline',
149 color: '#e37318',
150 bg: '#fff3e0',
151 footIcon: 'mdi-database',
152 foot: '字节累计',
153 },
154 {
155 label: '错误请求',
156 value: totalErr.toLocaleString(),
157 icon: 'mdi-alert-circle-outline',
158 color: '#d54941',
159 bg: '#fdecee',
160 footIcon: 'mdi-alert',
161 foot: '错误数',
162 },
163 ]
164})
165
166function rangeText() {
167 const m = { today: '今日', yesterday: '昨日', '7days': '近7天', '30days': '近30天' }
168 return m[range.value] || range.value
169}
170
171function fmtFlow(v) {
172 const n = Number(v) || 0
173 if (n === 0) return '0 B'
174 const units = ['B', 'KB', 'MB', 'GB', 'TB']
175 const pow = Math.min(Math.floor(Math.log(n) / Math.log(1024)), units.length - 1)
176 return (n / Math.pow(1024, pow)).toFixed(2) + ' ' + units[pow]
177}
178
179function onRangeChange() {
180 pagination.current = 1
181 loadAll()
182}
183
184function onTabChange() {
185 pagination.current = 1
186 loadDetail()
187}
188
189async function loadOverview() {
190 overviewLoading.value = true
191 const r = await getSiteStats('overview', range.value)
192 overviewLoading.value = false
193 if (r.ok && r.data) {
194 // 后端透传宝塔插件响应,数据可能在 data.msg 或 data 本身
195 const d = r.data
196 overview.value = d.msg || d.data || d
197 } else {
198 overview.value = null
199 }
200}
201
202async function loadDetail() {
203 detailLoading.value = true
204 const r = await getSiteStats(detailTab.value, range.value)
205 detailLoading.value = false
206 if (r.ok && r.data) {
207 const d = r.data
208 const msg = d.msg || d.data || d
209 const list = Array.isArray(msg) ? msg : (msg.list || msg.rows || msg.items || [])
210 rows.value = list
211 pagination.total = msg.total || d.total || list.length
212 } else {
213 rows.value = []
214 pagination.total = 0
215 if (!r.ok && r.message && r.message !== 'ok') {
216 MessagePlugin.warning(r.message)
217 }
218 }
219}
220
221async function loadAll() {
222 await Promise.all([loadOverview(), loadDetail()])
223}
224
225function onPageChange(p) {
226 pagination.current = p.current
227 pagination.pageSize = p.pageSize
228 loadDetail()
229}
230
231onMounted(loadAll)
232</script>
233
234<style scoped>
235.td-head-actions {
236 display: flex;
237 align-items: center;
238 gap: 8px;
239 flex-wrap: wrap;
240}
241
242/* ============== 统计卡片 ============== */
243.stat-row {
244 display: grid;
245 grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
246 gap: 14px;
247 margin-bottom: 18px;
248}
249.stat-card {
250 background: var(--td-surface);
251 border: 1px solid var(--td-border);
252 border-radius: var(--td-radius-lg);
253 padding: 18px 20px;
254 display: flex;
255 align-items: center;
256 justify-content: space-between;
257 gap: 14px;
258 box-shadow: var(--td-shadow-sm);
259 transition: transform var(--td-dur) var(--td-ease),
260 box-shadow var(--td-dur) var(--td-ease);
261 animation: td-fade-in var(--td-dur-lg) var(--td-ease-out);
262}
263.stat-card:hover {
264 transform: translateY(-2px);
265 box-shadow: var(--td-shadow-md);
266}
267.stat-card-body {
268 flex: 1;
269 min-width: 0;
270}
271.stat-label {
272 font-size: 12px;
273 color: var(--td-text-secondary);
274 margin-bottom: 4px;
275}
276.stat-num {
277 font-size: 24px;
278 font-weight: 700;
279 line-height: 1.1;
280 font-variant-numeric: tabular-nums;
281 letter-spacing: -0.3px;
282 word-break: break-all;
283}
284.stat-foot {
285 font-size: 11px;
286 color: var(--td-text-placeholder);
287 margin-top: 6px;
288 display: flex;
289 align-items: center;
290 gap: 4px;
291}
292.stat-card-icon {
293 width: 44px;
294 height: 44px;
295 border-radius: 12px;
296 display: grid;
297 place-items: center;
298 font-size: 22px;
299 flex-shrink: 0;
300}
301
302.td-empty {
303 text-align: center;
304 padding: 36px 16px;
305 color: var(--td-text-placeholder);
306 font-size: 13px;
307}
308.td-empty i {
309 font-size: 36px;
310 display: block;
311 margin-bottom: 8px;
312 color: #cbd5e1;
313}
314</style>