diff --git a/vehicle-data-platform/apps/web/src/App.tsx b/vehicle-data-platform/apps/web/src/App.tsx index 2c1aba37..4d30db30 100644 --- a/vehicle-data-platform/apps/web/src/App.tsx +++ b/vehicle-data-platform/apps/web/src/App.tsx @@ -82,8 +82,9 @@ export default function App() { replaceHash(page); }; - const openVehicle = async (keyword: string) => { + const openVehicle = async (keyword: string, protocol?: string) => { const lookupKey = keyword.trim(); + const nextProtocol = protocol?.trim() ?? ''; if (!lookupKey) { return; } @@ -91,9 +92,9 @@ export default function App() { const resolved = await api.vehicleResolve(new URLSearchParams({ keyword: lookupKey })); const nextKey = resolved.resolved && resolved.vin ? resolved.vin : lookupKey; setActiveVin(nextKey); - setActiveProtocol(''); + setActiveProtocol(nextProtocol); setActivePage('detail'); - replaceHash('detail', nextKey); + replaceHash('detail', nextKey, nextProtocol); if (!resolved.resolved) { Toast.warning('未匹配到车辆身份,已打开问题排查视图'); } diff --git a/vehicle-data-platform/apps/web/src/pages/History.tsx b/vehicle-data-platform/apps/web/src/pages/History.tsx index 46f72a3e..8cbf6013 100644 --- a/vehicle-data-platform/apps/web/src/pages/History.tsx +++ b/vehicle-data-platform/apps/web/src/pages/History.tsx @@ -33,7 +33,7 @@ function splitFields(value?: string) { .filter(Boolean); } -export function History({ initialVin, initialProtocol, onOpenVehicle }: { initialVin: string; initialProtocol?: string; onOpenVehicle: (vin: string) => void }) { +export function History({ initialVin, initialProtocol, onOpenVehicle }: { initialVin: string; initialProtocol?: string; onOpenVehicle: (vin: string, protocol?: string) => void }) { const initialFilters = { ...defaultFilters, keyword: initialVin || defaultFilters.keyword, protocol: initialProtocol }; const [filters, setFilters] = useState(initialFilters); const [locations, setLocations] = useState>(defaultPage); @@ -176,7 +176,7 @@ export function History({ initialVin, initialProtocol, onOpenVehicle }: { initia title: '操作', width: 110, render: (_: unknown, row: HistoryLocationRow) => ( - + ) } ]} @@ -210,7 +210,7 @@ export function History({ initialVin, initialProtocol, onOpenVehicle }: { initia render: (_: unknown, row: RawFrameRow) => ( - + ) } diff --git a/vehicle-data-platform/apps/web/src/pages/Mileage.tsx b/vehicle-data-platform/apps/web/src/pages/Mileage.tsx index 8b53b78e..39d3bf4b 100644 --- a/vehicle-data-platform/apps/web/src/pages/Mileage.tsx +++ b/vehicle-data-platform/apps/web/src/pages/Mileage.tsx @@ -32,7 +32,7 @@ function canOpenVehicle(vin?: string) { return Boolean(value && value !== 'unknown'); } -export function Mileage({ initialVin, initialProtocol, onOpenVehicle }: { initialVin: string; initialProtocol?: string; onOpenVehicle: (vin: string) => void }) { +export function Mileage({ initialVin, initialProtocol, onOpenVehicle }: { initialVin: string; initialProtocol?: string; onOpenVehicle: (vin: string, protocol?: string) => void }) { const [rows, setRows] = useState([]); const [summary, setSummary] = useState(emptySummary); const [loading, setLoading] = useState(true); @@ -134,7 +134,7 @@ export function Mileage({ initialVin, initialProtocol, onOpenVehicle }: { initia title: '操作', width: 110, render: (_: unknown, row: DailyMileageRow) => ( - + ) } ]} diff --git a/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx b/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx index def22c78..d0c7508c 100644 --- a/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx +++ b/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx @@ -76,15 +76,15 @@ export function VehicleDetail({ const identity = detail?.identity; const summary = detail?.realtimeSummary; - const latest = detail?.realtime[0]; + const latest = detail?.realtime?.[0]; const resolution = detail?.resolution; const resolvedVIN = resolution?.vin || detail?.vin || summary?.vin || identity?.vin || latest?.vin || query.keyword; const hasResolvedVIN = resolution?.resolved ?? detail?.lookupResolved ?? isLikelyVIN(resolvedVIN); const displayVIN = hasResolvedVIN ? resolvedVIN : '-'; const displayLookupKey = resolution?.lookupKey || detail?.lookupKey || query.keyword; const protocols = useMemo(() => resolution?.protocols?.length ? resolution.protocols : detail?.sources ?? [], [detail?.sources, resolution?.protocols]); - const latestRaw = detail?.raw.items[0]; - const qualityCount = detail?.quality.total ?? 0; + const latestRaw = detail?.raw?.items?.[0]; + const qualityCount = detail?.quality?.total ?? 0; const online = resolution?.online || summary?.online || identity?.online || false; const lastSeen = resolution?.lastSeen || summary?.lastSeen || latest?.lastSeen || '-'; const formKey = `${query.keyword}-${query.protocol ?? ''}`; @@ -93,7 +93,7 @@ export function VehicleDetail({ loading={loading} pagination={false} rowKey={(row?: QualityIssueRow) => `${row?.protocol ?? ''}-${row?.issueType ?? ''}-${row?.lastSeen ?? ''}`} - dataSource={detail?.quality.items ?? []} + dataSource={detail?.quality?.items ?? []} columns={[ { title: '来源', dataIndex: 'protocol', width: 130 }, { title: '问题', dataIndex: 'issueType', width: 150 }, @@ -260,7 +260,7 @@ export function VehicleDetail({ loading={loading} pagination={false} rowKey="deviceTime" - dataSource={detail?.history.items ?? []} + dataSource={detail?.history?.items ?? []} columns={[ { title: '来源', dataIndex: 'protocol', width: 130 }, { title: '经度', dataIndex: 'longitude', width: 120 }, @@ -277,7 +277,7 @@ export function VehicleDetail({ loading={loading} pagination={false} rowKey="id" - dataSource={detail?.raw.items ?? []} + dataSource={detail?.raw?.items ?? []} columns={[ { title: '来源', dataIndex: 'protocol', width: 130 }, { title: '帧类型', dataIndex: 'frameType', width: 190 }, @@ -294,7 +294,7 @@ export function VehicleDetail({ loading={loading} pagination={false} rowKey="date" - dataSource={detail?.mileage.items ?? []} + dataSource={detail?.mileage?.items ?? []} columns={[ { title: '日期', dataIndex: 'date', width: 130 }, { title: '来源', dataIndex: 'source', width: 130 }, diff --git a/vehicle-data-platform/apps/web/src/test/App.test.tsx b/vehicle-data-platform/apps/web/src/test/App.test.tsx index 0f949c65..68afd9a9 100644 --- a/vehicle-data-platform/apps/web/src/test/App.test.tsx +++ b/vehicle-data-platform/apps/web/src/test/App.test.tsx @@ -1,4 +1,4 @@ -import { render, screen, waitFor } from '@testing-library/react'; +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { afterEach, expect, test, vi } from 'vitest'; import App from '../App'; @@ -87,3 +87,84 @@ test('applies protocol from shareable history hash to API requests', async () => body: expect.stringContaining('"protocol":"JT808"') })); }); + +test('keeps row protocol when opening vehicle service from history results', async () => { + window.history.replaceState(null, '', '/#/history?keyword=%E7%B2%A4AG18312&protocol=JT808'); + vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { + const path = String(input); + if (path.includes('/api/ops/health')) { + return { + ok: true, + json: async () => ({ + data: { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } }, + traceId: 'trace-test', + timestamp: 1783094400000 + }) + } as Response; + } + if (path.includes('/api/history/locations')) { + return { + ok: true, + json: async () => ({ + data: { + items: [{ + vin: 'VIN-JT808-001', + plate: '粤AG18312', + protocol: 'JT808', + longitude: 113.2, + latitude: 23.1, + speedKmh: 30, + socPercent: 0, + totalMileageKm: 100, + lastSeen: '2026-07-03 20:12:10', + deviceTime: '2026-07-03 20:12:10', + serverTime: '2026-07-03 20:12:11' + }], + total: 1, + limit: 10, + offset: 0 + }, + traceId: 'trace-test', + timestamp: 1783094400000 + }) + } as Response; + } + if (path.includes('/api/vehicles/resolve')) { + return { + ok: true, + json: async () => ({ + data: { + lookupKey: 'VIN-JT808-001', + resolved: true, + vin: 'VIN-JT808-001', + plate: '粤AG18312', + phone: '13307795425', + oem: 'G7s', + protocols: ['JT808'], + online: true, + lastSeen: '2026-07-03 20:12:10' + }, + traceId: 'trace-test', + timestamp: 1783094400000 + }) + } as Response; + } + return { + ok: true, + json: async () => ({ + data: { items: [], total: 0, limit: 10, offset: 0 }, + traceId: 'trace-test', + timestamp: 1783094400000 + }) + } as Response; + }); + + render(); + + expect(await screen.findByText('VIN-JT808-001')).toBeInTheDocument(); + fireEvent.click(screen.getAllByRole('button', { name: '车辆服务' })[0]); + + await waitFor(() => { + expect(window.location.hash).toBe('#/detail?keyword=VIN-JT808-001&protocol=JT808'); + }); +});