From cfc703ae5b1f92deb3626eb614f9ea182cd31727 Mon Sep 17 00:00:00 2001 From: dekun Date: Thu, 25 Jun 2026 19:03:25 +0800 Subject: [PATCH] Fix double POST on open position in embed shell mode. Embed capture-phase form handler and allowManualOrderSubmit both submitted /add_order; skip custom forms and use a single fetch reload path. Co-authored-by: Cursor --- embed_templates/embed_boot_scripts.html | 4 ++++ embed_templates/embed_shell.html | 2 +- static/instance_embed.js | 25 +++++++++++++++++++++++++ static/key_monitor_form.js | 20 ++++++++++++++++---- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/embed_templates/embed_boot_scripts.html b/embed_templates/embed_boot_scripts.html index ead5c0d..f2251f0 100644 --- a/embed_templates/embed_boot_scripts.html +++ b/embed_templates/embed_boot_scripts.html @@ -889,6 +889,10 @@ function cancelExchangeTpsl(orderId, role){ function allowManualOrderSubmit(form){ form.dataset.rrOk = "1"; + if(document.body && document.body.getAttribute("data-embed-shell") === "1" && window.InstanceEmbed && window.InstanceEmbed.postFormAndReload){ + window.InstanceEmbed.postFormAndReload(form, "开仓提交中…"); + return; + } if(window.FormSubmitGuard){ if(FormSubmitGuard.isLocked(form)){ FormSubmitGuard.setSubmitLabel(form, "开仓提交中…"); diff --git a/embed_templates/embed_shell.html b/embed_templates/embed_shell.html index 0977200..da7204d 100644 --- a/embed_templates/embed_shell.html +++ b/embed_templates/embed_shell.html @@ -115,6 +115,6 @@ {% include 'embed_boot_scripts.html' %} - + diff --git a/static/instance_embed.js b/static/instance_embed.js index c490072..623ed9a 100644 --- a/static/instance_embed.js +++ b/static/instance_embed.js @@ -14,6 +14,9 @@ let navToken = 0; let loadingTab = false; + /** 自带校验后 form.submit() 的表单,勿在捕获阶段再 fetch 一份(会双发 POST) */ + const CUSTOM_SUBMIT_FORM_IDS = new Set(["add-order-form", "key-form"]); + function isEmbedShell() { return document.body && document.body.getAttribute("data-embed-shell") === "1"; } @@ -124,6 +127,26 @@ return loadTab(getTab(), { replace: true, skipUrl: true }); } + function postFormAndReload(form, label) { + if (!form) return Promise.resolve(); + if (global.FormSubmitGuard) { + if (global.FormSubmitGuard.isLocked(form)) { + global.FormSubmitGuard.setSubmitLabel(form, label || "提交中…"); + } else { + global.FormSubmitGuard.lock(form, label || "提交中…"); + } + } + const fd = new FormData(form); + return fetch(form.action, { + method: form.method || "POST", + body: fd, + credentials: "same-origin", + redirect: "manual", + }) + .then(() => reloadCurrentTab()) + .catch(() => reloadCurrentTab()); + } + function patchApplyListWindow() { if (typeof global.applyListWindow !== "function") return; global.applyListWindow = function embedApplyListWindow() { @@ -176,6 +199,7 @@ const form = ev.target; if (!(form instanceof HTMLFormElement)) return; if (form.method && form.method.toUpperCase() === "GET") return; + if (CUSTOM_SUBMIT_FORM_IDS.has(form.id)) return; ev.preventDefault(); const fd = new FormData(form); fetch(form.action, { @@ -221,6 +245,7 @@ loadTab, reloadCurrentTab, getTab, + postFormAndReload, }; if (document.readyState === "loading") { diff --git a/static/key_monitor_form.js b/static/key_monitor_form.js index 9edcb50..572d94e 100644 --- a/static/key_monitor_form.js +++ b/static/key_monitor_form.js @@ -74,6 +74,20 @@ }); } + function submitKeyForm(keyForm, label) { + if ( + document.body && + document.body.getAttribute("data-embed-shell") === "1" && + global.InstanceEmbed && + typeof global.InstanceEmbed.postFormAndReload === "function" + ) { + global.InstanceEmbed.postFormAndReload(keyForm, label || "提交中…"); + return; + } + if (global.FormSubmitGuard) global.FormSubmitGuard.nativeSubmitOnce(keyForm, label || "提交中…"); + else keyForm.submit(); + } + function bindKeyMonitorForm() { const keyForm = document.getElementById("key-form"); const keyTypeSel = document.querySelector('#key-form [name="type"]'); @@ -103,8 +117,7 @@ } const typeVal = (keyForm.querySelector('[name="type"]') || {}).value || ""; if (typeVal === "假突破") { - if (global.FormSubmitGuard) global.FormSubmitGuard.nativeSubmitOnce(keyForm, "提交中…"); - else keyForm.submit(); + submitKeyForm(keyForm, "提交中…"); return; } if (global.FormSubmitGuard) global.FormSubmitGuard.lock(keyForm, "校验排名中…"); @@ -125,8 +138,7 @@ if (global.FormSubmitGuard) global.FormSubmitGuard.unlock(keyForm); return; } - if (global.FormSubmitGuard) global.FormSubmitGuard.nativeSubmitOnce(keyForm, "提交中…"); - else keyForm.submit(); + submitKeyForm(keyForm, "提交中…"); }) .catch(() => { alert("日成交量排名检查失败,请稍后重试");