Files
crypto_key/index.html
T
2026-05-19 01:10:36 +08:00

542 lines
15 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API 密钥管理</title>
<style>
:root {
--bg: #0d0d0f;
--surface: #16161a;
--surface-hover: #1c1c22;
--border: #2a2a32;
--text: #e4e4e7;
--text-muted: #71717a;
--accent: #3b82f6;
--accent-hover: #2563eb;
--success: #22c55e;
--danger: #ef4444;
--radius: 8px;
--font: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
--mono: "Cascadia Code", "Consolas", "SF Mono", monospace;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: var(--font);
background: var(--bg);
color: var(--text);
min-height: 100vh;
line-height: 1.5;
}
.wrap {
max-width: 960px;
margin: 0 auto;
padding: 32px 20px 48px;
}
header {
margin-bottom: 32px;
padding-bottom: 20px;
border-bottom: 1px solid var(--border);
}
header h1 { font-size: 1.5rem; font-weight: 600; }
header p { margin-top: 6px; font-size: 0.875rem; color: var(--text-muted); }
.panel {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 20px;
margin-bottom: 24px;
}
.panel h2 {
font-size: 0.8125rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--text-muted);
margin-bottom: 16px;
}
.form-grid { display: grid; gap: 12px; }
@media (min-width: 720px) {
.form-grid { grid-template-columns: repeat(3, 1fr); }
.form-grid .span-3 { grid-column: span 3; }
.form-grid .span-2 { grid-column: span 2; }
}
label {
display: block;
font-size: 0.75rem;
color: var(--text-muted);
margin-bottom: 4px;
}
input, select {
width: 100%;
padding: 10px 12px;
background: var(--bg);
border: 1px solid var(--border);
border-radius: 6px;
color: var(--text);
font-size: 0.8125rem;
}
input { font-family: var(--mono); }
select { font-family: var(--font); cursor: pointer; }
input:focus, select:focus {
outline: none;
border-color: var(--accent);
}
.hidden { display: none !important; }
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 8px 14px;
font-size: 0.8125rem;
font-weight: 500;
border: none;
border-radius: 6px;
cursor: pointer;
font-family: var(--font);
white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-copy {
background: var(--bg);
color: var(--text-muted);
border: 1px solid var(--border);
padding: 4px 10px;
font-size: 0.75rem;
flex-shrink: 0;
}
.btn-copy:hover { color: var(--text); border-color: var(--accent); }
.btn-copy.copied { color: var(--success); border-color: var(--success); }
.btn-danger {
background: transparent;
color: var(--danger);
padding: 4px 8px;
font-size: 0.75rem;
}
.btn-danger:hover { background: rgba(239, 68, 68, 0.1); }
.search-row {
display: flex;
gap: 10px;
align-items: flex-end;
}
.search-row .search-select { flex: 1; }
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
.toolbar .count { font-size: 0.8125rem; color: var(--text-muted); }
.toggle-mask {
display: flex;
align-items: center;
gap: 8px;
font-size: 0.8125rem;
color: var(--text-muted);
cursor: pointer;
}
.toggle-mask input { width: auto; accent-color: var(--accent); }
.account-list { display: flex; flex-direction: column; gap: 12px; }
.account-card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 16px;
}
.account-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
padding-bottom: 10px;
border-bottom: 1px solid var(--border);
}
.account-title {
display: flex;
align-items: center;
gap: 8px;
}
.badge {
font-size: 0.6875rem;
font-weight: 600;
text-transform: uppercase;
padding: 2px 8px;
border-radius: 4px;
background: var(--bg);
border: 1px solid var(--border);
color: var(--accent);
}
.account-name { font-weight: 600; font-size: 0.9375rem; }
.field {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
}
.field:last-child { margin-bottom: 0; }
.field-label {
width: 100px;
flex-shrink: 0;
font-size: 0.75rem;
color: var(--text-muted);
}
.field-value {
flex: 1;
min-width: 0;
font-family: var(--mono);
font-size: 0.8125rem;
word-break: break-all;
}
.empty {
text-align: center;
padding: 48px 20px;
color: var(--text-muted);
font-size: 0.875rem;
border: 1px dashed var(--border);
border-radius: var(--radius);
}
.toast {
position: fixed;
bottom: 24px;
right: 24px;
padding: 10px 16px;
background: var(--surface);
border: 1px solid var(--success);
color: var(--success);
border-radius: 6px;
font-size: 0.8125rem;
opacity: 0;
transition: opacity 0.2s;
pointer-events: none;
z-index: 100;
}
.toast.show { opacity: 1; }
.error-msg { color: var(--danger); font-size: 0.8125rem; margin-top: 8px; }
</style>
</head>
<body>
<div class="wrap">
<header>
<h1>API 密钥管理</h1>
<p>按交易所分类存储 · 查询后显示 · 复制始终为明文</p>
</header>
<section class="panel">
<h2>添加账户</h2>
<form id="addForm" class="form-grid">
<div>
<label for="exchange">交易所</label>
<select id="exchange" name="exchange" required>
<option value="">请选择</option>
<option value="binance">Binance</option>
<option value="okx">OKX</option>
<option value="gate">Gate</option>
</select>
</div>
<div>
<label for="username">账户名称 (username)</label>
<input type="text" id="username" placeholder="例如: main_account" autocomplete="off" required>
</div>
<div id="okxPasswordWrap" class="hidden">
<label for="password">OKX 密码 (Passphrase)</label>
<input type="text" id="password" placeholder="OKX API 密码" autocomplete="off">
</div>
<div>
<label for="api_key">API Key</label>
<input type="text" id="api_key" placeholder="输入 API Key" autocomplete="off" required>
</div>
<div>
<label for="api_secret">API Secret</label>
<input type="text" id="api_secret" placeholder="输入 API Secret" autocomplete="off" required>
</div>
<div class="span-3">
<button type="submit" class="btn btn-primary">添加账户</button>
<p id="formError" class="error-msg" hidden></p>
</div>
</form>
</section>
<section class="panel">
<h2>查询账户</h2>
<div class="search-row">
<div class="search-select">
<label for="searchExchange">选择交易所</label>
<select id="searchExchange">
<option value="">请选择交易所</option>
<option value="binance">Binance</option>
<option value="okx">OKX</option>
<option value="gate">Gate</option>
</select>
</div>
<button type="button" id="searchConfirm" class="btn btn-primary">确认</button>
</div>
</section>
<section>
<div class="toolbar">
<span class="count" id="accountCount">未查询</span>
<label class="toggle-mask">
<input type="checkbox" id="maskToggle" checked>
隐藏敏感字段(复制仍为明文)
</label>
</div>
<div id="accountList" class="account-list">
<div class="empty" id="listPlaceholder">请选择交易所并点击「确认」查看账户</div>
</div>
</section>
</div>
<div id="toast" class="toast">已复制到剪贴板</div>
<script>
const API = "/api/accounts";
const EXCHANGE_LABEL = { binance: "Binance", okx: "OKX", gate: "Gate" };
let accounts = [];
let displayed = [];
let activeExchange = null;
let masked = true;
const listEl = document.getElementById("accountList");
const countEl = document.getElementById("accountCount");
const maskToggle = document.getElementById("maskToggle");
const toast = document.getElementById("toast");
const form = document.getElementById("addForm");
const formError = document.getElementById("formError");
const exchangeSelect = document.getElementById("exchange");
const okxPasswordWrap = document.getElementById("okxPasswordWrap");
const passwordInput = document.getElementById("password");
const searchExchange = document.getElementById("searchExchange");
const searchConfirm = document.getElementById("searchConfirm");
function maskValue(value) {
if (!masked || !value) return value || "";
return "•".repeat(Math.min(String(value).length, 24));
}
function showToast(msg) {
toast.textContent = msg;
toast.classList.add("show");
clearTimeout(showToast._t);
showToast._t = setTimeout(() => toast.classList.remove("show"), 1800);
}
async function copyText(text, btn) {
try {
await navigator.clipboard.writeText(text);
} catch {
const ta = document.createElement("textarea");
ta.value = text;
document.body.appendChild(ta);
ta.select();
document.execCommand("copy");
document.body.removeChild(ta);
}
btn.classList.add("copied");
btn.textContent = "已复制";
showToast("已复制到剪贴板");
setTimeout(() => {
btn.classList.remove("copied");
btn.textContent = "复制";
}, 1500);
}
function renderField(label, value, copyLabel) {
const row = document.createElement("div");
row.className = "field";
row.innerHTML = `<span class="field-label">${label}</span><span class="field-value"></span>`;
row.querySelector(".field-value").textContent = maskValue(value);
const btn = document.createElement("button");
btn.type = "button";
btn.className = "btn btn-copy";
btn.textContent = "复制";
btn.addEventListener("click", () => copyText(value || "", btn));
row.appendChild(btn);
return row;
}
function renderAccount(acc) {
const card = document.createElement("article");
card.className = "account-card";
const header = document.createElement("div");
header.className = "account-header";
const title = document.createElement("div");
title.className = "account-title";
const badge = document.createElement("span");
badge.className = "badge";
badge.textContent = EXCHANGE_LABEL[acc.exchange] || acc.exchange || "—";
const name = document.createElement("span");
name.className = "account-name";
name.textContent = maskValue(acc.username);
title.appendChild(badge);
title.appendChild(name);
const delBtn = document.createElement("button");
delBtn.type = "button";
delBtn.className = "btn btn-danger";
delBtn.textContent = "删除";
delBtn.addEventListener("click", () => deleteAccount(acc.id));
header.appendChild(title);
header.appendChild(delBtn);
card.appendChild(header);
card.appendChild(renderField("账户名称", acc.username, "账户名称"));
card.appendChild(renderField("API Key", acc.api_key, "API Key"));
card.appendChild(renderField("API Secret", acc.api_secret, "API Secret"));
if (acc.exchange === "okx" && acc.password) {
card.appendChild(renderField("OKX 密码", acc.password, "OKX 密码"));
}
return card;
}
function showPlaceholder(msg) {
listEl.innerHTML = `<div class="empty">${msg}</div>`;
countEl.textContent = "未查询";
}
function renderList() {
listEl.innerHTML = "";
if (!activeExchange) {
showPlaceholder("请选择交易所并点击「确认」查看账户");
return;
}
const label = EXCHANGE_LABEL[activeExchange] || activeExchange;
countEl.textContent = `${label} · 共 ${displayed.length} 个账户`;
if (displayed.length === 0) {
listEl.innerHTML = `<div class="empty">${label} 暂无账户</div>`;
return;
}
displayed.forEach(acc => listEl.appendChild(renderAccount(acc)));
}
async function loadAccounts() {
const res = await fetch(API);
accounts = await res.json();
}
async function queryByExchange() {
const ex = searchExchange.value;
if (!ex) {
showToast("请先选择交易所");
return;
}
activeExchange = ex;
const res = await fetch(`${API}?exchange=${encodeURIComponent(ex)}`);
displayed = await res.json();
renderList();
}
async function deleteAccount(id) {
if (!confirm("确定删除该账户?")) return;
await fetch(`${API}/${id}`, { method: "DELETE" });
accounts = accounts.filter(a => a.id !== id);
displayed = displayed.filter(a => a.id !== id);
renderList();
showToast("已删除");
}
function toggleOkxField() {
const isOkx = exchangeSelect.value === "okx";
okxPasswordWrap.classList.toggle("hidden", !isOkx);
passwordInput.required = isOkx;
if (!isOkx) passwordInput.value = "";
}
exchangeSelect.addEventListener("change", toggleOkxField);
form.addEventListener("submit", async (e) => {
e.preventDefault();
formError.hidden = true;
const payload = {
exchange: exchangeSelect.value,
username: document.getElementById("username").value,
api_key: document.getElementById("api_key").value,
api_secret: document.getElementById("api_secret").value,
};
if (payload.exchange === "okx") {
payload.password = passwordInput.value;
}
const res = await fetch(API, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
const data = await res.json();
if (!res.ok) {
formError.textContent = data.error || "添加失败";
formError.hidden = false;
return;
}
accounts.push(data);
form.reset();
toggleOkxField();
masked = true;
maskToggle.checked = true;
activeExchange = null;
displayed = [];
showPlaceholder("添加成功,请选择交易所并点击「确认」查看");
showToast("账户已添加");
});
searchConfirm.addEventListener("click", queryByExchange);
maskToggle.addEventListener("change", () => {
masked = maskToggle.checked;
if (activeExchange) renderList();
});
toggleOkxField();
loadAccounts();
</script>
</body>
</html>