feat: 增加贡献图直观展示数据读取情况

This commit is contained in:
2026-08-16 17:37:27 +08:00
parent c35c985eb0
commit 3c0dfc2f77
6 changed files with 444 additions and 22 deletions
@@ -0,0 +1,100 @@
<script setup lang="ts">
import { computed } from 'vue';
import type { AdvertisingCostItem } from '@/types/advertisingCost';
import {
buildHeatmapWeeks,
cellTitle,
monthLabelsForWeeks,
summarizeByDate,
type HeatDayCell,
} from '@/shared/dateHeatmap';
const props = defineProps<{
items: AdvertisingCostItem[];
pageDate?: string;
selectedDate?: string;
}>();
const emit = defineEmits<{
select: [date: string | undefined];
}>();
const WEEKDAY_LABELS = ['', '一', '', '三', '', '五', ''];
const stats = computed(() => summarizeByDate(props.items));
const weeks = computed(() =>
buildHeatmapWeeks({
weeks: 13,
stats: stats.value,
pageDate: props.pageDate,
}),
);
const monthLabelMap = computed(() => {
const map = new Map<number, string>();
for (const item of monthLabelsForWeeks(weeks.value)) {
map.set(item.index, item.label);
}
return map;
});
function onCellClick(cell: HeatDayCell) {
if (!cell.inRange || !cell.stat) return;
emit('select', props.selectedDate === cell.date ? undefined : cell.date);
}
function cellClass(cell: HeatDayCell): string[] {
const classes = [`ecbi-heat-l${cell.level}`];
if (!cell.inRange) classes.push('ecbi-heat-out');
if (cell.isToday) classes.push('ecbi-heat-today');
if (cell.isPageDate) classes.push('ecbi-heat-page');
if (props.selectedDate === cell.date) classes.push('ecbi-heat-selected');
if (cell.stat) classes.push('ecbi-heat-lit');
return classes;
}
</script>
<template>
<div class="ecbi-heat">
<div class="ecbi-heat-head">
<span class="ecbi-heat-title">读取日历</span>
<span class="ecbi-muted">点亮=已读 · 点击筛选当日</span>
</div>
<div class="ecbi-heat-body">
<div class="ecbi-heat-dows">
<span class="ecbi-heat-month-spacer" />
<span v-for="(label, i) in WEEKDAY_LABELS" :key="i" class="ecbi-heat-dow">{{ label }}</span>
</div>
<div class="ecbi-heat-weeks">
<div v-for="(week, wi) in weeks" :key="wi" class="ecbi-heat-week">
<span class="ecbi-heat-month">{{ monthLabelMap.get(wi) || '' }}</span>
<button
v-for="cell in week"
:key="cell.date"
type="button"
class="ecbi-heat-cell"
:class="cellClass(cell)"
:title="cellTitle(cell)"
:disabled="!cell.inRange || !cell.stat"
@click="onCellClick(cell)"
/>
</div>
</div>
</div>
<div class="ecbi-heat-legend">
<span class="ecbi-muted">少</span>
<span class="ecbi-heat-cell ecbi-heat-l0" />
<span class="ecbi-heat-cell ecbi-heat-l1" />
<span class="ecbi-heat-cell ecbi-heat-l2" />
<span class="ecbi-heat-cell ecbi-heat-l3" />
<span class="ecbi-heat-cell ecbi-heat-l4" />
<span class="ecbi-muted">多</span>
<span class="ecbi-heat-gap" />
<span class="ecbi-heat-cell ecbi-heat-l0 ecbi-heat-page" />
<span class="ecbi-muted">当前页日期</span>
</div>
</div>
</template>
@@ -3,6 +3,7 @@ import { computed, onMounted, onUnmounted, ref } from 'vue';
import { batchImportPddGoodsAdFee } from '@/api/open/pddGoodsAdFee';
import { listPddChannelShops, type PddChannelShop } from '@/api/open/pddShop';
import DateHeatmap from './DateHeatmap.vue';
import { resolveAdapter } from '@/platform/core/PlatformDetector';
import { matchChannelShopByMallName } from '@/platform/pdd/matchShop';
import { EXTENSION_NAME } from '@/shared/constants';
@@ -24,6 +25,7 @@ const shopsLoading = ref(false);
const pageContext = ref<PageContext | null>(null);
const batch = ref<AdvertisingCostBatch | null>(null);
const filterDate = ref<string | undefined>(undefined);
const statusText = ref('等待读取');
const errorText = ref('');
const syncResultText = ref('');
@@ -41,8 +43,15 @@ const totalFee = computed(() =>
(batch.value?.items ?? []).reduce((sum, item) => sum + Number(item.feeAmount || 0), 0),
);
const itemCount = computed(() => batch.value?.items.length ?? 0);
const accumulatedDates = computed(() => listFeeDates(batch.value?.items ?? []));
const dateCount = computed(() => accumulatedDates.value.length);
const dateCount = computed(() => listFeeDates(batch.value?.items ?? []).length);
const previewItems = computed(() => {
const items = batch.value?.items ?? [];
if (!filterDate.value) return items;
return items.filter((item) => item.feeDate === filterDate.value);
});
const previewFee = computed(() =>
previewItems.value.reduce((sum, item) => sum + Number(item.feeAmount || 0), 0),
);
const selectedShopLabel = computed(() => {
if (channelShopId.value == null) return '未选择渠道店';
return channelShopName.value
@@ -59,6 +68,14 @@ const accumulatedLabel = computed(() => {
if (!dateCount.value) return '尚未累计';
return `${dateCount.value} 天 · ${itemCount.value} 条`;
});
const previewLabel = computed(() => {
if (!filterDate.value) return '全部已读';
return filterDate.value;
});
function onHeatSelect(date: string | undefined) {
filterDate.value = date;
}
async function refreshContext() {
try {
@@ -177,6 +194,9 @@ async function handleRead(fromAuto = false) {
source: 'browser-extension',
importedAt: new Date().toISOString(),
};
if (incomingDate) {
filterDate.value = incomingDate;
}
await refreshContext();
const tag = parsed.platform === 'mock' ? 'Mock' : '页面';
statusText.value = incomingDate
@@ -193,6 +213,7 @@ async function handleRead(fromAuto = false) {
function clearAccumulated() {
batch.value = null;
filterDate.value = undefined;
syncResultText.value = '';
errorText.value = '';
statusText.value = '已清空累计数据';
@@ -350,6 +371,19 @@ onUnmounted(() => {
<span>{{ accumulatedLabel }}</span>
</div>
<section class="ecbi-card">
<DateHeatmap
:items="batch?.items ?? []"
:page-date="pageContext?.feeDate"
:selected-date="filterDate"
@select="onHeatSelect"
/>
<div v-if="filterDate" class="ecbi-heat-filter">
<span class="ecbi-muted">筛选:{{ filterDate }}</span>
<button class="ecbi-link" type="button" @click="filterDate = undefined">显示全部</button>
</div>
</section>
<div class="ecbi-field">
<div class="ecbi-row">
<span class="ecbi-label">BI 渠道店铺</span>
@@ -402,32 +436,27 @@ onUnmounted(() => {
</div>
<section class="ecbi-card">
<h2>数据预览</h2>
<h2>数据预览 · {{ previewLabel }}</h2>
<div class="ecbi-row">
<span class="ecbi-label">推广费用</span>
<span class="ecbi-fee">¥{{ totalFee.toFixed(2) }}</span>
</div>
<div class="ecbi-row">
<span class="ecbi-label">日期</span>
<span class="ecbi-muted" style="max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap">
{{ accumulatedDates.join('、') || '—' }}
</span>
<span class="ecbi-label">{{ filterDate ? '当日费用' : '累计费用' }}</span>
<span class="ecbi-fee">¥{{ (filterDate ? previewFee : totalFee).toFixed(2) }}</span>
</div>
<div class="ecbi-row">
<span class="ecbi-label">条数</span>
<span>{{ itemCount }}</span>
<span>{{ previewItems.length }}{{ filterDate ? ` / ${itemCount}` : '' }}</span>
</div>
<ul v-if="batch?.items?.length" class="ecbi-list">
<li v-for="item in batch.items" :key="`${item.feeDate}-${item.goodsId}`">
<ul v-if="previewItems.length" class="ecbi-list">
<li v-for="item in previewItems" :key="`${item.feeDate}-${item.goodsId}`">
<span>{{ item.feeDate }} · {{ item.goodsId }}</span>
<span>¥{{ Number(item.feeAmount).toFixed(2) }}</span>
</li>
</ul>
<p v-else class="ecbi-muted" style="margin: 8px 0 0">暂无数据,请在单日视图读取</p>
</section>
<p v-if="syncResultText" class="ecbi-ok">{{ syncResultText }}</p>
<p v-if="errorText" class="ecbi-error">{{ errorText }}</p>
<p class="ecbi-muted">请逐日切换「单日」视图读取并累计;范围视图不会追加。同步会提交全部已累计条目。</p>
<p class="ecbi-muted">日历点亮表示已读取;虚线框为当前页日期。同步提交全部已累计条目(不受筛选影响)。</p>
</div>
</div>
</template>
@@ -5,8 +5,8 @@
.ecbi-panel {
position: fixed;
z-index: 2147483646;
width: 360px;
max-height: min(560px, calc(100vh - 24px));
width: 380px;
max-height: min(640px, calc(100vh - 24px));
overflow: hidden;
display: flex;
flex-direction: column;
@@ -220,3 +220,154 @@
.ecbi-link:hover {
text-decoration: underline;
}
/* —— 读取日历(贡献图) —— */
.ecbi-heat {
display: flex;
flex-direction: column;
gap: 6px;
}
.ecbi-heat-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
}
.ecbi-heat-title {
font-size: 11px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
color: #64748b;
}
.ecbi-heat-body {
display: flex;
gap: 4px;
overflow-x: auto;
padding-bottom: 2px;
}
.ecbi-heat-dows {
display: flex;
flex-direction: column;
gap: 3px;
flex-shrink: 0;
}
.ecbi-heat-month-spacer {
height: 14px;
}
.ecbi-heat-dow {
height: 11px;
width: 14px;
font-size: 9px;
line-height: 11px;
color: #94a3b8;
text-align: right;
}
.ecbi-heat-weeks {
display: flex;
gap: 3px;
}
.ecbi-heat-week {
display: flex;
flex-direction: column;
gap: 3px;
width: 11px;
}
.ecbi-heat-month {
height: 14px;
font-size: 9px;
line-height: 14px;
color: #94a3b8;
white-space: nowrap;
width: max-content;
max-width: none;
position: relative;
z-index: 1;
pointer-events: none;
}
.ecbi-heat-cell {
width: 11px;
height: 11px;
border-radius: 2px;
border: 0;
padding: 0;
display: block;
box-sizing: border-box;
background: #ebedf0;
cursor: default;
}
button.ecbi-heat-cell:not(:disabled) {
cursor: pointer;
}
button.ecbi-heat-cell:disabled {
opacity: 1;
}
.ecbi-heat-l0 {
background: #ebedf0;
}
.ecbi-heat-l1 {
background: #bbf7d0;
}
.ecbi-heat-l2 {
background: #86efac;
}
.ecbi-heat-l3 {
background: #22c55e;
}
.ecbi-heat-l4 {
background: #15803d;
}
.ecbi-heat-out {
opacity: 0.35;
}
.ecbi-heat-page {
outline: 1px dashed #0369a1;
outline-offset: 0;
}
.ecbi-heat-today:not(.ecbi-heat-selected) {
box-shadow: inset 0 0 0 1px #64748b;
}
.ecbi-heat-selected {
outline: 2px solid #0f172a;
outline-offset: 0;
}
.ecbi-heat-legend {
display: flex;
align-items: center;
gap: 3px;
flex-wrap: wrap;
}
.ecbi-heat-gap {
width: 8px;
}
.ecbi-heat-filter {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 2px;
}
@@ -143,7 +143,7 @@ export function parsePromotionItems(
});
}
return items;
return items.sort((a, b) => Number(b.feeAmount) - Number(a.feeAmount));
}
export function isPddPromotionOverviewUrl(url: string): boolean {
+136
View File
@@ -0,0 +1,136 @@
import type { AdvertisingCostItem } from '@/types/advertisingCost';
export type HeatLevel = 0 | 1 | 2 | 3 | 4;
export interface DayStat {
date: string;
itemCount: number;
feeTotal: number;
level: HeatLevel;
}
export interface HeatDayCell {
date: string;
inRange: boolean;
isToday: boolean;
isPageDate: boolean;
stat: DayStat | null;
level: HeatLevel;
}
function toDateKey(d: Date): string {
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return `${y}-${m}-${day}`;
}
function startOfDay(d: Date): Date {
return new Date(d.getFullYear(), d.getMonth(), d.getDate());
}
function addDays(d: Date, n: number): Date {
const next = new Date(d);
next.setDate(next.getDate() + n);
return next;
}
/** 按花费分档:0 无数据,1~4 由浅到深 */
export function feeToLevel(feeTotal: number, maxFee: number): HeatLevel {
if (feeTotal <= 0 || maxFee <= 0) return 0;
const ratio = feeTotal / maxFee;
if (ratio <= 0.25) return 1;
if (ratio <= 0.5) return 2;
if (ratio <= 0.75) return 3;
return 4;
}
export function summarizeByDate(items: AdvertisingCostItem[]): Map<string, DayStat> {
const map = new Map<string, { itemCount: number; feeTotal: number }>();
for (const item of items) {
const prev = map.get(item.feeDate) ?? { itemCount: 0, feeTotal: 0 };
prev.itemCount += 1;
prev.feeTotal += Number(item.feeAmount) || 0;
map.set(item.feeDate, prev);
}
let maxFee = 0;
for (const value of map.values()) {
maxFee = Math.max(maxFee, value.feeTotal);
}
const result = new Map<string, DayStat>();
for (const [date, value] of map) {
result.set(date, {
date,
itemCount: value.itemCount,
feeTotal: value.feeTotal,
level: feeToLevel(value.feeTotal, maxFee),
});
}
return result;
}
/**
* GitHub 贡献图布局:列=周(旧→新),行=周日…周六。
* weeks 默认 13(约一季,适合窄面板)。
*/
export function buildHeatmapWeeks(options: {
weeks?: number;
endDate?: Date;
stats: Map<string, DayStat>;
pageDate?: string;
}): HeatDayCell[][] {
const weeks = options.weeks ?? 13;
const end = startOfDay(options.endDate ?? new Date());
const todayKey = toDateKey(end);
// 对齐到本周周六(与 GitHub 行末一致:日一二三四五六)
const endWeekday = end.getDay(); // 0=Sun
const gridEnd = addDays(end, 6 - endWeekday);
const totalDays = weeks * 7;
const gridStart = addDays(gridEnd, -(totalDays - 1));
const columns: HeatDayCell[][] = [];
for (let w = 0; w < weeks; w++) {
const col: HeatDayCell[] = [];
for (let dow = 0; dow < 7; dow++) {
const date = addDays(gridStart, w * 7 + dow);
const key = toDateKey(date);
const inRange = date.getTime() <= end.getTime();
const stat = options.stats.get(key) ?? null;
col.push({
date: key,
inRange,
isToday: key === todayKey,
isPageDate: Boolean(options.pageDate && key === options.pageDate),
stat,
level: inRange ? (stat?.level ?? 0) : 0,
});
}
columns.push(col);
}
return columns;
}
export function monthLabelsForWeeks(weeks: HeatDayCell[][]): Array<{ index: number; label: string }> {
const labels: Array<{ index: number; label: string }> = [];
let lastMonth = -1;
for (let i = 0; i < weeks.length; i++) {
const first = weeks[i]?.[0];
if (!first) continue;
const month = Number(first.date.slice(5, 7));
if (month !== lastMonth) {
labels.push({ index: i, label: `${month}月` });
lastMonth = month;
}
}
return labels;
}
export function cellTitle(cell: HeatDayCell): string {
if (!cell.inRange) return cell.date;
if (!cell.stat) {
return cell.isPageDate ? `${cell.date} · 当前页面日期(未读取)` : `${cell.date} · 未读取`;
}
return `${cell.date} · ${cell.stat.itemCount} 条 · ¥${cell.stat.feeTotal.toFixed(2)}`;
}
+10 -4
View File
@@ -4,9 +4,18 @@ function itemKey(item: AdvertisingCostItem): string {
return `${item.feeDate}::${item.goodsId}`;
}
function compareByAmountDesc(a: AdvertisingCostItem, b: AdvertisingCostItem): number {
const byAmount = Number(b.feeAmount) - Number(a.feeAmount);
if (byAmount !== 0) return byAmount;
const byDate = b.feeDate.localeCompare(a.feeDate);
if (byDate !== 0) return byDate;
return a.goodsId.localeCompare(b.goodsId);
}
/**
* 合并多次单日读取:按 (feeDate, goodsId) upsert。
* 换日再读保留已有日期;同日再读覆盖该日同商品。
* 合并后按金额降序。
*/
export function mergeAdvertisingItems(
previous: AdvertisingCostItem[],
@@ -19,10 +28,7 @@ export function mergeAdvertisingItems(
for (const item of incoming) {
map.set(itemKey(item), item);
}
return Array.from(map.values()).sort((a, b) => {
const byDate = a.feeDate.localeCompare(b.feeDate);
return byDate !== 0 ? byDate : a.goodsId.localeCompare(b.goodsId);
});
return Array.from(map.values()).sort(compareByAmountDesc);
}
export function listFeeDates(items: AdvertisingCostItem[]): string[] {