feat: 还车坐标统一 GCJ02 + 停车区多边形吸附,车辆卡片左右分栏
## 还车坐标(实测结论:上游只认 GCJ02,WGS84 偏差约 500 米必被拒) - 新增 park_area.py:parkeArea 多边形判定 + 最近停车区吸附; 自动还车在发送前就对齐坐标,不再拿同一个「不在停车区」的点重试三次 - 入口统一 WGS84 -> GCJ02(routes.rental.to_upstream_coords); 手动还车 / 临时锁车 / 共享还车一并转换,本地订单起终点与楼栋标签随之修正 - is_disable 语义按实测纠正:1=启用、2=停用(原来判反,22 块启用区全被丢弃) - 停车区读取优先用用户凭据,避免全局 token 失效后吸附静默失效 - 修复「订单信息不存在」(204010) 被当成还车成功;状态兜底再按车牌确认 ## 前端 - 车辆卡片改为左右两栏:左「距你 N 米 / 步行 N 分钟」,右「电量 / 状态」; 未定位时左栏隐藏,380px 断点防止挤压换行 - 停用停车区不再绘制(原来还把启用区画成灰色) ## 测试 - 新增 tests/test_park_area.py(20 条);全量 121 pytest + 26 node 通过
This commit is contained in:
@@ -24,6 +24,10 @@ _checker_lock = threading.Lock()
|
||||
# 10 分钟轮询会让 5/3/2/1/0 分钟提醒继续发送,改为每分钟核对一次。
|
||||
ORDER_CHECK_INTERVAL = 60
|
||||
|
||||
# 自动还车用的停车区缓存:{user_id: (ts, parkeArea)},避免续租链反复拉地图
|
||||
_PARK_AREA_CACHE: dict = {}
|
||||
_PARK_AREA_TTL = 60
|
||||
|
||||
|
||||
def _get_remaining_seconds(rental: dict) -> int:
|
||||
"""计算距离过期的秒数"""
|
||||
@@ -370,8 +374,58 @@ def _renewal_lock(rental_id: int):
|
||||
return _renewal_locks.setdefault(rental_id, threading.Lock())
|
||||
|
||||
|
||||
def _park_areas_for_user(user_id: int) -> list:
|
||||
"""用用户凭据取上游停车区多边形(GCJ02)。结果短缓存,避免续租链反复拉地图。"""
|
||||
import park_area
|
||||
cached = _PARK_AREA_CACHE.get(user_id)
|
||||
now = _time.time()
|
||||
if cached and now - cached[0] < _PARK_AREA_TTL:
|
||||
return cached[1]
|
||||
try:
|
||||
result = request_with_user_auth(
|
||||
user_id, "GET", "/project/map", params={"type": "1"}, _silent=True
|
||||
)
|
||||
except Exception as exc:
|
||||
print(f"[自动还车] 读取停车区失败: {exc}")
|
||||
return []
|
||||
if not isinstance(result, dict) or result.get("rtn_code") not in (None, 0):
|
||||
return []
|
||||
areas = park_area.parse_park_areas(result.get("data") or {})
|
||||
if areas:
|
||||
_PARK_AREA_CACHE[user_id] = (now, areas)
|
||||
return areas
|
||||
|
||||
|
||||
def _align_to_park_area(user_id: int, latitude, longitude):
|
||||
"""把自动还车的坐标对齐到停车区(入参/返回都是 GCJ02)。
|
||||
|
||||
返回 (lat, lng, moved, distance)。拿不到 parkeArea 时原样返回。
|
||||
自动还车前**先**对齐一次,绝大多数情况一次就还成功,不必靠重试撞。
|
||||
"""
|
||||
import park_area
|
||||
areas = _park_areas_for_user(user_id)
|
||||
if not areas:
|
||||
return latitude, longitude, False, 0.0
|
||||
try:
|
||||
resolved = park_area.resolve_return_coord(latitude, longitude, areas)
|
||||
except Exception as exc:
|
||||
print(f"[自动还车] 停车区判定异常: {exc}")
|
||||
return latitude, longitude, False, 0.0
|
||||
if resolved and resolved.get("moved"):
|
||||
return (resolved["latitude"], resolved["longitude"], True,
|
||||
float(resolved.get("distance") or 0.0))
|
||||
return latitude, longitude, False, 0.0
|
||||
|
||||
|
||||
def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = True):
|
||||
"""自动还车(含重试:最多3次),返回明确的成败结果。"""
|
||||
"""自动还车(含重试:最多3次),返回明确的成败结果。
|
||||
|
||||
坐标链路(与手动还车同一套停车区规则):
|
||||
1. 取车辆坐标(`/project/bicycle`,上游 = GCJ02,不要再转);
|
||||
2. **发还车前**先用本地 parkeArea 多边形判定一次,不在停车区就先吸附到
|
||||
最近的合法点 —— 原来直接发原始坐标,parkNoShow 时三次重试注定全失败;
|
||||
3. 万一对齐后仍失败,第 2 次重试前再吸附一次(应对上游边界与本地不一致)。
|
||||
"""
|
||||
conn = get_db()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM rentals WHERE id = ?", (rental_id,))
|
||||
@@ -393,8 +447,18 @@ def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = True):
|
||||
# 上游要求真实订单号;老记录可能只有本地自增 id,则兜底用它
|
||||
upstream_order_id = rental["order_id"] if rental["order_id"] else str(rental_id)
|
||||
|
||||
# 发还车前先对齐停车区:车辆坐标不在 parkeArea 内时直接换成最近的合法点,
|
||||
# 否则 parkNoShow 会让三次重试全都打在同一个错坐标上。
|
||||
latitude, longitude, aligned, aligned_m = _align_to_park_area(
|
||||
user_id, latitude, longitude
|
||||
)
|
||||
if aligned:
|
||||
print(f"[自动还车] 车辆坐标不在停车区,已对齐到最近停车区 "
|
||||
f"({latitude:.6f}, {longitude:.6f}) [{aligned_m:.0f}m]")
|
||||
|
||||
return_result = None
|
||||
last_error = None
|
||||
snap_attempted = aligned # 已经对齐过的话,重试前不必再对齐一次
|
||||
for attempt in range(1, 4):
|
||||
try:
|
||||
return_result = request_with_user_auth(
|
||||
@@ -411,6 +475,16 @@ def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = True):
|
||||
except Exception as e:
|
||||
last_error = str(e)
|
||||
print(f"[自动还车] 第{attempt}次异常: {last_error}")
|
||||
# 首次失败:再对齐一次停车区(应对上游边界与本地多边形不一致)
|
||||
if not snap_attempted and attempt < 3:
|
||||
snap_attempted = True
|
||||
new_lat, new_lng, moved, moved_m = _align_to_park_area(
|
||||
user_id, latitude, longitude
|
||||
)
|
||||
if moved:
|
||||
latitude, longitude = new_lat, new_lng
|
||||
print(f"[自动还车] 已吸附到最近停车区 "
|
||||
f"({latitude:.6f}, {longitude:.6f}) [{moved_m:.0f}m],准备重试")
|
||||
if attempt < 3:
|
||||
_time.sleep(5)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user