- 天阙会生活商户平台(mch-web.tianquetech.com)扫码交易记录导出:账密登录(图形验证码 OCR + AES 密码加密,token 按账号缓存复用)→ 平台导出接口生成 xlsx → 下载中心取文件直存本地 - 支持两套账号串行导出(第 2 套可留空),日期区间界面可选,产物按商户名区分
169 lines
8.1 KiB
Python
169 lines
8.1 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""金色湖畔扫码数据导出(XQ0079)- webhost 场景插件
|
||
|
||
天阙会生活商户平台(mch-web.tianquetech.com)扫码交易记录接口化导出:
|
||
- 登录:账密 + 图形验证码 OCR + AES 加密密码(与网页同款报文),token 按账号缓存复用;
|
||
- 查询:平台导出接口 exportNew 提交任务,下载中心取 OSS 预签名地址直接下载 xlsx;
|
||
- 多账号:界面填两套账号(第 2 套可留空),串行导出、互不影响,产物按商户名区分;
|
||
- 时间:默认「昨天 00:00:00 ~ 今天 23:59:59」,也可在界面指定日期段。
|
||
|
||
个性化设计:
|
||
- 账号密码经 fields 注入(plugin_configs 本机明文保存,下次免填);
|
||
- 平台账号之间完全独立,各自建立 session/token,避免串号;
|
||
- 单个账号失败不影响另一个账号,最终汇总返回各账号结果。
|
||
"""
|
||
import os
|
||
import random
|
||
import time
|
||
|
||
from webhost.backend.plugins.base import PLUGINS, ScenePlugin
|
||
|
||
from . import config
|
||
from .data import tianque_export
|
||
from .logic import date_range
|
||
|
||
|
||
def _default_save_dir():
|
||
return config.DEFAULT_SAVE_DIR
|
||
|
||
|
||
class TianqueTradeExportPlugin(ScenePlugin):
|
||
id = "xq0079"
|
||
name = "金色湖畔扫码数据导出"
|
||
version = "1.1.4"
|
||
description = ("天阙会生活商户平台扫码交易记录接口化导出:账密自动登录(图形验证码 OCR + AES 密码加密),"
|
||
"服务端生成 xlsx 后直取文件;支持两套账号串行导出,"
|
||
"默认昨天 00:00:00 ~ 今天 23:59:59")
|
||
platform = "" # 平台自带账密登录,不使用底座共享登录态
|
||
uses_month = False
|
||
capabilities = ["xq0079"]
|
||
permissions = {"platforms": []}
|
||
# 与 plugin.json.fields 一致(ui=true 前端自渲染,此处作为参数契约文档)
|
||
fields = {
|
||
"tq_account1": {"type": "string", "title": "账号一", "default": "",
|
||
"description": "天阙商户平台登录账号(手机号/商户编号)"},
|
||
"tq_password1": {"type": "string", "title": "密码一", "default": "",
|
||
"description": "账号一密码(明文存本机配置)"},
|
||
"tq_account2": {"type": "string", "title": "账号二", "default": "",
|
||
"description": "第二个账号,留空则只导出账号一"},
|
||
"tq_password2": {"type": "string", "title": "密码二", "default": "",
|
||
"description": "账号二密码"},
|
||
"login_mode": {"type": "string", "title": "登录方式", "default": config.LOGIN_AUTO,
|
||
"enum": config.LOGIN_MODE_CHOICES,
|
||
"description": "自动=接口登录(验证码 OCR,静默);人工登录=弹浏览器,人工输验证码后点登录"},
|
||
"date_from": {"type": "string", "format": "date", "title": "开始日期", "default": "",
|
||
"description": "yyyy-MM-dd;留空=昨天"},
|
||
"date_to": {"type": "string", "format": "date", "title": "结束日期", "default": "",
|
||
"description": "yyyy-MM-dd;留空=今天"},
|
||
"save_dir": {"type": "string", "title": "保存目录", "default": "",
|
||
"description": "留空=%s" % config.DEFAULT_SAVE_DIR},
|
||
"debug_mode": {"type": "boolean", "title": "调试日志", "default": False,
|
||
"description": "开启后输出完整调试日志"},
|
||
}
|
||
|
||
def run(self, fields, ctx) -> dict:
|
||
fields = fields or {}
|
||
config.set_log(ctx.log)
|
||
config.set_progress(ctx.progress)
|
||
config.set_debug(bool(fields.get("debug_mode")))
|
||
|
||
# ---------- 解析参数 ----------
|
||
accounts = []
|
||
for idx in (1, 2):
|
||
acc = str(fields.get("tq_account%d" % idx) or "").strip()
|
||
pwd = str(fields.get("tq_password%d" % idx) or "").strip()
|
||
if acc and not pwd:
|
||
raise RuntimeError("账号%d 已填写账号但缺少密码" % idx)
|
||
if pwd and not acc:
|
||
raise RuntimeError("账号%d 已填写密码但缺少账号" % idx)
|
||
if acc and pwd:
|
||
accounts.append((idx, acc, pwd))
|
||
if not accounts:
|
||
raise RuntimeError("请至少填写一套账号与密码(账号一)")
|
||
|
||
try:
|
||
begin, end = date_range.build_range(fields.get("date_from"), fields.get("date_to"))
|
||
except ValueError as exc:
|
||
raise RuntimeError("导出日期不正确:%s" % exc)
|
||
|
||
login_mode = str(fields.get("login_mode") or config.LOGIN_AUTO).strip()
|
||
if login_mode not in config.LOGIN_MODE_CHOICES:
|
||
login_mode = config.LOGIN_AUTO
|
||
|
||
save_dir = str(fields.get("save_dir") or "").strip().strip('"') or _default_save_dir()
|
||
os.makedirs(save_dir, exist_ok=True)
|
||
stamp = date_range.file_stamp(begin, end)
|
||
|
||
ctx.log("导出区间:%s ~ %s" % (begin, end))
|
||
ctx.log("账号数:%d(%s)" % (len(accounts), "、".join(a[1] for a in accounts)))
|
||
ctx.log("登录方式:%s%s" % (
|
||
login_mode, "(逐个账号弹出浏览器,人工输入验证码并点登录)"
|
||
if login_mode == config.LOGIN_MANUAL else ""))
|
||
ctx.log("保存目录:%s" % save_dir)
|
||
|
||
# ---------- 逐账号导出 ----------
|
||
span = 90 / len(accounts)
|
||
results = []
|
||
for pos, (idx, acc, pwd) in enumerate(accounts):
|
||
low = int(5 + span * pos)
|
||
high = int(5 + span * (pos + 1))
|
||
if pos > 0:
|
||
# 串行 + 随机停顿:避免同一秒内连登多个账号,减少「同设备短时多账号」风控特征
|
||
gap = random.uniform(*config.ACCOUNT_GAP_SECONDS)
|
||
ctx.log("账号间停顿 %.1f 秒后继续" % gap)
|
||
time.sleep(gap)
|
||
ctx.log("—— 账号%d 开始(%s)——" % (idx, acc))
|
||
try:
|
||
res = tianque_export.export_trade(acc, pwd, save_dir, begin, end, stamp,
|
||
progress_from=low, progress_to=high,
|
||
login_mode=login_mode)
|
||
except Exception as exc: # 单账号失败不影响后续账号
|
||
res = {"success": False, "merchant": acc, "file": "", "size": 0,
|
||
"message": str(exc)}
|
||
ctx.log("账号%d 导出失败:%s" % (idx, exc), "ERROR")
|
||
res["account_index"] = idx
|
||
res["account"] = acc
|
||
results.append(res)
|
||
if res.get("success"):
|
||
ctx.log("账号%d 导出完成:%s" % (idx, res.get("file")))
|
||
else:
|
||
ctx.log("账号%d 未完成:%s" % (idx, res.get("message")), "WARN")
|
||
|
||
config.progress(100)
|
||
|
||
# ---------- 汇总 ----------
|
||
ok = [r for r in results if r.get("success")]
|
||
bad = [r for r in results if not r.get("success")]
|
||
notes = []
|
||
for r in results:
|
||
tag = "成功" if r.get("success") else "失败"
|
||
notes.append("%s|账号%d %s|%s|%s" % (
|
||
tag, r["account_index"], r["account"],
|
||
r.get("merchant") or "-", r.get("message") or "-"))
|
||
|
||
if not ok:
|
||
raise RuntimeError("两个账号都未导出成功:\n" + "\n".join(notes))
|
||
|
||
summary = "交易记录导出完成:%d/%d 个账号,区间 %s ~ %s" % (
|
||
len(ok), len(results), begin, end)
|
||
if bad:
|
||
summary += "(%d 个账号失败,详见备注)" % len(bad)
|
||
|
||
return {
|
||
"summary": summary,
|
||
"detail": {
|
||
"begin": begin,
|
||
"end": end,
|
||
"stamp": stamp,
|
||
"save_dir": save_dir,
|
||
"accounts": results,
|
||
"ok_count": len(ok),
|
||
"fail_count": len(bad),
|
||
},
|
||
"path": ok[0].get("file") if len(ok) == 1 else save_dir,
|
||
"notes": notes,
|
||
}
|
||
|
||
|
||
PLUGINS.append(TianqueTradeExportPlugin())
|