Files
web_escort_admin/src/views/dashboard/index.vue
T

370 lines
12 KiB
Vue

<template>
<PageBreadcrumb title="工作台" subtitle="首页" />
<div class="container-xxl">
<BRow class="g-3">
<BCol md="6" xl="3" v-for="item in statCards" :key="item.label">
<RouterLink :to="item.to" class="text-reset text-decoration-none">
<BCard no-body class="h-100">
<BCardBody>
<div class="d-flex align-items-center justify-content-between">
<div>
<p class="text-muted mb-1">{{ item.label }}</p>
<h2 class="mb-0 fw-semibold">{{ item.value }}</h2>
</div>
<div class="avatar-lg d-flex align-items-center justify-content-center rounded-circle" :class="`bg-${item.variant}-subtle text-${item.variant}`">
<Icon :icon="item.icon" class="fs-28" />
</div>
</div>
<p class="mb-0 mt-3 fs-12 text-muted">{{ item.hint }}</p>
</BCardBody>
</BCard>
</RouterLink>
</BCol>
</BRow>
<BRow class="g-3 mt-1">
<BCol xl="8">
<BCard no-body class="h-100">
<BCardHeader>
<h5 class="card-title mb-0">近 7 日预约趋势</h5>
</BCardHeader>
<BCardBody>
<div v-if="loading" class="text-center py-5">
<BSpinner variant="primary" />
</div>
<EChart v-else :get-options="() => trendOptions" :extensions="CHART_EXTENSIONS" style="height: 280px; width: 100%" />
</BCardBody>
</BCard>
</BCol>
<BCol xl="4">
<BCard no-body class="h-100">
<BCardHeader>
<h5 class="card-title mb-0">订单状态分布</h5>
</BCardHeader>
<BCardBody>
<div v-if="loading" class="text-center py-5">
<BSpinner variant="primary" />
</div>
<EChart v-else :get-options="() => statusOptions" :extensions="CHART_EXTENSIONS" style="height: 280px; width: 100%" />
</BCardBody>
</BCard>
</BCol>
</BRow>
<BRow class="g-3 mt-1">
<BCol xl="8">
<BCard no-body class="h-100">
<BCardHeader class="d-flex justify-content-between align-items-center">
<h5 class="card-title mb-0">近期订单</h5>
<RouterLink to="/orders" class="btn btn-sm btn-light">
查看全部
<Icon icon="arrow-right" class="ms-1" />
</RouterLink>
</BCardHeader>
<div v-if="loading" class="text-center py-5">
<BSpinner variant="primary" />
</div>
<BTable
v-else
show-empty
empty-text="暂无订单"
:items="recent"
:fields="recentFields"
thead-class="bg-light align-middle bg-opacity-25 thead-sm"
hover
responsive
class="table table-custom table-nowrap table-centered mb-0 w-100"
>
<template #head()="data">
<span class="text-uppercase fs-xxs">{{ data.label }}</span>
</template>
<template #cell(patient)="{ item }">
<div>
<RouterLink :to="`/orders/${item._id}`" class="fw-semibold link-reset">
{{ item.patient?.name || '未填写' }}
</RouterLink>
<p class="text-muted mb-0 fs-12">{{ item.patient?.mobile || '-' }}</p>
</div>
</template>
<template #cell(schedule)="{ item }">
<div>{{ formatDate(item.schedule?.date) }}</div>
<p class="text-muted mb-0 fs-12">{{ item.schedule?.startTime || '未指定时间' }}</p>
</template>
<template #cell(hospital)="{ item }">
<div>{{ item.hospital?.name || '-' }}</div>
<p class="text-muted mb-0 fs-12">{{ item.hospital?.department || '未填写科室' }}</p>
</template>
<template #cell(payment)="{ item }">
<div>{{ formatMoney(item.payment?.totalFee) }}</div>
<span class="badge fs-xxs" :class="`badge-soft-${PAYMENT_VARIANTS[item.payment?.status || 'unpaid']}`">
{{ PAYMENT_LABELS[item.payment?.status || 'unpaid'] }}
</span>
</template>
<template #cell(status)="{ item }">
<span class="badge fs-xxs" :class="`badge-soft-${STATUS_VARIANTS[item.status || 'pending']}`">
{{ STATUS_LABELS[item.status || 'pending'] }}
</span>
<span v-if="item.cancelStatus === 'requested'" class="badge fs-xxs badge-soft-danger ms-1">取消申请</span>
</template>
</BTable>
</BCard>
</BCol>
<BCol xl="4">
<BCard no-body class="h-100">
<BCardHeader class="d-flex justify-content-between align-items-center">
<h5 class="card-title mb-0">待处理取消申请</h5>
<span class="badge badge-soft-danger">{{ summary.cancelRequested }}</span>
</BCardHeader>
<div v-if="loading" class="text-center py-5">
<BSpinner variant="primary" />
</div>
<div v-else-if="!cancelRequests.length" class="text-center text-muted py-5">
<Icon icon="circle-check" class="fs-32 mb-2" />
<p class="mb-0">暂无待处理申请</p>
</div>
<div v-else class="list-group list-group-flush">
<RouterLink
v-for="item in cancelRequests"
:key="item._id"
:to="`/orders/${item._id}`"
class="list-group-item list-group-item-action"
>
<div class="d-flex justify-content-between align-items-start">
<div>
<h6 class="mb-1">{{ item.patient?.name || '未填写' }}</h6>
<p class="text-muted mb-1 fs-12">
{{ formatDate(item.schedule?.date) }} {{ item.schedule?.startTime || '' }}
</p>
<p class="text-muted mb-0 fs-12 text-truncate" style="max-width: 220px">
原因:{{ item.cancelReason || '未填写' }}
</p>
</div>
<Icon icon="chevron-right" class="text-muted mt-1" />
</div>
</RouterLink>
</div>
</BCard>
</BCol>
</BRow>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { LineChart, PieChart } from 'echarts/charts'
import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import type { EChartsOption } from 'echarts'
import { CanvasRenderer } from 'echarts/renderers'
import { escortApi } from '~/api/escort'
import EChart from '~/components/wrappers/EChart.vue'
import PageBreadcrumb from '~/components/PageBreadcrumb.vue'
import Icon from '~/components/wrappers/Icon.vue'
import type { EscortRecord } from '~/types/api'
import {
PAYMENT_LABELS,
PAYMENT_VARIANTS,
STATUS_LABELS,
STATUS_VARIANTS,
formatDate,
formatMoney,
today,
} from '~/utils/format'
const CHART_EXTENSIONS = [LineChart, PieChart, GridComponent, TooltipComponent, LegendComponent, CanvasRenderer]
// 状态分布环形图配色(与 badge-soft 语义色对应)
const STATUS_COLORS: Record<string, string> = {
pending: '#f0ad4e',
confirmed: '#4d9ce8',
in_progress: '#5b6ee8',
completed: '#34c38f',
cancelled: '#98a6ad',
}
const loading = ref(true)
const recent = ref<EscortRecord[]>([])
const cancelRequests = ref<EscortRecord[]>([])
const summary = reactive({
today: 0,
pending: 0,
cancelRequested: 0,
unpaid: 0,
})
const statusCounts = reactive<Record<string, number>>({})
const trend = reactive<{ dates: string[]; counts: number[] }>({ dates: [], counts: [] })
const statCards = computed(() => [
{
label: '今日预约订单',
value: summary.today,
hint: '按预约就诊日期统计',
icon: 'calendar-event',
variant: 'primary',
to: `/orders?appointmentDate=${today()}`,
},
{
label: '待确认订单',
value: summary.pending,
hint: '等待平台确认接单',
icon: 'clock-hour-4',
variant: 'warning',
to: '/orders?status=pending',
},
{
label: '待处理取消申请',
value: summary.cancelRequested,
hint: '用户已提交取消,等待审核',
icon: 'alert-triangle',
variant: 'danger',
to: '/orders?cancelStatus=requested',
},
{
label: '未支付订单',
value: summary.unpaid,
hint: '需要跟进收款',
icon: 'credit-card',
variant: 'info',
to: '/orders?paymentStatus=unpaid',
},
])
const recentFields = [
{ key: 'patient', label: '就诊人' },
{ key: 'schedule', label: '预约时间' },
{ key: 'hospital', label: '就诊医院' },
{ key: 'payment', label: '金额 / 支付' },
{ key: 'status', label: '状态' },
]
const trendOptions = computed<EChartsOption>(() => ({
tooltip: { trigger: 'axis' },
grid: { left: 8, right: 16, top: 28, bottom: 0, containLabel: true },
xAxis: {
type: 'category',
boundaryGap: false,
data: trend.dates,
axisTick: { show: false },
},
yAxis: { type: 'value', minInterval: 1, splitLine: { lineStyle: { type: 'dashed' } } },
series: [
{
name: '预约订单',
type: 'line',
smooth: true,
symbolSize: 6,
data: trend.counts,
lineStyle: { width: 2.5 },
areaStyle: { opacity: 0.08 },
itemStyle: { color: '#5b6ee8' },
},
],
}))
const statusOptions = computed<EChartsOption>(() => ({
tooltip: { trigger: 'item' },
legend: { bottom: 0, icon: 'circle', itemWidth: 8, itemHeight: 8, textStyle: { fontSize: 12 } },
series: [
{
name: '订单状态',
type: 'pie',
radius: ['52%', '76%'],
center: ['50%', '44%'],
avoidLabelOverlap: true,
label: { show: false },
itemStyle: { borderRadius: 4, borderColor: '#fff', borderWidth: 2 },
data: Object.keys(STATUS_LABELS).map((status) => ({
name: STATUS_LABELS[status],
value: statusCounts[status] || 0,
itemStyle: { color: STATUS_COLORS[status] },
})),
},
],
}))
const shiftDate = (days: number) => {
const date = new Date()
date.setDate(date.getDate() - days)
return formatDate(date)
}
const load = async () => {
loading.value = true
try {
const [
todayRes,
cancelRes,
unpaidRes,
recentRes,
cancelListRes,
trendRes,
] = await Promise.all([
escortApi.list({ page: 1, pageSize: 1, appointmentDate: today() }),
escortApi.list({ page: 1, pageSize: 1, cancelStatus: 'requested' }),
escortApi.list({ page: 1, pageSize: 1, paymentStatus: 'unpaid' }),
escortApi.list({ page: 1, pageSize: 8 }),
escortApi.list({ page: 1, pageSize: 5, cancelStatus: 'requested' }),
// 近 7 日订单,按预约日期在前端聚合出趋势
escortApi.list({
page: 1,
pageSize: 500,
startDate: shiftDate(6),
endDate: today(),
includeCancelled: '1',
}),
])
summary.today = todayRes.total
summary.cancelRequested = cancelRes.total
summary.unpaid = unpaidRes.total
recent.value = recentRes.records
cancelRequests.value = cancelListRes.records
// 状态分布直接取列表接口的 counts:取消由 counts.cancelled 统计(含历史 status=cancelled 数据),
// 不能按 status=cancelled 查询,取消不在服务状态枚举内
const totals = recentRes.counts
statusCounts.pending = totals.pending || 0
statusCounts.confirmed = totals.confirmed || 0
statusCounts.in_progress = totals.inProgress || 0
statusCounts.completed = totals.completed || 0
statusCounts.cancelled = totals.cancelled || 0
summary.pending = statusCounts.pending
const countsByDate: Record<string, number> = {}
trendRes.records.forEach((item) => {
const date = formatDate(item.schedule?.date)
if (date !== '-') countsByDate[date] = (countsByDate[date] || 0) + 1
})
const dates: string[] = []
const counts: number[] = []
for (let i = 6; i >= 0; i--) {
const date = shiftDate(i)
dates.push(date.slice(5))
counts.push(countsByDate[date] || 0)
}
trend.dates = dates
trend.counts = counts
} catch {
// 错误提示由请求层统一弹出;401 会跳转登录页
} finally {
loading.value = false
}
}
onMounted(load)
</script>