feat: 新增多插件 UI 支持及运行日志集中回传功能
本次提交包含多项功能更新: 1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持 2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储 3. 新增插件分类字段支持,用于场景库分组管理 4. 优化 xq0034/0037/0048 等插件的功能及描述 5. 修复控制面响应兼容问题,支持新旧版本数据格式 6. 新增 xq0033 插件的状态存储模块,用于历史记录管理 7. 优化 CRM 登录窗口模式,支持全屏置顶
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""xq0048 CRM 数据导出 插件 - 配置常量与全局钩子。
|
||||
|
||||
插件自包含:本包只依赖 Python 标准库 / 第三方库 / 底座注入的 RunContext。
|
||||
业务模块统一经 config.log() 输出日志(转发到 ctx.log,无 ctx 时回落到 print)。
|
||||
"""
|
||||
import os
|
||||
|
||||
# ==================== 站点与路由 ====================
|
||||
SITE_URL = 'https://crm.37du.co/'
|
||||
LOGIN_URL = 'https://crm.37du.co/#/login'
|
||||
# 会话校验接口:cookies 有效返回 {"code":"S0","success":true,...}
|
||||
VALIDATE_URL = 'https://crm.37du.co/membership/main/user'
|
||||
BASE = 'https://crm.37du.co/membership/'
|
||||
BALANCE_URL = 'https://crm.37du.co/#/reort/balance' # 报表统计/储值卡余额(reort 为站点实际拼写)
|
||||
LOGIN_KEYWORD = 'login' # hash 片段含 login 即登录页
|
||||
PAGE_TITLE_KEYWORD = '储值卡余额' # 截图页就绪判定
|
||||
CRM_DOMAIN = 'crm.37du.co'
|
||||
|
||||
# ==================== 输出路径 ====================
|
||||
DEFAULT_SAVE_DIR = r'D:\资料\财务\CRM数据导出'
|
||||
|
||||
|
||||
def _app_data_dir():
|
||||
return os.path.join(
|
||||
os.environ.get('LOCALAPPDATA') or os.environ.get('APPDATA') or os.path.expanduser('~'),
|
||||
'37duAutomation')
|
||||
|
||||
|
||||
SESSIONS_DIR = os.path.join(_app_data_dir(), '.sessions')
|
||||
COOKIE_FILE = os.path.join(SESSIONS_DIR, 'crm_cookies.json') # 与影刀版同名,可互相复用
|
||||
COOKIE_HEADER_FILE = os.path.join(SESSIONS_DIR, 'crm_cookie_header.txt')
|
||||
TOKEN_FILE = os.path.join(SESSIONS_DIR, 'crm_tokens.json')
|
||||
|
||||
|
||||
def default_output_dir():
|
||||
"""默认输出目录(与影刀版一致)。"""
|
||||
return DEFAULT_SAVE_DIR
|
||||
|
||||
|
||||
def get_browser_profile_dir():
|
||||
"""独立固定浏览器用户目录:登录态跨次保留,不占用日常 Chrome 配置。
|
||||
|
||||
与影刀版(browser_sessions/crm)分开,避免两个宿主同时运行时抢同一 profile。"""
|
||||
return os.path.join(_app_data_dir(), 'browser_sessions', 'xq0048')
|
||||
|
||||
|
||||
# ==================== 浏览器窗口(全屏 + 置顶) ====================
|
||||
def apply_window_mode(page):
|
||||
"""把浏览器窗口设为全屏并置顶。
|
||||
|
||||
窗口被其它窗口遮挡或最小化时,渲染进程可能一直不出帧,Page.captureScreenshot
|
||||
会等到超时(DrissionPage 默认 30s)。全屏 + 置顶用于规避这种遮挡型超时;
|
||||
失败仅告警,不影响后续流程。"""
|
||||
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:
|
||||
user32.SetWindowPos(hwnd, wintypes.HWND(-1), 0, 0, 0, 0, 0x0001 | 0x0002) # NOSIZE | NOMOVE
|
||||
|
||||
|
||||
# ==================== 运行参数(项目规范:全局重试 3 次) ====================
|
||||
RETRY_MAX = 3 # 截图/单报表重试
|
||||
PAGE_SIZE = 13 # 与页面一致;实测服务端导出不受分页影响
|
||||
TIMEOUT = 180 # 大报表(数万行)下载超时
|
||||
LOAD_TIMEOUT = 30 # 等页面标题就绪的最长秒数
|
||||
PAGE_SETTLE = 5 # 数据渲染稳定等待(秒)
|
||||
LOGIN_SETTLE = 8 # 登录成功后等首屏请求结束再抓 cookie(过早快照会被轮换作废)
|
||||
LOGIN_WAIT = 300 # 等待扫码超时(秒),人工操作给足窗口
|
||||
PROFILE_REUSE_WAIT = 10 # 浏览器 profile 残留会话 / 静默重登的观察窗口
|
||||
SCAN_WAIT = 300 # 登录模块等待扫码超时(秒)
|
||||
HEADLESS = False # 扫码兜底需可见浏览器
|
||||
|
||||
UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
||||
"Chrome/120.0.0.0 Safari/537.36")
|
||||
|
||||
# ==================== 报表清单(名称, 导出接口, 日期规则, 附加参数, 日期参数字段) ====================
|
||||
# 规则:prev=前一天单日;prev_today=昨日~今日;month_to_today=本月1日~今日;
|
||||
# prev_prev=昨日~昨日;month1_to_prev=本月1日~前一天;last_month=上月完整;
|
||||
# last3_months=近 3 个自然月按月拆分(止日不超过前一天)
|
||||
# 日期参数字段:报表统计类为 reportDate;淘宝订单为 createTimePeriod(页面「购买时间」);
|
||||
# 微盟核销为 validateDatePeriod;开卡订单/会员退卡为 period;
|
||||
# 礼品卡订单为 receiveTimePeriod(接口限制跨度 ≤31 天,故按月拆分)
|
||||
REPORTS = [
|
||||
('团购支付数据', 'report/groupPay/export', 'prev', None, 'reportDate'),
|
||||
('现金支付', 'report/cashPay/export', 'prev', None, 'reportDate'),
|
||||
('第三方支付(门店)', 'report/thirdPayShop/export', 'prev', None, 'reportDate'),
|
||||
('支付数据', 'report/unionPay/export', 'prev', None, 'reportDate'),
|
||||
('团购数据', 'report/groupData/export', 'prev_today', None, 'reportDate'),
|
||||
('淘宝订单数据', 'report/taobaoTicket/export', 'prev_today', None, 'createTimePeriod'),
|
||||
('技师综合业绩', 'report/comprehensivePerformance/export', 'month_to_today', None, 'reportDate'),
|
||||
('微盟核销统计', 'report/weimeng/exportValidate', 'prev_prev', None, 'validateDatePeriod'), # 该页日期字段是 validateDatePeriod("起 止")
|
||||
('小程序订单', 'report/miniProgramOrder/export', 'last_month', None, 'reportDate'),
|
||||
('小程序订单', 'report/miniProgramOrder/export', 'month_to_today', None, 'reportDate'),
|
||||
('储值卡消费', 'report/debitCardConsume/export', 'month1_to_prev', None, 'reportDate'),
|
||||
('储值卡线上充值', 'report/depositData/export', 'month1_to_prev', {'channel': 3}, 'reportDate'), # 渠道=管理端
|
||||
('霸王餐充值明细', 'report/overlordOrderDeposit/export', 'month1_to_prev', None, 'reportDate'),
|
||||
('霸王餐消费明细', 'report/overlordOrderConsume/export', 'month1_to_prev', None, 'reportDate'),
|
||||
('霸王餐余额查询', 'report/overlordOrderBalance/export', 'month1_to_prev', None, 'reportDate'),
|
||||
('开卡订单', 'member/order/export', 'month1_to_prev', None, 'period'),
|
||||
('会员退卡', 'member/cancel/export', 'month1_to_prev', None, 'period'),
|
||||
('礼品卡订单', 'giftcard/order/export', 'last3_months', None, 'receiveTimePeriod'),
|
||||
]
|
||||
|
||||
# ==================== 导出内容选择(UI 勾选) ====================
|
||||
SCREENSHOT_KEY = '__screenshot__' # 储值卡余额整页截图的勾选键
|
||||
|
||||
|
||||
def report_key(entry):
|
||||
"""报表稳定勾选键:导出接口#日期规则(与 UI 清单一一对应,跨版本稳定)。"""
|
||||
_title, path, rule, _extra, _date_key = entry
|
||||
return f'{path}#{rule}'
|
||||
|
||||
|
||||
def filter_reports(keys):
|
||||
"""按 UI 勾选键筛选导出内容。
|
||||
|
||||
keys 为 None(旧配置/未传)→ 全部导出;否则按勾选键过滤。
|
||||
返回 (报表列表, 是否含储值卡余额截图)。
|
||||
"""
|
||||
if keys is None:
|
||||
return list(REPORTS), True
|
||||
wanted = {str(k) for k in keys}
|
||||
include_shot = SCREENSHOT_KEY in wanted
|
||||
reports = [e for e in REPORTS if report_key(e) in wanted]
|
||||
return reports, include_shot
|
||||
|
||||
|
||||
# ==================== 全局钩子(run() 启动时注入) ====================
|
||||
_log = None
|
||||
_progress = None
|
||||
_debug = False
|
||||
|
||||
|
||||
def set_log(fn):
|
||||
"""注入 ctx.log(RunContext.log),各模块经 log()/logger 输出。"""
|
||||
global _log
|
||||
_log = fn
|
||||
|
||||
|
||||
def log(msg, level="INFO"):
|
||||
if level == "DEBUG" and not _debug:
|
||||
return
|
||||
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()
|
||||
|
||||
|
||||
def set_progress(fn):
|
||||
"""注入 ctx.progress。"""
|
||||
global _progress
|
||||
_progress = fn
|
||||
|
||||
|
||||
def progress(pct):
|
||||
"""上报进度(0~100);无 ctx 或异常时静默忽略,不影响主流程。"""
|
||||
if not _progress:
|
||||
return
|
||||
try:
|
||||
_progress(max(0, min(100, int(pct))))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def set_debug(flag):
|
||||
global _debug
|
||||
_debug = bool(flag)
|
||||
|
||||
|
||||
def is_debug():
|
||||
return _debug
|
||||
Reference in New Issue
Block a user