mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 03:19:18 +08:00
feat: 新增浏览器插件
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import type {
|
||||
AdvertisingCostBatch,
|
||||
PageContext,
|
||||
PlatformType,
|
||||
} from '@/types/advertisingCost';
|
||||
|
||||
export interface PlatformAdapter {
|
||||
readonly platform: PlatformType;
|
||||
canHandle(url: string): boolean;
|
||||
getPageContext(): Promise<PageContext>;
|
||||
parseAdvertisingCost(feeDate: string): Promise<Omit<AdvertisingCostBatch, 'channelShopId'>>;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { PlatformType } from '@/types/advertisingCost';
|
||||
|
||||
import type { PlatformAdapter } from './PlatformAdapter';
|
||||
import { MockPlatformAdapter } from '../mock/MockPlatformAdapter';
|
||||
|
||||
const adapters: PlatformAdapter[] = [new MockPlatformAdapter()];
|
||||
|
||||
export function detectPlatform(url: string): PlatformType {
|
||||
const adapter = adapters.find((item) => item.canHandle(url));
|
||||
return adapter?.platform ?? 'unknown';
|
||||
}
|
||||
|
||||
export function resolveAdapter(url: string): PlatformAdapter {
|
||||
const matched = adapters.find((item) => item.canHandle(url));
|
||||
if (matched) {
|
||||
return matched;
|
||||
}
|
||||
// 第一阶段:未识别平台时回退 Mock,保证链路可跑通
|
||||
return adapters[0]!;
|
||||
}
|
||||
|
||||
export function listAdapters(): PlatformAdapter[] {
|
||||
return [...adapters];
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export type { PlatformAdapter } from './PlatformAdapter';
|
||||
export { detectPlatform, listAdapters, resolveAdapter } from './PlatformDetector';
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { PlatformAdapter } from '@/platform/core/PlatformAdapter';
|
||||
import type { AdvertisingCostBatch, PageContext } from '@/types/advertisingCost';
|
||||
|
||||
/**
|
||||
* 第一阶段 Mock Adapter:不读真实 DOM,返回固定样例数据。
|
||||
*/
|
||||
export class MockPlatformAdapter implements PlatformAdapter {
|
||||
readonly platform = 'mock' as const;
|
||||
|
||||
canHandle(_url: string): boolean {
|
||||
// 未接入真实平台前,任意页面均可由 Mock 处理
|
||||
return true;
|
||||
}
|
||||
|
||||
async getPageContext(): Promise<PageContext> {
|
||||
return {
|
||||
url: typeof location !== 'undefined' ? location.href : '',
|
||||
title: typeof document !== 'undefined' ? document.title : '',
|
||||
platform: 'mock',
|
||||
ready: true,
|
||||
message: '当前为 Mock Adapter(未解析真实平台页面)',
|
||||
};
|
||||
}
|
||||
|
||||
async parseAdvertisingCost(
|
||||
feeDate: string,
|
||||
): Promise<Omit<AdvertisingCostBatch, 'channelShopId'>> {
|
||||
const items = [
|
||||
{ goodsId: 'mock-goods-1001', feeAmount: 128.5 },
|
||||
{ goodsId: 'mock-goods-1002', feeAmount: 56.0 },
|
||||
{ goodsId: 'mock-goods-1003', feeAmount: 1050.06 },
|
||||
];
|
||||
|
||||
return {
|
||||
platform: 'mock',
|
||||
feeDate,
|
||||
items,
|
||||
source: 'browser-extension',
|
||||
importedAt: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user