162 lines
5.0 KiB
Python
162 lines
5.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""xq0077 携程数据导出 插件 - 配置常量与全局钩子。
|
||
|
||
插件自包含:本包只依赖 Python 标准库 / 第三方库 / 底座注入的 RunContext。
|
||
业务模块统一经 config.log() 输出日志(转发到 ctx.log,无 ctx 时回落到 print)。
|
||
|
||
站点(深大/lotsmall 管理端):
|
||
- 管理端 www.lotsmall.cn(/admin/api/checkCode 图片验证码 + /admin/storeMerchant/logining 账密登录,
|
||
返回 user.token 即管理端 tyAuthToken;statisticsSupport 换取报表接入参数);
|
||
- 报表平台 platform-report-sh1.zhiyoubao.com(换 JWT → preview → searchFilter → newExport)。
|
||
"""
|
||
import os
|
||
import time
|
||
from datetime import date, timedelta
|
||
|
||
# ==================== 站点与接口 ====================
|
||
ADMIN_BASE = 'https://www.lotsmall.cn'
|
||
REPORT_BASE = 'https://platform-report-sh1.zhiyoubao.com'
|
||
|
||
API_CHECKCODE = '/admin/api/checkCode' # 管理端图片验证码(JPEG 直出)
|
||
API_ADMIN_LOGIN = '/admin/storeMerchant/logining' # 管理端账密登录(返回 user.token)
|
||
API_STATISTICS_SUPPORT = '/statistics/webApi/statisticsSupport/'
|
||
API_VERIFY_SAAS = '/api/partner/auth/verifySaasAuthCode'
|
||
API_PREVIEW = '/api/report/preview/{report_id}'
|
||
API_SEARCH = '/api/report/searchFilter'
|
||
API_EXPORT = '/api/report/newExport'
|
||
|
||
# 登录页密码 RSA 公钥(PKCS#1 v1.5,平台前端硬编码,2026-11 实测;管理端与用户中心同一把)
|
||
RSA_PUBLIC_KEY = (
|
||
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI5kmdW9rmxQlTraZ6Wx+C7kYRDjL332N53dsqFsqq'
|
||
'T+PX6mosaM5hW4MS4vUbQNtUblhmQgzmUD0KBJFH9YZMmSPLdCC+G4r7d2PPFBrCy+9dFUMUNaT4Penya'
|
||
'VQJyAp3bhUJ7n93bRJT9jtxsdDwJ/Zl12A/kO93fr7+waaepwIDAQAB'
|
||
)
|
||
CAPTCHA_MIN_LEN = 3 # 管理端图片验证码长度范围(实测 4~5 位)
|
||
CAPTCHA_MAX_LEN = 6
|
||
|
||
# 管理端 token 失效(请跳转到登录页)
|
||
CODE_TOKEN_EXPIRED = 1004001011
|
||
CODE_OK = 200
|
||
|
||
# ==================== 报表(检票员检票报表) ====================
|
||
REPORT_ID = 3066
|
||
REPORT_NAME = '检票员检票报表'
|
||
REPORT_URL = f'https://platform-report-sh1.zhiyoubao.com/reportView?id={REPORT_ID}'
|
||
# 报表查询固定入参(平台页面实测值)
|
||
TERMINAL_TPE = '0'
|
||
MERCHANT_ID = '23985'
|
||
USER_INFO_ID = '59567'
|
||
SUB_ACCOUNT_ID = '59925'
|
||
ROLE_TYPE = '4'
|
||
APP_ID_DEFAULT = 'xj'
|
||
PAGE_INDEX = 1
|
||
PAGE_SIZE = 50
|
||
|
||
# ==================== 输出路径 ====================
|
||
DEFAULT_SAVE_DIR = r'D:\资料\财务\携程数据导出'
|
||
|
||
|
||
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')
|
||
TOKEN_FILE = os.path.join(SESSIONS_DIR, 'lotsmall_token.json') # {"accessToken": ...}
|
||
|
||
|
||
def default_output_dir():
|
||
"""默认输出目录。"""
|
||
return DEFAULT_SAVE_DIR
|
||
|
||
|
||
# ==================== 日期口径(固定规则) ====================
|
||
def date_window():
|
||
"""返回 (起, 止) 时间字符串:昨天 00:00:00 ~ 今天 23:59:59。"""
|
||
today = date.today()
|
||
prev = today - timedelta(days=1)
|
||
return f'{prev:%Y-%m-%d} 00:00:00', f'{today:%Y-%m-%d} 23:59:59'
|
||
|
||
|
||
def export_filename():
|
||
"""导出文件名:检票员检票报表_YYYYMMDD_HHMMSS.xlsx。"""
|
||
return f'{REPORT_NAME}_{time.strftime("%Y%m%d_%H%M%S")}.xlsx'
|
||
|
||
|
||
# ==================== 运行参数(项目规范:全局重试 3 次) ====================
|
||
RETRY_MAX = 3 # 接口请求/验证码轮次重试
|
||
TIMEOUT = 30 # 普通接口超时
|
||
DOWNLOAD_TIMEOUT = 180 # 导出 xlsx 下载超时
|
||
|
||
UA = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
|
||
'Chrome/120.0.0.0 Safari/537.36')
|
||
|
||
# ==================== 全局钩子(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
|