- 金贵微信端管理平台(vip6.wechat.goldenyun.cn)门票使用统计导出:接口登录 → 搜索接口取核销明细 → 调平台导出接口生成 xlsx - 核销时间日期段界面可选(默认昨天 00:00:00 ~ 今天 23:59:59);导出接口异常时按同一份数据本地生成同列序 xlsx 兜底
138 lines
6.3 KiB
Python
138 lines
6.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""金色湖畔小程序数据导出(XQ0078)- webhost 场景插件
|
||
|
||
金贵微信端管理平台(vip6.wechat.goldenyun.cn/chqhpwq.web)门票使用统计导出:
|
||
- 登录:接口登录(门店 + 账号密码),无验证码/无 CSRF/无密码加密;
|
||
- 查询:核销时间默认「昨日 00:00:00 ~ 今日 23:59:59」,可自定义区间;
|
||
走搜索接口 ticketconsumelistreport 取 JSON(不是页面 HTML);
|
||
- 导出:平台导出接口只认「文件名 + 表格 HTML」,故先取 JSON → 拼表格 → 提交导出;
|
||
导出接口异常时用同一份 JSON 本地生成同列序 xlsx,保证产物不丢。
|
||
|
||
个性化设计:
|
||
- 账号密码经 fields 注入(plugin_configs 本机明文保存,下次免填);
|
||
- 门店名留空 = 自动取登录页第一个门店;
|
||
- 平台无验证码,每次执行现场登录,不依赖底座共享登录态(platform="")。
|
||
"""
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
|
||
from webhost.backend.plugins.base import PLUGINS, ScenePlugin
|
||
|
||
from . import config
|
||
from .data import golden_login, golden_export
|
||
from .logic import date_range
|
||
|
||
|
||
def _default_save_dir():
|
||
return str(Path(config.get_desktop()) / f"金色湖畔小程序数据_{datetime.now():%Y%m%d}")
|
||
|
||
|
||
class JinseHupanExportPlugin(ScenePlugin):
|
||
id = "xq0078"
|
||
name = "金色湖畔小程序数据导出"
|
||
version = "1.0.4"
|
||
description = ("金贵微信端管理平台门票使用统计接口化导出:接口登录 → 搜索接口取核销明细 → "
|
||
"平台导出接口生成 xlsx;核销时间默认昨日 00:00:00 ~ 今日 23:59:59")
|
||
platform = "" # 平台自带登录页(无验证码),插件内接口登录,不用底座共享登录态
|
||
uses_month = False
|
||
capabilities = ["xq0078"]
|
||
permissions = {"platforms": []}
|
||
# 与 plugin.json.fields 一致(ui=true 前端自渲染,此处作为参数契约文档)
|
||
fields = {
|
||
"date_from": {"type": "string", "format": "date", "title": "开始日期", "default": "",
|
||
"description": "yyyy-MM-dd;留空=昨天"},
|
||
"date_to": {"type": "string", "format": "date", "title": "结束日期", "default": "",
|
||
"description": "yyyy-MM-dd;留空=与开始日期同一天"},
|
||
"vpn_name": {"type": "string", "title": "门店", "default": config.DEFAULT_VPN_NAME},
|
||
"jshp_username": {"type": "string", "title": "登录账号", "default": ""},
|
||
"jshp_password": {"type": "string", "title": "登录密码", "default": ""},
|
||
"save_dir": {"type": "string", "title": "保存目录", "default": ""},
|
||
}
|
||
|
||
def run(self, fields, ctx) -> dict:
|
||
fields = fields or {}
|
||
config.set_log(ctx.log)
|
||
|
||
# ---------- 解析参数 ----------
|
||
date_from = str(fields.get("date_from") or "").strip()
|
||
date_to = str(fields.get("date_to") or "").strip()
|
||
username = str(fields.get("jshp_username") or "").strip()
|
||
password = str(fields.get("jshp_password") or "").strip()
|
||
vpn_name = str(fields.get("vpn_name") or "").strip()
|
||
save_dir = str(fields.get("save_dir") or "").strip().strip('"') or _default_save_dir()
|
||
|
||
if not username or not password:
|
||
raise RuntimeError("登录账号/密码缺失:请在插件界面填写并保存配置后重试")
|
||
try:
|
||
begin, end = date_range.build_range(date_from, date_to)
|
||
except ValueError as e:
|
||
raise RuntimeError(f"核销时间区间不正确:{e}")
|
||
|
||
ctx.log(f"核销时间:{begin} ~ {end}")
|
||
|
||
# ---------- 登录 ----------
|
||
ctx.progress(5)
|
||
ctx.log("开始登录金贵微信端管理平台…")
|
||
session = golden_login.login(username, password, vpn_name)
|
||
ctx.progress(15)
|
||
|
||
# ---------- 列表页(取导出表单动态字段名) ----------
|
||
html = golden_export.open_list_page(session)
|
||
export_fields = golden_export.parse_export_fields(html)
|
||
ctx.progress(25)
|
||
|
||
# ---------- 搜索接口取明细 ----------
|
||
ctx.log("调用搜索接口获取核销明细…")
|
||
rows = golden_export.fetch_all_rows(session, begin, end)
|
||
ctx.progress(85)
|
||
if rows:
|
||
ctx.log(f"查询到 {len(rows)} 条核销明细")
|
||
else:
|
||
ctx.log("该时间范围内没有核销明细,将导出空表", "WARN")
|
||
|
||
# ---------- 导出 ----------
|
||
ctx.log("调用平台导出接口生成 Excel…")
|
||
stamp = date_range.file_stamp(begin, end)
|
||
filename = f"{config.EXPORT_TITLE}_{stamp}.xlsx"
|
||
out_dir = Path(save_dir)
|
||
out_dir.mkdir(parents=True, exist_ok=True)
|
||
out_path = out_dir / filename
|
||
|
||
result = golden_export.export_via_server(session, export_fields, rows)
|
||
kind = result.get("kind")
|
||
if kind == "xlsx":
|
||
out_path.write_bytes(result["content"])
|
||
source = "平台导出接口(xlsx)"
|
||
elif kind == "html_excel":
|
||
# 平台导出的是 HTML 伪装 Excel(UTF-16 的 <table>),数据与搜索接口一致;
|
||
# 统一用同一份数据生成为标准 xlsx,避免 .xls 扩展名格式不符的提示
|
||
golden_export.write_xlsx_local(rows, out_path)
|
||
source = "平台导出接口数据 + 本地标准 xlsx"
|
||
else:
|
||
ctx.log("平台导出接口未返回可识别文件,改用本地按同一份数据生成", "WARN")
|
||
golden_export.write_xlsx_local(rows, out_path)
|
||
source = "本地兜底生成"
|
||
ctx.progress(100)
|
||
|
||
return {
|
||
"summary": f"门票使用统计导出完成:{len(rows)} 条 → {out_path}({source})",
|
||
"rows": rows,
|
||
"detail": {
|
||
"count": len(rows),
|
||
"begin": begin,
|
||
"end": end,
|
||
"file": str(out_path),
|
||
"source": source,
|
||
},
|
||
"path": str(out_path),
|
||
"notes": [
|
||
f"核销时间:{begin} ~ {end}",
|
||
f"明细条数:{len(rows)}",
|
||
f"导出方式:{source}",
|
||
f"文件:{out_path}",
|
||
],
|
||
}
|
||
|
||
|
||
PLUGINS.append(JinseHupanExportPlugin())
|