124 lines
3.6 KiB
Vue
124 lines
3.6 KiB
Vue
<script setup lang="ts">
|
||
/** 首页:只统计「当前主体」的数据(设备 / 通道 / 扫码记录 + 最近一次扫码) */
|
||
import { onMounted, ref } from 'vue';
|
||
import { RouterLink } from 'vue-router';
|
||
import AppLayout from '@/components/AppLayout.vue';
|
||
import { ApiError } from '@/api/client';
|
||
import { fetchStats } from '@/api/web';
|
||
import type { Stats } from '@/api/types';
|
||
import { hasSubject } from '@/composables/useSubject';
|
||
import { formatTime, fromNow } from '@/utils/format';
|
||
|
||
const stats = ref<Stats | null>(null);
|
||
const error = ref('');
|
||
const loading = ref(true);
|
||
const noSubject = ref(!hasSubject.value);
|
||
|
||
onMounted(async () => {
|
||
if (noSubject.value) {
|
||
loading.value = false;
|
||
return;
|
||
}
|
||
try {
|
||
stats.value = await fetchStats();
|
||
} catch (err) {
|
||
error.value = err instanceof ApiError ? err.message : '加载失败';
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<AppLayout>
|
||
<template #title>首页</template>
|
||
|
||
<div v-if="error" class="alert alert-error">{{ error }}</div>
|
||
|
||
<div v-else-if="noSubject" class="card">
|
||
<h3>先创建一个主体</h3>
|
||
<p class="hint">
|
||
主体(租户)是设备、录像机、摄像头、工作台、扫码记录的归属单位。
|
||
创建主体并选择它之后,才会出现这些菜单。新主体默认赠送 7 天使用期限。
|
||
</p>
|
||
<RouterLink class="btn" to="/subjects">去创建主体</RouterLink>
|
||
</div>
|
||
|
||
<p v-else-if="loading" class="hint">加载中…</p>
|
||
|
||
<template v-else-if="stats">
|
||
<div class="stat-grid">
|
||
<RouterLink class="card stat" to="/devices">
|
||
<span class="stat-label">我的设备</span>
|
||
<b class="stat-value">{{ stats.deviceCount }}</b>
|
||
<span class="stat-foot">台(中控)</span>
|
||
</RouterLink>
|
||
<div class="card stat">
|
||
<span class="stat-label">已登记通道</span>
|
||
<b class="stat-value">{{ stats.channelCount }}</b>
|
||
<span class="stat-foot">个</span>
|
||
</div>
|
||
<RouterLink class="card stat" to="/records">
|
||
<span class="stat-label">扫码记录</span>
|
||
<b class="stat-value">{{ stats.recordCount }}</b>
|
||
<span class="stat-foot">条</span>
|
||
</RouterLink>
|
||
</div>
|
||
|
||
<div class="card">
|
||
<h3>最近一次扫码</h3>
|
||
<table v-if="stats.latestRecord" class="table">
|
||
<tbody>
|
||
<tr>
|
||
<th style="width: 120px">条码</th>
|
||
<td><code>{{ stats.latestRecord.barcode }}</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th>通道</th>
|
||
<td>{{ stats.latestRecord.channelId }}</td>
|
||
</tr>
|
||
<tr>
|
||
<th>设备</th>
|
||
<td>{{ stats.latestRecord.deviceSn || '—' }}</td>
|
||
</tr>
|
||
<tr>
|
||
<th>扫码时间</th>
|
||
<td>{{ formatTime(stats.latestRecord.scanTime) }}({{ fromNow(stats.latestRecord.scanTime) }})</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p v-else class="hint">还没有扫码记录。设备扫码登录后,用「测试页」可以模拟上报一条。</p>
|
||
</div>
|
||
</template>
|
||
</AppLayout>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.stat-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||
gap: 16px;
|
||
}
|
||
.stat {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 6px;
|
||
}
|
||
.stat-label {
|
||
color: var(--text-muted);
|
||
font-size: 13px;
|
||
}
|
||
.stat-value {
|
||
font-size: 30px;
|
||
line-height: 1.1;
|
||
}
|
||
.stat-foot {
|
||
color: var(--text-muted);
|
||
font-size: 12px;
|
||
}
|
||
h3 {
|
||
margin: 0 0 12px;
|
||
font-size: 15px;
|
||
}
|
||
</style>
|