Files
drawlogo/api-admin.html

682 lines
25 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">
<link rel="icon" type="image/svg+xml" href="logo/favicon.svg">
<title>API 管理 · 画个标</title>
<style>
:root {
--primary: #4f46e5;
--primary-dark: #4338ca;
--bg: #f3f4f6;
--panel: #fff;
--border: #e5e7eb;
--text: #1f2937;
--text-dim: #6b7280;
--text-faint: #9ca3af;
--danger: #dc2626;
--ok: #059669;
--radius: 12px;
--shadow: 0 1px 3px rgba(0,0,0,.06);
--font: "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
--mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
min-height: 100vh;
font-family: var(--font);
font-size: 13px;
color: var(--text);
background: var(--bg);
background-image: radial-gradient(900px 420px at 12% -12%, rgba(79,70,229,.08), transparent 62%);
background-repeat: no-repeat;
-webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
h1, h2, h3, p { margin: 0; }
button { font-family: inherit; cursor: pointer; }
[hidden] { display: none !important; }
/* 顶栏 */
.top {
position: sticky; top: 0; z-index: 20;
display: flex; align-items: center; gap: 14px;
height: 58px; padding: 0 22px;
background: rgba(255,255,255,.86);
backdrop-filter: saturate(180%) blur(12px);
border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 9px; }
.brand img { display: block; }
.brand b { font-size: 15px; letter-spacing: .2px; }
.brand .tag {
padding: 2px 8px; font-size: 11px; color: var(--primary);
background: #eef2ff; border-radius: 999px;
}
.top .spacer { flex: 1; }
.btn {
display: inline-flex; align-items: center; gap: 6px;
height: 32px; padding: 0 12px;
font-size: 12.5px; color: var(--text);
background: #fff; border: 1px solid var(--border); border-radius: 8px;
transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: #eef2ff; border-color: #c7d2fe; }
.btn.primary {
color: #fff; border-color: transparent;
background: linear-gradient(135deg, var(--primary), var(--primary-dark));
box-shadow: 0 4px 12px -6px rgba(79,70,229,.7);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.sm { height: 27px; padding: 0 9px; font-size: 12px; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: #fef2f2; border-color: #fecaca; }
.btn:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
/* 登录 */
.login-wrap {
display: flex; align-items: center; justify-content: center;
min-height: calc(100vh - 58px); padding: 24px;
}
.login-card {
width: 100%; max-width: 380px; padding: 28px;
background: var(--panel); border: 1px solid var(--border);
border-radius: 16px; box-shadow: 0 18px 50px -28px rgba(31,41,55,.35);
text-align: center;
}
.login-card .ico { font-size: 30px; }
.login-card h1 { margin-top: 10px; font-size: 19px; }
.login-card p { margin-top: 8px; font-size: 12px; color: var(--text-dim); line-height: 1.7; }
.login-card input {
width: 100%; height: 38px; margin-top: 18px; padding: 0 12px;
font-family: var(--mono); font-size: 12.5px;
background: #f9fafb; border: 1px solid var(--border); border-radius: 8px;
}
.login-card .btn { width: 100%; height: 38px; margin-top: 12px; justify-content: center; }
.login-err { min-height: 16px; margin-top: 10px; font-size: 12px; color: var(--danger); }
/* 主体 */
.wrap { max-width: 1180px; margin: 0 auto; padding: 26px 22px 50px; }
.head { display: flex; align-items: flex-end; gap: 14px; flex-wrap: wrap; margin-bottom: 18px; }
.head h1 { font-size: 22px; letter-spacing: -.2px; }
.head p { font-size: 12px; color: var(--text-dim); }
.head .spacer { flex: 1; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 12px; margin-bottom: 18px; }
.stat {
padding: 14px 16px; background: var(--panel);
border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.stat .k { font-size: 11.5px; color: var(--text-faint); }
.stat .v { margin-top: 5px; font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; }
.panel {
background: var(--panel); border: 1px solid var(--border);
border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.panel-head {
display: flex; align-items: center; gap: 10px;
padding: 13px 16px; border-bottom: 1px solid #f1f3f5;
}
.panel-head h2 { font-size: 13.5px; font-weight: 650; }
.panel-head .spacer { flex: 1; }
.panel-head input {
width: 200px; height: 30px; padding: 0 10px;
font-family: inherit; font-size: 12px;
background: #f9fafb; border: 1px solid var(--border); border-radius: 7px;
}
.tbl-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 1000px; }
th, td { padding: 11px 14px; text-align: left; vertical-align: middle; }
th {
font-size: 11px; font-weight: 500; color: var(--text-faint);
background: #fafbfc; border-bottom: 1px solid var(--border); white-space: nowrap;
}
td { border-bottom: 1px solid #f4f5f7; font-size: 12.5px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbff; }
.name { font-weight: 600; }
.sub { margin-top: 2px; font-family: var(--mono); font-size: 10.5px; color: var(--text-faint); }
.note { max-width: 200px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.key-cell { white-space: nowrap; }
.key-val {
display: inline-block; margin-right: 6px; padding: 2px 6px;
font-family: var(--mono); font-size: 10.5px; color: #4b5563;
background: #f8fafc; border: 1px solid var(--border); border-radius: 6px;
}
.key-old { font-size: 11px; color: var(--text-faint); }
.tip {
margin: 12px 16px 0; padding: 9px 12px;
font-size: 11.5px; line-height: 1.6; color: #92400e;
background: #fffbeb; border: 1px solid #fde68a; border-radius: 8px;
}
.pill {
display: inline-flex; align-items: center; gap: 5px;
padding: 2px 8px; font-size: 11px; border-radius: 999px; white-space: nowrap;
}
.pill::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: currentColor; }
.pill.ok { color: #047857; background: #ecfdf5; }
.pill.off { color: #92400e; background: #fffbeb; }
.pill.exp { color: #b91c1c; background: #fef2f2; }
.ops { display: flex; gap: 5px; justify-content: flex-end; white-space: nowrap; }
.empty { padding: 44px 16px; text-align: center; font-size: 12.5px; color: var(--text-faint); }
/* 弹窗 */
.mask {
position: fixed; inset: 0; z-index: 50;
display: flex; align-items: center; justify-content: center;
padding: 20px; background: rgba(17,24,39,.38);
}
.modal {
width: 100%; max-width: 440px; padding: 22px;
background: #fff; border-radius: 16px;
box-shadow: 0 30px 80px -30px rgba(17,24,39,.5);
animation: pop .16s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.985); } }
.modal h3 { font-size: 16px; }
.modal .hint { margin-top: 5px; font-size: 11.5px; color: var(--text-dim); line-height: 1.6; }
.field { margin-top: 14px; }
.field label { display: block; margin-bottom: 5px; font-size: 11.5px; color: var(--text-dim); }
.field input, .field textarea, .field select {
width: 100%; padding: 8px 10px;
font-family: inherit; font-size: 12.5px;
background: #f9fafb; border: 1px solid var(--border); border-radius: 8px;
}
.field textarea { resize: vertical; min-height: 58px; }
.field .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.modal-err { min-height: 16px; margin-top: 10px; font-size: 12px; color: var(--danger); }
/* Key 结果 */
.key-box {
margin-top: 14px; padding: 12px;
font-family: var(--mono); font-size: 12px; word-break: break-all;
background: #f5f7ff; border: 1px dashed #c7d2fe; border-radius: 9px;
}
.key-warn { margin-top: 12px; font-size: 11.5px; color: #92400e; background: #fffbeb; border: 1px solid #fde68a; border-radius: 8px; padding: 9px 11px; line-height: 1.6; }
@media (max-width: 720px) {
.stats { grid-template-columns: 1fr; }
.panel-head input { width: 130px; }
.wrap { padding: 18px 14px 40px; }
}
</style>
</head>
<body>
<header class="top">
<a class="brand" href="home.html" title="返回设计中枢">
<img src="logo/logo-mark.svg" alt="" width="22" height="22">
<b>画个标</b>
</a>
<span class="tag">API 管理</span>
<span class="spacer"></span>
<button class="btn sm" id="btn-logout" hidden>退出</button>
<a class="btn sm" href="home.html">← 设计中枢</a>
</header>
<!-- 登录 -->
<div class="login-wrap" id="login-view">
<div class="login-card">
<div class="ico">🔌</div>
<h1>素材 API 管理</h1>
<p>输入管理口令以查看与签发 API Key。<br>口令配置在服务器 <code>/opt/drawlogo-api/config.json</code> 的 <code>adminToken</code>。</p>
<input id="admin-token" type="password" placeholder="管理口令" autocomplete="current-password">
<button class="btn primary" id="btn-login">进入管理后台</button>
<div class="login-err" id="login-err"></div>
</div>
</div>
<!-- 管理界面 -->
<main class="wrap" id="admin-view" hidden>
<div class="head">
<div>
<h1>API Key 管理</h1>
<p>为每个调用方签发独立 Key,按使用者备注与限额单独管控。</p>
</div>
<span class="spacer"></span>
<button class="btn primary" id="btn-new">+ 新建 API Key</button>
</div>
<div class="stats">
<div class="stat"><div class="k">Key 总数</div><div class="v" id="st-total">0</div></div>
<div class="stat"><div class="k">启用中</div><div class="v" id="st-enabled">0</div></div>
<div class="stat"><div class="k">今日调用</div><div class="v" id="st-today">0</div></div>
</div>
<section class="panel">
<div class="panel-head">
<h2>调用方清单</h2>
<span class="spacer"></span>
<input id="filter" type="search" placeholder="筛选使用者 / 备注">
<button class="btn sm" id="btn-refresh">刷新</button>
</div>
<div class="tip" id="old-tip" hidden>早期生成的 Key 只保存了哈希,无法查看明文。如需复制接入信息,请重新生成一个(旧 Key 可删除)。</div>
<div class="tbl-wrap">
<table>
<thead>
<tr>
<th>使用者</th>
<th>口令</th>
<th>备注</th>
<th>状态</th>
<th>限额</th>
<th>今日</th>
<th>最近使用</th>
<th style="text-align:right">操作</th>
</tr>
</thead>
<tbody id="rows"></tbody>
</table>
</div>
<div class="empty" id="empty" hidden>还没有 API Key,点击右上角「新建 API Key」开始。</div>
</section>
</main>
<!-- 新建 / 编辑弹窗 -->
<div class="mask" id="form-mask" hidden>
<div class="modal" role="dialog" aria-modal="true">
<h3 id="form-title">新建 API Key</h3>
<p class="hint" id="form-hint">签发后明文只显示一次,请立即交给调用方保存。</p>
<div class="field">
<label>使用者(调用方名称)*</label>
<input id="f-name" placeholder="如:官网项目 / 小程序后台 / 合作方 A" maxlength="40">
</div>
<div class="field">
<label>备注</label>
<textarea id="f-note" placeholder="用途、负责人、对接方式等" maxlength="200"></textarea>
</div>
<div class="field">
<div class="row2">
<div>
<label>每分钟限流</label>
<input id="f-rate" type="number" min="1" step="1" value="60">
</div>
<div>
<label>每日配额</label>
<input id="f-quota" type="number" min="1" step="1" value="20000">
</div>
</div>
</div>
<div class="field">
<label>有效期(留空 = 长期有效)</label>
<input id="f-expires" type="date">
</div>
<div class="modal-err" id="form-err"></div>
<div class="modal-foot">
<button class="btn" id="btn-cancel">取消</button>
<button class="btn primary" id="btn-save">保存</button>
</div>
</div>
</div>
<!-- 新建结果 -->
<div class="mask" id="key-mask" hidden>
<div class="modal" role="dialog" aria-modal="true">
<h3>✅ API Key 已生成</h3>
<p class="hint" id="key-for"></p>
<div class="key-box" id="key-value"></div>
<div class="key-warn">内部使用:该口令已明文保存在服务器,可随时在本页复制。请勿发到公开群聊或写进前端代码。</div>
<div class="modal-foot">
<button class="btn" id="btn-copy">仅复制口令</button>
<button class="btn primary" id="btn-copy-info">复制接入信息</button>
<button class="btn" id="btn-close-key">关闭</button>
</div>
</div>
</div>
<script>
(function () {
'use strict';
var $ = function (s) { return document.querySelector(s); };
var TOKEN_KEY = 'hgb_admin_token';
var token = '';
var keys = [];
var editingId = null;
var lastCreated = null;
// 对外接入地址(复制给调用方用)
var API_ENDPOINT = 'https://drawlogo.zhuig.com/api/v1';
// 本地预览时直连本地 API;线上同源
var IS_LOCAL = /^(localhost|127\.0\.0\.1|\[::1\])$/.test(location.hostname);
var API_BASE = IS_LOCAL ? 'http://127.0.0.1:7891' : '';
function esc(s) {
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c];
});
}
/** 口令掩码:前 10 + 后 6 */
function maskKey(k) {
if (!k) return '';
if (k.length <= 20) return k;
return k.slice(0, 10) + '…' + k.slice(-6);
}
/** 一键复制用的接入信息(地址 + 使用者 + 口令) */
function infoText(rec) {
return '画个标素材 API\n地址:' + API_ENDPOINT + '\n使用者:' + rec.name + '\n口令:' + rec.key;
}
function api(path, opts) {
opts = opts || {};
opts.headers = Object.assign({ 'X-Admin-Token': token }, opts.headers || {});
if (opts.body !== undefined) {
opts.headers['Content-Type'] = 'application/json';
opts.body = JSON.stringify(opts.body);
}
return fetch(API_BASE + path, opts).then(function (res) {
return res.json().catch(function () { return {}; }).then(function (data) {
if (!res.ok || !data.ok) {
var err = new Error((data.error && data.error.message) || ('HTTP ' + res.status));
err.status = res.status;
throw err;
}
return data.data;
});
});
}
function fmtTime(iso) {
if (!iso) return '—';
var t = Date.parse(iso);
if (!isFinite(t)) return '—';
var diff = Date.now() - t;
if (diff < 60e3) return '刚刚';
if (diff < 3600e3) return Math.floor(diff / 60e3) + ' 分钟前';
if (diff < 86400e3) return Math.floor(diff / 3600e3) + ' 小时前';
if (diff < 7 * 86400e3) return Math.floor(diff / 86400e3) + ' 天前';
var d = new Date(t);
return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
}
function isExpired(k) {
return k.expiresAt && Date.parse(k.expiresAt) < Date.now();
}
/* ---------- 登录态 ---------- */
function showAdmin() {
$('#login-view').hidden = true;
$('#admin-view').hidden = false;
$('#btn-logout').hidden = false;
load();
}
function showLogin(msg) {
$('#login-view').hidden = false;
$('#admin-view').hidden = true;
$('#btn-logout').hidden = true;
if (msg) $('#login-err').textContent = msg;
}
function login() {
var v = $('#admin-token').value.trim();
if (!v) { $('#login-err').textContent = '请输入管理口令'; return; }
$('#login-err').textContent = '';
var btn = $('#btn-login');
btn.disabled = true;
btn.textContent = '验证中…';
token = v;
api('/api/v1/admin/keys').then(function (d) {
sessionStorage.setItem(TOKEN_KEY, token);
keys = d.keys || [];
showAdmin();
render(d);
}).catch(function (e) {
token = '';
showLogin(e.status === 401 ? '管理口令错误' : (e.message || '连接失败'));
}).then(function () {
btn.disabled = false;
btn.textContent = '进入管理后台';
});
}
/* ---------- 列表渲染 ---------- */
function render(data) {
var stats = (data && data.stats) || { total: keys.length, enabled: 0, todayCalls: 0 };
$('#st-total').textContent = stats.total;
$('#st-enabled').textContent = stats.enabled;
$('#st-today').textContent = stats.todayCalls;
var q = ($('#filter').value || '').trim().toLowerCase();
var list = keys.filter(function (k) {
if (!q) return true;
return (k.name + ' ' + (k.note || '') + ' ' + k.id).toLowerCase().indexOf(q) !== -1;
});
$('#old-tip').hidden = !keys.some(function (k) { return !k.key; });
$('#empty').hidden = list.length > 0;
$('#rows').innerHTML = list.map(function (k) {
var expired = isExpired(k);
var pill = !k.enabled ? '<span class="pill off">已禁用</span>'
: expired ? '<span class="pill exp">已过期</span>'
: '<span class="pill ok">启用中</span>';
return '<tr data-id="' + esc(k.id) + '">' +
'<td><div class="name">' + esc(k.name) + '</div><div class="sub">' + esc(k.id) + '</div></td>' +
'<td class="key-cell">' +
(k.key
? '<code class="key-val" title="' + esc(k.key) + '">' + esc(maskKey(k.key)) + '</code>' +
'<button class="btn sm" data-act="copy">复制</button>'
: '<span class="key-old">不可查看</span>') +
'</td>' +
'<td class="note" title="' + esc(k.note) + '">' + (k.note ? esc(k.note) : '<span style="color:#c3c9d2">—</span>') + '</td>' +
'<td>' + pill + '</td>' +
'<td><span class="mono">' + k.ratePerMin + '</span><span style="color:#9ca3af">/分</span><br>' +
'<span class="mono">' + Number(k.dailyQuota).toLocaleString('zh-CN') + '</span><span style="color:#9ca3af">/天</span></td>' +
'<td class="mono">' + k.todayCount + '</td>' +
'<td>' + fmtTime(k.lastUsedAt) + '</td>' +
'<td><div class="ops">' +
'<button class="btn sm" data-act="toggle">' + (k.enabled ? '禁用' : '启用') + '</button>' +
'<button class="btn sm" data-act="edit">编辑</button>' +
'<button class="btn sm danger" data-act="del">删除</button>' +
'</div></td>' +
'</tr>';
}).join('');
}
function load() {
return api('/api/v1/admin/keys').then(function (d) {
keys = d.keys || [];
render(d);
}).catch(function (e) {
if (e.status === 401) {
sessionStorage.removeItem(TOKEN_KEY);
token = '';
showLogin('登录已失效,请重新输入口令');
}
});
}
/* ---------- 弹窗 ---------- */
function openForm(rec) {
editingId = rec ? rec.id : null;
$('#form-title').textContent = rec ? '编辑 API Key' : '新建 API Key';
$('#form-hint').textContent = rec ? '修改立即生效,无需重启服务。' : '签发后明文只显示一次,请立即交给调用方保存。';
$('#f-name').value = rec ? rec.name : '';
$('#f-note').value = rec ? rec.note : '';
$('#f-rate').value = rec ? rec.ratePerMin : 60;
$('#f-quota').value = rec ? rec.dailyQuota : 20000;
$('#f-expires').value = rec && rec.expiresAt ? String(rec.expiresAt).slice(0, 10) : '';
$('#form-err').textContent = '';
$('#form-mask').hidden = false;
setTimeout(function () { $('#f-name').focus(); }, 30);
}
function closeForm() {
$('#form-mask').hidden = true;
editingId = null;
}
function save() {
var name = $('#f-name').value.trim();
if (!name) { $('#form-err').textContent = '请填写使用者名称'; return; }
var body = {
name: name,
note: $('#f-note').value.trim(),
ratePerMin: Math.max(1, parseInt($('#f-rate').value, 10) || 60),
dailyQuota: Math.max(1, parseInt($('#f-quota').value, 10) || 20000),
expiresAt: $('#f-expires').value ? $('#f-expires').value + 'T23:59:59.000Z' : null
};
var btn = $('#btn-save');
btn.disabled = true;
var req = editingId
? api('/api/v1/admin/keys/' + encodeURIComponent(editingId), { method: 'PATCH', body: body })
: api('/api/v1/admin/keys', { method: 'POST', body: body });
req.then(function (d) {
closeForm();
if (!editingId && d && d.key) {
lastCreated = d.record;
$('#key-for').textContent = '使用者:' + d.record.name + '(' + d.record.id + ')';
$('#key-value').textContent = d.key;
$('#key-mask').hidden = false;
}
return load();
}).catch(function (e) {
$('#form-err').textContent = e.message || '保存失败';
}).then(function () { btn.disabled = false; });
}
function toggle(id) {
var rec = keys.filter(function (k) { return k.id === id; })[0];
if (!rec) return;
api('/api/v1/admin/keys/' + encodeURIComponent(id), {
method: 'PATCH',
body: { enabled: !rec.enabled }
}).then(load).catch(function (e) { alert(e.message); });
}
function remove(id) {
var rec = keys.filter(function (k) { return k.id === id; })[0];
if (!rec) return;
if (!confirm('确定删除「' + rec.name + '」的 API Key?删除后该 Key 立即失效,且不可恢复。')) return;
api('/api/v1/admin/keys/' + encodeURIComponent(id), { method: 'DELETE' })
.then(load).catch(function (e) { alert(e.message); });
}
/* ---------- 事件 ---------- */
$('#btn-login').addEventListener('click', login);
$('#admin-token').addEventListener('keydown', function (e) { if (e.key === 'Enter') login(); });
$('#btn-logout').addEventListener('click', function () {
sessionStorage.removeItem(TOKEN_KEY);
token = '';
$('#admin-token').value = '';
showLogin('');
});
$('#btn-new').addEventListener('click', function () { openForm(null); });
$('#btn-refresh').addEventListener('click', load);
$('#filter').addEventListener('input', function () { render(); });
$('#btn-cancel').addEventListener('click', closeForm);
$('#btn-save').addEventListener('click', save);
$('#btn-close-key').addEventListener('click', function () { $('#key-mask').hidden = true; });
$('#btn-copy').addEventListener('click', function () {
var btn = this;
copyText($('#key-value').textContent, function () { flash(btn, '已复制口令'); });
});
$('#btn-copy-info').addEventListener('click', function () {
var btn = this;
if (!lastCreated || !lastCreated.key) return;
copyText(infoText(lastCreated), function () { flash(btn, '已复制接入信息'); });
});
function copyText(text, done) {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(done).catch(function () { fallbackCopy(text, done); });
} else {
fallbackCopy(text, done);
}
}
function flash(btn, text) {
var old = btn.textContent;
btn.textContent = text || '已复制';
setTimeout(function () { btn.textContent = old; }, 1500);
}
function fallbackCopy(text, done) {
var ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
try { document.execCommand('copy'); done(); } catch (e) { /* ignore */ }
document.body.removeChild(ta);
}
// 表格操作(事件委托)
$('#rows').addEventListener('click', function (e) {
var btn = e.target.closest('[data-act]');
if (!btn) return;
var tr = e.target.closest('tr');
if (!tr) return;
var id = tr.getAttribute('data-id');
var act = btn.getAttribute('data-act');
if (act === 'copy') {
var r = keys.filter(function (k) { return k.id === id; })[0];
if (r && r.key) copyText(infoText(r), function () { flash(btn); });
return;
}
if (act === 'toggle') toggle(id);
else if (act === 'del') remove(id);
else if (act === 'edit') {
var rec = keys.filter(function (k) { return k.id === id; })[0];
if (rec) openForm(rec);
}
});
// 点击遮罩关闭
['#form-mask', '#key-mask'].forEach(function (sel) {
$(sel).addEventListener('click', function (e) {
if (e.target === $(sel)) {
if (sel === '#form-mask') closeForm();
else $('#key-mask').hidden = true;
}
});
});
// Esc 关闭弹窗
document.addEventListener('keydown', function (e) {
if (e.key !== 'Escape') return;
if (!$('#key-mask').hidden) $('#key-mask').hidden = true;
else if (!$('#form-mask').hidden) closeForm();
});
/* ---------- 启动 ---------- */
var saved = sessionStorage.getItem(TOKEN_KEY);
if (saved) {
token = saved;
api('/api/v1/admin/keys').then(function (d) {
keys = d.keys || [];
showAdmin();
render(d);
}).catch(function () {
sessionStorage.removeItem(TOKEN_KEY);
token = '';
showLogin('');
});
} else {
showLogin('');
}
})();
</script>
</body>
</html>