Files
2026-09-16 22:43:21 +08:00

930 lines
36 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AI SaaS · 工作台</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
<style>
:root {
--bg: #f3f5f9;
--surface: #ffffff;
--text: #1f2937;
--muted: #6b7280;
--border: #e5e7eb;
--primary: #3b82f6;
--primary-soft: #eff6ff;
--green: #10b981;
--orange: #f59e0b;
--purple: #8b5cf6;
--red: #ef4444;
--sidebar-w: 220px;
--radius: 14px;
--shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.04);
--font: "PingFang SC", "Microsoft YaHei", "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
font-family: var(--font);
background: var(--bg);
color: var(--text);
display: flex;
min-height: 100%;
}
button, input { font: inherit; }
a { color: inherit; text-decoration: none; }
ul { list-style: none; }
.app { display: flex; width: 100%; min-height: 100%; }
/* Sidebar */
.sidebar {
width: var(--sidebar-w);
background: var(--surface);
border-right: 1px solid var(--border);
display: flex;
flex-direction: column;
position: sticky;
top: 0;
height: 100vh;
flex-shrink: 0;
}
.brand {
display: flex;
align-items: center;
gap: 10px;
padding: 20px 18px 16px;
font-weight: 700;
font-size: 18px;
}
.brand-mark {
width: 32px; height: 32px; border-radius: 9px;
background: linear-gradient(145deg, #60a5fa, #2563eb);
display: grid; place-items: center; color: #fff;
box-shadow: 0 6px 14px rgba(37, 99, 235, 0.28);
}
.nav { padding: 8px 12px; flex: 1; overflow: auto; }
.nav-item {
display: flex; align-items: center; gap: 10px;
padding: 11px 12px; border-radius: 10px;
color: #4b5563; font-size: 14px; margin-bottom: 2px;
cursor: pointer; transition: 0.15s ease;
}
.nav-item:hover { background: #f8fafc; color: var(--text); }
.nav-item.active {
background: var(--primary-soft);
color: var(--primary);
font-weight: 600;
}
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; opacity: 0.9; }
.user-box {
margin: 12px; padding: 12px;
border: 1px solid var(--border);
border-radius: 12px;
display: flex; align-items: center; gap: 10px;
}
.avatar {
width: 36px; height: 36px; border-radius: 50%;
background: linear-gradient(135deg, #93c5fd, #3b82f6);
color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px;
}
.user-meta { flex: 1; min-width: 0; }
.user-meta strong { display: block; font-size: 13px; }
.user-meta span { font-size: 11px; color: var(--muted); }
/* Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
display: flex; align-items: center; gap: 16px;
padding: 18px 24px 8px; flex-wrap: wrap;
}
.hello h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.hello p { color: var(--muted); font-size: 13px; margin-top: 4px; }
.search {
flex: 1; min-width: 220px; max-width: 520px; margin: 0 auto;
display: flex; align-items: center; gap: 8px;
background: var(--surface); border: 1px solid var(--border);
border-radius: 999px; padding: 10px 16px; box-shadow: var(--shadow);
}
.search input {
border: 0; outline: 0; width: 100%; background: transparent; color: var(--text);
}
.search input::placeholder { color: #9ca3af; }
.top-actions { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.icon-btn {
width: 38px; height: 38px; border-radius: 50%;
border: 1px solid var(--border); background: var(--surface);
display: grid; place-items: center; cursor: pointer; position: relative;
color: #64748b;
}
.icon-btn .badge {
position: absolute; top: -2px; right: -2px;
min-width: 16px; height: 16px; padding: 0 4px;
border-radius: 999px; background: var(--red); color: #fff;
font-size: 10px; font-weight: 700; display: grid; place-items: center;
}
.add-btn {
width: 38px; height: 38px; border-radius: 50%; border: 0;
background: var(--primary); color: #fff; font-size: 22px; line-height: 1;
cursor: pointer; box-shadow: 0 8px 18px rgba(59, 130, 246, 0.35);
}
.content { padding: 12px 24px 28px; display: flex; flex-direction: column; gap: 16px; }
.card {
background: var(--surface);
border: 1px solid rgba(229, 231, 235, 0.9);
border-radius: var(--radius);
box-shadow: var(--shadow);
}
/* KPI — 复合卡 */
.kpi-row {
display: grid;
grid-template-columns: repeat(5, minmax(0, 1fr));
gap: 14px;
}
.kpi-card {
padding: 16px 16px 14px;
position: relative;
cursor: default;
transition: border-color 0.15s, box-shadow 0.15s;
}
.kpi-card:hover {
border-color: #c7d2fe;
box-shadow: 0 10px 28px rgba(59, 130, 246, 0.1);
}
.kpi-head {
display: flex; align-items: flex-start; justify-content: space-between; gap: 8px;
}
.kpi-title {
font-size: 13px; color: var(--muted); font-weight: 500;
display: flex; flex-direction: column; gap: 2px;
}
.kpi-title .primary-name {
color: var(--text); font-weight: 600; font-size: 13px;
}
.kpi-title .hint {
font-size: 11px; color: #94a3b8;
}
.kpi-icon {
width: 36px; height: 36px; border-radius: 10px;
display: grid; place-items: center; flex-shrink: 0;
}
.kpi-icon.blue { background: #eff6ff; color: var(--primary); }
.kpi-icon.green { background: #ecfdf5; color: var(--green); }
.kpi-icon.purple { background: #f5f3ff; color: var(--purple); }
.kpi-icon.orange { background: #fffbeb; color: var(--orange); }
.kpi-icon.cyan { background: #ecfeff; color: #06b6d4; }
.kpi-value {
margin-top: 12px; font-size: 26px; font-weight: 700;
letter-spacing: -0.03em; line-height: 1.1;
}
.kpi-foot {
margin-top: 10px; font-size: 12px; color: var(--muted);
display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.kpi-trend { color: var(--green); font-weight: 600; }
.kpi-trend.down { color: var(--red); }
.kpi-link { color: var(--primary); font-weight: 500; }
.spark { width: 72px; height: 28px; }
.kpi-popover {
display: none;
position: absolute; left: 12px; right: 12px; top: calc(100% - 6px);
z-index: 20; background: #fff; border: 1px solid var(--border);
border-radius: 12px; box-shadow: 0 16px 40px rgba(15, 23, 42, 0.12);
padding: 8px; min-width: 200px;
}
.kpi-card:hover .kpi-popover,
.kpi-card:focus-within .kpi-popover { display: block; }
.kpi-popover .tip {
font-size: 11px; color: #94a3b8; padding: 4px 8px 8px;
}
.kpi-option {
width: 100%; border: 0; background: transparent; text-align: left;
padding: 8px 10px; border-radius: 8px; cursor: pointer;
display: flex; justify-content: space-between; gap: 10px; align-items: center;
}
.kpi-option:hover { background: #f8fafc; }
.kpi-option.active { background: var(--primary-soft); }
.kpi-option .lab { font-size: 12px; font-weight: 600; color: var(--text); }
.kpi-option .val { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.kpi-option .lab small {
margin-left: 6px; color: var(--primary); font-weight: 500; font-size: 10px;
}
/* Mid row */
.mid-row {
display: grid;
grid-template-columns: 1.7fr 1fr;
gap: 14px;
}
.panel { padding: 16px 18px 18px; }
.panel-hd {
display: flex; align-items: center; justify-content: space-between;
gap: 12px; margin-bottom: 12px; flex-wrap: wrap;
}
.panel-hd h2 { font-size: 16px; font-weight: 700; }
.tabs { display: flex; gap: 4px; background: #f1f5f9; padding: 3px; border-radius: 10px; }
.tab {
border: 0; background: transparent; padding: 6px 12px; border-radius: 8px;
font-size: 12px; color: var(--muted); cursor: pointer; font-weight: 500;
}
.tab.active { background: #fff; color: var(--primary); box-shadow: 0 1px 3px rgba(0,0,0,0.06); }
.select {
border: 1px solid var(--border); background: #fff; border-radius: 8px;
padding: 6px 10px; font-size: 12px; color: var(--muted);
}
.chart-wrap { height: 260px; position: relative; }
.todo-list { display: flex; flex-direction: column; gap: 10px; }
.todo-item {
display: flex; gap: 12px; align-items: flex-start;
padding: 12px; border-radius: 12px; background: #f8fafc;
border: 1px solid transparent;
}
.todo-item:hover { border-color: var(--border); background: #fff; }
.dot {
width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; flex-shrink: 0;
}
.dot.blue { background: var(--primary); }
.dot.green { background: var(--green); }
.dot.orange { background: var(--orange); }
.dot.red { background: var(--red); }
.todo-body { flex: 1; min-width: 0; }
.todo-body strong { display: block; font-size: 13px; margin-bottom: 2px; }
.todo-body span { font-size: 12px; color: var(--muted); }
.todo-time { font-size: 11px; color: #94a3b8; white-space: nowrap; }
/* Bottom row */
.bottom-row {
display: grid;
grid-template-columns: 1fr 1fr 1.25fr;
gap: 14px;
}
.donut-wrap {
height: 220px; position: relative;
display: flex; align-items: center; justify-content: center;
}
.donut-center {
position: absolute; text-align: center; pointer-events: none;
}
.donut-center .n { font-size: 22px; font-weight: 700; }
.donut-center .l { font-size: 12px; color: var(--muted); }
.legend {
display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px;
margin-top: 8px; font-size: 12px; color: var(--muted);
}
.legend i {
display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px;
}
.rank-list { display: flex; flex-direction: column; gap: 14px; margin-top: 8px; }
.rank-row { display: grid; grid-template-columns: 18px 1fr auto; gap: 10px; align-items: center; }
.rank-idx {
width: 18px; height: 18px; border-radius: 6px; background: #f1f5f9;
font-size: 11px; font-weight: 700; display: grid; place-items: center; color: #64748b;
}
.rank-idx.top { background: var(--primary-soft); color: var(--primary); }
.rank-name { font-size: 13px; font-weight: 500; margin-bottom: 6px; }
.bar {
height: 7px; border-radius: 999px; background: #eef2f7; overflow: hidden;
}
.bar > span {
display: block; height: 100%; border-radius: inherit;
background: linear-gradient(90deg, #60a5fa, #2563eb);
}
.rank-val { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
text-align: left; color: var(--muted); font-weight: 500; font-size: 12px;
padding: 8px 6px; border-bottom: 1px solid var(--border);
}
td { padding: 11px 6px; border-bottom: 1px solid #f1f5f9; }
tr:last-child td { border-bottom: 0; }
.tag {
display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 999px;
font-size: 11px; font-weight: 600;
}
.tag.paid { background: #ecfdf5; color: #059669; }
.tag.pending { background: #fffbeb; color: #d97706; }
.tag.cancel { background: #fef2f2; color: #dc2626; }
.notice-strip {
display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px;
}
.notice {
display: flex; gap: 12px; align-items: flex-start; padding: 14px 16px;
}
.notice-ico {
width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
background: var(--primary-soft); color: var(--primary); flex-shrink: 0;
}
.notice strong { display: block; font-size: 13px; margin-bottom: 2px; }
.notice span { font-size: 12px; color: var(--muted); }
.settings-fab {
position: fixed; right: 20px; bottom: 20px; z-index: 40;
border: 0; border-radius: 999px; padding: 10px 14px;
background: #111827; color: #fff; font-size: 12px; cursor: pointer;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.modal-mask {
display: none; position: fixed; inset: 0; background: rgba(15,23,42,0.35);
z-index: 50; align-items: center; justify-content: center; padding: 20px;
}
.modal-mask.open { display: flex; }
.modal {
width: min(520px, 100%); background: #fff; border-radius: 16px;
padding: 18px 20px 16px; box-shadow: 0 24px 60px rgba(0,0,0,0.2);
max-height: 85vh; overflow: auto;
}
.modal h3 { font-size: 16px; margin-bottom: 6px; }
.modal p.desc { font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.modal-card {
border: 1px solid var(--border); border-radius: 12px; padding: 12px; margin-bottom: 10px;
}
.modal-card label.head {
display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px;
margin-bottom: 8px; cursor: pointer;
}
.modal-card .radios {
display: flex; flex-wrap: wrap; gap: 8px 14px; padding-left: 22px;
}
.modal-card .radios label {
font-size: 12px; color: #475569; display: flex; align-items: center; gap: 4px; cursor: pointer;
}
.modal-actions {
display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px;
}
.btn {
border: 1px solid var(--border); background: #fff; border-radius: 10px;
padding: 8px 14px; font-size: 13px; cursor: pointer;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
@media (max-width: 1280px) {
.kpi-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.bottom-row, .notice-strip { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 960px) {
.sidebar { display: none; }
.mid-row, .bottom-row, .kpi-row, .notice-strip { grid-template-columns: 1fr; }
.search { order: 3; max-width: none; width: 100%; }
}
</style>
</head>
<body>
<div class="app">
<aside class="sidebar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2.2">
<path d="M4 19V9M10 19V5M16 19v-7M22 19H2"/>
</svg>
</div>
AI SaaS
</div>
<nav class="nav" id="nav">
<!-- filled by JS -->
</nav>
<div class="user-box">
<div class="avatar">A</div>
<div class="user-meta">
<strong>Admin</strong>
<span>超级管理员</span>
</div>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="#94a3b8" stroke-width="2"><path d="m6 9 6 6 6-6"/></svg>
</div>
</aside>
<div class="main">
<header class="topbar">
<div class="hello">
<h1>欢迎回来, Admin 👋</h1>
<p id="todayLabel">星期二, 2024年5月7日</p>
</div>
<div class="search">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#94a3b8" stroke-width="2"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>
<input placeholder="搜索功能、数据或用户..." />
</div>
<div class="top-actions">
<button class="icon-btn" type="button" aria-label="通知">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"/><path d="M10.3 21a1.94 1.94 0 0 0 3.4 0"/></svg>
<span class="badge">12</span>
</button>
<button class="icon-btn" type="button" aria-label="帮助">?</button>
<button class="add-btn" type="button" aria-label="新建">+</button>
</div>
</header>
<div class="content">
<section class="kpi-row" id="kpiRow"></section>
<section class="mid-row">
<div class="card panel">
<div class="panel-hd">
<h2>业务趋势</h2>
<div style="display:flex;gap:10px;align-items:center;flex-wrap:wrap">
<div class="tabs" id="trendTabs">
<button class="tab active" data-tab="revenue" type="button">收入趋势</button>
<button class="tab" data-tab="orders" type="button">订单趋势</button>
<button class="tab" data-tab="users" type="button">用户趋势</button>
</div>
<select class="select" aria-label="时间范围">
<option>近 7 天</option>
<option>近 30 天</option>
</select>
</div>
</div>
<div class="chart-wrap"><canvas id="trendChart"></canvas></div>
</div>
<div class="card panel">
<div class="panel-hd"><h2>待办事项</h2><a class="kpi-link" href="#">查看全部</a></div>
<ul class="todo-list" id="todoList"></ul>
</div>
</section>
<section class="bottom-row">
<div class="card panel">
<div class="panel-hd"><h2>数据分布</h2></div>
<div class="donut-wrap">
<canvas id="donutChart"></canvas>
<div class="donut-center">
<div class="n">8,560</div>
<div class="l">订单总数</div>
</div>
</div>
<div class="legend" id="donutLegend"></div>
</div>
<div class="card panel">
<div class="panel-hd"><h2>产品使用 TOP5</h2></div>
<div class="rank-list" id="rankList"></div>
</div>
<div class="card panel">
<div class="panel-hd"><h2>最新订单</h2><a class="kpi-link" href="#">查看全部</a></div>
<table>
<thead>
<tr>
<th>订单号</th><th>用户</th><th>产品</th><th>金额</th><th>状态</th>
</tr>
</thead>
<tbody id="orderBody"></tbody>
</table>
</div>
</section>
<section class="notice-strip" id="noticeStrip"></section>
</div>
</div>
</div>
<button class="settings-fab" type="button" id="openSettings">KPI 显示设置</button>
<div class="modal-mask" id="settingsMask">
<div class="modal" role="dialog" aria-modal="true" aria-labelledby="settingsTitle">
<h3 id="settingsTitle">KPI 显示设置</h3>
<p class="desc">勾选要展示的卡片,并为每张卡选择优先显示的指标。悬停卡片也可切换,设置保存在本浏览器。</p>
<div id="settingsBody"></div>
<div class="modal-actions">
<button class="btn" type="button" id="resetSettings">恢复默认</button>
<button class="btn" type="button" id="closeSettings">取消</button>
<button class="btn primary" type="button" id="saveSettings">保存</button>
</div>
</div>
</div>
<script>
const NAV = [
["数据驾驶舱", false],
["工作台", true],
["AI 助手", false],
["数据分析", false],
["用户管理", false],
["产品管理", false],
["订单管理", false],
["营销中心", false],
["内容管理", false],
["财务管理", false],
["系统设置", false],
];
const KPI_CARDS = [
{
id: "revenue",
title: "收入",
tone: "blue",
defaultPrimary: "总收入",
metrics: [
{ label: "总收入", value: "¥ 68,560", raw: 68560, trend: 16.3, spark: [42, 48, 45, 52, 58, 55, 68] },
{ label: "净收入", value: "¥ 52,180", raw: 52180, trend: 11.2, spark: [30, 34, 36, 40, 44, 48, 52] },
{ label: "退款额", value: "¥ 3,240", raw: 3240, trend: -4.1, spark: [8, 7, 9, 6, 5, 6, 4] },
],
},
{
id: "orders",
title: "订单",
tone: "blue",
defaultPrimary: "订单总数",
metrics: [
{ label: "订单总数", value: "8,560", raw: 8560, trend: 8.2, spark: [50, 52, 54, 60, 58, 62, 70] },
{ label: "已支付", value: "7,420", raw: 7420, trend: 9.1, spark: [40, 42, 45, 48, 50, 55, 60] },
{ label: "待处理", value: "860", raw: 860, trend: 2.4, spark: [12, 11, 13, 12, 14, 13, 15] },
],
},
{
id: "users",
title: "用户",
tone: "green",
defaultPrimary: "新增用户",
metrics: [
{ label: "新增用户", value: "1,256", raw: 1256, trend: 12.5, spark: [20, 24, 22, 28, 30, 34, 38] },
{ label: "活跃用户", value: "4,560", raw: 4560, trend: 9.3, spark: [30, 32, 35, 36, 40, 42, 45] },
{ label: "流失用户", value: "186", raw: 186, trend: -3.2, spark: [10, 9, 11, 8, 7, 8, 6] },
],
},
{
id: "conversion",
title: "转化",
tone: "orange",
defaultPrimary: "转化率",
metrics: [
{ label: "转化率", value: "12.5%", raw: 12.5, trend: 2.4, spark: [8, 9, 9.5, 10, 11, 11.5, 12.5] },
{ label: "下单率", value: "18.2%", raw: 18.2, trend: 1.1, spark: [14, 15, 15.5, 16, 17, 17.5, 18] },
{ label: "复购率", value: "26.8%", raw: 26.8, trend: 3.6, spark: [20, 21, 22, 23, 24, 25, 26] },
],
},
{
id: "todos",
title: "待办",
tone: "cyan",
defaultPrimary: "待办事项",
metrics: [
{ label: "待办事项", value: "28", raw: 28, trend: null, spark: null, link: "待处理任务 ›" },
{ label: "今日到期", value: "6", raw: 6, trend: null, spark: null },
{ label: "已逾期", value: "3", raw: 3, trend: null, spark: null },
],
},
];
const PREFS_KEY = "ai-saas-workbench-kpi-v1";
const DEFAULT_VISIBLE = ["revenue", "orders", "users", "conversion", "todos"];
function loadPrefs() {
try {
const raw = localStorage.getItem(PREFS_KEY);
if (!raw) return null;
return JSON.parse(raw);
} catch { return null; }
}
function savePrefs(prefs) {
localStorage.setItem(PREFS_KEY, JSON.stringify(prefs));
}
function defaultPrefs() {
const primaryByCardId = {};
KPI_CARDS.forEach((c) => { primaryByCardId[c.id] = c.defaultPrimary; });
return { visibleCardIds: [...DEFAULT_VISIBLE], primaryByCardId };
}
let prefs = Object.assign(defaultPrefs(), loadPrefs() || {});
prefs.visibleCardIds = (prefs.visibleCardIds || DEFAULT_VISIBLE).filter((id) =>
KPI_CARDS.some((c) => c.id === id)
);
if (!prefs.visibleCardIds.length) prefs.visibleCardIds = [...DEFAULT_VISIBLE];
KPI_CARDS.forEach((c) => {
const p = prefs.primaryByCardId?.[c.id];
if (!p || !c.metrics.some((m) => m.label === p)) {
prefs.primaryByCardId[c.id] = c.defaultPrimary;
}
});
function iconSvg(name) {
const paths = {
revenue: '<path d="M12 2v20M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/>',
orders: '<path d="M6 2h12l2 7H4L6 2z"/><path d="M4 9v11a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9"/><path d="M10 13h4"/>',
users: '<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87M16 3.13a4 4 0 0 1 0 7.75"/>',
conversion: '<path d="M22 12h-4l-3 9L9 3l-3 9H2"/>',
todos: '<path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/>',
};
return `<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${paths[name] || paths.revenue}</svg>`;
}
function sparkSvg(points, up = true) {
if (!points?.length) return "";
const w = 72, h = 28, pad = 2;
const min = Math.min(...points), max = Math.max(...points);
const span = max - min || 1;
const coords = points.map((v, i) => {
const x = pad + (i / (points.length - 1)) * (w - pad * 2);
const y = h - pad - ((v - min) / span) * (h - pad * 2);
return `${x},${y}`;
});
const color = up ? "#3b82f6" : "#ef4444";
return `<svg class="spark" viewBox="0 0 ${w} ${h}" aria-hidden="true"><polyline fill="none" stroke="${color}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" points="${coords.join(" ")}"/></svg>`;
}
function renderNav() {
document.getElementById("nav").innerHTML = NAV.map(([name, active]) => `
<div class="nav-item ${active ? "active" : ""}">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/></svg>
${name}
</div>
`).join("");
}
function getPrimaryMetric(card) {
const label = prefs.primaryByCardId[card.id] || card.defaultPrimary;
return card.metrics.find((m) => m.label === label) || card.metrics[0];
}
function setPrimary(cardId, label) {
prefs.primaryByCardId[cardId] = label;
savePrefs(prefs);
renderKpis();
}
function renderKpis() {
const row = document.getElementById("kpiRow");
const cards = KPI_CARDS.filter((c) => prefs.visibleCardIds.includes(c.id));
row.style.gridTemplateColumns = `repeat(${Math.min(cards.length, 5)}, minmax(0, 1fr))`;
row.innerHTML = cards.map((card) => {
const m = getPrimaryMetric(card);
const up = m.trend == null ? true : m.trend >= 0;
const trendHtml = m.trend == null
? (m.link ? `<a class="kpi-link" href="#">${m.link}</a>` : "<span></span>")
: `<span>较上周 <span class="kpi-trend ${up ? "" : "down"}">${up ? "↑" : "↓"} ${Math.abs(m.trend)}%</span></span>`;
const options = card.metrics.map((opt) => `
<button type="button" class="kpi-option ${opt.label === m.label ? "active" : ""}" data-card="${card.id}" data-label="${opt.label}">
<span class="lab">${opt.label}${opt.label === m.label ? "<small>当前</small>" : ""}</span>
<span class="val">${opt.value}${opt.trend != null ? ` · ${opt.trend >= 0 ? "+" : ""}${opt.trend}%` : ""}</span>
</button>
`).join("");
return `
<article class="card kpi-card" tabindex="0">
<div class="kpi-head">
<div class="kpi-title">
<span class="primary-name">${card.title} · ${m.label}</span>
<span class="hint">悬停切换同组指标</span>
</div>
<div class="kpi-icon ${card.tone}">${iconSvg(card.id)}</div>
</div>
<div class="kpi-value">${m.value}</div>
<div class="kpi-foot">
${trendHtml}
${m.spark ? sparkSvg(m.spark, up) : ""}
</div>
<div class="kpi-popover">
<div class="tip">点击切换优先显示</div>
${options}
</div>
</article>
`;
}).join("");
row.querySelectorAll(".kpi-option").forEach((btn) => {
btn.addEventListener("click", (e) => {
e.stopPropagation();
setPrimary(btn.dataset.card, btn.dataset.label);
});
});
}
function renderTodos() {
const items = [
["系统维护通知", "系统将于今晚 22:00 进行例行维护", "今天 10:00", "blue"],
["新功能上线", "AI 助手 v2.0 已正式发布", "昨天 15:30", "green"],
["数据备份完成", "上周数据备份已成功完成", "昨天 09:15", "blue"],
["订单异常提醒", "检测到 3 笔异常订单需要处理", "前天 16:45", "orange"],
["用户反馈汇总", "本周共收到 28 条用户反馈", "前天 11:20", "red"],
];
document.getElementById("todoList").innerHTML = items.map(([t, d, time, c]) => `
<li class="todo-item">
<i class="dot ${c}"></i>
<div class="todo-body"><strong>${t}</strong><span>${d}</span></div>
<span class="todo-time">${time}</span>
</li>
`).join("");
}
function renderRanks() {
const rows = [
["AI 助手", 8560, 100],
["数据分析平台", 6540, 76],
["智能客服", 4320, 50],
["内容生成器", 3210, 37],
["营销自动化", 2180, 25],
];
document.getElementById("rankList").innerHTML = rows.map(([name, val, pct], i) => `
<div class="rank-row">
<div class="rank-idx ${i < 3 ? "top" : ""}">${i + 1}</div>
<div>
<div class="rank-name">${name}</div>
<div class="bar"><span style="width:${pct}%"></span></div>
</div>
<div class="rank-val">${val.toLocaleString("zh-CN")}</div>
</div>
`).join("");
}
function renderOrders() {
const rows = [
["ORD-20240507001", "张三", "AI 助手 Pro", "¥299", "paid", "已支付"],
["ORD-20240507002", "李四", "数据分析基础版", "¥99", "pending", "待支付"],
["ORD-20240507003", "王五", "智能客服企业版", "¥999", "paid", "已支付"],
["ORD-20240507004", "赵六", "内容生成器", "¥199", "cancel", "已取消"],
["ORD-20240507005", "钱七", "营销自动化", "¥499", "paid", "已支付"],
];
document.getElementById("orderBody").innerHTML = rows.map((r) => `
<tr>
<td>${r[0]}</td><td>${r[1]}</td><td>${r[2]}</td><td>${r[3]}</td>
<td><span class="tag ${r[4]}">${r[5]}</span></td>
</tr>
`).join("");
}
function renderNotices() {
const items = [
["系统维护通知", "系统将于今晚 22:00 进行例行维护,预计持续 2 小时"],
["新功能上线", "AI 助手 v2.0 已正式发布,欢迎体验新功能"],
["数据备份完成", "上周数据备份已成功完成,可在设置中查看"],
["安全提醒", "建议开启二次验证以提升账户安全性"],
];
document.getElementById("noticeStrip").innerHTML = items.map(([t, d]) => `
<div class="card notice">
<div class="notice-ico">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></svg>
</div>
<div><strong>${t}</strong><span>${d}</span></div>
</div>
`).join("");
}
const TREND = {
revenue: [42000, 48000, 45000, 52000, 68560, 61000, 64000],
orders: [980, 1050, 990, 1120, 1280, 1190, 1250],
users: [160, 180, 170, 210, 256, 230, 240],
};
const LABELS = ["05-01", "05-02", "05-03", "05-04", "05-05", "05-06", "05-07"];
let trendChart;
function buildTrend(key) {
const ctx = document.getElementById("trendChart");
const data = TREND[key];
if (trendChart) trendChart.destroy();
trendChart = new Chart(ctx, {
type: "line",
data: {
labels: LABELS,
datasets: [{
label: key === "revenue" ? "收入" : key === "orders" ? "订单" : "用户",
data,
borderColor: "#3b82f6",
backgroundColor: (c) => {
const g = c.chart.ctx.createLinearGradient(0, 0, 0, c.chart.height);
g.addColorStop(0, "rgba(59,130,246,0.28)");
g.addColorStop(1, "rgba(59,130,246,0.02)");
return g;
},
fill: true,
tension: 0.4,
pointRadius: 3,
pointHoverRadius: 5,
pointBackgroundColor: "#fff",
pointBorderColor: "#3b82f6",
pointBorderWidth: 2,
}],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: "#111827",
padding: 10,
callbacks: {
label: (ctx) => {
const v = ctx.parsed.y;
if (key === "revenue") return `收入: ¥ ${v.toLocaleString("zh-CN")}`;
return `${ctx.dataset.label}: ${v.toLocaleString("zh-CN")}`;
},
},
},
},
scales: {
x: { grid: { display: false }, ticks: { color: "#94a3b8" }, border: { display: false } },
y: {
grid: { color: "#f1f5f9" },
ticks: {
color: "#94a3b8",
callback: (v) => key === "revenue" ? (v / 1000) + "k" : v,
},
border: { display: false },
},
},
},
});
}
function buildDonut() {
const slices = [
{ label: "基础版", value: 3200, color: "#3b82f6" },
{ label: "专业版", value: 2800, color: "#60a5fa" },
{ label: "企业版", value: 1800, color: "#93c5fd" },
{ label: "定制版", value: 760, color: "#bfdbfe" },
];
document.getElementById("donutLegend").innerHTML = slices.map((s) =>
`<div><i style="background:${s.color}"></i>${s.label} ${s.value.toLocaleString("zh-CN")}</div>`
).join("");
new Chart(document.getElementById("donutChart"), {
type: "doughnut",
data: {
labels: slices.map((s) => s.label),
datasets: [{ data: slices.map((s) => s.value), backgroundColor: slices.map((s) => s.color), borderWidth: 0, hoverOffset: 4 }],
},
options: {
responsive: true,
maintainAspectRatio: false,
cutout: "72%",
plugins: { legend: { display: false } },
},
});
}
/* Settings modal */
const mask = document.getElementById("settingsMask");
function openSettings() {
const body = document.getElementById("settingsBody");
body.innerHTML = KPI_CARDS.map((card) => {
const checked = prefs.visibleCardIds.includes(card.id) ? "checked" : "";
const radios = card.metrics.map((m) => `
<label>
<input type="radio" name="primary-${card.id}" value="${m.label}" ${prefs.primaryByCardId[card.id] === m.label ? "checked" : ""} />
${m.label}
</label>
`).join("");
return `
<div class="modal-card" data-id="${card.id}">
<label class="head"><input type="checkbox" class="vis" ${checked} /> ${card.title}</label>
<div class="radios">${radios}</div>
</div>
`;
}).join("");
mask.classList.add("open");
}
function closeSettings() { mask.classList.remove("open"); }
document.getElementById("openSettings").addEventListener("click", openSettings);
document.getElementById("closeSettings").addEventListener("click", closeSettings);
mask.addEventListener("click", (e) => { if (e.target === mask) closeSettings(); });
document.getElementById("resetSettings").addEventListener("click", () => {
prefs = defaultPrefs();
savePrefs(prefs);
openSettings();
renderKpis();
});
document.getElementById("saveSettings").addEventListener("click", () => {
const visible = [];
const primaryByCardId = { ...prefs.primaryByCardId };
document.querySelectorAll("#settingsBody .modal-card").forEach((el) => {
const id = el.dataset.id;
if (el.querySelector(".vis").checked) visible.push(id);
const checked = el.querySelector(`input[name="primary-${id}"]:checked`);
if (checked) primaryByCardId[id] = checked.value;
});
if (!visible.length) {
alert("请至少选择一张 KPI 卡片");
return;
}
prefs = { visibleCardIds: visible, primaryByCardId };
savePrefs(prefs);
renderKpis();
closeSettings();
});
document.getElementById("trendTabs").addEventListener("click", (e) => {
const btn = e.target.closest(".tab");
if (!btn) return;
document.querySelectorAll("#trendTabs .tab").forEach((t) => t.classList.remove("active"));
btn.classList.add("active");
buildTrend(btn.dataset.tab);
});
// Date label like screenshot
document.getElementById("todayLabel").textContent = "星期二, 2024年5月7日";
renderNav();
renderKpis();
renderTodos();
renderRanks();
renderOrders();
renderNotices();
buildTrend("revenue");
buildDonut();
</script>
</body>
</html>