mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
158 lines
5.1 KiB
TypeScript
158 lines
5.1 KiB
TypeScript
import type { AdvertisingCostItem } from '@/types/advertisingCost';
|
||
|
||
import {
|
||
DATE_VALUE_RE,
|
||
FEE_COLUMN_LABEL,
|
||
GOODS_ID_RE,
|
||
PDD_SELECTORS,
|
||
} from './selectors';
|
||
|
||
export type PddDateMode = 'single' | 'range' | 'unknown';
|
||
|
||
export interface PddReportDateInfo {
|
||
mode: PddDateMode;
|
||
feeDate?: string;
|
||
beginDate?: string;
|
||
endDate?: string;
|
||
message?: string;
|
||
}
|
||
|
||
export function normalizePddDate(raw: string | null | undefined): string | undefined {
|
||
if (!raw) return undefined;
|
||
const cleaned = raw.trim().replace(/(.*?)|\(.*?\)/g, '').trim();
|
||
const match = cleaned.match(DATE_VALUE_RE);
|
||
if (!match) return undefined;
|
||
const [, y, m, d] = match;
|
||
return `${y}-${m!.padStart(2, '0')}-${d!.padStart(2, '0')}`;
|
||
}
|
||
|
||
export function parseAmount(raw: string | null | undefined): number | undefined {
|
||
if (raw == null) return undefined;
|
||
const text = raw.replace(/[,\s¥¥]/g, '').trim();
|
||
if (!text || text === '-' || text === '--') return undefined;
|
||
const value = Number(text);
|
||
return Number.isFinite(value) ? value : undefined;
|
||
}
|
||
|
||
export function readMallName(doc: Document = document): string | undefined {
|
||
const el = doc.querySelector(PDD_SELECTORS.mallName);
|
||
const text = el?.textContent?.trim();
|
||
return text || undefined;
|
||
}
|
||
|
||
/** 优先 URL beginDate/endDate,其次主 RangePicker 两个 input */
|
||
export function readReportDate(doc: Document = document, href = location.href): PddReportDateInfo {
|
||
try {
|
||
const url = new URL(href);
|
||
const begin = normalizePddDate(url.searchParams.get('beginDate') ?? undefined);
|
||
const end = normalizePddDate(url.searchParams.get('endDate') ?? undefined);
|
||
if (begin && end) {
|
||
if (begin === end) {
|
||
return { mode: 'single', feeDate: begin, beginDate: begin, endDate: end };
|
||
}
|
||
return {
|
||
mode: 'range',
|
||
beginDate: begin,
|
||
endDate: end,
|
||
message: `当前为范围报表(${begin} ~ ${end}),仅单日查看时才记录`,
|
||
};
|
||
}
|
||
} catch {
|
||
// ignore invalid href
|
||
}
|
||
|
||
const pickers = Array.from(doc.querySelectorAll(PDD_SELECTORS.rangePicker));
|
||
for (const picker of pickers) {
|
||
// 跳过对比日期区(CompareDate 内嵌 picker)
|
||
if (picker.closest('[class*="CompareDate"]')) continue;
|
||
const inputs = Array.from(picker.querySelectorAll('input')).slice(0, 2);
|
||
if (inputs.length < 2) continue;
|
||
const begin = normalizePddDate(inputs[0]?.value);
|
||
const end = normalizePddDate(inputs[1]?.value);
|
||
if (!begin || !end) continue;
|
||
if (begin === end) {
|
||
return { mode: 'single', feeDate: begin, beginDate: begin, endDate: end };
|
||
}
|
||
return {
|
||
mode: 'range',
|
||
beginDate: begin,
|
||
endDate: end,
|
||
message: `当前为范围报表(${begin} ~ ${end}),仅单日查看时才记录`,
|
||
};
|
||
}
|
||
|
||
return { mode: 'unknown', message: '未能识别报表日期,请确认已打开商品推广总览且选择了日期' };
|
||
}
|
||
|
||
/**
|
||
* antd 横向滚动表会拆成「sticky 表头 table」+「表体 table」:
|
||
* 表头有「总花费」、无商品行;表体有商品行、无 thead。
|
||
* 因此列索引从表头取,行数据从含 goodsId 的 tr 取。
|
||
*/
|
||
function findFeeColumnIndex(root: ParentNode): number {
|
||
const headers = Array.from(root.querySelectorAll(PDD_SELECTORS.tableHeaderCell));
|
||
return headers.findIndex((th) => (th.textContent ?? '').includes(FEE_COLUMN_LABEL));
|
||
}
|
||
|
||
export function parsePromotionItems(
|
||
feeDate: string,
|
||
doc: Document = document,
|
||
): AdvertisingCostItem[] {
|
||
const wrap = doc.querySelector(PDD_SELECTORS.overviewTable) ?? doc;
|
||
|
||
const feeCol = findFeeColumnIndex(wrap);
|
||
if (feeCol < 0) {
|
||
throw new Error(`未找到「${FEE_COLUMN_LABEL}」列,请检查报表列配置是否包含总花费`);
|
||
}
|
||
|
||
const goodsNodes = Array.from(wrap.querySelectorAll(PDD_SELECTORS.goodsId));
|
||
if (goodsNodes.length === 0) {
|
||
throw new Error('未找到商品 ID,请确认推广商品表格已加载完成');
|
||
}
|
||
|
||
const items: AdvertisingCostItem[] = [];
|
||
const seen = new Set<string>();
|
||
|
||
for (const goodsEl of goodsNodes) {
|
||
const goodsText = goodsEl.textContent?.trim() ?? '';
|
||
const goodsMatch = goodsText.match(GOODS_ID_RE);
|
||
if (!goodsMatch) continue;
|
||
|
||
const goodsId = goodsMatch[1]!;
|
||
if (seen.has(goodsId)) continue;
|
||
|
||
const row = goodsEl.closest('tr');
|
||
if (!row) continue;
|
||
|
||
const cells = Array.from(row.querySelectorAll(':scope > td'));
|
||
const feeCell = cells[feeCol];
|
||
const feeAmount = parseAmount(feeCell?.textContent);
|
||
if (feeAmount == null) continue;
|
||
|
||
seen.add(goodsId);
|
||
items.push({
|
||
feeDate,
|
||
goodsId,
|
||
feeAmount,
|
||
metadata: {
|
||
goodsName: row
|
||
.querySelector('[class*="OverviewTable_goodsName"]')
|
||
?.getAttribute('title')
|
||
?.replace(/^商品名:/, ''),
|
||
},
|
||
});
|
||
}
|
||
|
||
return items.sort((a, b) => Number(b.feeAmount) - Number(a.feeAmount));
|
||
}
|
||
|
||
export function isPddPromotionOverviewUrl(url: string): boolean {
|
||
try {
|
||
const u = new URL(url);
|
||
if (!u.hostname.includes('yingxiao.pinduoduo.com')) return false;
|
||
return u.pathname.includes('/report/promotion/overView') || u.pathname.includes('/goods/report/promotion');
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|