- dashboard 服务新增货主范围(_ownerScope/_scopeBillWhere),概览、趋势、库存流水、 Top 商品、最近流水均按当前账号所属货主收窄;首页加货主范围标签 - 店铺编辑禁止修改所属货主(历史无货主可补齐一次并联动店铺商品),详情返回 subsidiary_name - 更新店铺契约描述
565 lines
18 KiB
Vue
565 lines
18 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onMounted, ref } from 'vue'
|
||
import { useRouter } from 'vue-router'
|
||
import { getDashboardReport, type DashboardBill, type DashboardReport } from '@/api/dashboard'
|
||
import { useAuthStore } from '@/store/auth'
|
||
import { usePrincipalStore } from '@/store/principal'
|
||
import { getAccessContext } from '@/api/access'
|
||
import { formatDateTime } from '@/utils/datetime'
|
||
|
||
/**
|
||
* 仪表盘(报表)
|
||
*
|
||
* 数据来自 GET /api/dashboard/report 一次聚合返回:概览指标、三类单据的状态分布与待办、
|
||
* 近 N 天趋势(单据量 / 出入库数量)、库存 Top 商品、最近出入库流水。
|
||
* 不引入图表库:趋势用 CSS 柱状条渲染(含 tooltip 与合计),保持零额外依赖。
|
||
*/
|
||
const auth = useAuthStore()
|
||
const principalStore = usePrincipalStore()
|
||
const router = useRouter()
|
||
|
||
/** 可选趋势区间(天) */
|
||
const RANGE_OPTIONS = [
|
||
{ label: '近 7 天', value: 7 },
|
||
{ label: '近 30 天', value: 30 },
|
||
{ label: '近 90 天', value: 90 },
|
||
]
|
||
/** 单据量趋势条颜色(按单据类型区分) */
|
||
const BAR_CLASSES: Record<string, string> = {
|
||
delivery: 'bar-delivery',
|
||
inbound: 'bar-inbound',
|
||
outbound: 'bar-outbound',
|
||
inbound_qty: 'bar-inbound',
|
||
outbound_qty: 'bar-outbound',
|
||
}
|
||
|
||
const loading = ref(false)
|
||
const days = ref(7)
|
||
const report = ref<DashboardReport | null>(null)
|
||
/** 是否所在公司权限(看板只统计自己所属公司的数据) */
|
||
const isCompanyScope = ref(false)
|
||
|
||
async function loadAccessContext() {
|
||
try {
|
||
const access = await getAccessContext()
|
||
isCompanyScope.value = !access.is_group
|
||
} catch {
|
||
isCompanyScope.value = false
|
||
}
|
||
}
|
||
|
||
const overview = computed(() => report.value?.overview || null)
|
||
/** 当前账号在当前主体下的名称(与右上角头像旁名称同口径) */
|
||
const accountName = computed(() => {
|
||
const relation = (auth.user?.principals || []).find((p) => p.id === principalStore.currentId)
|
||
return relation?.account_name || relation?.name || auth.user?.nickname || auth.user?.username || '当前账号'
|
||
})
|
||
/** 单据量趋势序列(发货单/入库单/出库单) */
|
||
const billSeries = computed(() =>
|
||
(report.value?.trend.series || []).filter((s) => [ 'delivery', 'inbound', 'outbound' ].includes(s.key)),
|
||
)
|
||
/** 出入库数量趋势序列 */
|
||
const qtySeries = computed(() => (report.value?.trend.series || []).filter((s) => s.key.endsWith('_qty')))
|
||
/** 待办单据总数(各待办项相加) */
|
||
const todoTotal = computed(() => (report.value?.todo || []).reduce((sum, item) => sum + item.count, 0))
|
||
const lastIndex = computed(() => Math.max((report.value?.trend.dates.length || 1) - 1, 0))
|
||
|
||
/** 拉取报表数据 */
|
||
async function fetchReport() {
|
||
loading.value = true
|
||
try {
|
||
report.value = await getDashboardReport({ days: days.value })
|
||
} catch {
|
||
// 错误已由响应拦截器统一提示
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
/** 数值化(后端 DECIMAL/SUM 可能返回字符串) */
|
||
function num(value: unknown): number {
|
||
const n = Number(value ?? 0)
|
||
return Number.isFinite(n) ? n : 0
|
||
}
|
||
|
||
/** 千分位展示 */
|
||
function nf(value: unknown, digits = 0): string {
|
||
return num(value).toLocaleString('zh-CN', { minimumFractionDigits: digits, maximumFractionDigits: digits })
|
||
}
|
||
|
||
/** 趋势条高度(按当前图表最大值归一化,0 保留 2px 底纹便于识别) */
|
||
function barHeight(value: number, max: number): string {
|
||
if (!value) return '2px'
|
||
return `${Math.max(4, Math.round((value / (max || 1)) * 72))}px`
|
||
}
|
||
|
||
/** 序列合计 */
|
||
function seriesTotal(values: number[]): number {
|
||
return values.reduce((sum, value) => sum + num(value), 0)
|
||
}
|
||
|
||
/** 跳转到对应列表页(可带筛选条件;列表页会读取 status 初始化筛选) */
|
||
function goto(path: string | null | undefined, query?: Record<string, unknown> | null) {
|
||
if (!path) return
|
||
const textQuery = query
|
||
? Object.fromEntries(Object.entries(query).map(([ k, v ]) => [ k, String(v) ]))
|
||
: undefined
|
||
router.push({ path, query: textQuery })
|
||
}
|
||
|
||
/** 点击状态跳转到该状态的单据列表 */
|
||
function statusJump(bill: DashboardBill, status: number) {
|
||
router.push({ path: bill.path, query: { status: String(status) } })
|
||
}
|
||
|
||
onMounted(() => {
|
||
fetchReport()
|
||
loadAccessContext()
|
||
})
|
||
</script>
|
||
|
||
<template>
|
||
<div v-loading="loading" class="dashboard">
|
||
<el-card class="dash-card" shadow="never">
|
||
<div class="dash-head">
|
||
<div>
|
||
<div class="dash-title">经营看板</div>
|
||
<div class="dash-sub">
|
||
{{ accountName }}
|
||
<template v-if="report"> · 统计区间 {{ report.range.start_date }} ~ {{ report.range.end_date }}</template>
|
||
<el-tag v-if="isCompanyScope" size="small" type="warning" effect="plain" class="scope-tag">
|
||
仅统计自己所属公司(货主)的数据
|
||
</el-tag>
|
||
</div>
|
||
</div>
|
||
<div class="dash-actions">
|
||
<el-radio-group v-model="days" size="small" @change="fetchReport">
|
||
<el-radio-button v-for="o in RANGE_OPTIONS" :key="o.value" :value="o.value">{{ o.label }}</el-radio-button>
|
||
</el-radio-group>
|
||
<el-button size="small" @click="fetchReport">刷新</el-button>
|
||
</div>
|
||
</div>
|
||
</el-card>
|
||
|
||
<template v-if="report && overview">
|
||
<!-- 概览指标 -->
|
||
<div class="metrics">
|
||
<div class="metric">
|
||
<div class="metric-label">商品 / 店铺 / 仓库</div>
|
||
<div class="metric-value">{{ overview.product_count }} / {{ overview.shop_count }} / {{ overview.warehouse_count }}</div>
|
||
<div class="metric-extra">系统在用的基础资料数量</div>
|
||
</div>
|
||
<div class="metric">
|
||
<div class="metric-label">库存总量</div>
|
||
<div class="metric-value">{{ nf(overview.stock_quantity) }}</div>
|
||
<div class="metric-extra">可用 {{ nf(overview.stock_available) }} · 锁定 {{ nf(overview.stock_locked) }}</div>
|
||
</div>
|
||
<div class="metric">
|
||
<div class="metric-label">正品 / 残次品库存</div>
|
||
<div class="metric-value">{{ nf(overview.stock_zc) }} / {{ nf(overview.stock_cc) }}</div>
|
||
<div class="metric-extra">有库存商品 {{ overview.stock_product_kind }} 种</div>
|
||
</div>
|
||
<div class="metric">
|
||
<div class="metric-label">单据总数</div>
|
||
<div class="metric-value">{{ nf(overview.bill_total) }}</div>
|
||
<div class="metric-extra">发货单 + 入库单 + 出库单</div>
|
||
</div>
|
||
<div class="metric">
|
||
<div class="metric-label">区间内出入库</div>
|
||
<div class="metric-value">{{ nf(report.movement.inbound_qty) }} / {{ nf(report.movement.outbound_qty) }}</div>
|
||
<div class="metric-extra">入库 / 出库数量(按库存流水统计)</div>
|
||
</div>
|
||
<div class="metric">
|
||
<div class="metric-label">近 30 天发货单</div>
|
||
<div class="metric-value">{{ nf(overview.delivery_count_30d) }}</div>
|
||
<div class="metric-extra">金额合计 {{ nf(overview.delivery_amount_30d, 2) }}</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 待办提醒 -->
|
||
<el-card class="dash-card" shadow="never">
|
||
<template #header>
|
||
<div class="card-head">
|
||
<span>待办提醒</span>
|
||
<span class="card-sub">{{ report.todo.length }} 项 / 共 {{ todoTotal }} 张单据</span>
|
||
</div>
|
||
</template>
|
||
<div v-if="report.todo.length" class="todo-list">
|
||
<el-tag
|
||
v-for="item in report.todo"
|
||
:key="item.key"
|
||
class="todo-item"
|
||
effect="plain"
|
||
:type="item.key.includes('push_fail') ? 'danger' : 'warning'"
|
||
@click="goto(item.path, item.query)"
|
||
>
|
||
{{ item.label }} · {{ item.count }}
|
||
</el-tag>
|
||
</div>
|
||
<el-empty v-else description="暂无待办" :image-size="56" />
|
||
</el-card>
|
||
|
||
<!-- 单据状态概览 -->
|
||
<div class="bill-grid">
|
||
<div v-for="bill in report.bills" :key="bill.key" class="bill-card">
|
||
<div class="bill-head">
|
||
<span class="bill-name" @click="goto(bill.path)">{{ bill.label }}</span>
|
||
<span class="bill-total">{{ nf(bill.total) }} 张</span>
|
||
</div>
|
||
<div class="bill-status">
|
||
<span
|
||
v-for="s in bill.by_status"
|
||
:key="s.status"
|
||
class="status-chip"
|
||
:class="{ zero: !s.count }"
|
||
@click="statusJump(bill, s.status)"
|
||
>
|
||
<span class="dot" :class="`dot-${s.status}`" />{{ s.label }} {{ s.count }}
|
||
</span>
|
||
</div>
|
||
<div class="bill-foot">
|
||
<span>区间新增 {{ nf(bill.range_count) }}</span>
|
||
<span v-if="num(bill.amount)">金额 {{ nf(bill.amount, 2) }}</span>
|
||
<span v-if="bill.push_fail" class="danger-text">推送失败 {{ bill.push_fail }}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 趋势 -->
|
||
<div class="two-col">
|
||
<div>
|
||
<el-card class="dash-card" shadow="never">
|
||
<template #header>
|
||
<div class="card-head">
|
||
<span>近 {{ report.trend.days }} 天单据量</span>
|
||
<span class="card-sub">按创建时间统计</span>
|
||
</div>
|
||
</template>
|
||
<div class="chart">
|
||
<div v-for="s in billSeries" :key="s.key" class="chart-row">
|
||
<span class="chart-label">{{ s.label }}</span>
|
||
<div class="chart-bars">
|
||
<el-tooltip
|
||
v-for="(v, i) in s.values"
|
||
:key="i"
|
||
:content="`${report.trend.dates[i]}:${v} ${s.unit}`"
|
||
placement="top"
|
||
>
|
||
<span class="bar" :class="BAR_CLASSES[s.key]" :style="{ height: barHeight(v, report.trend.max_bill) }" />
|
||
</el-tooltip>
|
||
</div>
|
||
<span class="chart-total">{{ seriesTotal(s.values) }}</span>
|
||
</div>
|
||
<div class="chart-axis">
|
||
<span>{{ report.trend.dates[0] }}</span>
|
||
<span>{{ report.trend.dates[lastIndex] }}</span>
|
||
</div>
|
||
</div>
|
||
</el-card>
|
||
</div>
|
||
<div>
|
||
<el-card class="dash-card" shadow="never">
|
||
<template #header>
|
||
<div class="card-head">
|
||
<span>近 {{ report.trend.days }} 天出入库数量</span>
|
||
<span class="card-sub">来自库存流水</span>
|
||
</div>
|
||
</template>
|
||
<div class="chart">
|
||
<div v-for="s in qtySeries" :key="s.key" class="chart-row">
|
||
<span class="chart-label">{{ s.label }}</span>
|
||
<div class="chart-bars">
|
||
<el-tooltip
|
||
v-for="(v, i) in s.values"
|
||
:key="i"
|
||
:content="`${report.trend.dates[i]}:${v} ${s.unit}`"
|
||
placement="top"
|
||
>
|
||
<span class="bar" :class="BAR_CLASSES[s.key]" :style="{ height: barHeight(v, report.trend.max_qty) }" />
|
||
</el-tooltip>
|
||
</div>
|
||
<span class="chart-total">{{ seriesTotal(s.values) }}</span>
|
||
</div>
|
||
<div class="chart-axis">
|
||
<span>{{ report.trend.dates[0] }}</span>
|
||
<span>{{ report.trend.dates[lastIndex] }}</span>
|
||
</div>
|
||
</div>
|
||
</el-card>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 库存 Top 商品 + 最近流水 -->
|
||
<div class="two-col">
|
||
<div>
|
||
<el-card class="dash-card" shadow="never">
|
||
<template #header>
|
||
<div class="card-head">
|
||
<span>库存 Top 商品</span>
|
||
<span class="card-sub">按可用数量倒序</span>
|
||
</div>
|
||
</template>
|
||
<el-table :data="report.top_products" size="small" max-height="240">
|
||
<el-table-column prop="item_code" label="商品编码" min-width="130" show-overflow-tooltip />
|
||
<el-table-column prop="item_name" label="商品名称" min-width="130" show-overflow-tooltip />
|
||
<el-table-column label="类型" width="80">
|
||
<template #default="{ row }">{{ row.item_type === 'CC' ? '残次品' : '正品' }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="可用" width="90" align="right">
|
||
<template #default="{ row }">{{ nf(row.available) }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="锁定" width="90" align="right">
|
||
<template #default="{ row }">{{ nf(row.lock_quantity) }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="总量" width="90" align="right">
|
||
<template #default="{ row }">{{ nf(row.quantity) }}</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
</el-card>
|
||
</div>
|
||
<div>
|
||
<el-card class="dash-card" shadow="never">
|
||
<template #header>
|
||
<div class="card-head">
|
||
<span>最近出入库流水</span>
|
||
<el-button link type="primary" size="small" @click="goto('/stock-record')">查看全部</el-button>
|
||
</div>
|
||
</template>
|
||
<el-table :data="report.recent_records" size="small" max-height="240">
|
||
<el-table-column label="记录时间" width="150">
|
||
<template #default="{ row }">{{ formatDateTime(row.created_at) }}</template>
|
||
</el-table-column>
|
||
<el-table-column prop="bill_no" label="单据号" width="110" show-overflow-tooltip />
|
||
<el-table-column prop="biz_type_label" label="业务动作" min-width="110" show-overflow-tooltip />
|
||
<el-table-column label="方向" width="80">
|
||
<template #default="{ row }">
|
||
<el-tag size="small" effect="plain" :type="row.direction === 1 ? 'success' : 'warning'">
|
||
{{ row.direction_label }}
|
||
</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="数量" width="90" align="right">
|
||
<template #default="{ row }">{{ nf(row.quantity) }}</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
</el-card>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<el-card v-else-if="!loading" class="dash-card" shadow="never">
|
||
<el-empty description="暂无报表数据" />
|
||
</el-card>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.dashboard {
|
||
padding: 10px;
|
||
}
|
||
|
||
.dash-card {
|
||
margin-bottom: 10px;
|
||
}
|
||
/* 两栏布局:用网格代替 el-row/el-col,避免 gutter 负外边距引发横向滚动 */
|
||
.two-col {
|
||
display: grid;
|
||
grid-template-columns: 1fr 1fr;
|
||
gap: 10px;
|
||
}
|
||
@media (max-width: 1100px) {
|
||
.two-col {
|
||
grid-template-columns: 1fr;
|
||
}
|
||
}
|
||
.dash-card :deep(.el-card__header) {
|
||
padding: 8px 12px;
|
||
}
|
||
|
||
.dash-head {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 10px;
|
||
}
|
||
.dash-title {
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
}
|
||
.dash-sub {
|
||
margin-top: 4px;
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
.scope-tag {
|
||
margin-left: 8px;
|
||
}
|
||
.dash-actions {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
}
|
||
|
||
.card-head {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 8px;
|
||
font-weight: 600;
|
||
}
|
||
.card-sub {
|
||
font-size: 12px;
|
||
font-weight: 400;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
|
||
/* 概览指标 */
|
||
.metrics {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||
gap: 10px;
|
||
margin-bottom: 10px;
|
||
}
|
||
.metric {
|
||
padding: 10px 12px;
|
||
background: var(--el-fill-color-lighter);
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 4px;
|
||
}
|
||
.metric-label {
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
.metric-value {
|
||
margin: 4px 0 2px;
|
||
font-size: 20px;
|
||
font-weight: 600;
|
||
line-height: 1.2;
|
||
}
|
||
.metric-extra {
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
|
||
/* 待办 */
|
||
.todo-list {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 8px;
|
||
}
|
||
.todo-item {
|
||
cursor: pointer;
|
||
}
|
||
|
||
/* 单据状态 */
|
||
.bill-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
|
||
gap: 10px;
|
||
margin-bottom: 10px;
|
||
}
|
||
.bill-card {
|
||
padding: 10px 12px;
|
||
background: #fff;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 4px;
|
||
}
|
||
.bill-head {
|
||
display: flex;
|
||
align-items: baseline;
|
||
justify-content: space-between;
|
||
margin-bottom: 8px;
|
||
}
|
||
.bill-name {
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
}
|
||
.bill-name:hover {
|
||
color: var(--el-color-primary);
|
||
}
|
||
.bill-total {
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
.bill-status {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 10px;
|
||
}
|
||
.status-chip {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
font-size: 12px;
|
||
cursor: pointer;
|
||
}
|
||
.status-chip.zero {
|
||
color: var(--el-text-color-placeholder);
|
||
cursor: default;
|
||
}
|
||
.dot {
|
||
width: 6px;
|
||
height: 6px;
|
||
margin-right: 4px;
|
||
border-radius: 50%;
|
||
background: var(--el-color-info);
|
||
}
|
||
.dot-2 { background: var(--el-color-warning); }
|
||
.dot-3 { background: var(--el-color-primary); }
|
||
.dot-4 { background: var(--el-color-success); }
|
||
.dot-5, .dot-9 { background: var(--el-color-danger); }
|
||
.bill-foot {
|
||
display: flex;
|
||
gap: 12px;
|
||
margin-top: 8px;
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
.danger-text {
|
||
color: var(--el-color-danger);
|
||
}
|
||
|
||
/* 趋势(纯 CSS 柱状条,不引入图表库) */
|
||
.chart-row {
|
||
display: flex;
|
||
align-items: flex-end;
|
||
gap: 8px;
|
||
margin-bottom: 6px;
|
||
}
|
||
.chart-label {
|
||
width: 68px;
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
.chart-bars {
|
||
display: flex;
|
||
flex: 1;
|
||
align-items: flex-end;
|
||
gap: 2px;
|
||
height: 76px;
|
||
border-bottom: 1px solid var(--el-border-color-lighter);
|
||
}
|
||
.bar {
|
||
flex: 1;
|
||
min-width: 3px;
|
||
min-height: 2px;
|
||
background: var(--el-color-primary-light-3);
|
||
border-radius: 1px;
|
||
}
|
||
.bar-delivery { background: var(--el-color-primary-light-3); }
|
||
.bar-inbound { background: var(--el-color-success-light-3); }
|
||
.bar-outbound { background: var(--el-color-warning-light-3); }
|
||
.chart-total {
|
||
width: 48px;
|
||
font-size: 12px;
|
||
text-align: right;
|
||
color: var(--el-text-color-regular);
|
||
}
|
||
.chart-axis {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
padding-left: 76px;
|
||
font-size: 12px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
</style>
|