187 lines
7.9 KiB
Python
187 lines
7.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""xq0045_xq0046 金色印象通联数据导出 插件 - 配置常量与全局钩子。
|
||
|
||
插件自包含:本包只依赖 Python 标准库 / 第三方库 / 底座注入的 RunContext。
|
||
业务模块统一经 config.log() 输出日志(转发到 ctx.log,无 ctx 时回落到 print)。
|
||
"""
|
||
import os
|
||
|
||
# ==================== 通联平台 ====================
|
||
LOGIN_URL = 'https://cus.allinpay.com/login'
|
||
MAIN_URL = 'https://cus.allinpay.com/main.dsr' # 已登录首页(预热/Referer 来源)
|
||
SEARCH_URL = 'https://cus.allinpay.com/tranx25/search'
|
||
STATUS_URL = 'https://cus.allinpay.com/export/statusdownload'
|
||
DOWNLOAD_INFO_URL = 'https://cus.allinpay.com/export/downloadexport/'
|
||
BILL_URL = 'https://cus.allinpay.com/checkacct25/download'
|
||
TRXCODE_URL = 'https://cus.allinpay.com/unauth/queryInterCode'
|
||
TRXSTATE_URL = 'https://cus.allinpay.com/unauth/queryRecordState'
|
||
|
||
# 服务端有 Referer 外链校验:直接访问会被重定向回 /main.dsr
|
||
SEARCH_HEADERS = {'Referer': MAIN_URL}
|
||
|
||
# ==================== 导出参数 ====================
|
||
EXPORT_CURRENT = 'onebtnexpt' # 页面「一键导出」:账号全量(含所有商户),插件实际使用
|
||
EXPORT_ALL = 'all' # 实测会忽略日期区间、导出当天全量,勿用(仅留作对照)
|
||
|
||
# ==================== 交易查询结果按商户分组拆分 ====================
|
||
# 交易查询 xlsx 下载后自动拆成 3 个工作簿:命中组1 → 综合门店,命中组2 → 品牌管理公司,
|
||
# 其余全部 → 其他账号;匹配规则=商户名称精确匹配(仅统一全角括号与首尾空白)。
|
||
# 每个拆分工作簿保留原文件两个 sheet(sheet1 交易明细 + 汇总),各自按本表商户名称列过滤。
|
||
SPLIT_GROUPS = [
|
||
("综合门店", [
|
||
"南岸区金色印象足浴馆227601",
|
||
"南岸区子石印象足浴馆(个体工商户)329909",
|
||
"九龙坡区金印养生足浴馆600402",
|
||
"金色印象",
|
||
"金色印象2",
|
||
]),
|
||
("品牌管理公司", [
|
||
"重庆金色印象品牌管理有限公司518344",
|
||
]),
|
||
]
|
||
SPLIT_OTHER_NAME = "其他账号" # 未命中任何分组的商户归入此组
|
||
|
||
# 异步导出状态轮询:每 6 秒一次,最多 12 次(服务端超时上限约 10 次/60 秒)
|
||
STATUS_POLL_INTERVAL = 6
|
||
STATUS_POLL_MAX = 12
|
||
|
||
# 登录/验证码 —— 风控敏感:每次提交都计入服务端登录尝试次数,从严控制重试,
|
||
# 避免连续多次提交触发平台「登录失败次数已达上限」锁定(曾实测踩雷,账号被锁 2 小时)
|
||
RETRY_MAX = 1 # 浏览器兜底登录轮数(失败即停,不反复登录)
|
||
CAPTCHA_RETRY = 3 # 浏览器每轮验证码重试次数
|
||
API_CAPTCHA_RETRY = 3 # 接口登录验证码重试次数
|
||
LOGIN_FAIL_BACKOFF = 8 # 登录提交失败后的退避秒数(让风控计数沉降)
|
||
TIMEOUT = 10 # 元素等待超时(秒)
|
||
|
||
# ==================== 登录方式 ====================
|
||
# 平台出现滑块/短信/点选类「安全验证」时,接口登录(requests)与自动浏览器(ddddocr)
|
||
# 都无法通过;且每次提交都计入失败次数,越试越容易锁号。
|
||
# 人工登录:弹出可见浏览器,账号密码/验证码/安全验证全部由人工完成,
|
||
# 插件只负责等待登录完成 → 抓 cookies → 落盘复用,后续导出仍走接口。
|
||
LOGIN_AUTO = '自动' # 接口优先,失败回退浏览器自动登录(默认)
|
||
LOGIN_API = '仅接口登录' # 静默不弹窗,只走接口登录(平台不加验证时最快)
|
||
LOGIN_MANUAL = '人工登录' # 弹浏览器,人工完成验证
|
||
LOGIN_MODES = [LOGIN_AUTO, LOGIN_API, LOGIN_MANUAL]
|
||
|
||
MANUAL_LOGIN_WAIT = 600 # 人工登录最长等待秒数(含账号密码/滑块/短信验证)
|
||
MANUAL_POLL_SECONDS = 2 # 人工登录轮询登录结果间隔(秒)
|
||
MANUAL_TIP_SECONDS = 60 # 人工登录期间提示剩余时间的间隔(秒)
|
||
MANUAL_SETTLE_WAIT = 5 # 登录跳转后等首屏请求结束再抓 cookie(秒)
|
||
|
||
# ==================== 默认保存目录 ====================
|
||
_DESKTOP = os.path.join(os.path.expanduser('~'), 'Desktop')
|
||
|
||
|
||
def get_desktop():
|
||
return _DESKTOP
|
||
|
||
|
||
# ==================== 浏览器用户目录 ====================
|
||
def get_browser_profile_dir():
|
||
"""独立固定浏览器用户目录:登录态跨次保留(免验证码),且不占用用户日常 Chrome 配置。
|
||
|
||
放 %LOCALAPPDATA% 下而非系统临时目录,避免被磁盘清理工具清掉导致登录态丢失。
|
||
"""
|
||
base = os.environ.get('LOCALAPPDATA') or os.path.expanduser('~')
|
||
return os.path.join(base, '37duAutomation', 'browser_sessions', 'xq0045_xq0046')
|
||
|
||
|
||
# ==================== 浏览器窗口(人工登录用:全屏 + 置顶) ====================
|
||
def apply_window_mode(page):
|
||
"""人工登录时把浏览器全屏并置顶:窗口被遮挡/最小化时人工容易漏看验证提示。
|
||
|
||
失败仅告警,不中断流程。
|
||
"""
|
||
try:
|
||
page.set.window.full()
|
||
except Exception as e:
|
||
log(f"[通联登录] 设置窗口全屏失败(忽略):{e}", "WARN")
|
||
if os.name == 'nt':
|
||
try:
|
||
_set_topmost(page.process_id)
|
||
except Exception as e:
|
||
log(f"[通联登录] 设置窗口置顶失败(忽略):{e}", "WARN")
|
||
|
||
|
||
def _set_topmost(pid):
|
||
"""把指定进程的主窗口设为 HWND_TOPMOST(Win32 SetWindowPos,仅 Windows)。"""
|
||
if not pid:
|
||
return
|
||
import ctypes
|
||
from ctypes import wintypes
|
||
|
||
user32 = ctypes.windll.user32
|
||
user32.IsWindowVisible.argtypes = [wintypes.HWND]
|
||
user32.GetWindowTextLengthW.argtypes = [wintypes.HWND]
|
||
user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)]
|
||
|
||
hwnds = []
|
||
|
||
@ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
|
||
def _collect(hwnd, _lparam):
|
||
if user32.IsWindowVisible(hwnd) and user32.GetWindowTextLengthW(hwnd) > 0:
|
||
proc = wintypes.DWORD()
|
||
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(proc))
|
||
if proc.value == pid:
|
||
hwnds.append(hwnd)
|
||
return True
|
||
|
||
user32.EnumWindows.argtypes = [ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM),
|
||
wintypes.LPARAM]
|
||
user32.EnumWindows(_collect, 0)
|
||
|
||
user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, ctypes.c_int,
|
||
ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint]
|
||
for hwnd in hwnds:
|
||
# HWND_TOPMOST = -1,NOSIZE(0x0001) | NOMOVE(0x0002)
|
||
user32.SetWindowPos(hwnd, wintypes.HWND(-1), 0, 0, 0, 0, 0x0001 | 0x0002)
|
||
|
||
|
||
# ==================== 登录会话复用 ====================
|
||
def get_session_file():
|
||
"""已登录会话 cookies 落盘路径:跨 run 复用通联登录,避免每次执行都重新登录。"""
|
||
base = os.environ.get('LOCALAPPDATA') or os.path.expanduser('~')
|
||
d = os.path.join(base, '37duAutomation', 'sessions')
|
||
os.makedirs(d, exist_ok=True)
|
||
return os.path.join(d, 'allinpay_xq0045_xq0046.json')
|
||
|
||
|
||
# ==================== 全局钩子(run() 启动时注入) ====================
|
||
_log = None
|
||
|
||
|
||
def set_log(fn):
|
||
"""注入 ctx.log(RunContext.log),各模块经 log()/logger 输出。"""
|
||
global _log
|
||
_log = fn
|
||
|
||
|
||
def log(msg, level="INFO"):
|
||
if _log:
|
||
_log(str(msg), level)
|
||
else:
|
||
print(f"[{level}] {msg}")
|
||
|
||
|
||
class _ModuleLogger:
|
||
"""模块级 logger 适配:统一转发到 config.log。"""
|
||
|
||
@staticmethod
|
||
def debug(msg, *args):
|
||
log(msg % args if args else msg, "DEBUG")
|
||
|
||
@staticmethod
|
||
def info(msg, *args):
|
||
log(msg % args if args else msg, "INFO")
|
||
|
||
@staticmethod
|
||
def warning(msg, *args):
|
||
log(msg % args if args else msg, "WARN")
|
||
|
||
@staticmethod
|
||
def error(msg, *args):
|
||
log(msg % args if args else msg, "ERROR")
|
||
|
||
|
||
logger = _ModuleLogger()
|