feat(product): 商品详情页对接真实API,替换mock数据

后端:
- 重写 ProductOverviewServiceImpl匹配新的扁平化VO结构
- ProductOverviewVO: 新增 totalSalesAmount/monthOverMonthGrowth/totalROI 等扁平KPI字段
- ProductOverviewQueryDTO: 支持 productId/startDate/endDate/taxRate

前端:
- types.vo.ts: 适配新VO结构 (KPI字段扁平化+趋势数据+规格汇总)
- types.dto.ts: 更新查询DTO
- details/index.vue: fetchOverview调用真实API,normalizeOverview保留所有字段
- details/components/: StatCards/SalesTrendChart/FinancialPanel/SpecSummaryTable 适配新数据
- 修复 formatPercentPlain 对 null 值崩溃的问题
- StatCards/SalesTrendChart/FinancialPanel 使用 v-if 保护避免初始渲染报错
This commit is contained in:
小蟑螂
2026-04-27 22:44:53 +08:00
parent c8651a7dd1
commit 89604da32e
14 changed files with 2121 additions and 429 deletions
@@ -11,7 +11,6 @@ import {
Button,
Card,
DatePicker,
Form,
Image,
Input,
InputNumber,
@@ -370,11 +369,11 @@ const saveEdit = async () => {
<Card
hoverable
:body-style="{ padding: 0 }"
class="group management-card h-full"
class="group management-card h-full dark:bg-[#111827] dark:border-[#374151] dark:shadow-none"
:class="{ editing: isEditing }"
>
<div class="card-top">
<div class="img-preview" :class="{ 'editing-img': isEditing }">
<div class="img-preview dark:bg-[#1f2937] dark:border-[#374151]" :class="{ 'editing-img': isEditing }">
<template v-if="!isEditing">
<Image
v-if="currentImageUrl && !imageLoadError"
@@ -383,8 +382,8 @@ const saveEdit = async () => {
class="product-image"
:preview="true"
/>
<div v-else class="no-image">
<ImageOffIcon class="mb-1 text-base" />
<div v-else class="no-image dark:text-[#9ca3af]">
<ImageOffIcon class="mb-1 text-base dark:text-[#6b7280]" />
<span>暂无图片</span>
</div>
</template>
@@ -397,11 +396,11 @@ const saveEdit = async () => {
class="product-image"
@error="handleImageError"
/>
<div v-else class="no-image">
<ImageOffIcon class="mb-1 text-base" />
<div v-else class="no-image dark:text-[#9ca3af]">
<ImageOffIcon class="mb-1 text-base dark:text-[#6b7280]" />
<span>点击上传</span>
</div>
<div class="image-overlay">
<div class="image-overlay dark:bg-black/60">
<ImageOffIcon class="text-base mb-1" />
<span class="text-xs">{{ currentImageUrl && !imageLoadError ? '更换图片' : '上传图片' }}</span>
</div>
@@ -415,7 +414,7 @@ const saveEdit = async () => {
<div class="info-main">
<div class="field-row">
<span class="field-label">商品名称</span>
<span v-if="!isEditing" class="field-text field-text--title">{{ product.name }}</span>
<span v-if="!isEditing" class="field-text field-text--title dark:text-[#e5e7eb]">{{ product.name }}</span>
<TextArea
v-else
v-model:value="draft.name"
@@ -426,9 +425,9 @@ const saveEdit = async () => {
</div>
<div class="meta-grid">
<div class="meta-box">
<span class="label">类目</span>
<span v-if="!isEditing" class="value">{{ product.categoryName || '未设置' }}</span>
<div class="meta-box dark:bg-[#1f2937]">
<span class="label dark:text-[#9ca3af]">类目</span>
<span v-if="!isEditing" class="value dark:text-[#e5e7eb]">{{ product.categoryName || '未设置' }}</span>
<TreeSelect
v-else
v-model:value="draft.categoryId"
@@ -440,24 +439,24 @@ const saveEdit = async () => {
tree-default-expand-all
/>
</div>
<div class="meta-box">
<span class="label">商品编码</span>
<span v-if="!isEditing" class="value">{{ product.productCode || '未填写' }}</span>
<div class="meta-box dark:bg-[#1f2937]">
<span class="label dark:text-[#9ca3af]">商品编码</span>
<span v-if="!isEditing" class="value dark:text-[#e5e7eb]">{{ product.productCode || '未填写' }}</span>
<Input v-else v-model:value="draft.productCode" class="field-input field-input--meta" placeholder="请输入编码" />
</div>
<div class="meta-box">
<span class="label">创建时间</span>
<span class="value">{{ product.createTime || '-' }}</span>
<div class="meta-box dark:bg-[#1f2937]">
<span class="label dark:text-[#9ca3af]">创建时间</span>
<span class="value dark:text-[#e5e7eb]">{{ product.createTime || '-' }}</span>
</div>
<div class="meta-box">
<span class="label">更新时间</span>
<span class="value">{{ product.updateTime || '-' }}</span>
<div class="meta-box dark:bg-[#1f2937]">
<span class="label dark:text-[#9ca3af]">更新时间</span>
<span class="value dark:text-[#e5e7eb]">{{ product.updateTime || '-' }}</span>
</div>
</div>
</div>
</div>
<div class="specs-wrapper">
<div class="specs-wrapper dark:bg-[#1a202c]">
<div class="specs-header-row">
<div class="specs-header">规格</div>
<Button v-if="isEditing" type="dashed" size="small" @click="addSpec">
@@ -466,19 +465,19 @@ const saveEdit = async () => {
</Button>
</div>
<div v-if="displaySpecs.length > 0" class="table-container">
<div class="spec-row spec-head">
<span class="cell-head cell-code">规格编码</span>
<span class="cell-head cell-name">规格名称</span>
<span class="cell-head cell-cost">商品成本</span>
<span class="cell-head cell-cost">运费成本</span>
<span class="cell-head cell-cost">其他成本</span>
<span v-if="isEditing" class="op-col">操作</span>
<div v-if="displaySpecs.length > 0" class="table-container dark:bg-[#1a202c] dark:border-[#374151]">
<div class="spec-row spec-head dark:bg-[#1f2937] dark:text-[#9ca3af]">
<span class="cell-head cell-code dark:text-[#9ca3af]">规格编码</span>
<span class="cell-head cell-name dark:text-[#9ca3af]">规格名称</span>
<span class="cell-head cell-cost dark:text-[#9ca3af]">商品成本</span>
<span class="cell-head cell-cost dark:text-[#9ca3af]">运费成本</span>
<span class="cell-head cell-cost dark:text-[#9ca3af]">其他成本</span>
<span v-if="isEditing" class="op-col dark:bg-[#1f2937] dark:text-[#9ca3af]">操作</span>
</div>
<div
v-for="(spec, index) in displaySpecs"
:key="spec.id"
class="spec-row"
class="spec-row dark:bg-[#1a202c]"
:class="{ 'editing-row': isEditing }"
>
<span class="cell cell-code">
@@ -486,7 +485,7 @@ const saveEdit = async () => {
<Input v-else v-model:value="draft.specs[index]!.specCode" class="field-input" placeholder="编码" />
</span>
<span class="cell cell-name">
<span v-if="!isEditing" class="field-text">{{ spec.name || '-' }}</span>
<span v-if="!isEditing" class="field-text dark:text-[#e5e7eb]">{{ spec.name || '-' }}</span>
<TextArea
v-else
v-model:value="draft.specs[index]!.name"
@@ -496,7 +495,7 @@ const saveEdit = async () => {
/>
</span>
<span class="cell cell-cost">
<span v-if="!isEditing" class="cost-text">{{ formatCost(spec.productCost) }}</span>
<span v-if="!isEditing" class="cost-text dark:text-[#e5e7eb]">{{ formatCost(spec.productCost) }}</span>
<InputNumber
v-else
:value="spec.productCost ?? 0"
@@ -505,11 +504,11 @@ const saveEdit = async () => {
placeholder="0"
:min="0"
:precision="2"
@update:value="(v) => { draft.specs[index]!.productCost = v ?? 0; }"
@update:value="(v) => { draft.specs[index]!.productCost = (v as number) ?? 0; }"
/>
</span>
<span class="cell cell-cost">
<span v-if="!isEditing" class="cost-text">{{ formatCost(spec.shippingCost) }}</span>
<span v-if="!isEditing" class="cost-text dark:text-[#e5e7eb]">{{ formatCost(spec.shippingCost) }}</span>
<InputNumber
v-else
:value="spec.shippingCost ?? 0"
@@ -518,11 +517,11 @@ const saveEdit = async () => {
placeholder="0"
:min="0"
:precision="2"
@update:value="(v) => { draft.specs[index]!.shippingCost = v ?? 0; }"
@update:value="(v) => { draft.specs[index]!.shippingCost = (v as number) ?? 0; }"
/>
</span>
<span class="cell cell-cost">
<span v-if="!isEditing" class="cost-text">{{ formatCost(spec.otherCost) }}</span>
<span v-if="!isEditing" class="cost-text dark:text-[#e5e7eb]">{{ formatCost(spec.otherCost) }}</span>
<InputNumber
v-else
:value="spec.otherCost ?? 0"
@@ -531,10 +530,10 @@ const saveEdit = async () => {
placeholder="0"
:min="0"
:precision="2"
@update:value="(v) => { draft.specs[index]!.otherCost = v ?? 0; }"
@update:value="(v) => { draft.specs[index]!.otherCost = (v as number) ?? 0; }"
/>
</span>
<span v-if="isEditing" class="cell op-col">
<span v-if="isEditing" class="cell op-col dark:bg-[#1a202c]">
<Button type="text" danger size="small" class="delete-spec-btn" @click="removeSpec(index)">
<template #icon><TrashIcon class="h-4 w-4" /></template>
</Button>
@@ -542,15 +541,15 @@ const saveEdit = async () => {
</div>
<div
v-if="!isEditing && (product.specs?.length || 0) > visibleSpecs.length"
class="more-tip"
class="more-tip dark:text-[#9ca3af] dark:bg-[#1a202c]"
>
仅展示前 {{ visibleSpecs.length }} 条规格,点击"统计"查看全部
</div>
</div>
<div v-else-if="!isEditing" class="empty-text">暂无规格数据</div>
<div v-else-if="!isEditing" class="empty-text dark:text-[#9ca3af] dark:bg-[#1a202c]">暂无规格数据</div>
</div>
<div class="card-footer">
<div class="card-footer dark:bg-[#1a202c]">
<template v-if="!isEditing">
<Button class="btn btn-edit" @click="startEdit" title="编辑">
<template #icon><EditIcon class="h-4 w-4" /></template>
@@ -666,6 +665,52 @@ const saveEdit = async () => {
border-bottom: 1px solid #f9f9f9;
}
:global(html.dark) .card-top {
border-color: #374151;
}
.info-main {
flex: 1;
min-width: 0;
}
:global(html.dark) .info-main {
background: transparent;
}
.field-row {
display: flex;
align-items: flex-start;
gap: 8px;
margin-bottom: 12px;
min-height: 32px;
}
.field-label {
font-size: 13px;
color: #8c8c8c;
font-weight: 400;
width: 70px;
flex-shrink: 0;
padding-top: 4px;
}
:global(html.dark) .field-label {
color: #9ca3af;
}
.card-footer {
padding: 16px 24px;
border-top: 1px solid #f5f5f5;
display: flex;
align-items: center;
gap: 12px;
}
:global(html.dark) .card-footer {
border-color: #374151;
}
.img-preview {
width: 140px;
height: 140px;
@@ -0,0 +1,100 @@
<script lang="ts" setup>
interface Props {
data: {
totalSalesAmount?: number;
totalOrderCount?: number;
averageOrderValue?: number;
totalPromotionFee?: number;
totalCostAmount?: number;
totalRefundAmount?: number;
totalProfitAfterTax?: number;
totalNetProfitAfterTax?: number;
totalTaxAmount?: number;
monthOverMonthGrowth?: number;
} | null;
taxRate: number;
}
const props = defineProps<Props>();
const formatAmount = (amount: number | undefined) => {
if (amount === undefined) return '-';
return `¥${amount.toLocaleString('zh-CN')}`;
};
const formatPercent = (value: number | undefined) => {
if (value === undefined) return '-';
return `${value > 0 ? '+' : ''}${value.toFixed(1)}%`;
};
</script>
<template>
<div
class="flex flex-col rounded-[20px] border border-slate-200/80 dark:border-[hsl(240deg_3.7%_22%)] bg-white/85 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-8 shadow-[0_4px_20px_rgba(0,0,0,0.02)] dark:shadow-none backdrop-blur-[12px]"
>
<h3 class="mb-6 text-lg font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
商品财务损益 (P&L)
</h3>
<div class="flex flex-1 flex-col space-y-4 text-sm">
<div class="flex justify-between border-b border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pb-2">
<span class="text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">推广费总支出</span>
<span class="font-bold text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
{{ data?.totalPromotionFee ? `-${formatAmount(data.totalPromotionFee)}` : '-' }}
</span>
</div>
<div class="flex justify-between border-b border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pb-2">
<span class="text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">成本总支出</span>
<span class="font-bold text-rose-600 dark:text-rose-400">
{{ data?.totalCostAmount ? `-${formatAmount(data.totalCostAmount)}` : '-' }}
</span>
</div>
<div class="flex justify-between border-b border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pb-2">
<span class="text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">退款金额</span>
<span class="font-bold text-amber-600 dark:text-amber-400">
{{ data?.totalRefundAmount ? `-${formatAmount(data.totalRefundAmount)}` : '-' }}
</span>
</div>
<div class="flex justify-between border-b border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pb-2">
<span class="text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">客单价 (AOV)</span>
<span class="font-bold text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
{{ data?.averageOrderValue ? formatAmount(data.averageOrderValue) : '-' }}
</span>
</div>
<div class="flex justify-between border-b border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pb-2">
<span class="text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">税后盈亏 (Profit)</span>
<span class="font-bold text-indigo-600 dark:text-indigo-400">
{{ data?.totalNetProfitAfterTax ? formatAmount(data.totalNetProfitAfterTax) : '-' }}
</span>
</div>
<!-- 扣除项 -->
<div class="mt-4 space-y-3 rounded-2xl bg-slate-50/80 dark:bg-[hsl(240deg_3.7%_15.9%/80)] p-4">
<div class="flex justify-between text-xs">
<span class="text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">销售额 {{ (props.taxRate * 100).toFixed(0) }}% 税额</span>
<span class="font-bold text-rose-500 dark:text-rose-400">
{{ data?.totalTaxAmount ? formatAmount(data.totalTaxAmount) : '-' }}
</span>
</div>
<div class="flex justify-between text-xs">
<span class="text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">较上月同期增长</span>
<span class="font-black text-emerald-500 dark:text-emerald-400">
{{ formatPercent(data?.monthOverMonthGrowth) }}
</span>
</div>
</div>
<!-- 最终结果 -->
<div
class="mt-auto flex items-center justify-between rounded-2xl bg-slate-900 p-5 text-white shadow-2xl dark:shadow-none"
>
<div>
<p class="text-[10px] font-black uppercase opacity-60">Final Net Profit</p>
<p class="text-xs font-bold">商品最终净利</p>
</div>
<div class="text-3xl font-black">
{{ data?.totalNetProfitAfterTax ? formatAmount(data.totalNetProfitAfterTax) : '-' }}
</div>
</div>
</div>
</div>
</template>
@@ -0,0 +1,216 @@
<script lang="ts" setup>
import type { EchartsUIType } from '@vben/plugins/echarts';
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
import dayjs from 'dayjs';
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
interface Props {
data: {
salesAmountTrend?: {
dailyDataList: { date: string; value: number }[];
};
orderCountTrend?: {
dailyDataList: { date: string; value: number }[];
};
} | null;
}
const props = defineProps<Props>();
const trendChartRef = ref<EchartsUIType>();
const { renderEcharts: renderTrendChart } = useEcharts(trendChartRef);
let isUnmounted = false;
let renderTimer: null | ReturnType<typeof setTimeout> = null;
let darkObserver: MutationObserver | null = null;
const renderChart = () => {
if (!trendChartRef.value || !props.data) return;
const salesTrend = props.data.salesAmountTrend;
const orderTrend = props.data.orderCountTrend;
if (!salesTrend || !orderTrend) return;
const salesList = Array.isArray(salesTrend.dailyDataList)
? salesTrend.dailyDataList
: [];
const orderList = Array.isArray(orderTrend.dailyDataList)
? orderTrend.dailyDataList
: [];
if (salesList.length === 0 && orderList.length === 0) {
return;
}
const dates = salesList.map((item) =>
dayjs(item.date).format('MM-DD'),
);
const salesData = salesList.map((item) => item.value);
const orderData = orderList.map((item) => item.value);
const isDark = document.documentElement.classList.contains('dark');
try {
renderTrendChart({
grid: {
left: '3%',
right: '3%',
bottom: '3%',
top: '5%',
containLabel: true,
},
tooltip: {
trigger: 'axis',
backgroundColor: isDark ? 'hsl(222.34deg 10.43% 12.27%)' : '#fff',
textStyle: {
color: isDark ? 'hsl(0deg 0% 95%)' : '#000',
},
},
xAxis: {
type: 'category',
boundaryGap: false,
data: dates,
axisLine: {
lineStyle: {
color: isDark ? 'hsl(240deg 3.7% 22%)' : '#e2e8f0',
},
},
},
yAxis: [
{
type: 'value',
name: '销售额',
nameTextStyle: {
color: isDark ? 'hsl(240deg 5% 64.9%)' : undefined,
},
axisLabel: {
color: isDark ? 'hsl(240deg 5% 64.9%)' : undefined,
},
splitLine: {
lineStyle: {
type: 'dashed',
color: isDark ? 'hsl(240deg 3.7% 22%)' : undefined,
},
},
},
{
type: 'value',
name: '单量',
position: 'right',
nameTextStyle: {
color: isDark ? 'hsl(240deg 5% 64.9%)' : undefined,
},
axisLabel: {
color: isDark ? 'hsl(240deg 5% 64.9%)' : undefined,
},
splitLine: { show: false },
},
],
series: [
{
name: '销售额',
type: 'line',
smooth: true,
symbol: 'none',
lineStyle: { width: 3, color: '#3b82f6' },
areaStyle: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: 'rgba(59, 130, 246, 0.4)' },
{ offset: 1, color: 'rgba(59, 130, 246, 0)' },
],
},
},
data: salesData,
},
{
name: '日单量',
type: 'line',
yAxisIndex: 1,
smooth: true,
symbol: 'none',
lineStyle: { width: 3, color: '#10b981' },
areaStyle: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{ offset: 0, color: 'rgba(16, 185, 129, 0.2)' },
{ offset: 1, color: 'rgba(16, 185, 129, 0)' },
],
},
},
data: orderData,
},
],
});
} catch (error) {
console.error('渲染趋势图失败:', error);
}
};
const scheduleRender = async () => {
if (renderTimer) {
clearTimeout(renderTimer);
renderTimer = null;
}
await nextTick();
if (isUnmounted) return;
renderTimer = setTimeout(() => {
if (isUnmounted) return;
renderChart();
}, 16);
};
watch(
() => props.data,
() => {
if (props.data) {
scheduleRender();
}
},
{ deep: true, immediate: true },
);
onMounted(() => {
isUnmounted = false;
darkObserver = new MutationObserver(() => {
if (props.data) scheduleRender();
});
darkObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ['class'],
});
});
onUnmounted(() => {
isUnmounted = true;
if (renderTimer) {
clearTimeout(renderTimer);
renderTimer = null;
}
darkObserver?.disconnect();
darkObserver = null;
});
defineExpose({ renderChart });
</script>
<template>
<div class="rounded-[20px] border border-slate-200/80 dark:border-[hsl(240deg_3.7%_22%)] bg-white/85 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-8 shadow-[0_4px_20px_rgba(0,0,0,0.02)] dark:shadow-none backdrop-blur-[12px] xl:col-span-2">
<h3 class="mb-6 text-lg font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
销售额与单量趋势分析
</h3>
<div class="h-[380px] w-full">
<EchartsUI ref="trendChartRef" />
</div>
</div>
</template>
@@ -0,0 +1,241 @@
<script lang="ts" setup>
interface SpecData {
specId: number;
specName: string;
specCode?: string;
salesAmount: number;
orderCount: number;
quantity: number;
averageOrderValue: number;
productCost: number;
shippingCost: number;
otherCost: number;
refundAmount: number;
promotionFee: number;
feeRatio: number;
profitBeforeTax: number;
salesTaxAmount: number;
roi: number;
profitAfterTax: number;
}
interface Props {
data: SpecData[];
taxRate?: number;
}
const props = withDefaults(defineProps<Props>(), {
taxRate: 4,
});
const formatAmount = (amount: number | undefined) => {
if (amount === undefined || amount === null) return '-';
return `¥${amount.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
};
const formatPercent = (value: number | undefined) => {
if (value === undefined || value === null) return '-';
return `${value.toFixed(1)}%`;
};
const formatCount = (value: number | undefined) => {
if (value === undefined || value === null) return '-';
return value.toLocaleString('zh-CN');
};
// 计算合计
const totalData = () => {
if (!props.data || props.data.length === 0) return null;
const total: SpecData = {
specId: 0,
specName: '合计',
salesAmount: 0,
orderCount: 0,
quantity: 0,
averageOrderValue: 0,
productCost: 0,
shippingCost: 0,
otherCost: 0,
refundAmount: 0,
promotionFee: 0,
feeRatio: 0,
profitBeforeTax: 0,
salesTaxAmount: 0,
roi: 0,
profitAfterTax: 0,
};
props.data.forEach((item) => {
total.salesAmount += item.salesAmount || 0;
total.orderCount += item.orderCount || 0;
total.quantity += item.quantity || 0;
total.productCost += item.productCost || 0;
total.shippingCost += item.shippingCost || 0;
total.otherCost += item.otherCost || 0;
total.refundAmount += item.refundAmount || 0;
total.promotionFee += item.promotionFee || 0;
total.profitBeforeTax += item.profitBeforeTax || 0;
total.salesTaxAmount += item.salesTaxAmount || 0;
total.profitAfterTax += item.profitAfterTax || 0;
});
if (total.salesAmount > 0) {
total.averageOrderValue = total.salesAmount / total.orderCount;
total.feeRatio = (total.promotionFee / total.salesAmount) * 100;
total.roi = total.profitAfterTax / (total.productCost + total.shippingCost + total.otherCost + total.promotionFee) * 100;
}
return total;
};
</script>
<template>
<div
class="overflow-hidden rounded-[20px] border border-slate-200/80 dark:border-[hsl(240deg_3.7%_22%)] bg-white/85 dark:bg-[hsl(222.34deg_10.43%_12.27%)] shadow-[0_4px_20px_rgba(0,0,0,0.02)] dark:shadow-none backdrop-blur-[12px]"
>
<div
class="flex items-center justify-between border-b border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] bg-white/40 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-6"
>
<div>
<h3 class="text-lg font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]">商品规格销售汇总</h3>
<p class="mt-1 text-xs font-bold text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
各规格经营明细:规模、成本及最终损益
</p>
</div>
</div>
<div class="table-container">
<table class="w-full whitespace-nowrap text-left text-sm">
<thead
class="bg-slate-50 dark:bg-[hsl(240deg_3.7%_15.9%)] text-xs font-black uppercase text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]"
>
<tr>
<th
rowspan="2"
class="sticky-col border-r-2 border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] bg-white dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-5 text-base font-black text-slate-900 dark:text-[hsl(0deg_0%_95%)] shadow-sm dark:shadow-none"
>
规格名称
</th>
<th
colspan="3"
class="border-b border-l border-slate-200 dark:border-b-[hsl(240deg_3.7%_22%)] dark:border-l-[hsl(240deg_3.7%_22%)] bg-blue-50/50 dark:bg-[hsl(217.2deg_32.6%_17.5%/50%)] p-3 text-center text-blue-700 dark:text-blue-400"
>
经营规模
</th>
<th
colspan="6"
class="border-b border-l border-slate-200 dark:border-b-[hsl(240deg_3.7%_22%)] dark:border-l-[hsl(240deg_3.7%_22%)] bg-orange-50/50 dark:bg-[hsl(24deg_9.8%_15%/50%)] p-3 text-center text-orange-700 dark:text-orange-400"
>
成本费用
</th>
<th
colspan="5"
class="border-b border-l border-slate-200 dark:border-b-[hsl(240deg_3.7%_22%)] dark:border-l-[hsl(240deg_3.7%_22%)] bg-emerald-50/50 dark:bg-[hsl(142deg_71.8%_29%/20%)] p-3 text-center text-emerald-700 dark:text-emerald-400"
>
盈利分析
</th>
</tr>
<tr class="bg-slate-50/80 dark:bg-[hsl(240deg_3.7%_15.9%/80)] text-sm">
<!-- 规模 -->
<th class="border-b border-l px-5 py-3 font-black text-slate-900 dark:text-[hsl(0deg_0%_95%)]">销售额</th>
<th class="border-b px-5 py-3">订单量</th>
<th class="border-b px-5 py-3">客单价</th>
<!-- 成本 -->
<th class="border-b border-l px-5 py-3">商品成本</th>
<th class="border-b px-5 py-3">运费</th>
<th class="border-b px-5 py-3">其他成本</th>
<th class="border-b px-5 py-3 font-bold text-rose-600 dark:text-rose-400">退款金额</th>
<th class="border-b px-5 py-3">推广费</th>
<th class="border-b px-5 py-3 text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">费比 %</th>
<!-- 盈利 -->
<th class="border-b border-l px-5 py-3 font-bold">税前盈亏</th>
<th class="border-b px-5 py-3 italic text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">{{ (taxRate * 100).toFixed(0) }}%税额</th>
<th class="border-b px-5 py-3 font-black text-indigo-700 dark:text-indigo-400">ROI</th>
<th class="border-b bg-emerald-600 dark:bg-emerald-600 px-5 py-3 font-black text-white">税后盈亏</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100 dark:divide-[hsl(240deg_3.7%_22%)]">
<tr
v-for="item in data"
:key="item.specId"
class="transition-colors hover:bg-slate-50/50 dark:hover:bg-[hsl(240deg_3.7%_15.9%/30%)]"
>
<!-- 规格名称 -->
<td class="sticky-col border-r-2 border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] bg-white dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-5 shadow-sm dark:shadow-none">
<div class="font-bold text-slate-800 dark:text-[hsl(0deg_0%_95%)]">{{ item.specName }}</div>
<div v-if="item.specCode" class="mt-0.5 font-mono text-xs text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">{{ item.specCode }}</div>
</td>
<!-- 规模 -->
<td class="p-5 font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]">{{ formatAmount(item.salesAmount) }}</td>
<td class="p-5 font-medium">
<span>{{ formatCount(item.orderCount) }}</span>
<span class="ml-1 text-xs text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">单</span>
<span class="ml-2 text-xs text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">({{ formatCount(item.quantity) }}件)</span>
</td>
<td class="p-5 font-medium">{{ formatAmount(item.averageOrderValue) }}</td>
<!-- 成本 -->
<td class="p-5">{{ item.productCost ? formatAmount(item.productCost) : '-' }}</td>
<td class="p-5">{{ item.shippingCost ? formatAmount(item.shippingCost) : '-' }}</td>
<td class="p-5">{{ item.otherCost ? formatAmount(item.otherCost) : '-' }}</td>
<td class="p-5 font-bold text-rose-600 dark:text-rose-400">
{{ item.refundAmount ? formatAmount(item.refundAmount) : '-' }}
</td>
<td class="p-5">{{ item.promotionFee ? formatAmount(item.promotionFee) : '-' }}</td>
<td class="p-5 text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">{{ formatPercent(item.feeRatio) }}</td>
<!-- 盈利 -->
<td class="p-5 font-bold" :class="item.profitBeforeTax >= 0 ? 'text-slate-800 dark:text-[hsl(0deg_0%_95%)]' : 'text-rose-600 dark:text-rose-400'">
{{ formatAmount(item.profitBeforeTax) }}
</td>
<td class="p-5 italic text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">{{ item.salesTaxAmount ? formatAmount(item.salesTaxAmount) : '-' }}</td>
<td class="p-5 font-black text-indigo-700 dark:text-indigo-400">{{ item.roi ? item.roi.toFixed(2) : '-' }}</td>
<td class="bg-emerald-50 dark:bg-[hsl(142deg_71.8%_29%/20%)] p-5 text-base font-black" :class="item.profitAfterTax >= 0 ? 'text-emerald-600 dark:text-emerald-400' : 'text-rose-600 dark:text-rose-400'">
{{ formatAmount(item.profitAfterTax) }}
</td>
</tr>
<tr v-if="!data || data.length === 0">
<td colspan="15" class="p-8 text-center text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">暂无规格数据</td>
</tr>
</tbody>
<tfoot v-if="totalData()" class="bg-slate-100 dark:bg-[hsl(240deg_3.7%_22%)]">
<tr class="font-black text-slate-900 dark:text-[hsl(0deg_0%_95%)]">
<td class="border-r-2 border-slate-200 dark:border-[hsl(240deg_3.7%_22%)] p-5 text-base">合计</td>
<td class="p-5">{{ formatAmount(totalData()!.salesAmount) }}</td>
<td class="p-5">
<span>{{ formatCount(totalData()!.orderCount) }}</span>
<span class="ml-1 text-xs text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">单</span>
<span class="ml-2 text-xs text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">({{ formatCount(totalData()!.quantity) }}件)</span>
</td>
<td class="p-5">-</td>
<td class="p-5">{{ formatAmount(totalData()!.productCost) }}</td>
<td class="p-5">{{ formatAmount(totalData()!.shippingCost) }}</td>
<td class="p-5">{{ formatAmount(totalData()!.otherCost) }}</td>
<td class="p-5 text-rose-600 dark:text-rose-400">{{ formatAmount(totalData()!.refundAmount) }}</td>
<td class="p-5">{{ formatAmount(totalData()!.promotionFee) }}</td>
<td class="p-5 text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">{{ formatPercent(totalData()!.feeRatio) }}</td>
<td class="p-5">{{ formatAmount(totalData()!.profitBeforeTax) }}</td>
<td class="p-5 italic text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">{{ formatAmount(totalData()!.salesTaxAmount) }}</td>
<td class="p-5 text-indigo-700 dark:text-indigo-400">{{ totalData()!.roi ? totalData()!.roi.toFixed(2) : '-' }}</td>
<td class="bg-emerald-100 dark:bg-emerald-900/30 p-5 text-base text-emerald-600 dark:text-emerald-400">{{ formatAmount(totalData()!.profitAfterTax) }}</td>
</tr>
</tfoot>
</table>
</div>
</div>
</template>
<style scoped>
.sticky-col {
position: sticky;
left: 0;
z-index: 10;
}
.table-container {
overflow-x: auto;
}
.table-container::-webkit-scrollbar {
height: 6px;
}
.table-container::-webkit-scrollbar-thumb {
background: #e2e8f0;
border-radius: 10px;
}
</style>
@@ -0,0 +1,147 @@
<script lang="ts" setup>
interface Props {
data: {
productName?: string;
totalSalesAmount?: number;
totalOrderCount?: number;
averageOrderValue?: number;
totalPromotionFee?: number;
totalCostAmount?: number;
totalRefundAmount?: number;
totalProfitAfterTax?: number;
totalNetProfitAfterTax?: number;
totalTaxAmount?: number;
monthOverMonthGrowth?: number;
salesAmountAchievementRate?: number;
orderCountAchievementRate?: number;
averageFeeRatio?: number;
totalROI?: number;
} | null;
loading?: boolean;
}
defineProps<Props>();
const formatAmount = (amount: number | undefined) => {
if (amount === undefined) return '-';
return `¥${amount.toLocaleString('zh-CN')}`;
};
const toNumber = (value: unknown): number | undefined => {
if (value === undefined || value === null || value === '') return undefined;
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
};
const formatPercentPlain = (value: number | undefined | null) => {
if (value == null) return '-';
return `${value.toFixed(1)}%`;
};
const formatPercent = (value: number | undefined) => {
if (value === undefined) return '-';
return `${value > 0 ? '+' : ''}${value.toFixed(1)}%`;
};
</script>
<template>
<div class="grid grid-cols-1 gap-6 sm:grid-cols-2 xl:grid-cols-4">
<!-- 销售额 -->
<div
class="flex flex-col justify-between rounded-[20px] border border-slate-200/80 dark:border-[hsl(240deg_3.7%_22%)] bg-white/85 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-6 shadow-[0_4px_20px_rgba(0,0,0,0.02)] dark:shadow-none backdrop-blur-[12px]"
>
<div class="mb-4 flex items-start justify-between">
<span class="text-xs font-black uppercase tracking-widest text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
商品销售总额
</span>
<span
v-if="data?.monthOverMonthGrowth !== undefined"
class="rounded bg-emerald-50 dark:bg-[hsl(142deg_71.8%_29%/20%)] px-2 py-1 text-xs font-bold text-emerald-600 dark:text-emerald-400"
>
增长率 {{ formatPercent(data.monthOverMonthGrowth) }}
</span>
</div>
<div class="text-4xl font-black leading-none text-slate-900 dark:text-[hsl(0deg_0%_95%)]">
{{ data?.totalSalesAmount ? formatAmount(data.totalSalesAmount) : '-' }}
</div>
<div
v-if="data?.salesAmountAchievementRate !== undefined"
class="mt-6 flex items-center justify-between border-t border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pt-4 text-xs font-bold"
>
<span class="text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">销售额目标达成</span>
<span class="font-black text-blue-600 dark:text-blue-400">
{{ formatPercentPlain(data.salesAmountAchievementRate) }}
</span>
</div>
</div>
<!-- 订单达成 -->
<div
class="flex flex-col justify-between rounded-[20px] border border-slate-200/80 dark:border-[hsl(240deg_3.7%_22%)] bg-white/85 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-6 shadow-[0_4px_20px_rgba(0,0,0,0.02)] dark:shadow-none backdrop-blur-[12px]"
>
<div class="mb-4 text-xs font-black uppercase tracking-widest text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
订单总量与达成
</div>
<div class="text-4xl font-black leading-none text-slate-900 dark:text-[hsl(0deg_0%_95%)]">
{{ data?.totalOrderCount?.toLocaleString('zh-CN') || '-' }}
<span class="ml-1 text-sm font-normal text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">单</span>
</div>
<div v-if="data?.orderCountAchievementRate !== undefined" class="mt-6">
<div class="mb-2 flex justify-between text-[10px] font-black text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
<span>目标达成率</span>
<span>
{{ formatPercentPlain(data.orderCountAchievementRate) }}
</span>
</div>
<div class="h-2 w-full overflow-hidden rounded-full bg-slate-100">
<div
class="h-full bg-indigo-500 transition-all duration-1000"
:style="{ width: `${Math.min(data.orderCountAchievementRate || 0, 100)}%` }"
></div>
</div>
</div>
</div>
<!-- 推广效率 -->
<div
class="flex flex-col justify-between rounded-[20px] border border-slate-200/80 dark:border-[hsl(240deg_3.7%_22%)] bg-white/85 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-6 shadow-[0_4px_20px_rgba(0,0,0,0.02)] dark:shadow-none backdrop-blur-[12px]"
>
<div class="mb-4 text-xs font-black uppercase tracking-widest text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
投资回报率 (ROI)
</div>
<div class="text-4xl font-black leading-none text-indigo-600 dark:text-indigo-400">
{{ toNumber(data?.totalROI)?.toFixed(2) || '-' }}
</div>
<div
v-if="data?.averageFeeRatio !== undefined"
class="mt-6 space-y-2 border-t border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pt-4"
>
<div class="flex justify-between text-xs font-bold">
<span class="text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">平均费比</span>
<span class="text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
{{ formatPercentPlain(data.averageFeeRatio) }}
</span>
</div>
</div>
</div>
<!-- 利润汇总 -->
<div
class="flex flex-col justify-between rounded-[20px] border-l-4 border-slate-200/80 dark:border-[hsl(240deg_3.7%_22%)] border-l-emerald-500 dark:border-l-emerald-500 bg-white/85 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-6 shadow-[0_4px_20px_rgba(0,0,0,0.02)] dark:shadow-none backdrop-blur-[12px]"
>
<div class="mb-4 text-xs font-black uppercase tracking-widest text-emerald-600 dark:text-emerald-400">
商品净利润 (税后)
</div>
<div class="text-4xl font-black leading-none text-emerald-600 dark:text-emerald-400">
{{ data?.totalNetProfitAfterTax ? formatAmount(data.totalNetProfitAfterTax) : '-' }}
</div>
<div
v-if="data?.totalTaxAmount !== undefined"
class="mt-6 flex justify-between border-t border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] pt-4 text-[11px] font-bold text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]"
>
<span>已扣除税额</span>
<span>{{ formatAmount(data.totalTaxAmount) }}</span>
</div>
</div>
</div>
</template>
@@ -0,0 +1,207 @@
<script lang="ts" setup>
import { Button, DatePicker, InputNumber } from 'ant-design-vue';
import dayjs from 'dayjs';
import type { Dayjs } from 'dayjs';
interface Props {
modelValue: {
taxRate: number;
activeTimeRange: '7days' | '30days' | 'custom' | 'today' | 'yesterday';
customDateRange: [Dayjs, Dayjs] | undefined;
dateRange: [Dayjs, Dayjs];
};
}
interface Emits {
(e: 'update:modelValue', val: Props['modelValue']): void;
}
const props = defineProps<Props>();
const emit = defineEmits<Emits>();
const TAX_RATE_KEY = 'overview-tax-rate';
const formatTaxRate = (value: null | number | string | undefined) => {
if (value === null || value === undefined || value === '') return '';
const num = Number(value);
if (!Number.isFinite(num)) return '';
return `${num * 100}%`;
};
const parseTaxRate = (value: string | undefined) => {
if (!value) return 0;
const num = Number(value.replace('%', '').trim());
return Number.isFinite(num) ? num / 100 : 0;
};
const switchTimeRange = (type: '7days' | '30days' | 'custom' | 'today' | 'yesterday') => {
let startDate: Dayjs;
let endDate: Dayjs = dayjs();
switch (type) {
case '7days':
startDate = dayjs().subtract(7, 'day');
break;
case '30days':
startDate = dayjs().subtract(30, 'day');
break;
case 'custom':
if (props.modelValue.customDateRange) {
emit('update:modelValue', {
...props.modelValue,
activeTimeRange: type,
dateRange: props.modelValue.customDateRange,
});
}
return;
case 'today':
startDate = dayjs();
endDate = dayjs();
break;
case 'yesterday':
startDate = dayjs().subtract(1, 'day');
endDate = dayjs().subtract(1, 'day');
break;
}
const newRange: [Dayjs, Dayjs] = [startDate, endDate];
emit('update:modelValue', {
...props.modelValue,
activeTimeRange: type,
dateRange: newRange,
customDateRange: newRange,
});
};
const handleCustomDateRangeChange = (
value: [Dayjs, Dayjs] | [string, string] | null,
) => {
if (value && Array.isArray(value) && value.length === 2) {
const dates = [
dayjs.isDayjs(value[0]) ? value[0] : dayjs(value[0]),
dayjs.isDayjs(value[1]) ? value[1] : dayjs(value[1]),
] as [Dayjs, Dayjs];
emit('update:modelValue', {
...props.modelValue,
activeTimeRange: 'custom',
dateRange: dates,
customDateRange: dates,
});
}
};
const handleTaxRateChange = (val: number | string | undefined) => {
const num = typeof val === 'string' ? parseTaxRate(val) : val ?? 0.04;
localStorage.setItem(TAX_RATE_KEY, String(num));
emit('update:modelValue', {
...props.modelValue,
taxRate: num,
});
};
</script>
<template>
<div class="flex items-center gap-2">
<!-- 税率输入 -->
<div
class="flex h-[50px] items-center gap-2 rounded-2xl border border-slate-200 bg-slate-200/50 dark:bg-[hsl(240deg_3.7%_15.9%/30%)] px-3"
>
<span class="text-xs font-bold text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">税率</span>
<InputNumber
:value="props.modelValue.taxRate"
:min="0"
:max="1"
:step="0.01"
:precision="2"
size="small"
class="tax-rate-input !h-8 !w-20 !rounded-lg !border-none !bg-white/30 dark:!bg-[hsl(240deg_3.7%_22%/30%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)] !text-sm !font-bold"
:formatter="formatTaxRate"
:parser="parseTaxRate"
@change="handleTaxRateChange"
/>
</div>
<!-- 时间选择器 -->
<div
class="flex items-center gap-1.5 rounded-2xl border border-slate-200 bg-slate-200/50 dark:bg-[hsl(240deg_3.7%_15.9%/30%)] p-1"
>
<div class="flex rounded-xl bg-white/40 dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-1 shadow-inner dark:shadow-none">
<Button
type="text"
size="small"
class="!h-auto !min-w-0 !rounded-lg !border-none !px-5 !py-2 !text-sm !font-bold !shadow-none !transition-all"
:class="
props.modelValue.activeTimeRange === 'today'
? '!bg-white !font-black !text-blue-600 dark:text-blue-400 dark:!shadow-none !shadow-sm'
: '!bg-transparent !text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] hover:!text-slate-800'
"
@click="switchTimeRange('today')"
>
今日
</Button>
<Button
type="text"
size="small"
class="!h-auto !min-w-0 !rounded-lg !border-none !px-5 !py-2 !text-sm !font-bold !shadow-none !transition-all"
:class="
props.modelValue.activeTimeRange === 'yesterday'
? '!bg-white !font-black !text-blue-600 dark:text-blue-400 dark:!shadow-none !shadow-sm'
: '!bg-transparent !text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] hover:!text-slate-800'
"
@click="switchTimeRange('yesterday')"
>
昨日
</Button>
<Button
type="text"
size="small"
class="!h-auto !min-w-0 !rounded-lg !border-none !px-5 !py-2 !text-sm !font-bold !shadow-none !transition-all"
:class="
props.modelValue.activeTimeRange === '7days'
? '!bg-white !font-black !text-blue-600 dark:text-blue-400 dark:!shadow-none !shadow-sm'
: '!bg-transparent !text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] hover:!text-slate-800'
"
@click="switchTimeRange('7days')"
>
近7日
</Button>
<Button
type="text"
size="small"
class="!h-auto !min-w-0 !rounded-lg !border-none !px-5 !py-2 !text-sm !font-bold !shadow-none !transition-all"
:class="
props.modelValue.activeTimeRange === '30days'
? '!bg-white !font-black !text-blue-600 dark:text-blue-400 dark:!shadow-none !shadow-sm'
: '!bg-transparent !text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] hover:!text-slate-800'
"
@click="switchTimeRange('30days')"
>
近30日
</Button>
</div>
<!-- 日期选择器 -->
<div class="flex items-center gap-2">
<span class="text-xs font-bold text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">自定义日期</span>
<DatePicker.RangePicker
:value="props.modelValue.customDateRange"
size="small"
format="YYYY-MM-DD"
:allow-clear="false"
:placeholder="['开始日期', '结束日期']"
class="!h-8 !cursor-pointer !border-none !bg-transparent !text-sm !font-bold"
@change="handleCustomDateRangeChange"
/>
</div>
</div>
</div>
</template>
<style scoped>
:deep(.tax-rate-input .ant-input-number-input) {
height: 32px;
line-height: 32px;
padding-top: 0;
padding-bottom: 0;
}
</style>
@@ -1,37 +1,204 @@
<script lang="ts" setup>
import { computed, ref } from 'vue';
import type { Dayjs } from 'dayjs';
import { computed, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { Page } from '@vben/common-ui';
import { Tabs } from 'ant-design-vue';
import { Spin, message } from 'ant-design-vue';
import dayjs from 'dayjs';
import OverviewTab from './tabs/OverviewTab.vue';
import TimeRangeBar from './components/TimeRangeBar.vue';
import StatCards from './components/StatCards.vue';
import SalesTrendChart from './components/SalesTrendChart.vue';
import FinancialPanel from './components/FinancialPanel.vue';
import SpecSummaryTable from './components/SpecSummaryTable.vue';
import { getProductOverviewApi } from '#/api/ecommerce/product-overview';
defineOptions({ name: 'ProductDetails' });
defineOptions({ name: 'ProductOverview' });
const route = useRoute();
// 商品ID(从路由参数获取)
const productId = computed(() => {
const id = route.params.id;
return id ? Number(id) : 0;
const parsed = id ? Number(id) : 0;
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
});
// 当前激活的 tab
const activeKey = ref('overview');
// 响应式数据
const loading = ref(false);
const overviewData = ref<any>(null);
const normalizeOverview = (raw: any) => {
if (!raw || typeof raw !== 'object') return null;
return {
// 保留所有 KPI 字段
...raw,
// 防御性处理嵌套结构
salesAmountTrend: {
...(raw.salesAmountTrend || {}),
dailyDataList: Array.isArray(raw.salesAmountTrend?.dailyDataList)
? raw.salesAmountTrend.dailyDataList
: [],
},
orderCountTrend: {
...(raw.orderCountTrend || {}),
dailyDataList: Array.isArray(raw.orderCountTrend?.dailyDataList)
? raw.orderCountTrend.dailyDataList
: [],
},
specSummaries: {
...(raw.specSummaries || {}),
list: Array.isArray(raw.specSummaries?.list) ? raw.specSummaries.list : [],
},
};
};
// 日期范围
const dateRange = ref<[Dayjs, Dayjs]>([
dayjs().subtract(30, 'day'),
dayjs(),
]);
// 当前选中的时间范围类型
const activeTimeRange = ref<
'7days' | '30days' | 'custom' | 'today' | 'yesterday'
>('30days');
// 自定义日期范围
const customDateRange = ref<[Dayjs, Dayjs] | undefined>([
dateRange.value[0],
dateRange.value[1],
]);
// 税率(持久化)
const TAX_RATE_KEY = 'overview-tax-rate';
const taxRate = ref<number>(0.04);
taxRate.value = Number(localStorage.getItem(TAX_RATE_KEY)) || 0.04;
watch(taxRate, () => {
fetchOverview();
});
// 时间范围状态(传递给子组件)
const timeRangeState = computed(() => ({
taxRate: taxRate.value,
activeTimeRange: activeTimeRange.value,
customDateRange: customDateRange.value,
dateRange: dateRange.value,
}));
// 监听子组件的时间范围变化
const handleTimeRangeChange = async (val: typeof timeRangeState.value) => {
taxRate.value = val.taxRate;
activeTimeRange.value = val.activeTimeRange;
customDateRange.value = val.customDateRange;
dateRange.value = val.dateRange;
await fetchOverview();
};
// 获取概览数据
async function fetchOverview() {
if (!productId.value) return;
loading.value = true;
try {
const res = await getProductOverviewApi({
productId: productId.value,
startDate: dateRange.value[0].format('YYYY-MM-DD'),
endDate: dateRange.value[1].format('YYYY-MM-DD'),
taxRate: taxRate.value,
});
overviewData.value = normalizeOverview(res);
} catch (err: any) {
message.error(err?.message || '加载失败');
} finally {
loading.value = false;
}
}
// 初始加载
watch(productId, (val) => {
if (val) fetchOverview();
}, { immediate: true });
// 计算 page title 用 productName
const pageTitle = computed(() => overviewData.value?.productName || '商品概览');
</script>
<template>
<Page auto-content-height content-class="flex flex-col">
<Tabs
v-model:active-key="activeKey"
class="flex min-h-0 flex-1 flex-col"
:tab-bar-style="{ marginBottom: '16px' }"
>
<Tabs.TabPane key="overview" tab="总览">
<OverviewTab :product-id="productId" />
</Tabs.TabPane>
</Tabs>
<Page
:title="pageTitle"
auto-content-height
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
>
<template #description>
<div class="mt-1 text-sm font-bold italic text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
商品ID: {{ productId || 'N/A' }} · 数据更新于: {{ dayjs().format('YYYY-MM-DD HH:mm:ss') }}
</div>
</template>
<template #extra>
<TimeRangeBar
:model-value="timeRangeState"
@update:model-value="handleTimeRangeChange"
/>
</template>
<Spin :spinning="loading">
<div v-if="overviewData" class="mx-auto w-full space-y-6 p-4 md:p-8">
<!-- 第一层:核心统计卡片 -->
<StatCards :data="overviewData" :loading="loading" />
<!-- 第二层:趋势与财务拆解 -->
<div class="grid grid-cols-1 gap-6 xl:grid-cols-3">
<SalesTrendChart :data="overviewData" />
<FinancialPanel :data="overviewData" :tax-rate="taxRate" />
</div>
<!-- 第三层:规格销售汇总 -->
<SpecSummaryTable
:data="overviewData?.specSummaries?.list"
:tax-rate="taxRate"
/>
</div>
<div v-else-if="!loading" class="flex items-center justify-center p-8 text-slate-400">
暂无数据
</div>
</Spin>
</Page>
</template>
<style scoped>
/* 表格滚动条优化 */
.table-container::-webkit-scrollbar {
height: 6px;
}
.table-container::-webkit-scrollbar-thumb {
background: #e2e8f0;
border-radius: 10px;
}
</style>
<style>
html.dark .product-overview-thead table thead th {
background-color: hsl(240deg 3.7% 15.9% / 80%) !important;
}
html.dark .product-overview-thead table thead th {
border-color: hsl(240deg 3.7% 22%) !important;
}
html.dark .table-container table tbody tr:hover td {
background-color: hsl(217.2deg 32.6% 17.5% / 30%) !important;
}
html.dark .table-container::-webkit-scrollbar-thumb {
background: hsl(240deg 3.7% 22%);
}
html.dark .table-container::-webkit-scrollbar-track {
background: hsl(240deg 3.7% 15.9%);
}
</style>
@@ -80,7 +80,7 @@ function handleModalSuccess() {
</script>
<template>
<Page :auto-content-height="true" content-class="flex-col">
<Page :auto-content-height="true" content-class="flex-col dark:bg-[#0f1623]">
<!-- 工具栏区域 -->
<template #extra>
<Flex gap="small" align="center">
@@ -122,13 +122,13 @@ function handleModalSuccess() {
/>
<!-- 网格视图 -->
<ProductGridView
v-else
ref="productGridViewRef"
v-model:search-form="searchForm"
@edit="handleEdit"
@delete="handleDelete"
/>
<div v-else class="flex-1 overflow-y-auto dark:bg-[#0f1623]">
<ProductGridView
ref="productGridViewRef"
v-model:search-form="searchForm"
@delete="handleDelete"
/>
</div>
<!-- 商品表单模态框 -->
<ProductFormModal ref="productFormModalRef" @success="handleModalSuccess" />
@@ -272,7 +272,7 @@ onUnmounted(() => {
<!-- 网格内容 -->
<Card
class="flex flex-1 flex-col overflow-y-auto"
class="flex flex-1 flex-col overflow-y-auto dark:bg-[#111827] dark:border-[#374151]"
:body-style="{
padding: '16px',
display: 'flex',