mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 06:29:20 +08:00
refacor: 重构目录架构
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
export const EXTENSION_NAME = 'ECBI 数据同步';
|
||||
|
||||
export const OPEN_API_PATHS = {
|
||||
pddShopList: '/open/v1/channel/pdd/shop/list',
|
||||
pddShopMatchByName: '/open/v1/channel/pdd/shop/match-by-name',
|
||||
pddGoodsAdFeeBatchImport: '/open/v1/channel/pdd/goods/adfee/batch-import',
|
||||
pddGoodsAdFeeImportTask: '/open/v1/channel/pdd/goods/adfee/import-task',
|
||||
extensionFeatureList: '/open/v1/extension/features',
|
||||
extensionFeatureScript: '/open/v1/extension/features',
|
||||
} as const;
|
||||
|
||||
export const OPEN_SCOPES = {
|
||||
pddShopRead: 'openapi:shop:pdd:read',
|
||||
pddGoodsAdFeeImport: 'openapi:product:adfee',
|
||||
extensionFeatureRead: 'openapi:extension:feature:read',
|
||||
} as const;
|
||||
@@ -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)}`;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
export type ExtensionErrorCode =
|
||||
| 'PLATFORM_NOT_SUPPORTED'
|
||||
| 'PAGE_NOT_READY'
|
||||
| 'DATA_PARSE_FAILED'
|
||||
| 'INVALID_DATA'
|
||||
| 'AUTH_FAILED'
|
||||
| 'API_REQUEST_FAILED'
|
||||
| 'API_BUSINESS_ERROR'
|
||||
| 'NETWORK_ERROR'
|
||||
| 'CONFIG_MISSING'
|
||||
| 'NO_ACTIVE_TAB'
|
||||
| 'UNKNOWN';
|
||||
|
||||
export class ExtensionError extends Error {
|
||||
readonly code: ExtensionErrorCode;
|
||||
|
||||
constructor(code: ExtensionErrorCode, message: string) {
|
||||
super(message);
|
||||
this.name = 'ExtensionError';
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
export function toUserMessage(error: unknown): string {
|
||||
if (error instanceof ExtensionError) {
|
||||
return error.message;
|
||||
}
|
||||
if (error instanceof Error && error.message) {
|
||||
return error.message;
|
||||
}
|
||||
return '发生未知错误,请稍后重试';
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { AdvertisingCostItem } from '@/types/advertisingCost';
|
||||
|
||||
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[],
|
||||
incoming: AdvertisingCostItem[],
|
||||
): AdvertisingCostItem[] {
|
||||
const map = new Map<string, AdvertisingCostItem>();
|
||||
for (const item of previous) {
|
||||
map.set(itemKey(item), item);
|
||||
}
|
||||
for (const item of incoming) {
|
||||
map.set(itemKey(item), item);
|
||||
}
|
||||
return Array.from(map.values()).sort(compareByAmountDesc);
|
||||
}
|
||||
|
||||
export function listFeeDates(items: AdvertisingCostItem[]): string[] {
|
||||
return [...new Set(items.map((item) => item.feeDate))].sort();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { AdvertisingCostBatch, PageContext } from '@/types/advertisingCost';
|
||||
import type { ExtensionFeatureManifest } from '@/features/runtime/types';
|
||||
|
||||
export type ExtensionMessage =
|
||||
| { type: 'GET_PAGE_CONTEXT' }
|
||||
| { type: 'PARSE_ADVERTISING_COST' }
|
||||
| { type: 'TOGGLE_FLOATING_PANEL' }
|
||||
| { type: 'SET_FLOATING_PANEL'; payload: { visible: boolean } }
|
||||
| { type: 'GET_FLOATING_PANEL_STATE' }
|
||||
| { type: 'MOUNT_FEATURE'; payload: { featureId: string; code: string } }
|
||||
| { type: 'UNMOUNT_FEATURE' }
|
||||
| { type: 'GET_ACTIVE_FEATURE' }
|
||||
| { type: 'PING' };
|
||||
|
||||
export type ExtensionResponse =
|
||||
| { ok: true; data: PageContext }
|
||||
| { ok: true; data: AdvertisingCostBatch }
|
||||
| { ok: true; data: { pong: true } }
|
||||
| { ok: true; data: { visible: boolean } }
|
||||
| { ok: true; data: { featureId: string | null } }
|
||||
| { ok: true; data: ExtensionFeatureManifest }
|
||||
| { ok: false; code: string; message: string };
|
||||
|
||||
const MESSAGE_TYPES = new Set([
|
||||
'GET_PAGE_CONTEXT',
|
||||
'PARSE_ADVERTISING_COST',
|
||||
'TOGGLE_FLOATING_PANEL',
|
||||
'SET_FLOATING_PANEL',
|
||||
'GET_FLOATING_PANEL_STATE',
|
||||
'MOUNT_FEATURE',
|
||||
'UNMOUNT_FEATURE',
|
||||
'GET_ACTIVE_FEATURE',
|
||||
'PING',
|
||||
]);
|
||||
|
||||
export function isExtensionMessage(value: unknown): value is ExtensionMessage {
|
||||
if (!value || typeof value !== 'object') return false;
|
||||
const type = (value as { type?: unknown }).type;
|
||||
return typeof type === 'string' && MESSAGE_TYPES.has(type);
|
||||
}
|
||||
Reference in New Issue
Block a user