feat: improve campus bike discovery and add independent live preview

This commit is contained in:
zzy
2026-11-04 16:42:47 +08:00
parent f986c8deae
commit b6b7b3dbe5
42 changed files with 5267 additions and 4313 deletions
+32 -66
View File
@@ -30,6 +30,9 @@ def _rule_row(row: dict, locations_by_id: dict) -> dict:
"auto_unlock": bool(row["auto_unlock"]),
"enabled": bool(row["enabled"]),
"last_fired_on": row.get("last_fired_on") or "",
"last_run_at": row.get("last_run_at"),
"last_run_status": row.get("last_run_status"),
"last_run_message": row.get("last_run_message"),
# 自动开锁筛选配置
"kinds": flt["kinds"],
"min_electric": flt["min_electric"],
@@ -105,74 +108,37 @@ def rules_update(rule_id: int):
conn.close()
return jsonify({"rtn_code": 404, "message": "规则不存在"})
current = dict(row)
updates = []
params = []
def _truthy(v):
return 1 if v in (True, 1, "1", "true") else 0
if "enabled" in data:
updates.append("enabled = ?")
params.append(_truthy(data.get("enabled")))
if "notify_enabled" in data:
updates.append("notify_enabled = ?")
params.append(_truthy(data.get("notify_enabled")))
if "auto_unlock" in data:
updates.append("auto_unlock = ?")
params.append(_truthy(data.get("auto_unlock")))
# 组合校验:任何一个筛选字段变化时,用「现值 + 新值」整体校验一次
filter_keys = ("kinds", "min_electric", "max_electric", "sort_by", "retry_times")
if any(k in data for k in filter_keys):
flt = sub.rule_filters(current)
merged = {
"location_id": current.get("location_id"),
"weekday": current.get("weekday"),
"time": "%02d:%02d" % (int(current.get("minute_of_day") or 0) // 60,
int(current.get("minute_of_day") or 0) % 60),
"kinds": data.get("kinds", flt["kinds"]),
"min_electric": data.get("min_electric", flt["min_electric"]),
"max_electric": data.get("max_electric", flt["max_electric"]),
"sort_by": data.get("sort_by", flt["sort_by"]),
"retry_times": data.get("retry_times", flt["retry_times"]),
"notify_enabled": True,
"auto_unlock": bool(current.get("auto_unlock")),
}
try:
fields = sub.normalize_rule(merged)
except ValueError as exc:
conn.close()
return jsonify({"rtn_code": 400, "message": str(exc)})
updates.extend(["kinds = ?", "min_electric = ?", "max_electric = ?",
"sort_by = ?", "retry_times = ?"])
params.extend([fields["kinds"], fields["min_electric"], fields["max_electric"],
fields["sort_by"], fields["retry_times"]])
if ("time" in data and "weekday" in data) or ("time" in data or "weekday" in data):
merged_time = {
"location_id": current.get("location_id"),
"weekday": data.get("weekday", current.get("weekday")),
"time": data.get("time") or "%02d:%02d" % (
int(current.get("minute_of_day") or 0) // 60,
int(current.get("minute_of_day") or 0) % 60),
}
try:
fields = sub.normalize_rule(merged_time)
except ValueError as exc:
conn.close()
return jsonify({"rtn_code": 400, "message": str(exc)})
updates.extend(["weekday = ?", "minute_of_day = ?"])
params.extend([fields["weekday"], fields["minute_of_day"]])
if not updates:
allowed = {"enabled", "notify_enabled", "auto_unlock", "location_id", "weekday", "time",
"kinds", "min_electric", "max_electric", "sort_by", "retry_times"}
if not data or not set(data).intersection(allowed):
conn.close()
return jsonify({"rtn_code": 400, "message": "没有需要更新的字段"})
params.extend([rule_id, user["id"]])
conn.execute(f"UPDATE subscribe_rules SET {', '.join(updates)} "
f"WHERE id = ? AND user_id = ?", params)
return jsonify(rtn_code=400, message="没有需要更新的字段"), 400
filters = sub.rule_filters(current)
merged = {**filters, "location_id": current["location_id"], "weekday": current["weekday"],
"time": "%02d:%02d" % divmod(current["minute_of_day"], 60),
"notify_enabled": bool(current["notify_enabled"]), "auto_unlock": bool(current["auto_unlock"])}
merged.update({k: v for k, v in data.items() if k in allowed})
try:
fields = sub.normalize_rule(merged)
from campus import get_location
location = get_location(fields["location_id"])
if not location or location.get("kind") != "parking":
raise ValueError("订阅只能选择有效停车点")
enabled = data.get("enabled", bool(current["enabled"]))
if enabled not in (True, False, 0, 1, "0", "1", "true", "false"):
raise ValueError("启用状态无效")
enabled = int(enabled in (True, 1, "1", "true"))
except (TypeError, ValueError) as exc:
conn.close()
return jsonify(rtn_code=400, message=str(exc)), 400
columns = ("location_id", "weekday", "minute_of_day", "notify_enabled", "auto_unlock",
"kinds", "min_electric", "max_electric", "sort_by", "retry_times")
values = [int(fields[k]) if k in ("notify_enabled", "auto_unlock") else fields[k] for k in columns]
conn.execute("UPDATE subscribe_rules SET " + ", ".join(k + " = ?" for k in columns) +
", enabled = ? WHERE id = ? AND user_id = ?", (*values, enabled, rule_id, user["id"]))
conn.commit()
conn.close()
return jsonify({"rtn_code": 0, "message": "规则已更新"})
return jsonify(rtn_code=0, message="规则已更新")
@bp.route("/api/subscribe/rules/<int:rule_id>", methods=["DELETE"])
@@ -197,4 +163,4 @@ def subscribe_options():
{"key": "red", "label": "红车"}],
"sort_by": [{"key": k, "label": v} for k, v in sub.SORT_OPTIONS.items()],
"auto_unlock_enabled": sub.AUTO_UNLOCK_ENABLED,
}})
}})