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:
+75
-120
@@ -1,6 +1,6 @@
|
||||
---
|
||||
name: data-page
|
||||
description: 数据页面脚手架:把 table/dashboard/kanban 模板复制到任务目录 .app/templates/,配合 db 工具产出 /app/{task_id}/ 可访问、经 /api/db/{task_id}/query 读写 data.db 的查看/管理页
|
||||
description: 数据页面脚手架:把 table/dashboard/kanban 模板复制到任务目录 .app/,配合 db 工具产出 /app/{task_id}/ 可访问、经 /api/db/{task_id}/query 读写 data.db 的查看/管理页
|
||||
visit_url: /app/{task_id}/
|
||||
---
|
||||
|
||||
@@ -24,14 +24,20 @@ visit_url: /app/{task_id}/
|
||||
- 数据库:任务目录下的 `data.db`
|
||||
- 页面目录:任务目录下的 `.app/`(带点目录,任务文件面板默认隐藏;URL 路径与目录名在路由层解耦)
|
||||
- 页面入口:`.app/index.html`
|
||||
- 访问地址:`/app/{task_id}/`(注意:URL 无点、目录有点,别写错路径;完整 URL = 站点地址 + 该路径,run 输出末尾的「访问地址」已拼好,直接转发)
|
||||
- 访问地址:`/app/{task_id}/`(URL 无点、目录有点,别写错;完整 URL = 站点地址 + 该路径,run 输出末尾的「访问地址」已拼好,直接转发)
|
||||
- 数据 API:`/api/db/{task_id}/query`
|
||||
|
||||
`{task_id}` 是任务目录的名称。页面通过 URL 动态解析,不要硬编码。
|
||||
|
||||
## 初始化
|
||||
|
||||
生成页面前,调用 `skill(name="data-page", op="run")`(无需参数,模板固定复制到 `.app/templates/`)。已存在则跳过,不会覆盖修改过的文件(重跑幂等)。
|
||||
调用 `skill(name="data-page", op="run")`,无需参数。它会把以下文件复制到 `.app/`:
|
||||
|
||||
- `table.html` / `dashboard.html` / `kanban.html` —— 三个页面模板
|
||||
- `base.css` —— 共用样式
|
||||
- `config-table.js` / `config-dashboard.js` / `config-kanban.js` —— 配置骨架
|
||||
|
||||
已存在则跳过,不会覆盖(重跑幂等)。
|
||||
|
||||
## 数据 API
|
||||
|
||||
@@ -73,130 +79,77 @@ const taskId = location.pathname.split('/').filter(Boolean)[1];
|
||||
|
||||
1. **明确数据结构**:与用户确认字段。如果描述模糊,可以建议合理的结构。
|
||||
2. **创建表并录入数据**:用 `db` 工具完成。
|
||||
3. **初始化模板**:调用 `skill(name="data-page", op="run")`(无参数)。
|
||||
4. **生成页面**:从 `.app/templates/` 选合适的模板,根据表结构调整字段和 SQL,写入 `.app/index.html`。
|
||||
5. **告知用户**:把 run 输出末尾「访问地址」那行的**完整 URL**(含域名)发给用户——只给 `/app/...` 相对路径用户打不开;再简要说明页面功能。
|
||||
3. **初始化**:调用 `skill(name="data-page", op="run")`。
|
||||
4. **为每个要生成的页面写 config 文件**(见下一节)。**不要读也不要改模板本身**。
|
||||
5. **选定入口页**:把要作为首页的模板复制为 `.app/index.html`(同目录复制,引用路径不变),或直接保留多个页面并在 index.html 里做导航。
|
||||
6. **告知用户**:把 run 输出末尾「访问地址」那行的完整 URL 发给用户,再简要说明页面功能。
|
||||
|
||||
页面必须包含:
|
||||
默认只写 config 就够。模板无法覆盖需求时,才走自定义路径(见「自定义页面」一节)。
|
||||
|
||||
- HTML 骨架(`<!DOCTYPE html>`、`<meta viewport>`、UTF-8)
|
||||
- 桌面优先、移动兼容的样式:容器宽度按页面类型给足(表格类 1200px、看板类 1400px 或不设、统计类 1080px),不要照搬 800px 级别的移动端尺寸
|
||||
- 通过 `fetch` 调用数据 API
|
||||
- 错误处理和加载状态
|
||||
- **返回应用入口**:右下角 FAB(房子图标),`backToApp()` 跳主界面深链 `/?s=<task_id>`——数据页面的 task_id 就是会话 id,一点直达发起本页的对话;若未来 APK 提供 JS 桥,只需替换 `backToApp()` 一处。**每一个 `.app/*.html` 页面(含 index.html)都要带**——用户可能从任意子页直入,缺出口即被困住
|
||||
## 页面配置
|
||||
|
||||
## 模板
|
||||
三个模板对应三份 config:
|
||||
|
||||
模板位于 `.app/templates/`。
|
||||
| 模板 | config 文件 | 用途 |
|
||||
|---|---|---|
|
||||
| `table.html` | `config-table.js` | 清单、列表、增删改 |
|
||||
| `dashboard.html` | `config-dashboard.js` | 统计、汇总、明细 |
|
||||
| `kanban.html` | `config-kanban.js` | 按字段分列,状态流转 |
|
||||
|
||||
- **table.html**:标准表格,适合清单、列表。含排序、搜索、分页。
|
||||
- **dashboard.html**:统计看板,顶部卡片 + 简单图表 + 明细表。
|
||||
- **kanban.html**:分组看板,按字段分列展示。
|
||||
每份 config 的结构:
|
||||
|
||||
优先使用模板。模板都不合适时才从零写。
|
||||
|
||||
## 最小示例
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>采购清单</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; margin: 0; padding: 16px; background: #f5f5f5; }
|
||||
h1 { font-size: 20px; margin: 0 0 16px; }
|
||||
.card { background: white; border-radius: 12px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
th, td { padding: 8px; text-align: left; border-bottom: 1px solid #eee; font-size: 14px; }
|
||||
th { color: #666; font-weight: 500; }
|
||||
.empty { text-align: center; color: #999; padding: 32px; }
|
||||
.error { color: #d33; padding: 12px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>采购清单</h1>
|
||||
<div class="card" id="content">加载中…</div>
|
||||
|
||||
<script>
|
||||
const taskId = location.pathname.split('/').filter(Boolean)[1];
|
||||
const API = `/api/db/${taskId}/query`;
|
||||
|
||||
async function query(sql, params = []) {
|
||||
const res = await fetch(API, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({sql, params})
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.error) throw new Error(data.error);
|
||||
return data;
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
return String(s ?? '').replace(/[&<>"']/g, c => ({
|
||||
'&':'&','<':'<','>':'>','"':'"',"'":'''
|
||||
}[c]));
|
||||
}
|
||||
|
||||
async function render() {
|
||||
const el = document.getElementById('content');
|
||||
try {
|
||||
const rows = await query('SELECT * FROM parts ORDER BY id');
|
||||
if (!rows.length) {
|
||||
el.innerHTML = '<div class="empty">暂无数据</div>';
|
||||
return;
|
||||
}
|
||||
el.innerHTML = `<table>
|
||||
<thead><tr><th>名称</th><th>规格</th><th>类别</th><th>数量</th><th>单位</th><th>备注</th></tr></thead>
|
||||
<tbody>${rows.map(r => `<tr>
|
||||
<td>${escapeHtml(r.name)}</td>
|
||||
<td>${escapeHtml(r.spec)}</td>
|
||||
<td>${escapeHtml(r.category)}</td>
|
||||
<td>${escapeHtml(r.qty)}</td>
|
||||
<td>${escapeHtml(r.unit)}</td>
|
||||
<td>${escapeHtml(r.note)}</td>
|
||||
</tr>`).join('')}</tbody>
|
||||
</table>`;
|
||||
} catch (e) {
|
||||
el.innerHTML = `<div class="error">加载失败:${escapeHtml(e.message)}</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
render();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
```javascript
|
||||
window.PAGE_CONFIG = {
|
||||
table: '表名',
|
||||
pk: 'id',
|
||||
title: '页面标题',
|
||||
// ...字段见下
|
||||
};
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
只写实际需要的字段,其余用模板默认值。
|
||||
|
||||
**前端**
|
||||
### config-table.js 字段
|
||||
|
||||
- 用原生 JS + fetch,不引入框架
|
||||
- 样式用内联 `<style>` 或独立 CSS,不依赖外部 CSS 库
|
||||
- 图表用纯 SVG 或 CSS 绘制
|
||||
- 桌面优先、移动兼容:数据页面主要在电脑上看,窄屏靠横向滚动兑底
|
||||
- 所有可能横向溢出的内容(表格、长文本、代码)包一层 `overflow-x: auto` 容器;卡片内滚动可配合负 margin 贴边
|
||||
- flex 居中用 `width: fit-content; margin: 0 auto`,不用 `justify-content: center`(内容超出滚动容器时居中会裁掉首屏左侧)
|
||||
- 显示加载状态和错误提示
|
||||
- 提交类按钮加禁用状态,避免重复点击
|
||||
- `table`、`pk`、`title`、`pageSize`、`editable`
|
||||
- `labels`:`{ 列名: '中文标签' }`,未列出的列用原名
|
||||
- `numCols`:`['数值列名', ...]`,数字列右对齐
|
||||
- `options`:`{ 列名: ['枚举值1', '枚举值2'] }`,编辑时用下拉
|
||||
- `pills`:`{ 列名: { '值': 'ok|ship|pay|refund' } }`,状态着色
|
||||
|
||||
**数据库**
|
||||
### config-dashboard.js 字段
|
||||
|
||||
- 使用参数化查询
|
||||
- 大表查询加 `LIMIT`
|
||||
- `table`、`pk`、`title`、`groupBy`、`detailColumns`、`detailLimit`
|
||||
- `metric`:`{ type: 'count' }` 或 `{ type: 'sum', column: '列名', label: '标签' }`
|
||||
|
||||
**安全**
|
||||
### config-kanban.js 字段
|
||||
|
||||
- 用户输入渲染到 HTML 时用 `textContent`,或用 `escapeHtml` 转义后再用 `innerHTML`
|
||||
- `table`、`pk`、`title`
|
||||
- `groupField`:分列字段
|
||||
- `groups`:`['列1', '列2', ...]`,列的显示顺序
|
||||
- `titleField`:卡片主标题字段
|
||||
- `metaFields`:`['字段1', '字段2']`,卡片上显示的次要信息
|
||||
|
||||
**用户体验**
|
||||
## 自定义页面
|
||||
|
||||
- 页面标题反映数据内容
|
||||
- 空数据时显示友好提示
|
||||
- 操作有反馈(保存成功、删除确认)
|
||||
只有当模板无法覆盖需求时(特殊图表、多表关联、自定义交互等)才使用。
|
||||
|
||||
自定义页面必须满足:
|
||||
|
||||
1. 使用独立文件名(如 `.app/custom.html`)
|
||||
2. 引用 `./base.css` 继承统一视觉
|
||||
3. 提供返回会话入口(FAB,跳 `/?s=<task_id>`)
|
||||
4. 在回复中说明为什么没用模板
|
||||
5. HTML 骨架完整:`<!DOCTYPE html>`、`<meta charset="UTF-8">`、`<meta name="viewport" content="width=device-width, initial-scale=1">`
|
||||
6. 用户数据渲染必转义:优先 `textContent`;用 `innerHTML` 先过 `esc()`(照抄模板里的实现),绝不把 API 查询结果裸拼进 HTML——模板自带转义,手写页面没有这层保护
|
||||
7. 所有 SQL 参数化(`?`);fetch 结果检查 `error` 字段(失败仍是 HTTP 200),并有加载态/错误提示
|
||||
8. 大表查询加 `LIMIT`:服务端单次超 2000 行直接报 error
|
||||
|
||||
`.app/` 下每个 HTML 页面(含 index.html)都要有返回会话入口——用户可能从任意子页直入,缺出口即被困住。
|
||||
|
||||
## 视觉规范
|
||||
|
||||
配色、圆角、字体、阴影、容器宽度、FAB、看板卡片等基础样式由 `base.css` 统一提供,模板和自定义页面都引用它。自定义页面不要重写这些基础样式。
|
||||
|
||||
## 常见错误
|
||||
|
||||
@@ -205,16 +158,18 @@ const taskId = location.pathname.split('/').filter(Boolean)[1];
|
||||
3. 没检查 `error` 字段 —— API 错误时仍返回 200
|
||||
4. 表名或字段名拼错
|
||||
5. 引入 CDN 外部库 —— 容器可能无网络,优先纯原生
|
||||
6. 页面文件不在 `.app/` 下 —— 无法通过 `/app/{task_id}/` 访问(目录带点、URL 不带点,两者别写反)
|
||||
7. SQL 占位符与参数数量不匹配 —— 优先用结构化 insert,避免手写
|
||||
6. 页面文件不在 `.app/` 下 —— 无法通过 `/app/{task_id}/` 访问(目录带点、URL 不带点)
|
||||
7. SQL 占位符与参数数量不匹配 —— 优先用结构化 insert
|
||||
8. 读或改模板文件 —— 应只写 config
|
||||
9. 自定义页面没引用 `base.css` —— 视觉会和其它页面割裂
|
||||
|
||||
## 检查清单
|
||||
|
||||
- [ ] 数据库表已创建
|
||||
- [ ] 数据已录入
|
||||
- [ ] `.app/index.html` 已写入
|
||||
- [ ] 数据库表已创建、数据已录入
|
||||
- [ ] 调用了 `skill run`
|
||||
- [ ] 写好了对应的 `config-*.js`
|
||||
- [ ] 选定入口页(复制为 `.app/index.html` 或保留多页 + 导航)
|
||||
- [ ] 页面动态解析 task_id
|
||||
- [ ] 包含错误处理和加载状态
|
||||
- [ ] 用户输入渲染时已转义
|
||||
- [ ] 每个 `.app/*.html` 页面右下角都有「返回会话」FAB(backToApp → `/?s=<task_id>` 深链)
|
||||
- [ ] 已告知用户完整访问 URL(run 输出「访问地址」那行,含域名,非相对路径)
|
||||
- [ ] 每个 `.app/*.html` 都有返回会话入口
|
||||
- [ ] 自定义页面:引用了 `base.css`、数据渲染已转义、SQL 参数化、大表加 LIMIT
|
||||
- [ ] 已告知用户完整访问 URL(含域名)
|
||||
|
||||
+39
-22
@@ -1,12 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
# data-page 技能入口:把技能自带 templates/*.html 复制到会话任务目录的 .app/templates/。
|
||||
# 目标目录固定(SKILL.md 环境约定:页面必须在 .app/ 下才能被 /app/{task_id}/ 访问;
|
||||
# 带点是为了任务文件面板默认隐藏,URL 路径与目录名已在路由层解耦)。
|
||||
# 不开 dest 参数:manager 对已是绝对路径的参数不做沙箱校验,开放 dest = 开放任意目录写入;
|
||||
# 且路径写死在文档流程里,可配置只会造成漂移。
|
||||
# 已存在的同名文件跳过不覆盖(AI 改过的模板要能经受重跑);纯 stdlib,无 requirements。
|
||||
# cwd 契约:远端模式(生产)cwd 即会话任务目录;本地模式 cwd 是技能目录——
|
||||
# 那会把模板抄进技能家,检测到直接报错拒跑(手动调试请 cd 到任务目录再运行)。
|
||||
# data-page 技能入口:把技能自带 templates/ 下的文件复制到会话任务目录的 .app/。
|
||||
# 目标目录固定为 <任务目录>/.app/(不带 dest 参数:manager 对绝对路径参数不做沙箱
|
||||
# 校验,开放 dest 等于开放任意目录写入;路径也已写死在文档流程里,可配置只会造成漂移)。
|
||||
# 复制内容:
|
||||
# *.html → .app/ 页面模板(table / dashboard / kanban)
|
||||
# base.css → .app/ 共用样式
|
||||
# config-*.js → .app/ 配置骨架(AI 只改这些)
|
||||
# 已存在的同名文件跳过不覆盖(重跑幂等,AI 改过的 config 不会被覆盖回默认)。
|
||||
# 纯 stdlib,无 requirements。
|
||||
# cwd 契约:生产模式下 cwd 即会话任务目录;本地直跑时 cwd 是技能目录——
|
||||
# 那会把模板抄进技能家,检测到直接报错拒跑(手动调试请 cd 到任务目录再跑)。
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
@@ -15,48 +18,62 @@ from pathlib import Path
|
||||
|
||||
|
||||
def main():
|
||||
# ── 参数校验:本技能无参数 ─────────────────────────────
|
||||
if len(sys.argv) >= 2 and sys.argv[1].strip():
|
||||
try:
|
||||
args = json.loads(sys.argv[1])
|
||||
if not isinstance(args, dict):
|
||||
raise ValueError('input_args 必须是 JSON 对象')
|
||||
if args:
|
||||
print('本技能无参数(模板固定复制到任务目录 .app/templates/),收到:'
|
||||
+ json.dumps(args, ensure_ascii=False), file=sys.stderr)
|
||||
print('本技能无参数,收到:' + json.dumps(args, ensure_ascii=False),
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
except (ValueError, json.JSONDecodeError) as e:
|
||||
print('参数解析失败:' + str(e), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
# ── 源目录:技能自带的 templates/ ──────────────────────
|
||||
skill_dir = Path(__file__).resolve().parent
|
||||
src = skill_dir / 'templates'
|
||||
if not src.is_dir():
|
||||
print('技能自身缺少 templates/ 目录:' + str(src), file=sys.stderr)
|
||||
return 1
|
||||
tpl = sorted(src.glob('*.html'))
|
||||
if not tpl:
|
||||
print('templates/ 下没有 .html 模板', file=sys.stderr)
|
||||
|
||||
files = sorted(
|
||||
list(src.glob('*.html')) +
|
||||
list(src.glob('*.css')) +
|
||||
list(src.glob('config-*.js'))
|
||||
)
|
||||
if not files:
|
||||
print('templates/ 下没有可复制的文件', file=sys.stderr)
|
||||
return 1
|
||||
|
||||
dest = (Path.cwd() / '.app' / 'templates').resolve()
|
||||
if str(dest) == str(skill_dir) or str(dest).startswith(str(skill_dir) + os.sep):
|
||||
# ── 目标目录:<任务目录>/.app/ ─────────────────────────
|
||||
app_dir = (Path.cwd() / '.app').resolve()
|
||||
if str(app_dir) == str(skill_dir) or str(app_dir).startswith(str(skill_dir) + os.sep):
|
||||
print('当前工作目录不是会话任务目录(本地直跑 cwd=技能目录会污染技能;'
|
||||
'生产经 executor 运行天然正确,手动调试请 cd 到任务目录再跑)', file=sys.stderr)
|
||||
'生产经 executor 运行天然正确,手动调试请 cd 到任务目录再跑)',
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
|
||||
dest.mkdir(parents=True, exist_ok=True)
|
||||
app_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# ── 逐个复制,已存在则跳过 ─────────────────────────────
|
||||
copied, skipped = [], []
|
||||
for f in tpl:
|
||||
target = dest / f.name
|
||||
for f in files:
|
||||
target = app_dir / f.name
|
||||
if target.exists():
|
||||
skipped.append(f.name)
|
||||
else:
|
||||
shutil.copy2(f, target)
|
||||
copied.append(f.name)
|
||||
|
||||
print('模板目录:' + str(dest))
|
||||
print('新复制 ' + str(len(copied)) + ' 个:' + (', '.join(copied) if copied else '无'))
|
||||
print('跳过(已存在,不覆盖)' + str(len(skipped)) + ' 个:' + (', '.join(skipped) if skipped else '无'))
|
||||
# ── 输出结果 ───────────────────────────────────────────
|
||||
print('页面目录:' + str(app_dir))
|
||||
print('新复制 ' + str(len(copied)) + ' 个:'
|
||||
+ (', '.join(copied) if copied else '无'))
|
||||
print('跳过(已存在,不覆盖)' + str(len(skipped)) + ' 个:'
|
||||
+ (', '.join(skipped) if skipped else '无'))
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
/* ── 设计变量 ─────────────────────────────────────────── */
|
||||
:root {
|
||||
--bg: #f5f5f7; --surface: #fff; --border: #e8e8ed; --text: #1d1d1f;
|
||||
--text-2: #6e6e73; --accent: #0071e3; --accent-h: #0077ed; --danger: #d70015;
|
||||
--accent-2: #5ac8fa; --accent-3: #af52de;
|
||||
--ok: #34c759; --warn: #ff9f0a; --bad: #ff3b30;
|
||||
--r: 12px; --r-sm: 8px;
|
||||
--shadow: 0 1px 3px rgba(0,0,0,0.04);
|
||||
--shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
|
||||
}
|
||||
|
||||
/* ── 基础 ─────────────────────────────────────────────── */
|
||||
* { 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(72px + env(safe-area-inset-bottom));
|
||||
}
|
||||
.container { max-width: 1200px; margin: 0 auto; }
|
||||
h1 { font-size: 20px; margin: 4px 0 20px; letter-spacing: -0.02em; }
|
||||
|
||||
/* ── 卡片 ─────────────────────────────────────────────── */
|
||||
.card { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow); overflow: hidden; }
|
||||
.card-pad { 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; }
|
||||
|
||||
/* ── 按钮 ─────────────────────────────────────────────── */
|
||||
.btn { appearance: none; border: none; background: var(--accent); color: #fff; font: inherit; font-weight: 500;
|
||||
padding: 7px 14px; border-radius: 999px; cursor: pointer; transition: background .15s, transform .05s; }
|
||||
.btn:hover { background: var(--accent-h); }
|
||||
.btn:active { transform: scale(0.97); }
|
||||
|
||||
/* ── 搜索框 ───────────────────────────────────────────── */
|
||||
.search { width: 100%; border: 1px solid var(--border); background: var(--bg); border-radius: var(--r-sm);
|
||||
padding: 8px 12px; font: inherit; outline: none; transition: border-color .15s, background .15s; }
|
||||
.search:focus { border-color: var(--accent); background: var(--surface); }
|
||||
|
||||
/* ── 状态提示 ─────────────────────────────────────────── */
|
||||
.msg { text-align: center; padding: 56px 20px; color: var(--text-2); }
|
||||
.msg.err { color: var(--danger); }
|
||||
|
||||
/* ── 状态 pill ────────────────────────────────────────── */
|
||||
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px;
|
||||
font-size: 12px; background: var(--bg); color: var(--text-2); }
|
||||
.pill.ok { color: var(--ok); background: rgba(52,199,89,.12); }
|
||||
.pill.ship { color: #0a84ff; background: rgba(90,200,250,.18); }
|
||||
.pill.pay { color: var(--warn); background: rgba(255,159,10,.15); }
|
||||
.pill.refund { color: var(--bad); background: rgba(255,59,48,.12); }
|
||||
|
||||
/* ── 表格 ─────────────────────────────────────────────── */
|
||||
.tw { overflow-x: auto; }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
th, td { padding: 10px 18px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
|
||||
th { color: var(--text-2); font-weight: 500; font-size: 13px; cursor: pointer; user-select: none; }
|
||||
th:hover { color: var(--text); }
|
||||
th .ar { font-size: 10px; margin-left: 4px; opacity: .7; }
|
||||
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
|
||||
td.ed { cursor: text; }
|
||||
td.ed:hover { background: rgba(0,113,227,0.04); }
|
||||
tr:last-child td { border-bottom: none; }
|
||||
.col-ac { width: 40px; }
|
||||
.del { appearance: none; background: none; border: none; color: var(--text-2); cursor: pointer;
|
||||
font-size: 18px; line-height: 1; padding: 2px 8px; border-radius: 6px; }
|
||||
.del:hover { color: var(--danger); background: rgba(215,0,21,0.08); }
|
||||
|
||||
/* ── 分页 ─────────────────────────────────────────────── */
|
||||
.pager { display: flex; align-items: center; justify-content: center; gap: 14px;
|
||||
padding: 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--text-2); }
|
||||
.pager button { appearance: none; border: 1px solid var(--border); background: var(--surface); font: inherit;
|
||||
padding: 5px 12px; border-radius: 999px; cursor: pointer; }
|
||||
.pager button:disabled { opacity: .4; cursor: default; }
|
||||
|
||||
/* ── 对话框表单 ───────────────────────────────────────── */
|
||||
dialog { border: none; border-radius: var(--r); padding: 0; box-shadow: var(--shadow-lg);
|
||||
width: min(420px, calc(100vw - 32px)); }
|
||||
dialog::backdrop { background: rgba(0,0,0,0.35); }
|
||||
.form { padding: 22px; }
|
||||
.form h3 { margin: 0 0 18px; font-size: 16px; }
|
||||
.fld { margin-bottom: 14px; }
|
||||
.fld label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 4px; }
|
||||
.fld input, .fld select { width: 100%; border: 1px solid var(--border); border-radius: var(--r-sm);
|
||||
padding: 8px 10px; font: inherit; outline: none; transition: border-color .15s;
|
||||
background: var(--surface); color: var(--text); }
|
||||
.fld input:focus, .fld select:focus { border-color: var(--accent); }
|
||||
.acts { display: flex; gap: 8px; justify-content: flex-end; margin-top: 22px; }
|
||||
.acts button { font: inherit; padding: 8px 16px; border-radius: 999px; cursor: pointer;
|
||||
border: 1px solid var(--border); background: var(--surface); color: var(--text); }
|
||||
.acts .pri { background: var(--accent); color: #fff; border-color: var(--accent); }
|
||||
.acts .pri:hover { background: var(--accent-h); }
|
||||
|
||||
/* ── 统计卡片 ─────────────────────────────────────────── */
|
||||
.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; }
|
||||
|
||||
/* ── 横向条 ───────────────────────────────────────────── */
|
||||
.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-scroll { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
|
||||
|
||||
/* ── 看板 ─────────────────────────────────────────────── */
|
||||
.board-outer { overflow-x: auto; padding-bottom: 8px; scroll-snap-type: x proximity; }
|
||||
.board { display: flex; gap: 12px; width: fit-content; margin: 0 auto; }
|
||||
.col { flex: 0 0 260px; scroll-snap-align: start; background: var(--surface); border-radius: var(--r);
|
||||
box-shadow: var(--shadow); display: flex; flex-direction: column; max-height: calc(100vh - 140px); }
|
||||
.col-hd { display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 13px; font-weight: 600; }
|
||||
.col-hd .n { color: var(--text-2); font-weight: 400; font-size: 12px;
|
||||
background: var(--bg); padding: 1px 8px; border-radius: 999px; }
|
||||
.col-bd { padding: 10px; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; }
|
||||
.kanban-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
|
||||
display: flex; flex-direction: column; gap: 8px; transition: box-shadow .15s; }
|
||||
.kanban-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
|
||||
.kanban-card .t { font-size: 13px; font-weight: 500; line-height: 1.4; }
|
||||
.kanban-card .meta { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||
.kanban-card .meta span { font-size: 11px; color: var(--text-2); background: var(--bg); padding: 2px 7px; border-radius: 999px; }
|
||||
.kanban-card .mv { display: flex; gap: 6px; margin-top: 2px; }
|
||||
.kanban-card .mv button { appearance: none; border: 1px solid var(--border); background: var(--surface);
|
||||
color: var(--text-2); cursor: pointer; font: inherit; font-size: 12px; padding: 3px 9px;
|
||||
border-radius: 6px; transition: all .15s; }
|
||||
.kanban-card .mv button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
|
||||
.kanban-card .mv button:disabled { opacity: .3; cursor: default; }
|
||||
.col-bd .empty { text-align: center; color: var(--text-2); padding: 20px 8px; font-size: 12px; }
|
||||
|
||||
/* ── 返回应用 FAB ─────────────────────────────────────── */
|
||||
.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); }
|
||||
|
||||
/* ── 响应式 ───────────────────────────────────────────── */
|
||||
@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; }
|
||||
th, td { padding: 8px 12px; font-size: 13px; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// 统计看板配置 —— 按你的表结构修改
|
||||
window.PAGE_CONFIG = {
|
||||
table: 'items',
|
||||
pk: 'id',
|
||||
title: '数据看板',
|
||||
groupBy: 'category', // 分组维度
|
||||
// 指标定义:count = 计数(默认);sum = 对某列求和
|
||||
metric: { type: 'count' },
|
||||
// metric: { type: 'sum', column: 'amount', label: '总金额' },
|
||||
detailColumns: ['name', 'category', 'qty'], // 明细表展示的列
|
||||
detailLimit: 20,
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
// 分列看板配置 —— 按你的表结构修改
|
||||
window.PAGE_CONFIG = {
|
||||
table: 'tasks',
|
||||
pk: 'id',
|
||||
title: '任务看板',
|
||||
groupField: 'status', // 用来分列的字段
|
||||
groups: ['待办', '进行中', '已完成'], // 列的显示顺序
|
||||
titleField: 'title', // 卡片主标题字段
|
||||
metaFields: ['assignee', 'due'], // 卡片上显示的次要信息
|
||||
};
|
||||
@@ -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) : '/';
|
||||
|
||||
@@ -4,57 +4,9 @@
|
||||
<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; --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));
|
||||
}
|
||||
h1 { font-size: 20px; margin: 4px 0 20px; letter-spacing: -0.02em; }
|
||||
.container { max-width: 1400px; margin: 0 auto; }
|
||||
/* 外层滚、内层居中:board 用 fit-content+auto margin——列少居中、列多靠边滚动;
|
||||
若在滚动容器上直接 justify-content:center,溢出时首列会被裁掉(滚动起点落在中间)。 */
|
||||
.board-outer { overflow-x: auto; padding-bottom: 8px; scroll-snap-type: x proximity; }
|
||||
.board { display: flex; gap: 12px; width: fit-content; margin: 0 auto; }
|
||||
.col { flex: 0 0 260px; scroll-snap-align: start; background: var(--surface); border-radius: var(--r);
|
||||
box-shadow: var(--shadow); display: flex; flex-direction: column; max-height: calc(100vh - 140px); }
|
||||
.col-hd { display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 13px; font-weight: 600; }
|
||||
.col-hd .n { color: var(--text-2); font-weight: 400; font-size: 12px;
|
||||
background: var(--bg); padding: 1px 8px; border-radius: 999px; }
|
||||
.col-bd { padding: 10px; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; }
|
||||
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
|
||||
display: flex; flex-direction: column; gap: 8px; transition: box-shadow .15s, transform .05s; }
|
||||
.card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
|
||||
.card .t { font-size: 13px; font-weight: 500; line-height: 1.4; }
|
||||
.card .meta { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||
.card .meta span { font-size: 11px; color: var(--text-2); background: var(--bg); padding: 2px 7px; border-radius: 999px; }
|
||||
.card .mv { display: flex; gap: 6px; margin-top: 2px; }
|
||||
.card .mv button { appearance: none; border: 1px solid var(--border); background: var(--surface);
|
||||
color: var(--text-2); cursor: pointer; font: inherit; font-size: 12px; padding: 3px 9px;
|
||||
border-radius: 6px; transition: all .15s; }
|
||||
.card .mv button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
|
||||
.card .mv button:disabled { opacity: .3; cursor: default; }
|
||||
.col-bd .empty { text-align: center; color: var(--text-2); padding: 20px 8px; font-size: 12px; }
|
||||
.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; } }
|
||||
/* 返回应用 FAB:固定右下、零版面占用(看板列高按视口算,footer 会直接吃掉一截);
|
||||
图标用内联 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: 1400px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -67,16 +19,16 @@ 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-kanban.js"></script>
|
||||
<script>
|
||||
const CONFIG = {
|
||||
table: 'tasks',
|
||||
pk: 'id',
|
||||
title: '任务看板',
|
||||
groupField: 'status', // 用来分列的字段
|
||||
groups: ['待办', '进行中', '已完成'], // 列的顺序
|
||||
titleField: 'title', // 卡片主标题字段
|
||||
metaFields: ['assignee', 'due'], // 卡片上显示的次要字段
|
||||
const DEFAULT = {
|
||||
table: 'tasks', pk: 'id', title: '任务看板',
|
||||
groupField: 'status',
|
||||
groups: ['待办', '进行中', '已完成'],
|
||||
titleField: 'title',
|
||||
metaFields: ['assignee', 'due'],
|
||||
};
|
||||
const CONFIG = Object.assign({}, DEFAULT, window.PAGE_CONFIG || {});
|
||||
|
||||
const API = `/api/db/${location.pathname.split('/').filter(Boolean)[1]}/query`;
|
||||
const esc = s => String(s ?? '').replace(/[&<>"']/g, c =>
|
||||
@@ -124,7 +76,7 @@ function card(r, gi) {
|
||||
const meta = CONFIG.metaFields
|
||||
.filter(f => r[f] != null && r[f] !== '')
|
||||
.map(f => `<span>${esc(r[f])}</span>`).join('');
|
||||
return `<div class="card" data-pk="${esc(r[CONFIG.pk])}">
|
||||
return `<div class="kanban-card" data-pk="${esc(r[CONFIG.pk])}">
|
||||
<div class="t">${esc(r[CONFIG.titleField] ?? '(无标题)')}</div>
|
||||
${meta ? `<div class="meta">${meta}</div>` : ''}
|
||||
<div class="mv">
|
||||
@@ -135,7 +87,7 @@ function card(r, gi) {
|
||||
}
|
||||
|
||||
function bind() {
|
||||
document.querySelectorAll('.card').forEach(cardEl => {
|
||||
document.querySelectorAll('.kanban-card').forEach(cardEl => {
|
||||
const pk = cardEl.dataset.pk;
|
||||
cardEl.querySelectorAll('.mv button').forEach(btn => {
|
||||
btn.onclick = async () => {
|
||||
@@ -160,9 +112,6 @@ function bind() {
|
||||
});
|
||||
}
|
||||
|
||||
// 返回应用=回到发起本页的对话:主界面深链 /?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) : '/';
|
||||
|
||||
@@ -4,82 +4,7 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<title>数据表格</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #f5f5f7; --surface: #fff; --border: #e8e8ed; --text: #1d1d1f;
|
||||
--text-2: #6e6e73; --accent: #0071e3; --accent-h: #0077ed; --danger: #d70015;
|
||||
--r: 12px; --r-sm: 8px;
|
||||
--shadow: 0 1px 3px rgba(0,0,0,0.04);
|
||||
--shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
|
||||
}
|
||||
* { 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: 1200px; margin: 0 auto; }
|
||||
.card { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow); overflow: hidden; }
|
||||
.hd { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
|
||||
.hd h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
|
||||
.btn { appearance: none; border: none; background: var(--accent); color: #fff; font: inherit; font-weight: 500;
|
||||
padding: 7px 14px; border-radius: 999px; cursor: pointer; transition: background .15s, transform .05s; }
|
||||
.btn:hover { background: var(--accent-h); }
|
||||
.btn:active { transform: scale(0.97); }
|
||||
.tb { padding: 10px 18px; border-bottom: 1px solid var(--border); }
|
||||
.search { width: 100%; border: 1px solid var(--border); background: var(--bg); border-radius: var(--r-sm);
|
||||
padding: 8px 12px; font: inherit; outline: none; transition: border-color .15s, background .15s; }
|
||||
.search:focus { border-color: var(--accent); background: var(--surface); }
|
||||
.tw { overflow-x: auto; }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
th, td { padding: 10px 18px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
|
||||
th { color: var(--text-2); font-weight: 500; font-size: 13px; cursor: pointer; user-select: none; }
|
||||
th:hover { color: var(--text); }
|
||||
th .ar { font-size: 10px; margin-left: 4px; opacity: .7; }
|
||||
td.ed { cursor: text; }
|
||||
td.ed:hover { background: rgba(0,113,227,0.04); }
|
||||
tr:last-child td { border-bottom: none; }
|
||||
.col-ac { width: 40px; }
|
||||
.del { appearance: none; background: none; border: none; color: var(--text-2); cursor: pointer;
|
||||
font-size: 18px; line-height: 1; padding: 2px 8px; border-radius: 6px; }
|
||||
.del:hover { color: var(--danger); background: rgba(215,0,21,0.08); }
|
||||
.msg { text-align: center; padding: 56px 20px; color: var(--text-2); }
|
||||
.msg.err { color: var(--danger); }
|
||||
.pager { display: flex; align-items: center; justify-content: center; gap: 14px;
|
||||
padding: 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--text-2); }
|
||||
.pager button { appearance: none; border: 1px solid var(--border); background: var(--surface); font: inherit;
|
||||
padding: 5px 12px; border-radius: 999px; cursor: pointer; }
|
||||
.pager button:disabled { opacity: .4; cursor: default; }
|
||||
dialog { border: none; border-radius: var(--r); padding: 0; box-shadow: var(--shadow-lg);
|
||||
width: min(420px, calc(100vw - 32px)); }
|
||||
dialog::backdrop { background: rgba(0,0,0,0.35); }
|
||||
.form { padding: 22px; }
|
||||
.form h3 { margin: 0 0 18px; font-size: 16px; }
|
||||
.fld { margin-bottom: 14px; }
|
||||
.fld label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 4px; }
|
||||
.fld input { width: 100%; border: 1px solid var(--border); border-radius: var(--r-sm);
|
||||
padding: 8px 10px; font: inherit; outline: none; transition: border-color .15s; }
|
||||
.fld input:focus { border-color: var(--accent); }
|
||||
.acts { display: flex; gap: 8px; justify-content: flex-end; margin-top: 22px; }
|
||||
.acts button { font: inherit; padding: 8px 16px; border-radius: 999px; cursor: pointer;
|
||||
border: 1px solid var(--border); background: var(--surface); color: var(--text); }
|
||||
.acts .pri { background: var(--accent); color: #fff; border-color: var(--accent); }
|
||||
.acts .pri:hover { background: var(--accent-h); }
|
||||
@media (max-width: 480px) {
|
||||
body { padding: 8px; padding-bottom: calc(64px + env(safe-area-inset-bottom)); } th, td { padding: 8px 12px; font-size: 13px; }
|
||||
}
|
||||
/* 返回应用 FAB:固定右下、零版面占用(桌面优先方针下 footer 会吃掉长表/看板的垂直空间)。
|
||||
图标用内联 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)); }
|
||||
</style>
|
||||
<link rel="stylesheet" href="./base.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
@@ -110,14 +35,20 @@ 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>
|
||||
|
||||
<style>
|
||||
/* 表格页面特有样式 */
|
||||
.hd { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
|
||||
.hd h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
|
||||
.tb { padding: 10px 18px; border-bottom: 1px solid var(--border); }
|
||||
</style>
|
||||
|
||||
<script src="./config-table.js"></script>
|
||||
<script>
|
||||
const CONFIG = {
|
||||
table: 'items', // ← 修改为你的表名
|
||||
pk: 'id', // ← 修改为主键列名
|
||||
title: '数据表格', // ← 修改为页面标题
|
||||
pageSize: 10,
|
||||
editable: true,
|
||||
const DEFAULT = {
|
||||
table: 'items', pk: 'id', title: '数据表格', pageSize: 10, editable: true,
|
||||
labels: {}, numCols: [], options: {}, pills: {},
|
||||
};
|
||||
const CONFIG = Object.assign({}, DEFAULT, window.PAGE_CONFIG || {});
|
||||
|
||||
const API = `/api/db/${location.pathname.split('/').filter(Boolean)[1]}/query`;
|
||||
const S = { cols: [], rows: [], sortKey: null, sortDir: 'asc', kw: '', page: 1, loading: true, err: null };
|
||||
@@ -164,6 +95,12 @@ function view() {
|
||||
return r;
|
||||
}
|
||||
|
||||
function fmt(c, v) {
|
||||
const pm = CONFIG.pills[c];
|
||||
if (pm && v != null && pm[v]) return `<span class="pill ${pm[v]}">${esc(v)}</span>`;
|
||||
return esc(v);
|
||||
}
|
||||
|
||||
function render() {
|
||||
const root = document.getElementById('root');
|
||||
if (S.loading) return void (root.innerHTML = '<div class="msg">加载中…</div>');
|
||||
@@ -178,8 +115,10 @@ function render() {
|
||||
|
||||
let h = '<div class="tw"><table><thead><tr>';
|
||||
for (const c of S.cols) {
|
||||
const isNum = CONFIG.numCols.includes(c);
|
||||
const lb = CONFIG.labels[c] || c;
|
||||
const ar = S.sortKey === c ? (S.sortDir === 'asc' ? '↑' : '↓') : '';
|
||||
h += `<th data-c="${esc(c)}">${esc(c)}<span class="ar">${ar}</span></th>`;
|
||||
h += `<th data-c="${esc(c)}"${isNum ? ' class="num"' : ''}>${esc(lb)}<span class="ar">${ar}</span></th>`;
|
||||
}
|
||||
if (CONFIG.editable) h += '<th class="col-ac"></th>';
|
||||
h += '</tr></thead><tbody>';
|
||||
@@ -187,8 +126,10 @@ function render() {
|
||||
for (const r of cur) {
|
||||
h += `<tr data-pk="${esc(r[CONFIG.pk])}">`;
|
||||
for (const c of S.cols) {
|
||||
const ed = CONFIG.editable && c !== CONFIG.pk ? ' class="ed"' : '';
|
||||
h += `<td${ed} data-c="${esc(c)}">${esc(r[c])}</td>`;
|
||||
const cls = [];
|
||||
if (CONFIG.editable && c !== CONFIG.pk) cls.push('ed');
|
||||
if (CONFIG.numCols.includes(c)) cls.push('num');
|
||||
h += `<td${cls.length ? ` class="${cls.join(' ')}"` : ''} data-c="${esc(c)}">${fmt(c, r[c])}</td>`;
|
||||
}
|
||||
if (CONFIG.editable) h += '<td class="col-ac"><button class="del" title="删除">×</button></td>';
|
||||
h += '</tr>';
|
||||
@@ -224,9 +165,14 @@ function edit(td) {
|
||||
if (td.classList.contains('editing')) return;
|
||||
const tr = td.closest('tr'), col = td.dataset.c, pk = tr.dataset.pk, orig = td.textContent;
|
||||
td.classList.add('editing');
|
||||
td.innerHTML = `<input value="${esc(orig)}" style="width:100%;border:1px solid var(--accent);border-radius:4px;padding:2px 6px;font:inherit;outline:none">`;
|
||||
const opt = CONFIG.options[col];
|
||||
const style = 'width:100%;border:1px solid var(--accent);border-radius:4px;padding:2px 6px;font:inherit;outline:none;background:var(--surface)';
|
||||
const ctl = opt
|
||||
? `<select style="${style}">${opt.map(o => `<option${o === orig ? ' selected' : ''}>${esc(o)}</option>`).join('')}</select>`
|
||||
: `<input value="${esc(orig)}" type="${CONFIG.numCols.includes(col) ? 'number' : 'text'}" style="${style}">`;
|
||||
td.innerHTML = ctl;
|
||||
const inp = td.firstChild;
|
||||
inp.focus(); inp.select();
|
||||
inp.focus(); if (inp.select) inp.select();
|
||||
let done = false;
|
||||
const finish = async (save) => {
|
||||
if (done) return; done = true;
|
||||
@@ -235,7 +181,7 @@ function edit(td) {
|
||||
try {
|
||||
await q(`UPDATE ${CONFIG.table} SET ${col} = ? WHERE ${CONFIG.pk} = ?`, [v, pk]);
|
||||
const r = S.rows.find(x => String(x[CONFIG.pk]) === String(pk));
|
||||
if (r) r[col] = v;
|
||||
if (r) r[col] = CONFIG.numCols.includes(col) ? Number(v) : v;
|
||||
render();
|
||||
} catch (e) { alert('保存失败:' + e.message); render(); }
|
||||
};
|
||||
@@ -262,7 +208,14 @@ document.getElementById('title').textContent = CONFIG.title;
|
||||
document.getElementById('addBtn').onclick = () => {
|
||||
const dlg = document.getElementById('dlg'), flds = document.getElementById('fields');
|
||||
const cols = S.cols.filter(c => c !== CONFIG.pk);
|
||||
flds.innerHTML = cols.map(c => `<div class="fld"><label>${esc(c)}</label><input name="${esc(c)}" autocomplete="off"></div>`).join('');
|
||||
flds.innerHTML = cols.map(c => {
|
||||
const lb = CONFIG.labels[c] || c;
|
||||
const opt = CONFIG.options[c];
|
||||
const ctl = opt
|
||||
? `<select name="${esc(c)}">${opt.map(o => `<option>${esc(o)}</option>`).join('')}</select>`
|
||||
: `<input name="${esc(c)}" type="${CONFIG.numCols.includes(c) ? 'number' : 'text'}" autocomplete="off">`;
|
||||
return `<div class="fld"><label>${esc(lb)}</label>${ctl}</div>`;
|
||||
}).join('');
|
||||
dlg.showModal();
|
||||
};
|
||||
|
||||
@@ -271,7 +224,10 @@ document.getElementById('dlgForm').onsubmit = async e => {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const cols = S.cols.filter(c => c !== CONFIG.pk);
|
||||
const vals = cols.map(c => form.elements[c].value);
|
||||
const vals = cols.map(c => {
|
||||
const v = form.elements[c].value;
|
||||
return CONFIG.numCols.includes(c) && v !== '' ? Number(v) : v;
|
||||
});
|
||||
try {
|
||||
await q(`INSERT INTO ${CONFIG.table} (${cols.join(',')}) VALUES (${cols.map(() => '?').join(',')})`, vals);
|
||||
document.getElementById('dlg').close();
|
||||
@@ -279,9 +235,6 @@ document.getElementById('dlgForm').onsubmit = async e => {
|
||||
} catch (e) { alert('新增失败:' + e.message); }
|
||||
};
|
||||
|
||||
// 返回应用=回到发起本页的对话:主界面深链 /?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) : '/';
|
||||
|
||||
Reference in New Issue
Block a user