feat(platform-web): keep protocol when opening vehicle rows

This commit is contained in:
lingniu
2026-07-04 01:22:41 +08:00
parent 5f84139c00
commit 55834a4651
5 changed files with 98 additions and 16 deletions

View File

@@ -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('未匹配到车辆身份,已打开问题排查视图');
}

View File

@@ -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<HistoryFilters>(initialFilters);
const [locations, setLocations] = useState<Page<HistoryLocationRow>>(defaultPage);
@@ -176,7 +176,7 @@ export function History({ initialVin, initialProtocol, onOpenVehicle }: { initia
title: '操作',
width: 110,
render: (_: unknown, row: HistoryLocationRow) => (
<Button disabled={!canOpenVehicle(row.vin)} onClick={() => onOpenVehicle(row.vin)}></Button>
<Button disabled={!canOpenVehicle(row.vin)} onClick={() => onOpenVehicle(row.vin, row.protocol)}></Button>
)
}
]}
@@ -210,7 +210,7 @@ export function History({ initialVin, initialProtocol, onOpenVehicle }: { initia
render: (_: unknown, row: RawFrameRow) => (
<Space>
<Button onClick={() => setSelectedRaw(row)}></Button>
<Button disabled={!canOpenVehicle(row.vin)} onClick={() => onOpenVehicle(row.vin)}></Button>
<Button disabled={!canOpenVehicle(row.vin)} onClick={() => onOpenVehicle(row.vin, row.protocol)}></Button>
</Space>
)
}

View File

@@ -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<DailyMileageRow[]>([]);
const [summary, setSummary] = useState<MileageSummary>(emptySummary);
const [loading, setLoading] = useState(true);
@@ -134,7 +134,7 @@ export function Mileage({ initialVin, initialProtocol, onOpenVehicle }: { initia
title: '操作',
width: 110,
render: (_: unknown, row: DailyMileageRow) => (
<Button disabled={!canOpenVehicle(row.vin)} onClick={() => onOpenVehicle(row.vin)}></Button>
<Button disabled={!canOpenVehicle(row.vin)} onClick={() => onOpenVehicle(row.vin, row.source)}></Button>
)
}
]}

View File

@@ -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 },

View File

@@ -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(<App />);
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');
});
});