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
+35 -11
View File
@@ -4,6 +4,7 @@
import json
import math
import random
import sqlite3
import datetime as dt
import threading
import time
@@ -670,23 +671,37 @@ def order_unlock():
end_time = now_beijing + dt.timedelta(minutes=duration)
conn = get_db()
cursor = conn.cursor()
cursor.execute("""
INSERT INTO rentals (id, order_id, bike_code, email, user_id, start_time, end_time,
duration, expired, source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 'unlock')
""", (int(order_id), order_id, code, user["email"], user["id"],
now_beijing.strftime("%Y-%m-%d %H:%M:%S"),
end_time.strftime("%Y-%m-%d %H:%M:%S"), duration))
try:
cursor.execute("""
INSERT INTO rentals (id, order_id, bike_code, email, user_id, start_time, end_time,
duration, expired, source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 'unlock')
""", (int(order_id), order_id, code, user["email"], user["id"],
now_beijing.strftime("%Y-%m-%d %H:%M:%S"),
end_time.strftime("%Y-%m-%d %H:%M:%S"), duration))
rental_row_id = int(order_id)
except sqlite3.IntegrityError:
# rentals.id 可能被 reminder/sync 的自增行占用(序列漂移):改用自增 id,
# order_id 仍记录真实上游单号。用 lastrowid 当 rental_id,别把计时器挂到别人单上。
print(f"[开锁] rentals.id={order_id} 已被占用,改用自增 id")
cur2 = conn.execute("""
INSERT INTO rentals (order_id, bike_code, email, user_id, start_time, end_time,
duration, expired, source)
VALUES (?, ?, ?, ?, ?, ?, ?, 0, 'unlock')
""", (order_id, code, user["email"], user["id"],
now_beijing.strftime("%Y-%m-%d %H:%M:%S"),
end_time.strftime("%Y-%m-%d %H:%M:%S"), duration))
rental_row_id = cur2.lastrowid
conn.commit()
conn.close()
start_rental_timer(rental_id=int(order_id), email=user["email"],
start_rental_timer(rental_id=int(rental_row_id), email=user["email"],
bike_code=code, duration_minutes=duration, user_id=user["id"])
_record_rental_start(int(order_id), latitude, longitude)
_record_rental_start(int(rental_row_id), latitude, longitude)
# 带上轮次预估:前端租车条要显示「共N轮 · 预计HH:MM还车」
row = get_db().execute(
"SELECT renewal_round, rounds_override, end_time FROM rentals WHERE id = ?",
(int(order_id),)).fetchone()
(int(rental_row_id),)).fetchone()
summary = _rounds_summary(
dict(row) if row else {"order_id": str(order_id),
"end_time": end_time.strftime("%Y-%m-%d %H:%M:%S")},
@@ -696,10 +711,19 @@ def order_unlock():
else:
from wechat import friendly_upstream_message
raw_open_msg = open_result.get("message", "")
# 上车失败必须把刚下的单撤掉,否则上游留一张没人骑的「未支付」订单;
# 下次开锁命中「未支付」分支时会用月卡把它付掉 —— 白扣一次卡次。
try:
from subscribe import _discard_pending_order
cleaned = _discard_pending_order(user["id"], order_id, code, latitude, longitude)
except Exception as exc:
cleaned = False
print(f"[开锁] 撤销半成品订单异常: {exc}")
if open_result.get("rtn_code") == 102001 or "token" in raw_open_msg.lower():
return jsonify({"rtn_code": 102001, "message": "Token失效,微信凭据已过期,请在个人中心重新配置"})
return jsonify({"rtn_code": open_result.get("rtn_code", 500),
"message": friendly_upstream_message(raw_open_msg, fallback="开锁失败,请稍后重试"),
"message": friendly_upstream_message(raw_open_msg, fallback="开锁失败,请稍后重试")
+ ("(已撤销本次下单)" if cleaned else ""),
"order_id": order_id})