fallback_page.py
7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
"""截图兜底:DMS 页面截图失败时,把建单后接口回查到的真实数据(采购单列表行 + 明细)渲染成仿 DMS 页面再截图。
页面版式参照模拟 DMS(edge/dev/mock_dms.py 的采购订单管理 / 详情页),顶部固定一条醒目说明:
「DMS 页面截图失败,以下为系统接口实际返回数据渲染,非 DMS 原页面」+ 失败原因 + 数据来源接口 + 渲染时间。
本地 HTML 用本机 Chromium(独立的临时 profile,不碰 DMS 登录态)打开截图,不访问任何网络。
"""
import html
import json
import logging
import os
import shutil
import tempfile
import time
log = logging.getLogger("edge")
NOTICE = "DMS 页面截图失败,以下为系统接口实际返回数据渲染,非 DMS 原页面"
STATUS = {"1": "待提交", "2": "已提交", "3": "部分到货", "4": "已完成"}
TYPES = {"S": "SO/库存补充订单", "L": "Local/本地订单", "V": "VOR订单"}
STYLE = """
body{margin:0;font-family:"Microsoft YaHei",sans-serif;background:#f0f2f5;color:#333}
.notice{background:#fff1f0;border:3px solid #d03050;color:#a8071a;padding:12px 18px;font-size:18px;font-weight:bold}
.notice small{display:block;font-size:13px;font-weight:normal;color:#5c0011;margin-top:6px;line-height:1.6}
.top{height:44px;background:#c8102e;color:#fff;display:flex;align-items:center;padding:0 20px;font-size:16px}
.main{padding:16px}.card{background:#fff;border-radius:4px;padding:16px;margin-bottom:12px}
.crumb{color:#888;font-size:13px;margin-bottom:10px}h3{margin:0 0 12px;font-size:16px}
table{width:100%;border-collapse:collapse;font-size:13px}th{background:#fafafa;font-weight:600}
th,td{border-bottom:1px solid #eee;padding:8px;text-align:left;white-space:nowrap}
.tag{display:inline-block;padding:0 6px;border-radius:2px;font-size:12px;background:#fff7e6;color:#d46b08;border:1px solid #ffd591}
.kv{display:grid;grid-template-columns:repeat(4,1fr);gap:8px 16px;font-size:13px}.kv b{color:#888;font-weight:normal;margin-right:6px}
.foot{color:#999;font-size:12px;padding:8px 16px}
"""
def _e(v):
return html.escape("" if v is None else str(v))
INCOMPLETE = "DMS 采购单详情翻页截图不完整,以下为系统接口实际返回的完整明细渲染,非 DMS 原页面"
def _page(title, body, reason, source, notice=NOTICE):
return ("<!doctype html><html><head><meta charset='utf-8'><title>%s</title><style>%s</style></head><body>"
"<div class=notice>⚠ %s<small>原因:%s<br>数据来源:%s<br>渲染时间:%s(取数端本机生成,仅用于核对单据内容)</small></div>"
"<div class=top><b>广汽丰田 DMS</b> (接口数据渲染)</div><div class=main>%s</div>"
"<div class=foot>%s</div></body></html>") % (
_e(title), STYLE, notice, _e(reason)[:400], _e(source), time.strftime("%Y-%m-%d %H:%M:%S"), body, notice)
def list_html(row, reason=""):
r = row or {}
st = str(r.get("orderStatus") or "")
body = ("<div class=crumb>配件 / 采购管理 / 采购订单管理</div><div class=card><h3>采购订单管理</h3><table>"
"<tr><th>订单号</th><th>订单类型</th><th>供应商</th><th>订单状态</th><th>项数</th><th>采购金额</th>"
"<th>创建人</th><th>创建时间</th><th>更新时间</th><th>经销商</th><th>备注</th></tr>"
"<tr><td>%s</td><td>%s</td><td>%s</td><td><span class=tag>%s</span></td><td>%s</td><td>¥%s</td>"
"<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr></table></div>") % (
_e(r.get("orderNo")), _e(TYPES.get(r.get("orderType"), r.get("orderType"))), _e(r.get("supplierCode")),
_e(STATUS.get(st, st)), _e(r.get("itemNum")), _e(r.get("totalAmount")), _e(r.get("createByName")),
_e(r.get("createTime")), _e(r.get("updateTime")), _e(r.get("dealerCode")), _e(r.get("remark")))
return _page("采购订单管理(接口数据渲染)", body, reason, "get-parts-po-page(采购单列表接口)")
def detail_html(row, details, reason="", notice=NOTICE):
r = row or {}
st = str(r.get("orderStatus") or "")
head = ("<div class=crumb>配件 / 采购管理 / 采购订单管理 / 采购订单详情</div><div class=card><h3>采购订单详情</h3>"
"<div class=kv><div><b>订单号</b>%s</div><div><b>订单类型</b>%s</div><div><b>供应商</b>%s</div>"
"<div><b>订单状态</b><span class=tag>%s</span></div><div><b>创建人</b>%s</div><div><b>创建时间</b>%s</div>"
"<div><b>采购金额</b>¥%s</div><div><b>备注</b>%s</div></div></div>") % (
_e(r.get("orderNo")), _e(TYPES.get(r.get("orderType"), r.get("orderType"))), _e(r.get("supplierCode")),
_e(STATUS.get(st, st)), _e(r.get("createByName")), _e(r.get("createTime")), _e(r.get("totalAmount")),
_e(r.get("remark")))
rows = "".join("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (
_e(d.get("itemNo") or i), _e(d.get("partNo")), _e(d.get("partName")), _e(d.get("purchaseQty")),
_e(d.get("unitPrice")), _e(d.get("totalPriceSum") or d.get("totalPrice")), _e(d.get("dailySurplusPurQty")))
for i, d in enumerate(details or [], 1))
body = head + ("<div class=card><table><tr><th>序号</th><th>配件代码</th><th>配件名称</th><th>采购数量</th><th>单价</th>"
"<th>金额</th><th>当日剩余可订</th></tr>%s</table></div>") % rows
return _page("采购订单详情(接口数据渲染)", body, reason, "get-parts-po-detail-page(采购单明细接口)", notice)
def capture(cfg, row, details, out_dir, reason=""):
"""渲染两张兜底截图(列表 + 详情)。返回 [(文件路径, 标题)];本机 Chromium 不可用时抛异常(调用方改发文字明细)。"""
os.makedirs(out_dir, exist_ok=True)
pages = [("po_list_fallback.png", "采购订单管理(接口数据渲染)", list_html(row, reason)),
("po_detail_fallback.png", "采购订单详情(接口数据渲染)", detail_html(row, details, reason))]
with open(os.path.join(out_dir, "fallback_data.json"), "w", encoding="utf-8") as f:
json.dump({"reason": reason, "row": row, "details": details}, f, ensure_ascii=False, indent=2, default=str)
return _render(cfg, pages, out_dir)
def capture_detail(cfg, row, details, out_dir, reason=""):
"""DMS 详情翻页截图不完整时补的一张:接口数据渲染的完整明细(顶部注明不是 DMS 原页面)。返回文件路径。"""
os.makedirs(out_dir, exist_ok=True)
with open(os.path.join(out_dir, "detail_full_data.json"), "w", encoding="utf-8") as f:
json.dump({"reason": reason, "row": row, "details": details}, f, ensure_ascii=False, indent=2, default=str)
pages = [("po_detail_full_rendered.png", "采购订单详情完整明细(接口数据渲染)",
detail_html(row, details, reason, INCOMPLETE))]
return _render(cfg, pages, out_dir)[0][0]
def _render(cfg, pages, out_dir):
"""本机 Chromium(临时 profile,不碰 DMS 登录态)打开本地 HTML 逐张截图。返回 [(文件路径, 标题)]。"""
from . import cdp
from .screenshot import shoot
profile = tempfile.mkdtemp(prefix="edge_fallback_")
chrome = None
out = []
try:
cdp.CANCEL.clear()
chrome = cdp.Chrome(cfg.chrome_path, profile, True, cfg.window_size, list(cfg.extra_args or []))
for fname, title, page in pages:
hp = os.path.join(out_dir, fname.replace(".png", ".html"))
with open(hp, "w", encoding="utf-8") as f:
f.write(page)
chrome.navigate("file:///" + os.path.abspath(hp).replace("\\", "/"))
time.sleep(1.0)
path = os.path.join(out_dir, fname)
shoot(chrome, cfg, path)
out.append((path, title))
log.warning("截图兜底:已用接口数据渲染 %s", fname)
finally:
if chrome:
chrome.close()
shutil.rmtree(profile, ignore_errors=True)
return out