Files
37duAutomation/plugins/XQ0001-0100/XQ0047 微盟数据导出/__init__.py
T

78 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""微盟数据导出 - webhost 场景插件(XQ0047)
流程:
1) 复用有效登录态(API 校验 weimob_cookies.json),失效则打开可见浏览器登录;
微盟登录可能弹腾讯验证码,脚本无法绕过,会提示「请在浏览器窗口完成验证」并等待;
2) 探测「上月1日 00:00 ~ 此刻」订单总数,按单段 45000 单等分并逐段校验细分;
3) 批量创建 exportOrder 导出任务 → 统一轮询 download/list → 并发下载 zip 解压;
4) 各段 Excel 合并为单个 xlsx(桌面/微盟订单导出/微盟订单_YYYYMMDD_HHMMSS.xlsx)。
"""
import os
from webhost.backend.plugins.base import PLUGINS, ScenePlugin
from . import config
from .data import weimob_export, weimob_login
class WeimobExportPlugin(ScenePlugin):
id = "xq0047"
name = "微盟数据导出"
version = "1.0.1"
description = ("微盟商家后台订单数据自动导出:登录微盟(腾讯验证码需人工完成,登录态缓存复用),"
"按订单量自动等分时间区间批量创建导出任务,统一轮询下载并合并为一个 Excel")
platform = "" # 微盟自带登录页(腾讯验证码),插件内现场真登录,不使用底座共享登录态
uses_month = False
capabilities = ["xq0047"]
permissions = {"platforms": []}
# 与 plugin.json.fields 一致(ui=true 前端自渲染,此处分层作为参数契约文档)
fields = {
"wm_username": {"type": "string", "title": "微盟登录手机号", "default": ""},
"wm_password": {"type": "string", "title": "微盟登录密码", "default": "",
"format": "password"},
"save_dir": {"type": "string", "title": "保存目录", "default": ""},
}
def run(self, fields, ctx) -> dict:
fields = fields or {}
config.set_log(ctx.log)
# ---------- 解析与校验 ----------
username = str(fields.get("wm_username") or "").strip()
password = str(fields.get("wm_password") or "").strip()
save_dir = str(fields.get("save_dir") or "").strip().strip('"') or config.default_output_dir()
if not username or not password:
raise RuntimeError("微盟登录账号/密码缺失:请在插件界面填写并保存配置后重试")
os.makedirs(save_dir, exist_ok=True)
ctx.log(f"保存目录:{save_dir}")
# ---------- 登录(缓存复用,失效时浏览器现场登录) ----------
ctx.progress(2)
login = weimob_login.ensure_login(username, password)
if not login.get("success"):
raise RuntimeError(f"微盟登录失败:{login.get('message')}")
ctx.log(login.get("message") or "登录态就绪")
ctx.progress(10)
# ---------- 导出 ----------
result = weimob_export.run_export(save_dir, ctx.progress)
if not result.get("success"):
raise RuntimeError(result.get("message") or "微盟订单导出失败")
ctx.progress(100)
return {
"summary": result["message"],
"detail": {
"excel_path": result.get("excel_path"),
"segments": result.get("segments", 0),
"rows": result.get("rows", 0),
"save_dir": save_dir,
},
"path": result.get("excel_path"),
}
PLUGINS.append(WeimobExportPlugin())