import { Hono } from 'hono'; import { config } from '../config'; import { generateMockRevenueForDate, listMockBusinessProjects } from '../services/roi/revenue-ingest/mock-generator'; /** * Mock 营收 API,严格按"附录 A:外部营收 API 接入规范"实现。 * 仅在 MOCK_REVENUE_API=true 时挂载,挂在 /mock(不在 /api/* 下,避开 JWT auth)。 * * 路由: * GET /mock/revenue/daily?date=YYYY-MM-DD * GET /mock/revenue/projects */ export const mockRevenueRoutes = new Hono(); mockRevenueRoutes.use('*', async (c, next) => { // 鉴权:严格按附录 A 的 Bearer Token const auth = c.req.header('Authorization') || ''; const match = auth.match(/^Bearer\s+(.+)$/i); if (!match || match[1] !== config.REVENUE_API_KEY) { return c.json({ error: 'UNAUTHORIZED' }, 401); } await next(); }); mockRevenueRoutes.get('/revenue/daily', async (c) => { const date = c.req.query('date'); if (!date || !/^\d{4}-\d{2}-\d{2}$/.test(date)) { return c.json({ error: 'INVALID_DATE', message: 'date must be YYYY-MM-DD' }, 400); } try { const events = await generateMockRevenueForDate(date); return c.json({ date, events, nextCursor: null, // mock 不分页(数据量小) totalCount: events.length, }); } catch (e) { return c.json({ error: 'INTERNAL', message: (e as Error).message }, 500); } }); mockRevenueRoutes.get('/revenue/projects', async (c) => { try { const projects = await listMockBusinessProjects(); return c.json({ projects, totalCount: projects.length }); } catch (e) { return c.json({ error: 'INTERNAL', message: (e as Error).message }, 500); } });