Auto-reconnect CTP with new front-end addresses after saving settings

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-25 17:51:18 +08:00
parent b641a4eaa0
commit ca541d5fc3
3 changed files with 21 additions and 8 deletions
+13 -7
View File
@@ -1715,20 +1715,26 @@ def settings():
from ctp_settings import save_ctp_settings_from_form
save_ctp_settings_from_form(request.form, set_setting)
flash_msg = "CTP 配置已保存,正在使用新地址重连…"
try:
from vnpy_bridge import get_bridge, _persist_last_error
from vnpy_bridge import get_bridge
from trading_context import get_trading_mode
b = get_bridge()
b.mark_disconnected()
if (request.form.get("simnow_password") or "").strip() or (
request.form.get("ctp_live_password") or ""
).strip():
b._clear_login_cooldown()
b._last_error = ""
_persist_last_error("")
except Exception:
pass
flash("CTP 配置已保存,请在持仓监控页重连 CTP")
mode = get_trading_mode(get_setting)
info = b.reconnect_after_settings_saved(mode)
if info.get("cooldown"):
flash_msg = "CTP 配置已保存;当前处于登录冷却,请稍后再连"
elif not info.get("started") and info.get("connected"):
flash_msg = "CTP 配置已保存,当前连接正常"
except Exception as exc:
app.logger.warning("CTP reconnect after settings save: %s", exc)
flash_msg = "CTP 配置已保存,请稍后在持仓监控页重连"
flash(flash_msg)
elif action == "nav":
items = {k: request.form.get(f"nav_{k}") == "on" for k in NAV_TOGGLES}
save_nav_items(set_setting, items)