fix(批次A): 明文监听、续租换单后假成功、share 假成功、开锁失败不撤单、admin 口令哈希

- app.py:默认监听改 127.0.0.1(原本 0.0.0.0,与同文件注释矛盾,
  明文 HTTP 可直取 Bearer token/access_token;需要时用 XBCR_HOST 显式放宽)
- admin 口令:支持 admin_password_hash(pbkdf2)+ 恒定时间比较,
  登录成功写 session.permanent;本地 admin_config 已改为哈希存储(口令 070517zhan)
- timer._auto_return_bike:读完行立刻关连接(原来一直挂到函数末尾,中途异常就泄漏);
  还车成功后 pop routes.rental._user_info_cache —— 续租会换 order_id,
  不清缓存的话前端 60 秒内还车会拿到旧单号被判「已结束」,而车已在第 2 轮计费
- timer._mark_returned:try/finally 关连接
- timer 复活 expired=1 的行时同时复位 notified_*(否则提醒与 0 分钟自动还车永久失效,
  极简 schema 无这些列时自动回退)
- routes/share.py:_upstream_order_finished 补「同车牌仍在骑行」兜底(与 rental 版对齐);
  204010/「订单信息不存在」直接硬拒,不再走状态确认假成功
- routes/rental.py 开锁失败:调用 _discard_pending_order 撤销半成品订单
  (上游不残留未支付单,避免下次开锁用月卡替它付钱);
  rentals.id 撞历史自增行时改用 lastrowid,计时器不再挂到别人单上
- subscribe 自动开锁落单冲突兜底:改用 lastrowid 当 rental_id

测试:161 pytest + 32 node 全绿
This commit is contained in:
zzy
2026-11-15 22:34:53 +08:00
parent 33df0b0b11
commit 32d3b8d426
8 changed files with 214 additions and 71 deletions
+57 -30
View File
@@ -240,26 +240,30 @@ def _mark_returned(rental_id: int, latitude=None, longitude=None):
label = None
conn = get_db()
try:
conn.execute("""
UPDATE rentals SET expired = 1, returned_at = datetime('now', '+8 hours'),
end_latitude = ?, end_longitude = ?, end_label = ?
WHERE id = ?
""", (latitude, longitude, label, rental_id))
except sqlite3.OperationalError:
# 轻量兜底:旧库/极简 schema 缺列时补齐再重试一次
for col_def in [("returned_at", "TIMESTAMP"), ("end_latitude", "REAL"),
("end_longitude", "REAL"), ("end_label", "TEXT")]:
try:
conn.execute(f"ALTER TABLE rentals ADD COLUMN {col_def[0]} {col_def[1]}")
except sqlite3.OperationalError:
pass
conn.execute("""
UPDATE rentals SET expired = 1, returned_at = datetime('now', '+8 hours'),
end_latitude = ?, end_longitude = ?, end_label = ?
WHERE id = ?
""", (latitude, longitude, label, rental_id))
conn.commit()
conn.close()
try:
conn.execute("""
UPDATE rentals SET expired = 1, returned_at = datetime('now', '+8 hours'),
end_latitude = ?, end_longitude = ?, end_label = ?
WHERE id = ?
""", (latitude, longitude, label, rental_id))
except sqlite3.OperationalError:
# 轻量兜底:旧库/极简 schema 缺列时补齐再重试一次
for col_def in [("returned_at", "TIMESTAMP"), ("end_latitude", "REAL"),
("end_longitude", "REAL"), ("end_label", "TEXT")]:
try:
conn.execute(f"ALTER TABLE rentals ADD COLUMN {col_def[0]} {col_def[1]}")
except sqlite3.OperationalError:
pass
conn.execute("""
UPDATE rentals SET expired = 1, returned_at = datetime('now', '+8 hours'),
end_latitude = ?, end_longitude = ?, end_label = ?
WHERE id = ?
""", (latitude, longitude, label, rental_id))
conn.commit()
finally:
# 写失败也要关连接:以前这里异常会泄漏连接(fd 累积),
# 而且调用方拿不到「状态没落库」的信号
conn.close()
def _reopen_same_bike(user_id: int, bike_code: str) -> dict:
@@ -440,15 +444,18 @@ def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = True):
3. 万一对齐后仍失败,第 2 次重试前再吸附一次(应对上游边界与本地不一致)。
"""
conn = get_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM rentals WHERE id = ?", (rental_id,))
rental = cursor.fetchone()
if not rental:
try:
cursor = conn.cursor()
cursor.execute("SELECT * FROM rentals WHERE id = ?", (rental_id,))
rental = cursor.fetchone()
finally:
# 这个连接只用来读这一行,读完立刻关:以前一直挂到函数末尾,
# 中间任何异常(上游超时、写库失败)都会把连接漏掉。
conn.close()
if not rental:
print(f"[自动还车] 记录不存在: rental_id={rental_id}")
return {"success": False, "rtn_code": 404, "message": "本地租车记录不存在"}
if rental["expired"]:
conn.close()
return {"success": True, "rtn_code": 0, "message": "订单已结束"}
bike_code = rental["bike_code"]
@@ -517,7 +524,14 @@ def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = True):
# 让邮件按钮可重试,后台对账也能继续跟踪真实状态。
if success:
_mark_returned(rental_id, latitude, longitude)
conn.close()
# 续租会换新的 order_id;不清掉 60 秒的 /user/info 缓存的话,
# 前端在缓存窗口内点还车会拿到旧 order_id、被判「已结束」返回 rtn 0,
# 而车其实已经在第 2 轮计费。
try:
from routes.rental import _user_info_cache
_user_info_cache.pop(user_id, None)
except Exception:
pass
renewal_result = {"renewed": False}
if success:
_cancel_rental_timers(rental_id)
@@ -1193,10 +1207,23 @@ def _check_user_order(user_id: int):
if existing:
rental_id = existing["id"]
cursor.execute("""
UPDATE rentals SET expired = 0, source = 'sync',
start_time = ?, end_time = ?, duration = ?, email = ? WHERE id = ?
""", (start_str, end_str, duration, email, rental_id))
# 复活这张单时把 notified_* 一起复位:本地可能刚被 _close_rental_locally /
# _finish_active_wechat_rentals 置过 expired=1,那些提醒位还留着 1,
# 复活的单就再也收不到 5/3/2/1 提醒和 0 分钟自动还车了。
try:
cursor.execute("""
UPDATE rentals SET expired = 0, source = 'sync',
start_time = ?, end_time = ?, duration = ?, email = ?,
notified_10 = 0, notified_5 = 0, notified_3 = 0,
notified_2 = 0, notified_1 = 0, notified_0 = 0
WHERE id = ?
""", (start_str, end_str, duration, email, rental_id))
except sqlite3.OperationalError:
# 极简 schema 没有这些列:退回原来的更新
cursor.execute("""
UPDATE rentals SET expired = 0, source = 'sync',
start_time = ?, end_time = ?, duration = ?, email = ? WHERE id = ?
""", (start_str, end_str, duration, email, rental_id))
old_source = existing["source"]
print(f"[订单检测] 升级租车记录: rental_id={rental_id}, order_id={order_id}, "
f"source={old_source}→sync, 剩余={remaining}分钟")