feat: 添加汇率服务和缓存服务,优化数据源和日志服务

refactor: 重构数据源工厂和类型定义,提升代码可维护性

fix: 修复类型转换和状态机文档中的错误

docs: 更新服务架构文档,添加新的服务闭环流程

test: 添加汇率服务单元测试

chore: 清理无用代码和注释,优化代码结构
This commit is contained in:
2026-03-19 14:19:01 +08:00
parent 0dac26d781
commit aa2cf560c6
120 changed files with 33383 additions and 4347 deletions

View File

@@ -4,6 +4,8 @@
* 仅在USE_MOCK=true时启用
*/
import { BaseDataSource, BaseMockDataSource, DataSourceFactory } from './dataSourceFactory';
export interface LeaderboardEntry {
rank: number;
tenant_id: string;
@@ -36,7 +38,31 @@ export interface ILeaderboardDataSource {
fetchMyRank(period: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'ALL_TIME'): Promise<MyRank>;
}
class MockLeaderboardDataSource implements ILeaderboardDataSource {
class ApiLeaderboardDataSource extends BaseDataSource<any, any> implements ILeaderboardDataSource {
constructor() {
super('/api/leaderboard');
}
async fetchLeaderboard(period: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'ALL_TIME'): Promise<LeaderboardData> {
const response = await fetch(`${this.baseUrl}?period=${period}`);
if (!response.ok) throw new Error('Failed to fetch leaderboard');
return response.json();
}
async fetchMyRank(period: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'ALL_TIME'): Promise<MyRank> {
const response = await fetch(`${this.baseUrl}/my-rank?period=${period}`);
if (!response.ok) throw new Error('Failed to fetch my rank');
return response.json();
}
}
class MockLeaderboardDataSource extends BaseMockDataSource<any, any> implements ILeaderboardDataSource {
/** Mock数据源名称 */
readonly __MOCK_NAME__ = 'MockLeaderboardDataSource';
/** Mock数据 */
protected mockData: any[] = [];
private generateRankings(count: number, type: 'revenue' | 'roi' | 'growth'): LeaderboardEntry[] {
return Array.from({ length: count }, (_, i) => ({
rank: i + 1,
@@ -54,7 +80,7 @@ class MockLeaderboardDataSource implements ILeaderboardDataSource {
}
async fetchLeaderboard(period: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'ALL_TIME'): Promise<LeaderboardData> {
await new Promise(resolve => setTimeout(resolve, 500));
await this.delay(500);
return {
revenue: { rankings: this.generateRankings(10, 'revenue'), total: 100 },
roi: { rankings: this.generateRankings(10, 'roi'), total: 100 },
@@ -65,7 +91,7 @@ class MockLeaderboardDataSource implements ILeaderboardDataSource {
}
async fetchMyRank(period: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'ALL_TIME'): Promise<MyRank> {
await new Promise(resolve => setTimeout(resolve, 300));
await this.delay(300);
return {
revenue: { rank: 15, percentile: 85 },
roi: { rank: 8, percentile: 92 },
@@ -75,23 +101,17 @@ class MockLeaderboardDataSource implements ILeaderboardDataSource {
}
}
class ApiLeaderboardDataSource implements ILeaderboardDataSource {
private baseUrl = '/api/leaderboard';
export const leaderboardDataSource = DataSourceFactory.createWithMethods<
any,
any,
ILeaderboardDataSource
>({
apiDataSource: ApiLeaderboardDataSource,
mockDataSource: MockLeaderboardDataSource,
});
async fetchLeaderboard(period: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'ALL_TIME'): Promise<LeaderboardData> {
const response = await fetch(`${this.baseUrl}?period=${period}`);
if (!response.ok) throw new Error('Failed to fetch leaderboard');
return response.json();
}
async fetchMyRank(period: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'ALL_TIME'): Promise<MyRank> {
const response = await fetch(`${this.baseUrl}/my-rank?period=${period}`);
if (!response.ok) throw new Error('Failed to fetch my rank');
return response.json();
}
}
const useMock = process.env.REACT_APP_USE_MOCK === 'true';
export const leaderboardDataSource: ILeaderboardDataSource = useMock
? new MockLeaderboardDataSource()
: new ApiLeaderboardDataSource();
/**
* Mock状态标记
* 用于调试和开发环境识别
*/
export { __MOCK__, __DATA_SOURCE_TYPE__ } from './dataSourceFactory';