Files
37duAutomation/app/service/hrbp/oa/common/sso.py
T

65 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
@Module: hrbp.oa.sso
@FileName: sso.py
@Author: 小布丁
@CreateTime: 2026-08-05 23:50
@Description: SSO 链接解析模块:浏览器访问 SSO 链接,从重定向后的详情页 URL 提取 taskId/fkId 和 businessKey
@Version: 1.0.1
"""
from urllib.parse import urlparse, parse_qs
from typing import Dict
from app.utils.logger import get_logger
logger = get_logger(__name__)
try:
from DrissionPage import ChromiumPage
except ImportError:
ChromiumPage = None
def resolve_sso_params(
browser: ChromiumPage,
sso_url: str,
wait_timeout: float = 20,
) -> Dict[str, str]:
"""
用浏览器打开 SSO 链接,等待 JS 重定向到详情页,
从最终 URL 的 query 参数中提取 taskId(部分页面命名为 fkId)和 businessKey。
Args:
browser: 已登录的 ChromiumPage 对象(SSO 的 JS 跳转必须用真浏览器)
sso_url: SSO 跳转链接(完整 URL)
wait_timeout: 等待 JS 跳转完成的超时时间(秒,默认 20)
Returns:
包含 taskId 和 businessKey 的字典
Raises:
RuntimeError: 跳转失败或 URL 中缺少 taskId/businessKey
"""
logger.info("解析 SSO 重定向: %s", sso_url)
# 浏览器打开 SSO 链接,页面 JS 自动重定向到详情页。
# 跳转链: sso.mob -> unifyWorkFlow.jsp(中间页,一闪而过) -> goToWorkFlow 详情页。
# 因此不能等中间页(会匹配不到而超时卡 wait_timeout 秒),改为等待地址栏离开 SSO 链接。
browser.get(sso_url)
browser.wait.url_change(sso_url, exclude=True, timeout=wait_timeout, raise_err=False)
logger.info("重定向最终 URL: %s", browser.url)
# 从详情页 URL 提取 taskId(fkId 是同一字段的别名)和 businessKey
params = parse_qs(urlparse(browser.url).query)
task_id = params.get("taskId", [None])[0] or params.get("fkId", [None])[0]
business_key = params.get("businessKey", [None])[0]
if not task_id or not business_key:
raise RuntimeError(
f"无法从重定向 URL 提取 taskId 或 businessKey: {browser.url}"
)
logger.info("SSO 解析成功: taskId=%s, businessKey=%s", task_id, business_key)
return {"taskId": task_id, "businessKey": business_key}