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:
@@ -40,17 +40,20 @@ class OrderControlTests(unittest.TestCase):
|
||||
})
|
||||
|
||||
self.assertEqual(0, response.get_json()["rtn_code"])
|
||||
# 浏览器上报的是 WGS84,入口会先转成 GCJ02 再发给上游(实测上游只认 GCJ02)
|
||||
exp_lat, exp_lng = rental.to_upstream_coords(36.671, 116.981)
|
||||
self.assertFalse(exp_lng == 116.981) # 确实转过
|
||||
self.assertEqual([
|
||||
((7, "POST", "/record/checkLocation"), {
|
||||
"data": {"longitude": 116.981, "latitude": 36.671,
|
||||
"data": {"longitude": exp_lng, "latitude": exp_lat,
|
||||
"bicycleCode": "50001"}
|
||||
}),
|
||||
((7, "POST", "/project/coordinate"), {
|
||||
"data": {"order_id": "9001", "code": "50001"}
|
||||
}),
|
||||
((7, "POST", "/record/close"), {
|
||||
"data": {"order_id": "9001", "end_longitude": 116.981,
|
||||
"end_latitude": 36.671}
|
||||
"data": {"order_id": "9001", "end_longitude": exp_lng,
|
||||
"end_latitude": exp_lat}
|
||||
}),
|
||||
], [(call.args, call.kwargs) for call in upstream.call_args_list])
|
||||
|
||||
|
||||
@@ -145,6 +145,7 @@ class OrderSyncTests(unittest.TestCase):
|
||||
with patch.object(timer, "get_db", self._get_db), \
|
||||
patch.object(timer, "get_user_auth", return_value=("token", "access")), \
|
||||
patch.object(timer, "get_user_rental_settings", return_value=settings), \
|
||||
patch.object(timer, "_park_areas_for_user", return_value=[]), \
|
||||
patch.object(timer, "request_with_user_auth", side_effect=responses), \
|
||||
patch.object(timer, "start_rental_timer"):
|
||||
result = timer._check_user_order(7)
|
||||
@@ -167,6 +168,7 @@ class OrderSyncTests(unittest.TestCase):
|
||||
{"rtn_code": 500, "message": "return rejected"},
|
||||
]
|
||||
with patch.object(timer, "get_db", self._get_db), \
|
||||
patch.object(timer, "_park_areas_for_user", return_value=[]), \
|
||||
patch.object(timer, "request_with_user_auth", side_effect=responses), \
|
||||
patch.object(timer._time, "sleep"), \
|
||||
patch.object(timer, "_cancel_rental_timers") as cancel, \
|
||||
@@ -184,6 +186,7 @@ class OrderSyncTests(unittest.TestCase):
|
||||
{"rtn_code": 0, "message": "ok"},
|
||||
]
|
||||
with patch.object(timer, "get_db", self._get_db), \
|
||||
patch.object(timer, "_park_areas_for_user", return_value=[]), \
|
||||
patch.object(timer, "request_with_user_auth", side_effect=responses), \
|
||||
patch.object(timer, "_try_card_pay_auto", return_value={"success": True}), \
|
||||
patch.object(timer, "_cancel_rental_timers") as cancel, \
|
||||
@@ -206,6 +209,7 @@ class OrderSyncTests(unittest.TestCase):
|
||||
"rental_rounds": 2, "protect_card": True,
|
||||
}
|
||||
with patch.object(timer, "get_db", self._get_db), \
|
||||
patch.object(timer, "_park_areas_for_user", return_value=[]), \
|
||||
patch.object(timer, "request_with_user_auth", side_effect=responses), \
|
||||
patch.object(timer, "get_user_rental_settings", return_value=settings), \
|
||||
patch.object(timer, "_try_card_pay_auto", return_value={"success": True, "message": "月卡支付成功"}), \
|
||||
@@ -240,6 +244,7 @@ class OrderSyncTests(unittest.TestCase):
|
||||
"rental_rounds": 2, "protect_card": True,
|
||||
}
|
||||
with patch.object(timer, "get_db", self._get_db), \
|
||||
patch.object(timer, "_park_areas_for_user", return_value=[]), \
|
||||
patch.object(timer, "request_with_user_auth", side_effect=responses), \
|
||||
patch.object(timer, "get_user_rental_settings", return_value=settings), \
|
||||
patch.object(timer, "_try_card_pay_auto", return_value={"success": True}), \
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""停车区(parkeArea)判定、还车点吸附与坐标系边界。
|
||||
|
||||
覆盖四件事:
|
||||
1. 坐标转换方向别搞反(浏览器 WGS84 / 上游 GCJ02,只在入口转一次);
|
||||
2. 吸附点必须真的落在多边形内;
|
||||
3. 自动还车「发还车之前」就对齐停车区,而不是让三次重试打同一个错坐标;
|
||||
4. 临时的上游抖动不该把订单挂死。
|
||||
"""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import campus
|
||||
import park_area
|
||||
import timer
|
||||
from routes import rental
|
||||
|
||||
# 山管院附近一块方形停车区(GCJ02,上游 parkeArea 的坐标系)
|
||||
PARK_SQUARE = [
|
||||
[36.5313, 116.7797],
|
||||
[36.5313, 116.7807],
|
||||
[36.5323, 116.7807],
|
||||
[36.5323, 116.7797],
|
||||
]
|
||||
PARK_AREAS = [{"points": PARK_SQUARE}]
|
||||
|
||||
|
||||
class CoordinateConversionTests(unittest.TestCase):
|
||||
def test_wgs84_to_gcj02_matches_known_offset(self):
|
||||
lng, lat = campus.wgs84_to_gcj02(116.404, 39.915)
|
||||
# 北京一带:GCJ02 相对 WGS84 偏移在 0.004~0.007 度量级
|
||||
self.assertTrue(0.003 < lng - 116.404 < 0.008)
|
||||
self.assertTrue(-0.003 < lat - 39.915 < 0.003)
|
||||
|
||||
def test_roundtrip_is_sub_meter(self):
|
||||
for lat, lng in ((36.5318, 116.7802), (39.915, 116.404), (22.5431, 114.0579)):
|
||||
g_lng, g_lat = campus.wgs84_to_gcj02(lng, lat)
|
||||
back_lng, back_lat = campus.gcj02_to_wgs84(g_lng, g_lat)
|
||||
self.assertLess(campus.haversine_m(lat, lng, back_lat, back_lng), 1.0)
|
||||
|
||||
def test_out_of_china_is_passthrough(self):
|
||||
self.assertEqual([-122.4194, 37.7749], campus.wgs84_to_gcj02(-122.4194, 37.7749))
|
||||
self.assertEqual([-122.4194, 37.7749], campus.gcj02_to_wgs84(-122.4194, 37.7749))
|
||||
|
||||
|
||||
class ParseParkAreaTests(unittest.TestCase):
|
||||
def test_accepts_dict_and_list_points_and_skips_invalid(self):
|
||||
list_area = [[36.53, 116.78], [36.53, 116.79], [36.54, 116.79]]
|
||||
payload = {
|
||||
"parkeArea": [
|
||||
{"points": [{"latitude": 36.53, "longitude": 116.78}] * 3, "is_disable": 1},
|
||||
{"points": list_area, "is_disable": 1},
|
||||
{"points": [[36.53, 116.78], [36.53, 116.79]]}, # 少于 3 点
|
||||
{"points": PARK_SQUARE, "is_disable": 2}, # 停用(实测语义)
|
||||
{"points": []},
|
||||
"not-a-dict",
|
||||
]
|
||||
}
|
||||
areas = park_area.parse_park_areas(payload)
|
||||
self.assertEqual(2, len(areas))
|
||||
self.assertEqual(3, len(areas[0]["points"]))
|
||||
self.assertEqual(list_area, areas[1]["points"])
|
||||
self.assertNotIn(PARK_SQUARE, [a["points"] for a in areas])
|
||||
|
||||
def test_is_disable_1_is_kept_and_2_is_dropped(self):
|
||||
"""语义是实测出来的:1=启用,2=停用。判反等于把所有能还车的区全丢掉。"""
|
||||
payload = {"parkeArea": [
|
||||
{"points": PARK_SQUARE, "is_disable": 1},
|
||||
{"points": PARK_SQUARE, "is_disable": 2},
|
||||
{"points": PARK_SQUARE},
|
||||
]}
|
||||
areas = park_area.parse_park_areas(payload)
|
||||
self.assertEqual(2, len(areas))
|
||||
|
||||
def test_missing_or_malformed_data_returns_empty(self):
|
||||
for payload in (None, {}, {"parkeArea": None}, {"parkeArea": {}}, []):
|
||||
self.assertEqual([], park_area.parse_park_areas(payload))
|
||||
|
||||
|
||||
class ResolveReturnCoordTests(unittest.TestCase):
|
||||
def test_point_already_inside_is_kept_untouched(self):
|
||||
resolved = park_area.resolve_return_coord(36.5318, 116.7802, PARK_AREAS)
|
||||
self.assertIsNotNone(resolved)
|
||||
self.assertTrue(resolved["inside"])
|
||||
self.assertFalse(resolved["moved"])
|
||||
self.assertEqual((36.5318, 116.7802),
|
||||
(resolved["latitude"], resolved["longitude"]))
|
||||
|
||||
def test_outside_point_is_snapped_inside_the_polygon(self):
|
||||
resolved = park_area.resolve_return_coord(36.5250, 116.7700, PARK_AREAS)
|
||||
self.assertIsNotNone(resolved)
|
||||
self.assertTrue(resolved["moved"])
|
||||
self.assertGreater(resolved["distance"], 100)
|
||||
self.assertTrue(campus.point_in_polygon(
|
||||
resolved["latitude"], resolved["longitude"], PARK_SQUARE))
|
||||
|
||||
def test_no_area_returns_none(self):
|
||||
self.assertIsNone(park_area.resolve_return_coord(36.5, 116.7, []))
|
||||
self.assertIsNone(park_area.resolve_return_coord(None, 116.7, PARK_AREAS))
|
||||
|
||||
def test_nearest_of_two_areas_wins(self):
|
||||
east = [[p[0], p[1] + 0.02] for p in PARK_SQUARE]
|
||||
areas = [{"points": PARK_SQUARE}, {"points": east}]
|
||||
resolved = park_area.resolve_return_coord(36.5318, 116.7995, areas)
|
||||
self.assertIsNotNone(resolved)
|
||||
self.assertGreater(resolved["longitude"], 116.7990)
|
||||
|
||||
|
||||
class BoundaryCoordTests(unittest.TestCase):
|
||||
"""入口坐标:浏览器 WGS84 -> 上游 GCJ02,只转一次。"""
|
||||
|
||||
def test_to_upstream_coords_shifts_into_gcj02(self):
|
||||
lat, lng = rental.to_upstream_coords(36.5250, 116.7700)
|
||||
moved = campus.haversine_m(36.5250, 116.7700, lat, lng)
|
||||
self.assertGreater(moved, 100)
|
||||
self.assertLess(moved, 800)
|
||||
back_lng, back_lat = campus.gcj02_to_wgs84(lng, lat)
|
||||
self.assertLess(campus.haversine_m(36.5250, 116.7700, back_lat, back_lng), 1.0)
|
||||
|
||||
def test_to_upstream_coords_tolerates_bad_input(self):
|
||||
self.assertEqual((None, None), rental.to_upstream_coords(None, None))
|
||||
self.assertEqual(("a", "b"), rental.to_upstream_coords("a", "b"))
|
||||
|
||||
|
||||
class OrderLocationTests(unittest.TestCase):
|
||||
def test_upstream_pass_returns_gcj02_not_raw_browser_value(self):
|
||||
with patch.object(rental, "request_with_user_auth",
|
||||
return_value={"rtn_code": 0, "parkNoShow": False}) as upstream:
|
||||
lat, lng, used_nearest = rental._resolve_order_location(
|
||||
7, "50001", 36.5318, 116.7802)
|
||||
self.assertFalse(used_nearest)
|
||||
expected = rental.to_upstream_coords(36.5318, 116.7802)
|
||||
self.assertAlmostEqual(expected[0], lat, places=6)
|
||||
self.assertAlmostEqual(expected[1], lng, places=6)
|
||||
# checkLocation 也必须拿 GCJ02 去问,否则上游按自己坐标系判必偏 500 米
|
||||
sent = upstream.call_args.kwargs["data"]
|
||||
self.assertAlmostEqual(expected[1], sent["longitude"], places=6)
|
||||
self.assertAlmostEqual(expected[0], sent["latitude"], places=6)
|
||||
|
||||
def test_park_no_show_snaps_into_park_area(self):
|
||||
with patch.object(rental, "request_with_user_auth",
|
||||
return_value={"rtn_code": 0, "parkNoShow": True}), \
|
||||
patch.object(rental, "_park_areas", return_value=PARK_AREAS):
|
||||
out_lat, out_lng, used_nearest = rental._resolve_order_location(
|
||||
7, "50001", 36.5250, 116.7700)
|
||||
self.assertTrue(used_nearest)
|
||||
self.assertTrue(campus.point_in_polygon(out_lat, out_lng, PARK_SQUARE))
|
||||
|
||||
def test_get_nearest_park_point_returns_gcj02_from_legacy_bikes(self):
|
||||
station = {"rtn_code": 0, "data": {"bicycleStation": [
|
||||
{"latitude": 36.5318, "longitude": 116.7802}]}}
|
||||
with patch.object(rental, "_park_areas", return_value=[]), \
|
||||
patch.object(rental, "request_api", return_value=station):
|
||||
point = rental.get_nearest_park_point(36.5250, 116.7700)
|
||||
self.assertIsNotNone(point)
|
||||
# 入参/返回同为 GCJ02:不再转换,值应与车点一致
|
||||
self.assertAlmostEqual(36.5318, point["latitude"], places=6)
|
||||
self.assertAlmostEqual(116.7802, point["longitude"], places=6)
|
||||
|
||||
|
||||
class AutoReturnAlignTests(unittest.TestCase):
|
||||
def test_align_moves_outside_point_into_area(self):
|
||||
with patch.object(timer, "_park_areas_for_user", return_value=PARK_AREAS):
|
||||
lat, lng, moved, dist = timer._align_to_park_area(7, 36.5250, 116.7700)
|
||||
self.assertTrue(moved)
|
||||
self.assertGreater(dist, 100)
|
||||
self.assertTrue(campus.point_in_polygon(lat, lng, PARK_SQUARE))
|
||||
|
||||
def test_align_leaves_inside_point_alone(self):
|
||||
with patch.object(timer, "_park_areas_for_user", return_value=PARK_AREAS):
|
||||
lat, lng, moved, _ = timer._align_to_park_area(7, 36.5318, 116.7802)
|
||||
self.assertFalse(moved)
|
||||
self.assertEqual((36.5318, 116.7802), (lat, lng))
|
||||
|
||||
def test_align_without_park_areas_is_noop(self):
|
||||
with patch.object(timer, "_park_areas_for_user", return_value=[]):
|
||||
self.assertEqual(
|
||||
(36.5250, 116.7700, False, 0.0),
|
||||
timer._align_to_park_area(7, 36.5250, 116.7700))
|
||||
|
||||
|
||||
class AutoReturnTests(unittest.TestCase):
|
||||
RENTAL_ROW = {"id": 1, "bike_code": "50001", "order_id": "9001",
|
||||
"expired": 0, "end_time": "2026-01-01 00:00:00"}
|
||||
|
||||
def _run(self, bike_lat, bike_lng, return_results):
|
||||
calls = []
|
||||
|
||||
def fake_upstream(user_id, method, path, params=None, data=None, **kwargs):
|
||||
if path == "/project/bicycle":
|
||||
return {"rtn_code": 0,
|
||||
"data": {"latitude": bike_lat, "longitude": bike_lng}}
|
||||
if path == "/record/return":
|
||||
calls.append(dict(data))
|
||||
return return_results[min(len(calls) - 1, len(return_results) - 1)]
|
||||
return {"rtn_code": 0}
|
||||
|
||||
with patch.object(timer, "get_db") as get_db, \
|
||||
patch.object(timer, "request_with_user_auth", side_effect=fake_upstream), \
|
||||
patch.object(timer, "_park_areas_for_user", return_value=PARK_AREAS), \
|
||||
patch.object(timer, "_mark_returned"), \
|
||||
patch.object(timer, "mark_return_tokens_used"), \
|
||||
patch.object(timer, "_cancel_rental_timers"), \
|
||||
patch.object(timer, "_maybe_renew_after_return",
|
||||
return_value={"renewed": False}), \
|
||||
patch.object(timer, "_time") as fake_time:
|
||||
fake_time.sleep = lambda *_: None
|
||||
get_db.return_value.cursor.return_value.fetchone.return_value = dict(self.RENTAL_ROW)
|
||||
result = timer._auto_return_bike(1, 7, allow_renewal=False)
|
||||
return result, calls
|
||||
|
||||
def test_return_goes_to_park_area_on_the_first_try(self):
|
||||
"""车停在区外:发还车之前就对齐,不该让三次重试打同一个错坐标。"""
|
||||
result, calls = self._run(36.5250, 116.7700, [{"rtn_code": 0, "message": "ok"}])
|
||||
self.assertTrue(result["success"])
|
||||
self.assertEqual(1, len(calls))
|
||||
self.assertNotEqual(116.7700, calls[0]["longitude"])
|
||||
self.assertTrue(campus.point_in_polygon(
|
||||
calls[0]["latitude"], calls[0]["longitude"], PARK_SQUARE))
|
||||
|
||||
def test_inside_point_is_sent_as_is_and_transient_failure_retries(self):
|
||||
"""车已在区内:坐标原样发;上游抖动一次后重试成功。"""
|
||||
result, calls = self._run(36.5318, 116.7802, [
|
||||
{"rtn_code": 500, "message": "busy"},
|
||||
{"rtn_code": 0, "message": "ok"},
|
||||
])
|
||||
self.assertTrue(result["success"])
|
||||
self.assertEqual(2, len(calls))
|
||||
for call in calls:
|
||||
self.assertAlmostEqual(116.7802, call["longitude"], places=6)
|
||||
self.assertAlmostEqual(36.5318, call["latitude"], places=6)
|
||||
|
||||
def test_failure_after_inside_point_really_fails_and_keeps_order_open(self):
|
||||
result, calls = self._run(36.5318, 116.7802, [
|
||||
{"rtn_code": 400, "message": "请到指定停车点还车"},
|
||||
])
|
||||
self.assertFalse(result["success"])
|
||||
self.assertEqual(3, len(calls))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user