Files
ReelShortWeb/src/views/dashboard/DashboardMetricWall.vue
T
2026-10-30 17:14:51 +08:00

456 lines
11 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!-- Author: fruit.com.cn -->
<!-- 工作台可视化大屏:用户、设备、留存与内容趋势。 -->
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import type { ECharts } from 'echarts/core'
import type { DashboardOverview, DashboardTrend } from '@/api/admin/dashboard'
import { formatCount } from '@/utils/format'
const props = defineProps<{
overview: DashboardOverview | null
trend?: DashboardTrend | null
fallback?: Record<string, number>
}>()
const trendRef = ref<HTMLElement>()
const deviceRef = ref<HTMLElement>()
const clock = ref('')
let trendChart: ECharts | null = null
let deviceChart: ECharts | null = null
let echartsPromise: Promise<typeof import('@/plugins/echarts').default> | null = null
let active = true
let timer = 0
const AXIS = '#909399'
const SPLIT = '#e6e8eb'
const BRAND = '#c4322a'
const AMBER = '#e9aa4b'
const kpis = computed(() => [
{ label: '总用户数', value: text('user', 'total'), accent: BRAND },
{ label: '今日新增', value: text('user', 'newToday'), accent: '#55b987' },
{ label: '近 7 天新增', value: text('user', 'new7d'), accent: AMBER },
{ label: '近 7 天活跃', value: text('user', 'active7d'), accent: '#d3615b' },
{ label: '累计播放', value: text('playback', 'viewTotal'), accent: BRAND },
{ label: '已上线短剧', value: text('content', 'dramaPublished'), accent: '#55b987' }
])
const rails = computed(() => [
{ label: '短剧总数', value: text('content', 'dramaTotal') },
{ label: '待审核', value: text('content', 'dramaReviewing') },
{ label: '有片源', value: text('content', 'episodeWithMedia') },
{ label: '近 7 天播放', value: text('playback', 'playEvents7d') },
{ label: '完播率', value: rate('playback', 'finishRate') },
{ label: '金币消耗', value: text('revenue', 'coinConsumed') },
{ label: '转码中', value: text('cost', 'transcodeRunning') },
{ label: '转码排队', value: text('cost', 'transcodeQueued') }
])
const retentionDay = computed(() => percent('user', 'retention1d'))
const retentionWeek = computed(() => percent('user', 'retention7d'))
function metric(group: string, key: string) {
const source = props.overview?.[group as keyof DashboardOverview]
if (source && typeof source === 'object') {
const value = (source as Record<string, number | null>)[key]
if (value !== undefined && value !== null) return Number(value)
}
const fallback = props.fallback?.[`${group}.${key}`]
return fallback == null ? null : Number(fallback)
}
function text(group: string, key: string) {
const value = metric(group, key)
return value == null || Number.isNaN(value) ? '--' : formatCount(value)
}
function percent(group: string, key: string) {
const value = metric(group, key)
if (value == null || Number.isNaN(value)) return null
return Math.max(0, Math.min(100, Math.round(value * 1000) / 10))
}
function rate(group: string, key: string) {
const value = percent(group, key)
return value == null ? '--' : `${value.toFixed(1)}%`
}
function tick() {
clock.value = new Date().toLocaleString('zh-CN', { hour12: false })
}
async function loadEcharts() {
echartsPromise ??= import('@/plugins/echarts').then((module) => module.default)
return echartsPromise
}
async function renderCharts() {
const echarts = await loadEcharts()
if (!active) return
renderTrend(echarts)
renderDevices(echarts)
}
function renderTrend(echarts: typeof import('@/plugins/echarts').default) {
if (!trendRef.value) return
trendChart = trendChart ?? echarts.init(trendRef.value)
const trend = props.trend
trendChart.setOption({
backgroundColor: 'transparent',
tooltip: {
trigger: 'axis',
backgroundColor: '#fff',
borderColor: '#e6e8eb',
textStyle: { color: '#303640' }
},
legend: { top: 0, left: 0, textStyle: { color: AXIS }, itemWidth: 12, itemHeight: 8 },
grid: { left: 42, right: 16, top: 32, bottom: 24 },
xAxis: {
type: 'category',
data: trend?.dates ?? [],
axisLine: { lineStyle: { color: 'rgba(255,255,255,.16)' } },
axisLabel: { color: AXIS, fontSize: 11 }
},
yAxis: {
type: 'value',
splitLine: { lineStyle: { color: SPLIT, type: 'dashed' } },
axisLabel: { color: AXIS, fontSize: 11 }
},
series: [
{
name: '新增用户',
type: 'line',
smooth: true,
showSymbol: false,
data: trend?.newUsers ?? [],
lineStyle: { width: 2, color: BRAND },
areaStyle: { color: 'rgba(196, 50, 42, 0.12)' }
},
{
name: '播放事件',
type: 'line',
smooth: true,
showSymbol: false,
data: trend?.playEvents ?? [],
lineStyle: { width: 2, color: AMBER }
}
]
})
}
function renderDevices(echarts: typeof import('@/plugins/echarts').default) {
if (!deviceRef.value) return
deviceChart = deviceChart ?? echarts.init(deviceRef.value)
const android = metric('user', 'androidActive7d') ?? 0
const ios = metric('user', 'iosActive7d') ?? 0
deviceChart.setOption({
backgroundColor: 'transparent',
tooltip: {
trigger: 'item',
backgroundColor: '#fff',
borderColor: '#e6e8eb',
textStyle: { color: '#303640' }
},
legend: { bottom: 0, textStyle: { color: AXIS }, itemWidth: 10, itemHeight: 10 },
title: {
text: formatCount(android + ios),
subtext: '活跃设备',
left: 'center',
top: '38%',
textStyle: { color: '#303640', fontSize: 22, fontWeight: 700 },
subtextStyle: { color: AXIS, fontSize: 12 }
},
series: [
{
type: 'pie',
radius: ['58%', '76%'],
center: ['50%', '46%'],
label: { show: false },
data: [
{ name: 'Android', value: android, itemStyle: { color: '#55b987' } },
{ name: 'iOS', value: ios, itemStyle: { color: BRAND } }
]
}
]
})
}
function onResize() {
trendChart?.resize()
deviceChart?.resize()
}
watch(
() => [props.overview, props.trend],
() => nextTick(renderCharts),
{ deep: true }
)
onMounted(() => {
tick()
timer = window.setInterval(tick, 1000)
window.addEventListener('resize', onResize)
nextTick(renderCharts)
})
onBeforeUnmount(() => {
active = false
window.clearInterval(timer)
window.removeEventListener('resize', onResize)
trendChart?.dispose()
deviceChart?.dispose()
})
</script>
<template>
<section class="screen">
<header class="screen-head">
<h2>运营概览</h2>
<time>{{ clock }}</time>
</header>
<div class="kpi-row">
<article v-for="item in kpis" :key="item.label" class="kpi">
<i :style="{ background: item.accent }"></i>
<div>
<strong>{{ item.value }}</strong>
<span>{{ item.label }}</span>
</div>
</article>
</div>
<div class="viz-row">
<article class="panel trend-panel">
<h3>近 7 日新增与播放</h3>
<div ref="trendRef" class="chart"></div>
</article>
<article class="panel">
<h3>活跃设备占比</h3>
<div ref="deviceRef" class="chart"></div>
</article>
<article class="panel retain">
<h3>用户留存</h3>
<div class="rings">
<div class="ring" :style="{ '--p': `${retentionDay ?? 0}%` }">
<strong>{{ retentionDay == null ? '--' : `${retentionDay.toFixed(1)}%` }}</strong>
<span>次日留存</span>
</div>
<div class="ring week" :style="{ '--p': `${retentionWeek ?? 0}%` }">
<strong>{{ retentionWeek == null ? '--' : `${retentionWeek.toFixed(1)}%` }}</strong>
<span>7 日留存</span>
</div>
</div>
</article>
</div>
<div class="rail">
<div v-for="item in rails" :key="item.label">
<strong>{{ item.value }}</strong>
<span>{{ item.label }}</span>
</div>
</div>
</section>
</template>
<style scoped lang="less">
.screen {
display: flex;
flex-direction: column;
gap: 12px;
min-width: 0;
min-height: 0;
color: #303640;
}
.screen-head {
display: flex;
justify-content: space-between;
align-items: center;
h2 {
margin: 0;
font-size: 14px;
font-weight: 600;
color: #2f343c;
}
}
time {
font-size: 12px;
color: #909399;
font-variant-numeric: tabular-nums;
}
.kpi-row,
.viz-row,
.rail {
min-width: 0;
}
.kpi-row {
display: grid;
grid-template-columns: repeat(6, minmax(0, 1fr));
gap: 12px;
}
.kpi {
display: flex;
gap: 10px;
align-items: center;
min-width: 0;
min-height: 72px;
padding: 12px 14px;
background: #fff;
border: 1px solid #e6e8eb;
border-radius: 6px;
i {
width: 3px;
height: 32px;
border-radius: 3px;
flex: none;
}
strong {
display: block;
overflow: hidden;
font-size: 22px;
line-height: 1.1;
color: #303640;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
span {
display: block;
margin-top: 4px;
font-size: 12px;
color: #909399;
}
}
.viz-row {
display: grid;
grid-template-columns: minmax(0, 1.4fr) minmax(0, 0.8fr) minmax(0, 0.7fr);
gap: 12px;
flex: 1;
min-height: 220px;
}
.panel {
display: flex;
flex-direction: column;
min-width: 0;
min-height: 0;
padding: 12px 14px 8px;
background: #fff;
border: 1px solid #e6e8eb;
border-radius: 6px;
h3 {
margin: 0;
font-size: 13px;
font-weight: 600;
color: #2f343c;
}
}
.chart {
width: 100%;
height: 240px;
}
.rings {
display: flex;
flex: 1;
justify-content: space-around;
align-items: center;
min-height: 180px;
}
.ring {
--p: 0%;
display: grid;
width: 112px;
height: 112px;
background: conic-gradient(#c4322a var(--p), #f5d9d8 0);
border-radius: 50%;
place-items: center;
&::after {
width: 82px;
height: 82px;
background: #fff;
border-radius: 50%;
content: '';
grid-area: 1 / 1;
}
strong,
span {
grid-area: 1 / 1;
z-index: 1;
}
strong {
margin-top: -12px;
font-size: 16px;
color: #303640;
}
span {
margin-top: 24px;
font-size: 12px;
color: #909399;
}
}
.week {
background: conic-gradient(#e9aa4b var(--p), #fff5e7 0);
}
.rail {
display: grid;
grid-template-columns: repeat(8, minmax(0, 1fr));
gap: 12px;
div {
min-width: 0;
padding: 10px 8px;
text-align: center;
background: #fff;
border: 1px solid #e6e8eb;
border-radius: 6px;
}
strong {
display: block;
overflow: hidden;
font-size: 16px;
color: #303640;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
span {
font-size: 12px;
color: #909399;
}
}
@media (width <= 760px) {
.kpi-row {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.viz-row,
.rail {
grid-template-columns: minmax(0, 1fr);
}
}
</style>