| | |
| | | import Taro from '@tarojs/taro'; |
| | | import { useEffect, useRef } from 'react'; |
| | | import { mapOrderStatusToTripStatus } from '../../../../utils/Robotaxi-public-method'; |
| | | import { queryOrderTrajectory, shareQueryOrderInfo, shareQueryOrderRoute } from '../../../api/begin-route'; |
| | | import { |
| | | queryOrderTrajectorySilently, |
| | | shareQueryOrderInfoSilently, |
| | | shareQueryOrderRouteSilently |
| | | } from '../../../api/begin-route'; |
| | | import { normalizeDistanceKm, normalizeDurationSeconds } from '../../../utils/format'; |
| | | import { TripStatus } from '../../../constants/trip-status'; |
| | | |
| | | type LocationPoint = { latitude: number; longitude: number }; |
| | | |
| | | type RouteDistance = { value: number; unit: 'km' | 'm' } | null; |
| | | type SilentApiResponse<T> = { |
| | | code?: number; |
| | | status?: number; |
| | | msg?: string; |
| | | data?: T; |
| | | }; |
| | | |
| | | const SHARE_TRIP_POLL_INTERVAL_MS = 5000; // 分享页轮询间隔 |
| | | const MAX_SHARE_TRIP_POLL_COUNT = 6000; // 分享页最多轮询 10 分钟 |
| | |
| | | const toFiniteNumber = (value: any) => { |
| | | const num = Number(value); |
| | | return Number.isFinite(num) ? num : null; |
| | | }; |
| | | |
| | | /** |
| | | * 分享页静默接口统一解包。 |
| | | * 这里不走全局默认异常 toast,由页面自行决定如何处理。 |
| | | */ |
| | | const unwrapSilentResponse = <T>(response: SilentApiResponse<T>) => { |
| | | const code = Number(response?.code ?? response?.status ?? 200); |
| | | if (code === 200) { |
| | | return response?.data ?? response; |
| | | } |
| | | throw response; |
| | | }; |
| | | |
| | | /** |
| | |
| | | */ |
| | | const requestShareRouteByStatus = (targetStatus: TripStatus) => { |
| | | if ([TripStatus.TripFinished, TripStatus.PaymentSuccess].includes(targetStatus)) { |
| | | return queryOrderTrajectory({ |
| | | return queryOrderTrajectorySilently({ |
| | | orderId: deps.orderId, |
| | | routeType: 2, |
| | | }); |
| | | } |
| | | return shareQueryOrderRoute(deps.orderId); |
| | | return shareQueryOrderRouteSilently(deps.orderId); |
| | | }; |
| | | |
| | | useEffect(() => { |
| | |
| | | }; |
| | | |
| | | const fetchInitialShareInfo = async () => { |
| | | const info = await shareQueryOrderInfo({ orderId: deps.orderId }); |
| | | const infoResponse = await shareQueryOrderInfoSilently({ orderId: deps.orderId }); |
| | | const info = unwrapSilentResponse(infoResponse); |
| | | if (cancelled) return null; |
| | | return applyShareInfo(info || {}); |
| | | }; |
| | |
| | | stopPolling(); |
| | | return false; |
| | | } |
| | | let routeResponse = await requestShareRouteByStatus(tripStatusRef.current); |
| | | let routeResponse = unwrapSilentResponse( |
| | | await requestShareRouteByStatus(tripStatusRef.current) |
| | | ); |
| | | if (cancelled) return false; |
| | | const routeStatus = resolveRouteOrderStatus(routeResponse); |
| | | const shouldFetchInfo = routeStatus !== null && routeStatus !== infoStatusRef.current; |
| | |
| | | let nextEndSite = endSiteRef.current; |
| | | |
| | | if (shouldFetchInfo) { |
| | | const info = await shareQueryOrderInfo({ orderId: deps.orderId }); |
| | | const infoResponse = await shareQueryOrderInfoSilently({ orderId: deps.orderId }); |
| | | const info = unwrapSilentResponse(infoResponse); |
| | | if (cancelled) return false; |
| | | const infoState = applyShareInfo(info || {}); |
| | | mappedStatus = infoState.mappedStatus; |
| | |
| | | // TripFinished 继续走分享实时路线接口,完单态走完整轨迹接口, |
| | | // 这样地图才能基于最新路线按 order_detail 视口完整展示。 |
| | | if (mappedStatus >= TripStatus.TripFinished) { |
| | | routeResponse = await requestShareRouteByStatus(mappedStatus); |
| | | routeResponse = unwrapSilentResponse( |
| | | await requestShareRouteByStatus(mappedStatus) |
| | | ); |
| | | if (cancelled) return false; |
| | | } |
| | | } |