feat(shipping_notice): 支持无 PO 表头的邮件分配表解析并锁定 STP 进行中的草稿
邮件分配表在无 PO 表头时,若已映射料号或预约号仍解析行以供 merge mpn_fallback,二者皆无才整表跳过;STP 等待入队/排队/执行 期间拒绝草稿内容写入并从可用动作中剥离 edit_draft/validate, 会话摘要与工作台同源认活跃 STP Job 以避免误判为待人工。
This commit is contained in:
1 parent
f77f267091
commit
2841f4a598
18 files changed
+1050
-73
No files matched your search
@@ -1,9 +1,11 @@
|
||||
"""
|
||||
scripts/rpa/dsv_auth.py
|
||||
scripts/rpa/connectors/dsv/dsv_auth.py
|
||||
|
||||
用途:DSV 360 PingID 分步登录(邮箱 → 密码方式 → 密码 → portal)。
|
||||
用途:DSV 360 PingID 分步登录(邮箱 → 可选切简体 → 密码方式 → 密码 → portal)。
|
||||
依赖:rpa_common、dsv_debug;登录主编排见 dsv_auth_login_flow。
|
||||
逻辑:由 dsv_session / dsv_report_export 在 ensure session 时调用 maybe_auto_login_dsv。
|
||||
逻辑:由 dsv_session / dsv_report_export 在 ensure session 时调用 maybe_auto_login_dsv;
|
||||
方法页点击须兼容英文 “Choose a sign in method” 标题+副标题行(2026-09-16);
|
||||
英文页可经 #kc_locale 尝试切「中文简体」(失败不阻断)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -39,7 +41,175 @@ from dsv_debug import (
|
||||
)
|
||||
|
||||
|
||||
def _is_dsv_password_method_label(text: str) -> bool:
|
||||
"""
|
||||
判断 MFA「选择登录方式」行文案是否为密码登录选项。
|
||||
|
||||
核心逻辑:兼容旧版精确标题(密码/Password)与新版英文行
|
||||
「Password + Enter your password to sign in.」整行副标题;排除 Passkey /
|
||||
Mobile App / E-Mail Code / 忘记密码等干扰行。
|
||||
|
||||
Args:
|
||||
text: 方法行可见文案(可含换行,内部会归一化空白)。
|
||||
|
||||
Returns:
|
||||
True 表示应点击该行进入密码输入页。
|
||||
"""
|
||||
normalized = " ".join(str(text or "").split()).strip()
|
||||
if not normalized:
|
||||
return False
|
||||
lowered = normalized.lower()
|
||||
exclude_needles = (
|
||||
"忘记密码",
|
||||
"forgot password",
|
||||
"通行密钥",
|
||||
"passkey",
|
||||
"电子邮件验证码",
|
||||
"e-mail code",
|
||||
"email code",
|
||||
"移动应用程序",
|
||||
"mobile app",
|
||||
"pingid",
|
||||
"取消",
|
||||
"cancel",
|
||||
"返回",
|
||||
)
|
||||
if any(needle in normalized or needle in lowered for needle in exclude_needles):
|
||||
return False
|
||||
if normalized in {"密码", "Password"}:
|
||||
return True
|
||||
if normalized.startswith("密码") or normalized.startswith("Password"):
|
||||
return True
|
||||
if "输入您的密码并登录" in normalized:
|
||||
return True
|
||||
if "enter your password to sign in" in lowered:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _dsv_switch_zh_cn_enabled() -> bool:
|
||||
"""是否启用「英文登录页尝试切中文简体」加固;默认开启,可用环境变量关闭。"""
|
||||
raw = str(_env("SHIPPING_NOTICE_RPA_WORKER_DSV_SWITCH_ZH_CN", "true") or "true").strip().lower()
|
||||
return raw not in {"0", "false", "no", "off"}
|
||||
|
||||
|
||||
def _read_dsv_locale_nav_label(page: Any) -> str:
|
||||
"""
|
||||
读取 SIMS 语言按钮 `#lanNavSel` 当前文案。
|
||||
|
||||
Args:
|
||||
page: Playwright Page(可含 iframe)。
|
||||
|
||||
Returns:
|
||||
归一化后的语言标签;找不到时返回空串。
|
||||
"""
|
||||
roots = [page, *getattr(page, "frames", [])]
|
||||
for root in roots:
|
||||
try:
|
||||
locator = root.locator("#lanNavSel").first
|
||||
locator.wait_for(state="visible", timeout=500)
|
||||
return " ".join(str(locator.inner_text(timeout=500) or "").split()).strip()
|
||||
except Exception:
|
||||
continue
|
||||
return ""
|
||||
|
||||
|
||||
def _maybe_switch_dsv_locale_to_zh_cn(page: Any) -> bool:
|
||||
"""
|
||||
若登录页语言为 English,尝试切换为「中文简体」。
|
||||
|
||||
核心逻辑:读 `#lanNavSel`;已是中文则跳过;否则点 `#kc_locale` 打开下拉,
|
||||
再点 `a.language.dropdown-item` 文案「中文简体」。整段失败只打日志,不抛异常。
|
||||
|
||||
Args:
|
||||
page: Playwright Page(可含 iframe)。
|
||||
|
||||
Returns:
|
||||
True 表示已是中文简体或切换成功;False 表示关闭开关/未检测到/切换失败。
|
||||
"""
|
||||
if not _dsv_switch_zh_cn_enabled():
|
||||
return False
|
||||
label = _read_dsv_locale_nav_label(page)
|
||||
if not label:
|
||||
return False
|
||||
if "中文简体" in label or label in {"中文", "简体中文"}:
|
||||
return True
|
||||
if "english" not in label.lower() and "英文" not in label:
|
||||
# 其他语言也尽量切到简体,避免后续文案漂移
|
||||
print(f"[dsv-login] locale is {label!r}; trying 中文简体")
|
||||
else:
|
||||
print(f"[dsv-login] English locale detected ({label!r}); switching to 中文简体")
|
||||
|
||||
roots = [page, *getattr(page, "frames", [])]
|
||||
try:
|
||||
# [STEP 1] 打开语言下拉
|
||||
opened = False
|
||||
for root in roots:
|
||||
try:
|
||||
toggle = root.locator("#kc_locale").first
|
||||
toggle.wait_for(state="visible", timeout=1_000)
|
||||
toggle.click(timeout=2_000, force=True)
|
||||
opened = True
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if not opened:
|
||||
print("[dsv-login] locale switch skipped: #kc_locale not clickable")
|
||||
return False
|
||||
|
||||
# [STEP 2] 点击「中文简体」
|
||||
clicked = False
|
||||
zh_selectors = (
|
||||
'a.language.dropdown-item:has-text("中文简体")',
|
||||
'a.dropdown-item:has-text("中文简体")',
|
||||
'text="中文简体"',
|
||||
)
|
||||
for root in roots:
|
||||
for selector in zh_selectors:
|
||||
try:
|
||||
item = root.locator(selector).first
|
||||
item.wait_for(state="visible", timeout=2_000)
|
||||
item.click(timeout=2_000, force=True)
|
||||
clicked = True
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if clicked:
|
||||
break
|
||||
if not clicked:
|
||||
print("[dsv-login] locale switch skipped: 中文简体 item not found")
|
||||
return False
|
||||
|
||||
# [STEP 3] 确认标签变为中文简体(页面可能短暂重绘)
|
||||
deadline = time.monotonic() + 8
|
||||
while time.monotonic() < deadline:
|
||||
current = _read_dsv_locale_nav_label(page)
|
||||
if "中文简体" in current or current in {"中文", "简体中文"}:
|
||||
print("[dsv-login] locale switched to 中文简体")
|
||||
return True
|
||||
time.sleep(0.3)
|
||||
print(f"[dsv-login] locale switch unverified; label={_read_dsv_locale_nav_label(page)!r}")
|
||||
return False
|
||||
except Exception as exc:
|
||||
print(f"[dsv-login] locale switch failed (non-blocking): {exc}")
|
||||
return False
|
||||
|
||||
|
||||
def _click_dsv_password_method(page: Any) -> bool:
|
||||
"""
|
||||
在 PingID「选择登录方式」页点击「密码」选项并确认进入密码框。
|
||||
|
||||
核心逻辑:先用 DOM 脚本按方法行点击(兼容 mfaForm / device-list / 泛化 listitem);
|
||||
失败再按 Playwright 行选择器与精确文案 `Password`/`密码` 点击;任一路经须
|
||||
`transition_confirmed`(密码框出现或离开方法页)才算成功。
|
||||
|
||||
Args:
|
||||
page: Playwright Page(可含 iframe)。
|
||||
|
||||
Returns:
|
||||
是否已确认进入密码阶段。
|
||||
"""
|
||||
|
||||
def password_ready() -> bool:
|
||||
"""密码输入框已出现即视为方法页切换成功;兼容轻量探测与通用等待。"""
|
||||
if _find_dsv_password_locator(page, timeout_ms=250) is not None:
|
||||
@@ -66,6 +236,8 @@ def _click_dsv_password_method(page: Any) -> bool:
|
||||
("输入您的密码" in body_text or "忘记密码" in body_text or "password" in lowered)
|
||||
and "通行密钥" not in body_text
|
||||
and "passkey" not in lowered
|
||||
and "choose a sign in method" not in lowered
|
||||
and "choose a sign-on method" not in lowered
|
||||
and "验证失败" not in body_text
|
||||
and "validation failed" not in lowered
|
||||
)
|
||||
@@ -76,6 +248,31 @@ def _click_dsv_password_method(page: Any) -> bool:
|
||||
time.sleep(0.3)
|
||||
return False
|
||||
|
||||
def try_click_locator(locator: Any) -> bool:
|
||||
try:
|
||||
locator.scroll_into_view_if_needed(timeout=3_000)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
locator.click(timeout=3_000, force=True)
|
||||
except Exception:
|
||||
try:
|
||||
locator.evaluate(
|
||||
"""
|
||||
(node) => {
|
||||
if (typeof node.click === 'function') node.click();
|
||||
for (const type of ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click']) {
|
||||
node.dispatchEvent(new MouseEvent(type, { bubbles: true, cancelable: true, view: window }));
|
||||
}
|
||||
}
|
||||
""",
|
||||
timeout=2_000,
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
return transition_confirmed()
|
||||
|
||||
# [STEP 1] DOM 脚本:放宽标题匹配 + 扩大方法行选择器(旧 mfaForm 与新英文 MFA 页)
|
||||
script_row_click = """
|
||||
() => {
|
||||
const textOf = (node) => ((node && (node.innerText || node.textContent || node.value)) || '').replace(/\\s+/g, ' ').trim();
|
||||
@@ -84,20 +281,27 @@ def _click_dsv_password_method(page: Any) -> bool:
|
||||
const style = window.getComputedStyle(node);
|
||||
return !(style.display === 'none' || style.visibility === 'hidden' || rect.width <= 0 || rect.height <= 0);
|
||||
};
|
||||
const isPasswordMethod = (text) => (
|
||||
(text === '密码' || text === 'Password' || text === '输入您的密码并登录。' || text.includes('输入您的密码并登录'))
|
||||
&& !text.includes('忘记密码')
|
||||
&& !text.includes('取消')
|
||||
&& !text.includes('返回')
|
||||
&& !text.includes('通行密钥')
|
||||
&& !text.includes('电子邮件验证码')
|
||||
&& !text.includes('移动应用程序')
|
||||
&& !text.toLowerCase().includes('cancel')
|
||||
&& !text.toLowerCase().includes('back')
|
||||
&& !text.toLowerCase().includes('passkey')
|
||||
);
|
||||
const rows = Array.from(document.querySelectorAll("form[name='mfaForm'] li.mb-2, form[name='mfaForm'] .mb-2, .device-list li.mb-2, .device-list .mb-2"))
|
||||
.filter((node) => isVisible(node));
|
||||
const isPasswordMethod = (text) => {
|
||||
const lowered = (text || '').toLowerCase();
|
||||
const excluded = (
|
||||
text.includes('忘记密码') || lowered.includes('forgot password')
|
||||
|| text.includes('通行密钥') || lowered.includes('passkey')
|
||||
|| text.includes('电子邮件验证码') || lowered.includes('e-mail code') || lowered.includes('email code')
|
||||
|| text.includes('移动应用程序') || lowered.includes('mobile app') || lowered.includes('pingid')
|
||||
|| text.includes('取消') || lowered.includes('cancel')
|
||||
|| text.includes('返回')
|
||||
);
|
||||
if (excluded) return false;
|
||||
return (
|
||||
text === '密码' || text === 'Password'
|
||||
|| text.startsWith('密码') || text.startsWith('Password')
|
||||
|| text.includes('输入您的密码并登录')
|
||||
|| lowered.includes('enter your password to sign in')
|
||||
);
|
||||
};
|
||||
const rows = Array.from(document.querySelectorAll(
|
||||
"form[name='mfaForm'] li.mb-2, form[name='mfaForm'] .mb-2, .device-list li.mb-2, .device-list .mb-2, li.mb-2, [role='listitem'], .device-type, button, a, [role='button']"
|
||||
)).filter((node) => isVisible(node));
|
||||
const row = rows.find((node) => isPasswordMethod(textOf(node)));
|
||||
if (!row) return false;
|
||||
const target = row.querySelector('.device-continue, button, a, [role="button"], input[type="submit"]') || row;
|
||||
@@ -122,31 +326,53 @@ def _click_dsv_password_method(page: Any) -> bool:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# [STEP 2] Playwright:按方法行选择器 + 精确文案 Password/密码 点击
|
||||
row_selectors = (
|
||||
"form[name='mfaForm'] li.mb-2",
|
||||
"form[name='mfaForm'] .mb-2",
|
||||
".device-list li.mb-2",
|
||||
".device-list .mb-2",
|
||||
"li.mb-2",
|
||||
"[role='listitem']",
|
||||
)
|
||||
text_selectors = (
|
||||
'text="Password"',
|
||||
"text=Password",
|
||||
'text="密码"',
|
||||
"text=密码",
|
||||
'button:has-text("Password")',
|
||||
'button:has-text("密码")',
|
||||
'[role="button"]:has-text("Password")',
|
||||
'[role="button"]:has-text("密码")',
|
||||
'a:has-text("Password")',
|
||||
'a:has-text("密码")',
|
||||
)
|
||||
deadline = time.monotonic() + 10
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
rows = page.locator("form[name='mfaForm'] li.mb-2").all()
|
||||
for row in rows:
|
||||
text = row.inner_text(timeout=1_000)
|
||||
lowered = text.lower()
|
||||
if ("密码" in text or "Password" in text) and "忘记密码" not in text and "取消" not in text and "返回" not in text and "cancel" not in lowered and "back" not in lowered:
|
||||
row.scroll_into_view_if_needed(timeout=3_000)
|
||||
roots = [page, *getattr(page, "frames", [])]
|
||||
for root in roots:
|
||||
for selector in row_selectors:
|
||||
try:
|
||||
rows = root.locator(selector).all()
|
||||
except Exception:
|
||||
rows = []
|
||||
for row in rows:
|
||||
try:
|
||||
row.click(timeout=3_000, force=True)
|
||||
text = row.inner_text(timeout=1_000)
|
||||
except Exception:
|
||||
row.evaluate(
|
||||
"""
|
||||
(node) => {
|
||||
if (typeof node.click === 'function') node.click();
|
||||
node.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
|
||||
}
|
||||
""",
|
||||
timeout=2_000,
|
||||
)
|
||||
if transition_confirmed():
|
||||
continue
|
||||
if not _is_dsv_password_method_label(text):
|
||||
continue
|
||||
if try_click_locator(row):
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
for selector in text_selectors:
|
||||
try:
|
||||
locator = root.locator(selector).first
|
||||
locator.wait_for(state="visible", timeout=500)
|
||||
except Exception:
|
||||
continue
|
||||
if try_click_locator(locator):
|
||||
return True
|
||||
time.sleep(0.5)
|
||||
return False
|
||||
|
||||
@@ -569,7 +795,15 @@ def _looks_like_dsv_login_method_page(page: Any) -> bool:
|
||||
# 宽泛 needle 造成误判(反复尝试点击“密码方式”而非关闭弹窗);命中升级提示时直接判非方法页。
|
||||
if _has_dsv_password_upgrade_prompt(page):
|
||||
return False
|
||||
needles = ("选择登录方法", "密码", "Password", "输入您的密码并登录", "Choose a sign-on method")
|
||||
# 含英文新版 “Choose a sign in method”(无连字符);旧版为 sign-on。
|
||||
needles = (
|
||||
"选择登录方法",
|
||||
"密码",
|
||||
"Password",
|
||||
"输入您的密码并登录",
|
||||
"Choose a sign-on method",
|
||||
"Choose a sign in method",
|
||||
)
|
||||
for frame in page.frames:
|
||||
try:
|
||||
text = frame.locator("body").inner_text(timeout=300)
|
||||
@@ -578,7 +812,7 @@ def _looks_like_dsv_login_method_page(page: Any) -> bool:
|
||||
lowered = text.lower()
|
||||
if "real person" in lowered and ("rabbit" in lowered or "scale" in lowered):
|
||||
continue
|
||||
if any(needle in text for needle in needles):
|
||||
if any(needle in text or needle.lower() in lowered for needle in needles):
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -1002,8 +1236,11 @@ has_dsv_password_upgrade_prompt = _has_dsv_password_upgrade_prompt
|
||||
is_dsv_auth_ping_url = _is_dsv_auth_ping_url
|
||||
is_dsv_blank_auth_transition = _is_dsv_blank_auth_transition
|
||||
is_dsv_oauth_handshake_url = _is_dsv_oauth_handshake_url
|
||||
is_dsv_password_method_label = _is_dsv_password_method_label
|
||||
is_dsv_username_page = _is_dsv_username_page
|
||||
looks_like_dsv_login_method_page = _looks_like_dsv_login_method_page
|
||||
maybe_switch_dsv_locale_to_zh_cn = _maybe_switch_dsv_locale_to_zh_cn
|
||||
read_dsv_locale_nav_label = _read_dsv_locale_nav_label
|
||||
submit_dsv_password_form = _submit_dsv_password_form
|
||||
submit_dsv_username_form = _submit_dsv_username_form
|
||||
try_fill_dsv_username_locator = _try_fill_dsv_username_locator
|
||||
|
||||
@@ -3,7 +3,8 @@ scripts/rpa/dsv_auth_login_flow.py
|
||||
|
||||
用途:DSV 360 PingID 自动登录主编排(maybe_auto_login_dsv)。
|
||||
依赖:rpa_common、dsv_debug、dsv_auth(表单/探测辅助)、dsv_session(登录后首页 ready)。
|
||||
逻辑:由 dsv_auth 在模块末尾 re-export;dsv_report_export ensure 时触发。
|
||||
逻辑:由 dsv_auth 在模块末尾 re-export;dsv_report_export ensure 时触发;
|
||||
账号页/方法页若为英文,先尝试切「中文简体」再继续(失败不阻断)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -58,6 +59,7 @@ _is_dsv_blank_auth_transition = dsv_auth._is_dsv_blank_auth_transition
|
||||
_is_dsv_oauth_handshake_url = dsv_auth._is_dsv_oauth_handshake_url
|
||||
_is_dsv_password_upgrade_redirect = dsv_auth._is_dsv_password_upgrade_redirect
|
||||
_is_dsv_username_page = dsv_auth._is_dsv_username_page
|
||||
_maybe_switch_dsv_locale_to_zh_cn = dsv_auth._maybe_switch_dsv_locale_to_zh_cn
|
||||
_submit_dsv_password_form = dsv_auth._submit_dsv_password_form
|
||||
_wait_for_dsv_auth_page_ready = dsv_auth._wait_for_dsv_auth_page_ready
|
||||
_wait_for_dsv_human_check_if_present = dsv_auth._wait_for_dsv_human_check_if_present
|
||||
@@ -174,6 +176,8 @@ def _maybe_auto_login_dsv(page: Any) -> None:
|
||||
return
|
||||
|
||||
_wait_for_dsv_auth_page_ready(page)
|
||||
# [STEP 1.5] 英文 SIMS 页先尝试切「中文简体」,失败不阻断;须在填账号前执行以免切语言清空表单。
|
||||
_maybe_switch_dsv_locale_to_zh_cn(page)
|
||||
login_ready_ms = _dsv_login_ready_timeout_ms()
|
||||
print("[dsv-login] waiting username field")
|
||||
username_locator = _wait_for_visible_selector(
|
||||
@@ -258,6 +262,7 @@ def _maybe_auto_login_dsv(page: Any) -> None:
|
||||
)
|
||||
password_method_clicked_at = 0.0
|
||||
username_retry_allowed_at = time.monotonic() + 6
|
||||
locale_switch_on_method_attempted = False
|
||||
while time.monotonic() < login_deadline:
|
||||
# [STEP 1] DSV 在密码阶段前(authorization.ping)也会弹出“使用密码升级登录!”
|
||||
# 引导弹窗(也许以后/立即启用),会遮挡密码表单和登录按钮;先点“也许以后”关闭再探测。
|
||||
@@ -309,6 +314,10 @@ def _maybe_auto_login_dsv(page: Any) -> None:
|
||||
_collect_dsv_reports_probe(page),
|
||||
)
|
||||
# #endregion
|
||||
# 方法页若仍为英文,再尝试一次切简体(账号页未带上语言偏好时)。
|
||||
if not locale_switch_on_method_attempted:
|
||||
_maybe_switch_dsv_locale_to_zh_cn(page)
|
||||
locale_switch_on_method_attempted = True
|
||||
if password_method_clicked_at and time.monotonic() - password_method_clicked_at < 2:
|
||||
time.sleep(1)
|
||||
continue
|
||||
|
||||
@@ -494,9 +494,11 @@ from dsv_auth import ( # noqa: E402
|
||||
find_dsv_password_locator,
|
||||
has_dsv_human_check_prompt,
|
||||
has_dsv_password_upgrade_prompt,
|
||||
is_dsv_password_method_label,
|
||||
is_dsv_username_page,
|
||||
looks_like_dsv_login_method_page,
|
||||
maybe_auto_login_dsv,
|
||||
maybe_switch_dsv_locale_to_zh_cn,
|
||||
submit_dsv_password_form,
|
||||
wait_for_dsv_human_check_if_present,
|
||||
wait_for_dsv_login_method_page,
|
||||
|
||||
@@ -96,6 +96,8 @@ _maybe_auto_login_dsv = dsv_session.maybe_auto_login_dsv
|
||||
_fill_dsv_username = dsv_session.fill_dsv_username
|
||||
_click_dsv_continue = dsv_session.click_dsv_continue
|
||||
_click_dsv_password_method = dsv_session.click_dsv_password_method
|
||||
_is_dsv_password_method_label = dsv_session.is_dsv_password_method_label
|
||||
_maybe_switch_dsv_locale_to_zh_cn = dsv_session.maybe_switch_dsv_locale_to_zh_cn
|
||||
_fill_dsv_password_page_fields = dsv_session.fill_dsv_password_page_fields
|
||||
_wait_for_dsv_human_check_if_present = dsv_session.wait_for_dsv_human_check_if_present
|
||||
_wait_for_dsv_app_after_login = dsv_session.wait_for_dsv_app_after_login
|
||||
|
||||
Reference in new issue
Block a user