perf(shipping-notice): 优化发货任务性能,修复上传流程与终态问题

This commit is contained in:
hugo committed 2026-08-27 19:25:16 +08:00
1 parent 434c04ed2d
commit bcdd8285ae
29 files changed
+1074 -128

No files matched your search

+130 -2
View File
@@ -1748,6 +1748,28 @@ class CraneUploadAdapter(_BaseUploadAdapter):
启用前(enabled=true)管理端必须已确认提交按钮与成功判定字段;否则 fail-closed。
"""
# SweetAlert2 上传失败弹窗识别(受控 Adapter 内置,禁止开放到 upload_config):Crane 上传
# 失败会弹 swal2-icon-error(如 “The Customer account [...] was not found” / “Warehouse [...] not found”),
# 必须读取原因并点击 OK 关闭,否则页面被模态框拦截,无法继续上传下一个文件。
_ERROR_DIALOG_SELECTORS = (
'.swal2-popup.swal2-icon-error',
'.swal2-icon.swal2-error',
'.swal2-x-mark',
)
_ERROR_MESSAGE_SELECTORS = (
'.swal2-html-container',
'.swal2-content',
'.swal2-title',
)
_ERROR_OK_SELECTOR = '.swal2-confirm'
# SweetAlert2 上传成功弹窗识别(绿色勾):Crane 上传成功同样以 swal2 弹窗反馈。
# 即便 upload_config.result_selector 配置错误,也能据此确认成功,避免误报 unknown。
_SUCCESS_DIALOG_SELECTORS = (
'.swal2-popup.swal2-icon-success',
'.swal2-icon.swal2-success',
)
def run(self, page: Any, *, login_url: str) -> dict[str, Any]:
self._require('entry_url', 'file_input_selector', 'submit_mode')
if str(self.config.get('submit_mode') or '').strip() == 'click_submit':
@@ -1772,17 +1794,115 @@ class CraneUploadAdapter(_BaseUploadAdapter):
else:
raise RuntimeError('Crane upload requires a supported result_mode')
def _take_error_popup_reason(self, page: Any) -> str | None:
"""检测 Crane 上传失败的 SweetAlert2 错误弹窗。
命中时读取失败原因(swal2-html-container,回退 swal2-content / swal2-title)并点击 OK 关闭,
关闭后返回原因文本;未出现错误弹窗时返回 None。点击 OK 是继续上传下一个文件的前提。
"""
try:
popup = None
for selector in self._ERROR_DIALOG_SELECTORS:
loc = page.locator(selector).first
if loc.count() > 0:
try:
if loc.is_visible():
popup = loc
break
except Exception:
continue
if popup is None:
return None
reason = ''
# 优先逐条取 html-container 内的错误行(.my-1),多错误用分号连接;
# 无则回退到容器整体文本,再回退标题。
try:
items = [
str(text or '').strip()
for text in page.locator('.swal2-html-container .my-1').all_text_contents()
if str(text or '').strip()
]
if items:
reason = '; '.join(items)
except Exception:
items = []
if not reason:
for selector in self._ERROR_MESSAGE_SELECTORS:
try:
text = str(page.locator(selector).first.text_content(timeout=1000) or '').strip()
if text:
reason = text
break
except Exception:
continue
ok = page.locator(self._ERROR_OK_SELECTOR).first
try:
if ok.count() > 0:
ok.click(timeout=2000)
except Exception:
pass
return reason or 'Crane upload rejected'
except Exception:
return None
def _take_success_popup(self, page: Any) -> str | None:
"""检测 Crane 上传成功的 SweetAlert2 成功弹窗(绿色勾),返回命中的选择器;未出现返回 None。"""
try:
for selector in self._SUCCESS_DIALOG_SELECTORS:
loc = page.locator(selector).first
if loc.count() > 0:
try:
if loc.is_visible():
return selector
except Exception:
continue
return None
except Exception:
return None
def _raise_if_crane_error(self, page: Any) -> None:
"""若出现 Crane 上传失败弹窗:读取原因、点击 OK 关闭后抛出失败,交由调用方上报真实原因。"""
reason = self._take_error_popup_reason(page)
if reason:
raise RuntimeError(f"Crane upload rejected: {reason}")
def _success_from_popup(self, page: Any, filename: str) -> dict[str, Any] | None:
"""返回成功弹窗的 success_evidence;未命中成功弹窗时返回 None。"""
selector = self._take_success_popup(page)
if selector:
return {
'reference': '',
'success_evidence': _capture_success_page_evidence(
page, kind='crane_page_notice', selector=selector,
filename=filename, reference=None,
),
}
return None
def _resolve_result(self, page: Any) -> dict[str, Any]:
result_mode = str(self.config.get('result_mode') or '').strip()
filename = self.file_path.name
# 提交后先探测一次失败/成功弹窗;判定期内仍会持续探测(结果选择器可能晚于弹窗出现)。
self._raise_if_crane_error(page)
success = self._success_from_popup(page, filename)
if success:
return success
if result_mode == 'history_record':
result_selector = str(self.config.get('result_selector') or '').strip()
try:
loc = page.locator(result_selector).first
loc.wait_for(state='visible', timeout=self._timeout_ms())
except Exception as exc:
# 判定期内可能出现失败弹窗(如 Customer/Warehouse 未找到);先读取并关闭,
# 以真实原因上报失败;否则仍按“结果未知”转人工。
self._raise_if_crane_error(page)
success = self._success_from_popup(page, filename)
if success:
return success
raise _UploadResultUnknownError(
f"Crane upload history not confirmed for {filename!r}: {exc}"
f"无法判断上传是否成功/失败:{filename!r} 历史结果未确认({exc});"
f"可能已上传成功但页面未出现成功提示,或 upload_config 的 result_selector/success_texts 配置有误;"
f"请人工在 CRANE 核查,或用「确认已上传」登记,或修正配置后重试。"
) from exc
try:
reference = str(loc.text_content(timeout=self._timeout_ms()) or '').strip()
@@ -1801,6 +1921,12 @@ class CraneUploadAdapter(_BaseUploadAdapter):
deadline = time.monotonic() + (self._timeout_ms() / 1000)
last_snippet = ''
while time.monotonic() < deadline:
# 上传失败时 Crane 会先弹错误窗而非成功提示;命中则读取原因、点 OK 关闭并上报失败。
self._raise_if_crane_error(page)
# 成功也可能以 SweetAlert2 成功弹窗反馈;优先于配置 result_selector 判定,避免配置错误误报 unknown。
success = self._success_from_popup(page, filename)
if success:
return success
try:
loc = page.locator(result_selector).first
if loc.count() > 0 and loc.is_visible():
@@ -1818,7 +1944,9 @@ class CraneUploadAdapter(_BaseUploadAdapter):
pass
time.sleep(1)
raise _UploadResultUnknownError(
f"Crane upload page notice not confirmed for {filename!r}; last_snippet={last_snippet!r}"
f"无法判断上传是否成功/失败:{filename!r} 结果未确认(last_snippet={last_snippet!r});"
f"可能已上传成功但页面未出现成功提示,或 upload_config 的 result_selector/success_texts 配置有误;"
f"请人工在 CRANE 核查,或用「确认已上传」登记,或修正配置后重试。"
)