hejianhao
2026-04-15 46d52f60ea68c33ae36dfab5468ae9882da56b1e
fix(route): 修复路线擦除逻辑和状态同步问题

- 移除调试日志打印
- 修复 WebSocket 通知顺序,确保状态更新后触发监听
- 修正行程状态同步逻辑,避免状态回退
- 优化路线擦除算法,移除对 servedPoints 的依赖,改为基于前后路线点差分
- 修复强制重绘路线时的初始化条件
- 确保擦除操作后 React 状态与原生地图同步
4个文件已修改
62 ■■■■■ 已修改文件
src/robotaxi/pages/begin-route/components/calling-car-modules/door-trunk-control.tsx 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/robotaxi/pages/begin-route/hooks/use-route-manager.ts 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/robotaxi/pages/begin-route/hooks/use-web-socket-events.ts 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/websocket.ts 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/robotaxi/pages/begin-route/components/calling-car-modules/door-trunk-control.tsx
@@ -132,8 +132,6 @@
                }
            }
        }).catch(() => {
            console.log(res,'***********');
            Taro.showToast({ title: '解锁失败,请重试或联系客服', icon: 'none' });
        });
    };
src/robotaxi/pages/begin-route/hooks/use-route-manager.ts
@@ -113,19 +113,11 @@
    orderRouteInitializedRef.current = false;
  };
  // 擦除已行驶路段
  const erasePassedRouteSegments = (newPoints: any[], servedPoints: any[] = []) => {
  // 擦除已行驶路段(仅基于前后两次路线点差分,不依赖后端 served)
  const erasePassedRouteSegments = (newPoints: any[], _servedPoints: any[] = []) => {
    const oldPoints = currentFullRouteRef.current;
    if (!oldPoints?.length || !newPoints?.length) return;
    const servedLast = Array.isArray(servedPoints) && servedPoints.length > 0
      ? servedPoints[servedPoints.length - 1]
      : null;
    const hasServedBoundary = !!(
      servedLast &&
      typeof servedLast.latitude === 'number' &&
      typeof servedLast.longitude === 'number'
    );
    const boundaryPoint = hasServedBoundary ? servedLast : newPoints[0];
    const boundaryPoint = newPoints[0];
    if (
      !boundaryPoint ||
      typeof boundaryPoint.latitude !== 'number' ||
@@ -147,7 +139,7 @@
      }
      if (dist > bestDistance * 3 && bestDistance < 100) break;
    }
    if (bestMatchIndex >= startIndex && bestDistance < 500) {
    if (bestMatchIndex >= startIndex && bestDistance < 120) {
      Taro.createMapContext('map').eraseLines({
        lines: [{
          id: 2001,
@@ -159,19 +151,15 @@
          clear: true
        }]
      });
      if (hasServedBoundary) {
        currentFullRouteRef.current = [...newPoints];
        lastErasedIndexRef.current = 0;
      } else {
        lastErasedIndexRef.current = bestMatchIndex;
      }
      // 同步 React 路由状态,避免仅原生擦线导致线和车不同步
      currentFullRouteRef.current = [...newPoints];
      lastErasedIndexRef.current = 0;
      applyRoutePoints([...newPoints]);
      return;
    }
    currentFullRouteRef.current = [...newPoints];
    lastErasedIndexRef.current = 0;
    lastRoutePointRef.current = null;
    lastRouteUpdateAtRef.current = 0;
    setRoutePoints([...newPoints]);
    applyRoutePoints([...newPoints]);
  };
  // 确保路线请求可用城市信息
src/robotaxi/pages/begin-route/hooks/use-web-socket-events.ts
@@ -67,8 +67,8 @@
        return null;
    }
    const currentTripStatus = Number(deps.tripStatusRef.current);
    if (currentTripStatus > mappedTripStatus && currentTripStatus !== 10) {
        return null;
    if (currentTripStatus === mappedTripStatus) {
        return mappedTripStatus;
    }
    deps.setTripStatus(mappedTripStatus);
    deps.tripStatusRef.current = mappedTripStatus;
@@ -193,7 +193,7 @@
const handleEraseRouteStatus = (points: any[], servedPoints: any[], deps: UseWebSocketEventsDeps) => {
    const targetPoint = resolveVehiclePoint(points, servedPoints);
    if (deps.orderRouteInitializedRef.current && deps.currentFullRouteRef.current.length > 0) {
        deps.erasePassedRouteSegments(points, servedPoints);
        deps.erasePassedRouteSegments(points);
    } else {
        deps.orderRouteInitializedRef.current = true;
        deps.currentFullRouteRef.current = [...points];
@@ -230,11 +230,13 @@
    deps.lastRouteUpdateAtRef.current = 0;
    Taro.nextTick(() => {
        deps.orderRouteInitializedRef.current = true;
        deps.orderRouteInitializedRef.current = points.length > 0;
        deps.currentFullRouteRef.current = [...points];
        deps.lastErasedIndexRef.current = 0;
        deps.applyRoutePoints(points);
        syncVehiclePointWithRoute(points, deps, servedPoints);
        if (points.length > 0) {
            syncVehiclePointWithRoute(points, deps, servedPoints);
        }
    });
};
src/utils/websocket.ts
@@ -467,16 +467,16 @@
          cached.latest = points;
        }
        Taro.setStorageSync('orderRoutePoints', cached);
        this.notifyListeners('order_route', {
          routeType,
          direction,
          points,
          servedPoints,
          data,
          content,
          forceFullRedraw: data?.type === 'ORDER_UPDATE_LINE'
        });
      }
      this.notifyListeners('order_route', {
        routeType,
        direction,
        points,
        servedPoints,
        data,
        content,
        forceFullRedraw: data?.type === 'ORDER_UPDATE_LINE'
      });
    }
    if (data?.type === 'ORDER_UPDATE_PRICE') {
      const orderId = content?.orderId ?? data?.orderId;