data-page重构评审后两项修补:①自定义页面护栏回填——重构砍掉的旧版最佳实践恰是手写页最需要的底线(骨架三件套/用户数据必转义优先textContent、innerHTML先过esc/SQL参数化+查error字段+加载错误态/大表LIMIT超2000行服务端报error),模板自带这层保护、AI自由发挥的custom.html没有,SKILL.md补为硬要求5-8并把检查清单扩成四项联动;②dashboard count模式去重复卡——metric为count时与固定「总条数」卡同值,只渲两张且省一条冗余COUNT(*) SQL,metric查询按isCount短路为null(渲染分支同守卫);另评审确认:模板/config/base.css三层职责切分成立,类覆盖无悬空,kanban-card改名接线一致,config缺失静默回退DEFAULT属可接受降级

This commit is contained in:
2026-09-24 09:41:20 +08:00
parent 94436ff1f1
commit a2c62717f3
9 changed files with 371 additions and 369 deletions
+27 -71
View File
@@ -4,68 +4,20 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>数据看板</title>
<link rel="stylesheet" href="./base.css">
<style>
:root {
--bg: #f5f5f7; --surface: #fff; --border: #e8e8ed; --text: #1d1d1f;
--text-2: #6e6e73; --accent: #0071e3; --accent-2: #5ac8fa; --accent-3: #af52de;
--r: 12px; --shadow: 0 1px 3px rgba(0,0,0,0.04);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", Arial, sans-serif;
background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5;
-webkit-font-smoothing: antialiased;
padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom));
}
.container { max-width: 1080px; margin: 0 auto; }
h1 { font-size: 20px; margin: 4px 0 20px; letter-spacing: -0.02em; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow); padding: 16px 18px; }
.stat .lbl { color: var(--text-2); font-size: 12px; margin-bottom: 4px; }
.stat .val { font-size: 24px; font-weight: 600; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.card { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; overflow: hidden; }
.card h2 { font-size: 14px; font-weight: 600; margin: 0 0 14px; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.05em; }
/* 表格可能横向溢出:滚动容器用负 margin 贴到卡片边缘,不撞破圆角 */
.table-scroll { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: minmax(80px, 140px) 1fr auto; gap: 12px; align-items: center; }
.bar-row .k { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row .bar { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.bar-row .bar > i { display: block; height: 100%; background: var(--accent); border-radius: 4px; transition: width .4s ease; }
.bar-row:nth-child(3n+2) .bar > i { background: var(--accent-2); }
.bar-row:nth-child(3n) .bar > i { background: var(--accent-3); }
.bar-row .v { font-size: 13px; font-variant-numeric: tabular-nums; color: var(--text-2); min-width: 40px; text-align: right; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 0; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--text-2); font-weight: 500; font-size: 12px; }
tr:last-child td { border-bottom: none; }
.msg { text-align: center; padding: 40px 20px; color: var(--text-2); }
.msg.err { color: #d70015; }
@media (max-width: 480px) {
body { padding: 8px; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
h1 { font-size: 18px; margin-bottom: 14px; }
.stat .val { font-size: 20px; }
}
/* 返回应用 FAB:固定右下、零版面占用;图标用内联 SVG 不用 ⌂ 字符(WebView 字体不保证) */
.fab { position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
width: 48px; height: 48px; border-radius: 50%; background: var(--accent); color: #fff;
border: none; box-shadow: 0 4px 12px rgba(0,113,227,0.3); cursor: pointer; z-index: 100;
display: flex; align-items: center; justify-content: center; }
.fab:hover { filter: brightness(1.08); }
.fab:active { transform: scale(0.94); }
body { padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
.container { max-width: 1080px; }
</style>
</head>
<body>
<div class="container">
<h1 id="title">数据看板</h1>
<div class="stats" id="stats"></div>
<div class="card" id="chartCard">
<div class="card card-pad">
<h2 id="chartTitle">分布</h2>
<div class="bars" id="bars"></div>
</div>
<div class="card" id="tableCard">
<div class="card card-pad">
<h2>明细</h2>
<div class="table-scroll" id="table"></div>
</div>
@@ -75,17 +27,19 @@ body { padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 10.5 12 3l9 7.5"/><path d="M5 9.5V21h14V9.5"/></svg>
</button>
<script src="./config-dashboard.js"></script>
<script>
const CONFIG = {
table: 'items',
pk: 'id',
title: '数据看板',
groupBy: 'category', // 分组维度(柱状图用)
sumColumn: 'qty', // 求和的数值列
sumLabel: '总量',
detailColumns: ['name', 'category', 'qty'], // 明细表展示的列
const DEFAULT = {
table: 'items', pk: 'id', title: '数据看板',
groupBy: 'category',
metric: { type: 'count' },
detailColumns: ['name', 'category', 'qty'],
detailLimit: 20,
};
const CONFIG = Object.assign({}, DEFAULT, window.PAGE_CONFIG || {});
if (window.PAGE_CONFIG && window.PAGE_CONFIG.metric) {
CONFIG.metric = Object.assign({}, DEFAULT.metric, window.PAGE_CONFIG.metric);
}
const API = `/api/db/${location.pathname.split('/').filter(Boolean)[1]}/query`;
const esc = s => String(s ?? '').replace(/[&<>"']/g, c =>
@@ -104,20 +58,24 @@ async function q(sql, params = []) {
async function init() {
document.getElementById('title').textContent = CONFIG.title;
const isCount = CONFIG.metric.type !== 'sum';
const expr = isCount ? 'COUNT(*)' : `SUM(${CONFIG.metric.column})`;
const metricLabel = isCount ? '总条数' : (CONFIG.metric.label || `合计 ${CONFIG.metric.column}`);
try {
const [cnt, sum, group, detail] = await Promise.all([
const [cnt, metric, group, detail] = await Promise.all([
q(`SELECT COUNT(*) AS v FROM ${CONFIG.table}`),
q(`SELECT SUM(${CONFIG.sumColumn}) AS v FROM ${CONFIG.table}`),
q(`SELECT ${CONFIG.groupBy} AS k, SUM(${CONFIG.sumColumn}) AS v
isCount ? null : q(`SELECT ${expr} AS v FROM ${CONFIG.table}`),
q(`SELECT ${CONFIG.groupBy} AS k, ${expr} AS v
FROM ${CONFIG.table} GROUP BY ${CONFIG.groupBy} ORDER BY v DESC LIMIT 20`),
q(`SELECT * FROM ${CONFIG.table} LIMIT ${CONFIG.detailLimit}`),
]);
document.getElementById('stats').innerHTML = `
<div class="stat"><div class="lbl">总条数</div><div class="val">${cnt[0]?.v ?? 0}</div></div>
<div class="stat"><div class="lbl">${esc(CONFIG.sumLabel)}</div><div class="val">${sum[0]?.v ?? 0}</div></div>
<div class="stat"><div class="lbl">分组数</div><div class="val">${group.length}</div></div>
`;
// count 模式下 metric 就是总条数,不重复渲染第三张卡(也省一条同值 SQL);sum 模式才三张
const statsCards = [`<div class="stat"><div class="lbl">总条数</div><div class="val">${cnt[0]?.v ?? 0}</div></div>`];
if (!isCount) statsCards.push(`<div class="stat"><div class="lbl">${esc(metricLabel)}</div><div class="val">${metric[0]?.v ?? 0}</div></div>`);
statsCards.push(`<div class="stat"><div class="lbl">分组数</div><div class="val">${group.length}</div></div>`);
document.getElementById('stats').innerHTML = statsCards.join('');
document.getElementById('chartTitle').textContent = `按 ${CONFIG.groupBy} 分布`;
const max = Math.max(...group.map(g => g.v || 0), 1);
@@ -140,9 +98,7 @@ async function init() {
document.getElementById('table').innerHTML = '';
}
}
// 返回应用=回到发起本页的对话:主界面深链 /?s=<会话id>(数据页的 task_id 就是会话 id,
// engine 里 1:1),前端启动时消费一次打开该会话。刻意不用 history.back():多页数据应用里
// back 退的是上一张数据页——那是「上一页」语义,归顶部「返回导航」管。
function backToApp() {
const id = location.pathname.split('/').filter(Boolean)[1];
location.href = id ? '/?s=' + encodeURIComponent(id) : '/';