80 lines
3.5 KiB
Python
80 lines
3.5 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""CRM数据导出 - webhost 场景插件(XQ0048)
|
||
|
||
流程:
|
||
1) 登录态预检(API 校验 %LOCALAPPDATA%\\37duAutomation\\.sessions\\crm_cookie_header.txt);
|
||
有效则全程免扫码,失效则打开可见浏览器等钉钉扫码一次(同次运行最多弹一次);
|
||
2) 打开「报表统计/储值卡余额」整页截图 → 储值卡余额-YYYYMMDD.png;
|
||
3) requests 复用同一登录态,按日期规则导出 18 张报表 xlsx
|
||
(礼品卡订单因接口限制跨度 ≤31 天,近 3 个自然月按月拆分)。
|
||
"""
|
||
import os
|
||
|
||
from webhost.backend.plugins.base import PLUGINS, ScenePlugin
|
||
|
||
from . import config
|
||
from .data import crm_export
|
||
|
||
|
||
class CrmDataExportPlugin(ScenePlugin):
|
||
id = "xq0048"
|
||
name = "CRM数据导出"
|
||
version = "1.1.6"
|
||
description = ("37du CRM(crm.37du.co)数据导出:储值卡余额整页截图 + 18 张报表自动导出(导出内容可勾选),"
|
||
"登录态钉钉扫码一次后缓存复用(约 30 分钟内免扫码),"
|
||
"按各报表日期规则自动计算区间,礼品卡订单按月拆分")
|
||
platform = "" # CRM 为钉钉扫码登录,插件内自带登录流程,不使用底座共享登录态
|
||
uses_month = False
|
||
capabilities = ["xq0048"]
|
||
permissions = {"platforms": []}
|
||
# 与 plugin.json.fields 一致
|
||
fields = {
|
||
"reports": {"type": "array", "title": "导出内容", "default": [],
|
||
"description": "勾选键列表(截图=__screenshot__,报表=接口#规则);空/缺省=全部导出"},
|
||
"save_dir": {"type": "string", "title": "保存目录", "default": "",
|
||
"description": "留空=D:\\资料\\财务\\CRM数据导出"},
|
||
"debug_mode": {"type": "boolean", "title": "调试日志", "default": False},
|
||
}
|
||
|
||
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")))
|
||
|
||
save_dir = str(fields.get("save_dir") or "").strip().strip('"') or config.default_output_dir()
|
||
os.makedirs(save_dir, exist_ok=True)
|
||
ctx.log(f"保存目录:{save_dir}")
|
||
|
||
report_keys = fields.get("reports")
|
||
reports, include_shot = config.filter_reports(report_keys)
|
||
if not reports and not include_shot:
|
||
raise RuntimeError("未勾选任何导出内容,请至少勾选一项")
|
||
if report_keys is None:
|
||
ctx.log("导出内容:全部(截图 + %d 张报表)" % len(reports))
|
||
else:
|
||
ctx.log(f"导出内容:{'截图、' if include_shot else ''}{len(reports)} 张报表(按勾选)")
|
||
|
||
result = crm_export.run_export(save_dir, report_keys=report_keys)
|
||
config.progress(100)
|
||
|
||
files = result.get("exports") or []
|
||
shot_part = f"截图 {1 if result.get('screenshot') else 0}/1," if include_shot else ""
|
||
summary = (f"{shot_part}"
|
||
f"报表 {result.get('done')}/{result.get('total')},"
|
||
f"共 {len(files) + (1 if result.get('screenshot') else 0)} 个文件")
|
||
return {
|
||
"summary": summary,
|
||
"detail": {
|
||
"screenshot": result.get("screenshot") or "",
|
||
"reports_done": result.get("done", 0),
|
||
"reports_total": result.get("total", 0),
|
||
"files": files,
|
||
"save_dir": save_dir,
|
||
},
|
||
"path": save_dir,
|
||
}
|
||
|
||
|
||
PLUGINS.append(CrmDataExportPlugin())
|