199 lines
16 KiB
Python
199 lines
16 KiB
Python
#!/usr/bin/env python3
|
||
"""Build an offline review gallery from untouched, locally captured app screenshots."""
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import html
|
||
import json
|
||
import pathlib
|
||
import hashlib
|
||
import struct
|
||
import subprocess
|
||
import zipfile
|
||
from collections import Counter
|
||
|
||
|
||
TITLES = {
|
||
"home": "机巢工作台", "statistics": "工作台 · 统计区域", "orders": "我的拍摄订单",
|
||
"payment-empty": "套餐收款 · 未选择套餐", "payment": "套餐收款", "receipts": "收款记录",
|
||
"preparation": "拍摄准备", "task-ready": "拍摄任务 · 到达待开拍", "task-shooting": "拍摄任务 · 拍摄中",
|
||
"task-completion-dialog": "拍摄任务 · 完成提示", "task-completed": "拍摄任务 · 拍摄已完成",
|
||
"task-paused": "拍摄任务 · 手动暂停", "task-returning": "拍摄任务 · 返航中",
|
||
"task-returned": "拍摄任务 · 已回巢", "task-pending-pause": "拍摄任务 · 暂停指令等待",
|
||
"task-starting": "拍摄任务 · 起飞准备", "task-failed": "拍摄任务 · 失败",
|
||
"task-going": "拍摄任务 · 前往点位",
|
||
"materials": "订单素材库", "material-preview": "素材预览", "delivery-method": "交付方式选择",
|
||
"delivery-cloud": "云端交付", "delivery-nearby": "面对面闪传", "confirmation": "订单交付确认",
|
||
"confirmation-code": "确认收货二维码", "confirmation-completed": "订单交付确认 · 已完成",
|
||
"project-picker": "套餐选择", "amount-edit": "设置金额", "live-fullscreen": "图传全屏",
|
||
}
|
||
|
||
ALIASES = {
|
||
"dock-workbench": "home", "dock-workbench-statistics": "statistics", "dock-orders": "orders",
|
||
"dock-payment-before-selection": "payment-empty", "dock-payment": "payment", "dock-receipts": "receipts",
|
||
"dock-preparation": "preparation", "dock-materials": "materials", "dock-material-preview": "material-preview",
|
||
"dock-delivery-methods": "delivery-method", "dock-delivery-cloud": "delivery-cloud",
|
||
"dock-delivery-nearby": "delivery-nearby", "dock-confirmation-manifest": "confirmation",
|
||
"dock-confirmation-code": "confirmation-code", "task-returning-complete": "task-completed",
|
||
"task-control-paused": "task-paused", "task-control-returning": "task-returning",
|
||
"task-control-returned": "task-returned", "task-control-pending-pause": "task-pending-pause",
|
||
"task-control-starting": "task-starting",
|
||
}
|
||
|
||
ORDER = ["home", "statistics", "orders", "payment-empty", "payment", "project-picker", "amount-edit", "receipts", "preparation",
|
||
"task-starting", "task-going", "task-ready", "task-shooting", "task-paused", "task-returning", "task-returned", "task-completed",
|
||
"task-completion-dialog", "task-failed", "task-pending-pause", "materials", "material-preview", "delivery-method",
|
||
"delivery-cloud", "delivery-nearby", "confirmation", "confirmation-code", "confirmation-completed", "live-fullscreen"]
|
||
|
||
|
||
def read_manifest(root: pathlib.Path, platform: str):
|
||
directory = root / platform
|
||
path = directory / "manifest.json"
|
||
metadata = json.loads(path.read_text()) if path.exists() else {}
|
||
if isinstance(metadata, list):
|
||
records = metadata
|
||
metadata = {}
|
||
else:
|
||
records = metadata.get("screenshots", metadata.get("screens", metadata.get("pages", [])))
|
||
files = {p.name: p for p in directory.glob("*.png")}
|
||
result = []
|
||
seen = set()
|
||
for record in records:
|
||
if isinstance(record, str):
|
||
record = {"filename": record}
|
||
filename = record.get("filename", record.get("file", record.get("path", "")))
|
||
filename = pathlib.Path(filename).name
|
||
if filename not in files:
|
||
continue
|
||
stem = pathlib.Path(filename).stem
|
||
key = record.get("pageKey", record.get("key", record.get("page", stem)))
|
||
key = ALIASES.get(key, ALIASES.get(stem, key))
|
||
item = {**record, "filename": filename, "key": key, "platform": platform,
|
||
"path": str(files[filename].relative_to(root)), "title": record.get("title", TITLES.get(key, stem))}
|
||
result.append(item)
|
||
seen.add(filename)
|
||
for filename, path in files.items():
|
||
if filename in seen:
|
||
continue
|
||
key = ALIASES.get(path.stem, path.stem)
|
||
result.append({"filename": filename, "key": key, "platform": platform,
|
||
"path": str(path.relative_to(root)), "title": TITLES.get(key, path.stem)})
|
||
used_keys = {}
|
||
for item in result:
|
||
path = root / item["path"]
|
||
raw = path.read_bytes()
|
||
width, height = struct.unpack(">II", raw[16:24])
|
||
item["dimensions"] = f"{width} × {height}"
|
||
item["sha256"] = hashlib.sha256(raw).hexdigest()
|
||
if item["key"] == "task-failed" and platform == "ios":
|
||
item["note"] = "执行失败,等待设备确认回巢;右侧为中断后已回巢,模拟状态不同。"
|
||
if item["key"] == "delivery-method":
|
||
item["note"] = "由工作台打开交付方式弹层。" if platform == "ios" else "由订单素材库打开交付方式弹层。"
|
||
count = used_keys.get(item["key"], 0) + 1
|
||
used_keys[item["key"]] = count
|
||
if count > 1:
|
||
item["original_key"] = item["key"]
|
||
item["key"] = f'{item["key"]}-extra-{count}'
|
||
return metadata, result
|
||
|
||
|
||
def pair_records(ios, android):
|
||
ios_map = {r["key"]: r for r in ios}
|
||
android_map = {r["key"]: r for r in android}
|
||
keys = list(dict.fromkeys(ORDER + list(ios_map) + list(android_map)))
|
||
return [{"key": k, "title": TITLES.get(k, (ios_map.get(k) or android_map.get(k))["title"]),
|
||
"ios": ios_map.get(k), "android": android_map.get(k)}
|
||
for k in keys if k in ios_map or k in android_map]
|
||
|
||
|
||
def build_html(root: pathlib.Path, pairs, platforms):
|
||
escaped = lambda value: html.escape(str(value), quote=True)
|
||
navigation = "".join(f'<a href="#{escaped(p["key"])}">{i:02d} {escaped(p["title"])}</a>' for i, p in enumerate(pairs, 1))
|
||
sections = []
|
||
for i, pair in enumerate(pairs, 1):
|
||
columns = []
|
||
for platform, label in [("ios", "iOS"), ("android", "Android")]:
|
||
screen = pair[platform]
|
||
if screen:
|
||
path = escaped(screen["path"])
|
||
title = escaped(pair["title"])
|
||
dimensions = screen.get("dimensions", "")
|
||
note = screen.get("note", screen.get("notes", ""))
|
||
if isinstance(note, list):
|
||
note = ";".join(note)
|
||
columns.append(f'<figure><figcaption><strong>{label}</strong><span>{escaped(dimensions)}</span></figcaption>'
|
||
f'<a href="{path}" target="_blank" aria-label="打开 {label} {title} 原图"><img src="{path}" loading="lazy" alt="{label} {title}"></a>'
|
||
f'<p class="caption">{escaped(note)}</p></figure>')
|
||
else:
|
||
columns.append(f'<figure><figcaption><strong>{label}</strong></figcaption><div class="missing">本次未提供该状态截图</div></figure>')
|
||
recommendation = pair["recommendation"]
|
||
note = recommendation.get("note", "")
|
||
note_html = f'<p class="rec-note">{escaped(note)}</p>' if note else ''
|
||
columns.append(
|
||
f'<aside class="recommendation" aria-labelledby="rec-{escaped(pair["key"])}">'
|
||
f'<h3 id="rec-{escaped(pair["key"])}">推荐以哪端为准</h3>'
|
||
f'<p class="rec-choice rec-{escaped(recommendation["baseline"])}">{escaped(recommendation["label"])}</p>'
|
||
f'<p class="rec-reason">{escaped(recommendation["reason"])}</p>'
|
||
f'<p class="rec-action"><strong>建议同步</strong>{escaped(recommendation["action"])}</p>'
|
||
f'{note_html}</aside>'
|
||
)
|
||
unmatched = '' if pair['ios'] and pair['android'] else '<span class="tag">单端补充</span>'
|
||
comparison_path = f'comparison/{i:02d}-{pair["key"]}.png'
|
||
sections.append(f'<section id="{escaped(pair["key"])}"><h2><span>{i:02d}</span> {escaped(pair["title"])} {unmatched} '
|
||
f'<a class="combined" href="{escaped(comparison_path)}" target="_blank">三列图</a></h2><div class="pair">{"".join(columns)}</div></section>')
|
||
source = escaped(json.dumps(platforms, ensure_ascii=False, indent=2))
|
||
output = '''<!doctype html>
|
||
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<title>机巢控制台 · iOS / Android UI 对比</title>
|
||
<style>
|
||
:root{font-family:-apple-system,BlinkMacSystemFont,"PingFang SC","Microsoft YaHei",sans-serif;color:#20242c;background:#f4f6f8;font-size:15px;line-height:1.6}
|
||
*{box-sizing:border-box}body{margin:0}a{color:#1556b3}header{max-width:1640px;margin:auto;padding:34px 28px 24px}h1{font-size:26px;line-height:1.3;margin:0 0 12px}header p{margin:6px 0;color:#53606f}header .links{display:flex;gap:20px;flex-wrap:wrap;margin-top:14px}
|
||
.layout{max-width:1640px;margin:auto;display:grid;grid-template-columns:208px minmax(0,1fr);gap:24px;padding:0 28px 40px}nav{position:sticky;top:16px;align-self:start;max-height:calc(100vh - 32px);overflow:auto;padding-bottom:14px}nav a{display:block;text-decoration:none;color:#3c4656;padding:7px 10px;border-radius:5px;font-size:13px}nav a:hover,nav a:focus{background:#e1e8f1}main{min-width:0}section{scroll-margin-top:18px;margin:0 0 32px;padding:18px 20px 22px;background:white;border-radius:10px;box-shadow:0 2px 10px #1c2a3e08}h2{margin:0 0 16px;font-size:18px;font-weight:600}h2>span:first-child{font-variant-numeric:tabular-nums;color:#8390a1;margin-right:5px}.combined{font-size:12px;font-weight:400;float:right;margin-top:4px}.tag{font-size:11px;color:#6a7482;background:#edf0f4;padding:3px 7px;border-radius:4px;vertical-align:2px;white-space:nowrap}.pair{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr) minmax(220px,.74fr);gap:18px;align-items:start}figure{margin:0;min-width:0}figcaption{display:flex;align-items:baseline;justify-content:space-between;gap:10px;margin-bottom:9px}figcaption span{color:#7b8795;font-size:11px}img{display:block;width:100%;height:auto;background:#f4f6f8;border:1px solid #e5e9ef}a:focus img{outline:3px solid #2b74ca;outline-offset:3px}.caption{font-size:12px;color:#738093;margin:8px 0 0}.missing{background:#f4f6f8;padding:55px 16px;text-align:center;color:#7c8795;font-size:13px}details{margin:20px 0}summary{cursor:pointer;color:#4c5b70}pre{font-size:12px;background:white;padding:16px;overflow:auto;white-space:pre-wrap;overflow-wrap:anywhere}
|
||
.recommendation{min-width:0;border-left:1px solid #dce3ec;padding-left:18px;overflow-wrap:anywhere}.recommendation h3{font-size:15px;font-weight:600;margin:0 0 12px;color:#475467}.rec-choice{display:inline-block;margin:0 0 12px;padding:5px 10px;background:#edf3fc;color:#174b91;border-radius:5px;font-size:15px;font-weight:600}.rec-keep{background:#f0f3f6;color:#475467}.rec-insufficient{background:#fff5dd;color:#805d13}.rec-reason{margin:0 0 14px;color:#273448}.rec-action{margin:0 0 14px;color:#465569}.rec-action strong{display:block;font-size:13px;margin-bottom:4px;color:#28384e}.rec-note{margin:0;color:#687587;font-size:12px;line-height:1.65}.review-scope{border-left:3px solid #b5c9e5;padding-left:12px}
|
||
@media(max-width:1240px){.layout{display:block}nav{position:static;max-height:160px;columns:3;margin-bottom:24px;background:#eaf0f6;padding:10px}}@media(max-width:900px){.pair{grid-template-columns:repeat(2,minmax(0,1fr))}.recommendation{grid-column:1/-1;border-left:0;border-top:1px solid #dce3ec;padding:16px 0 0}.recommendation h3{margin-bottom:8px}.rec-action strong{display:inline;margin-right:8px}}
|
||
@media(max-width:760px){header{padding:24px 16px}.layout{display:block;padding:0 12px 24px}nav{position:static;max-height:200px;margin-bottom:20px;background:#eaf0f6;padding:10px;columns:2}section{padding:14px 10px}.pair{gap:9px}h2{font-size:16px}figcaption{font-size:13px;display:block}figcaption span{display:block}}@media print{nav,.links{display:none}.layout{display:block}section{box-shadow:none;break-inside:avoid}header{padding:0}body{background:white}.pair{gap:20px}}
|
||
</style></head><body><header><h1>机巢控制台 · iOS / Android</h1><p>2026-11-05 · 当前源码正式页面 · 隔离模拟数据</p><p>左列 iOS,中列 Android,右列为推荐基准与理由;窄屏时建议显示在截图下方。点击截图可查看完整原图,两端按等宽展示。</p><p>iOS 使用截图专用副本,仅加入依赖替身和启动入口,正式页面布局原样保留。Android 使用当前正式 Screen 与隔离测试替身。</p><p>任务、设备、游客、图传均为本地模拟来源;两端模拟图传配置及业务数据可能不同,画面状态差异不能代表真实设备行为。二维码为本地测试图形,不对应真实付款或收货。</p><p class="review-scope">推荐仅针对本次可见的布局、文案与操作层级。两端一致时保留各端;缺图或模拟状态不同的组注明待核对。系统导航、键盘与安全区遵循平台习惯。</p><div class="links"><a href="overview.png">查看总览图</a><a href="screenshots.zip" download>下载全部截图与图集</a><a href="manifest.json">查看截图清单</a></div></header><div class="layout"><nav aria-label="页面导航">NAVIGATION</nav><main>SECTIONS<details><summary>运行环境和截图来源</summary><pre>SOURCE</pre></details></main></div></body></html>'''
|
||
(root / "index.html").write_text(output.replace("NAVIGATION", navigation).replace("SECTIONS", "".join(sections)).replace("SOURCE", source))
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument("root", type=pathlib.Path)
|
||
parser.add_argument("--render-swift", default=str(pathlib.Path(__file__).with_name("render_gallery.swift")))
|
||
parser.add_argument("--no-render", action="store_true")
|
||
args = parser.parse_args()
|
||
root = args.root.resolve()
|
||
ios_metadata, ios = read_manifest(root, "ios")
|
||
android_metadata, android = read_manifest(root, "android")
|
||
if not ios or not android:
|
||
raise SystemExit(f"Both screenshot sets required: iOS {len(ios)}, Android {len(android)}")
|
||
pairs = pair_records(ios, android)
|
||
review = json.loads((root / "recommendations.json").read_text())
|
||
recommendations = {entry["key"]: entry for entry in review["entries"]}
|
||
expected_keys = {pair["key"] for pair in pairs}
|
||
if len(recommendations) != len(review["entries"]) or set(recommendations) != expected_keys:
|
||
raise SystemExit("Recommendations must cover every comparison group exactly once")
|
||
for pair in pairs:
|
||
pair["recommendation"] = recommendations[pair["key"]]
|
||
metadata = {"ios": ios_metadata, "android": android_metadata}
|
||
manifest = {"title": "机巢控制台 iOS / Android UI 对比", "capture_date": "2026-11-05", "source": "当前正式页面 + 隔离模拟数据",
|
||
"platforms": metadata, "pair_count": len(pairs), "paired_count": sum(bool(p["ios"] and p["android"]) for p in pairs),
|
||
"ui_review": {"reviewed_at": review["reviewed_at"], "scope": review["scope"],
|
||
"baseline_counts": dict(Counter(r["baseline"] for r in recommendations.values()))},
|
||
"pairs": pairs}
|
||
(root / "manifest.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2))
|
||
build_html(root, pairs, metadata)
|
||
if not args.no_render:
|
||
subprocess.run(["swift", "-module-cache-path", "/private/tmp/drone-gallery-swift-cache", args.render_swift, str(root)], check=True)
|
||
archive = root / "screenshots.zip"
|
||
with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as target:
|
||
for path in sorted(root.rglob("*")):
|
||
if path.is_file() and path != archive and path.suffix not in {".log", ".apk", ".pyc"}:
|
||
target.write(path, path.relative_to(root))
|
||
print(json.dumps({"pairs": len(pairs), "paired": manifest["paired_count"], "ios": len(ios), "android": len(android),
|
||
"html": str(root / "index.html"), "archive_bytes": archive.stat().st_size}, ensure_ascii=False))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|