mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-06 00:01:05 +00:00
809 lines
32 KiB
HTML
809 lines
32 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 data-i18n="cache.pageTitle">Grok2API - 缓存管理</title>
|
|
<link rel="icon" href="/favicon.ico">
|
|
<link href="https://cdn.jsdelivr.net/npm/geist@1.0.0/dist/fonts/geist-sans/style.css" rel="stylesheet">
|
|
<link href="/static/css/app.css" rel="stylesheet">
|
|
<style>
|
|
body { background: #FAF9F5 }
|
|
|
|
/* Page header */
|
|
.page-hd { display:flex; align-items:flex-start; justify-content:space-between; margin-bottom:20px }
|
|
.page-title{ font-size:22px; font-weight:700; line-height:1.2 }
|
|
.page-sub { font-size:13px; color:var(--fg-muted); margin-top:5px }
|
|
.page-actions { display:flex; gap:8px; align-items:center; flex-shrink:0 }
|
|
.section-head {
|
|
display:flex;
|
|
align-items:baseline;
|
|
justify-content:space-between;
|
|
gap:12px;
|
|
margin:28px 0 14px;
|
|
}
|
|
.section-title {
|
|
font-size:13px;
|
|
font-weight:600;
|
|
color:#222;
|
|
letter-spacing:.01em;
|
|
}
|
|
.page-action-btn {
|
|
height:32px; padding:0 14px; border-radius:999px;
|
|
display:inline-flex; align-items:center; justify-content:center; gap:6px;
|
|
font-size:13px; font-weight:600;
|
|
border:1px solid #e6e6e6; background:#fafafa; color:#444;
|
|
}
|
|
.page-action-btn:hover { background:#f3f3f3; border-color:#dcdcdc }
|
|
.page-action-btn-primary { background:#111; border-color:#111; color:#fff }
|
|
.page-action-btn-primary:hover { background:#222; border-color:#222 }
|
|
|
|
/* Stats */
|
|
.stat-grid { display:grid; grid-template-columns:repeat(4, minmax(0, 1fr)); gap:12px; margin-bottom:30px }
|
|
.stat-cell { min-height:88px; padding:14px 16px; border-radius:12px; background:#fff; display:flex; flex-direction:column; gap:12px }
|
|
.stat-top { display:flex; align-items:center; justify-content:space-between; gap:10px }
|
|
.stat-label{ font-size:11px; color:#8a8a8a; letter-spacing:.01em; white-space:nowrap }
|
|
.stat-icon { width:22px; height:22px; display:inline-flex; align-items:center; justify-content:center; color:#a3a3a3; flex:0 0 auto }
|
|
.stat-icon svg { width:14px; height:14px; stroke:currentColor }
|
|
.stat-num { font-size:22px; font-weight:600; line-height:1; letter-spacing:-.02em; margin-top:auto }
|
|
|
|
/* Section tabs */
|
|
.sec-tabs {
|
|
display:flex;
|
|
align-items:center;
|
|
gap:6px;
|
|
align-self:flex-start;
|
|
flex-wrap:wrap;
|
|
padding:0;
|
|
}
|
|
.sec-tab {
|
|
height:30px; padding:0 12px; border-radius:999px;
|
|
font-size:12px; font-weight:500; color:#8f8f8f;
|
|
background:#f5f5f5; cursor:pointer;
|
|
transition:background .15s, color .15s;
|
|
}
|
|
.sec-tab:hover { color:#555; background:#f1f1f1 }
|
|
.sec-tab.active { background:#111; color:#fff; font-weight:600 }
|
|
.sec-panel { display:none }
|
|
.sec-panel.active { display:block }
|
|
|
|
/* Sub-tabs (image / video) */
|
|
.filter-bar { display:flex; align-items:center; gap:6px; margin-bottom:14px }
|
|
.ftab { height:30px; padding:0 12px; border-radius:999px; font-size:12px; font-weight:500; color:#8f8f8f; background:#f5f5f5; display:flex; align-items:center; gap:6px; transition:background .15s, color .15s; white-space:nowrap }
|
|
.ftab:hover { color:#555; background:#f1f1f1 }
|
|
.ftab.active { background:#111; color:#fff }
|
|
.ftab-n { min-width:18px; height:18px; padding:0 5px; border-radius:999px; background:rgba(255,255,255,.72); font-size:11px; font-weight:600; color:inherit; display:inline-flex; align-items:center; justify-content:center; font-variant-numeric:tabular-nums }
|
|
.ftab.active .ftab-n { background:rgba(255,255,255,.16); color:#fff }
|
|
|
|
/* Toolbar */
|
|
.table-toolbar { display:flex; align-items:center; gap:8px; margin-bottom:10px; flex-wrap:wrap }
|
|
.table-toolbar-left { flex:1; display:flex; align-items:center; gap:6px }
|
|
.table-toolbar-actions { display:flex; align-items:center; gap:8px }
|
|
.toolbar-action { height:28px; padding:0 12px; border-radius:999px; background:#fafafa; font-size:12px; font-weight:500; color:#5f5f5f; white-space:nowrap }
|
|
.toolbar-action:hover { background:#f3f3f3 }
|
|
.toolbar-action-danger { color:#b42318; background:#fff5f4; border-color:#f3d2cf }
|
|
.toolbar-action-danger:hover { background:#feeceb; border-color:#eab8b3 }
|
|
.pagi-btn { width:26px; height:26px; border-radius:6px; display:inline-flex; align-items:center; justify-content:center; background:#fafafa; border:1px solid #ececec; color:#666 }
|
|
.pagi-btn:hover { background:#f3f3f3 }
|
|
.pagi-btn:disabled { opacity:.35; pointer-events:none }
|
|
.pagi-meta { display:flex; align-items:center; gap:8px; min-height:26px }
|
|
.pagi-page { font-size:12px; font-weight:500; color:#444 }
|
|
.pagi-dot { width:4px; height:4px; border-radius:50%; background:#d4d4d4; flex:0 0 auto }
|
|
.pagi-info { font-size:11px; color:#a3a3a3 }
|
|
|
|
/* Table */
|
|
.table-card { background:#fff; border:none; border-radius:14px; overflow-x:auto; overflow-y:hidden; -webkit-overflow-scrolling:touch }
|
|
.table-card > table { width:max-content; min-width:100%; border-collapse:collapse }
|
|
.table-freeze-last { --table-sticky-bg:#fff; --table-sticky-hover-bg:#fdfdfd }
|
|
table { width:100%; border-collapse:collapse }
|
|
th { background:#fff; font-size:11px; font-weight:500; color:#9b9b9b; padding:11px 16px; text-align:left; letter-spacing:.01em }
|
|
td { font-size:13px; padding:13px 16px; border-bottom:none; vertical-align:middle; color:#3f3f3f }
|
|
tr:last-child td { border-bottom:none }
|
|
tr:hover td { background:#fdfdfd }
|
|
.table-empty { text-align:center; color:#bbb; font-size:13px; padding:40px 16px }
|
|
.row-name {
|
|
display:flex;
|
|
align-items:center;
|
|
gap:10px;
|
|
min-width:0;
|
|
}
|
|
.row-icon {
|
|
width:18px;
|
|
height:18px;
|
|
display:inline-flex;
|
|
align-items:center;
|
|
justify-content:center;
|
|
color:#9a9a9a;
|
|
flex:0 0 auto;
|
|
}
|
|
.row-icon svg {
|
|
width:13px;
|
|
height:13px;
|
|
stroke:currentColor;
|
|
stroke-linecap:round;
|
|
stroke-linejoin:round;
|
|
fill:none;
|
|
}
|
|
.row-value {
|
|
min-width:0;
|
|
font-family:'Geist Mono','Courier New',monospace;
|
|
font-size:12px;
|
|
word-break:break-all;
|
|
}
|
|
.row-actions {
|
|
display:flex;
|
|
align-items:center;
|
|
gap:10px;
|
|
flex-wrap:wrap;
|
|
}
|
|
.row-action {
|
|
color:#929292;
|
|
font-size:11px;
|
|
font-weight:500;
|
|
display:inline-flex;
|
|
align-items:center;
|
|
justify-content:center;
|
|
white-space:nowrap;
|
|
}
|
|
.row-action:hover { color:#555 }
|
|
.row-action-danger { color:#b7726a }
|
|
.meta-badge {
|
|
display:inline-flex;
|
|
align-items:center;
|
|
height:20px;
|
|
padding:0 8px;
|
|
border-radius:999px;
|
|
font-size:11px;
|
|
font-weight:500;
|
|
color:#6d6d6d;
|
|
background:#f7f7f7;
|
|
}
|
|
|
|
/* Online asset panel */
|
|
.asset-desc {
|
|
max-width:620px;
|
|
margin-bottom:14px;
|
|
font-size:11px;
|
|
color:#9a9a9a;
|
|
line-height:1.6;
|
|
}
|
|
.asset-desc strong { color:#666; font-weight:600; margin-right:4px }
|
|
/* asset sub-rows */
|
|
.sub-row td { background:#fafafa; padding:9px 16px 9px 36px; font-size:12px; color:#666; border-top:1px solid #f5f5f5 }
|
|
.sub-row:hover td { background:#f5f5f5 }
|
|
.expand-btn { width:22px; height:22px; display:inline-flex; align-items:center; justify-content:center; background:none; color:#9a9a9a; transition:transform .15s, color .15s }
|
|
.expand-btn:hover { color:#555 }
|
|
.expand-btn.open { transform:rotate(90deg) }
|
|
.asset-count-badge { display:inline-flex; align-items:center; height:20px; padding:0 8px; border-radius:999px; font-size:11px; font-weight:500; background:#f7f7f7; color:#6d6d6d }
|
|
.asset-count-badge.has-items { background:#f7f7f7; color:#6d6d6d }
|
|
.asset-err { font-size:11px; color:#dc2626 }
|
|
@keyframes spin { to { transform:rotate(360deg) } }
|
|
.progress-bar-outer { height:6px; background:#f0f0f0; border-radius:999px; overflow:hidden }
|
|
.progress-bar-inner { height:100%; background:#111; border-radius:999px; transition:width .3s }
|
|
|
|
@media (max-width: 840px) {
|
|
.page-hd,
|
|
.section-head,
|
|
.table-toolbar {
|
|
flex-direction:column;
|
|
align-items:flex-start;
|
|
}
|
|
.table-toolbar-left {
|
|
width:100%;
|
|
justify-content:flex-start;
|
|
flex-wrap:wrap;
|
|
}
|
|
.table-toolbar-actions { width:100% }
|
|
.stat-grid {
|
|
grid-template-columns:repeat(2, minmax(0, 1fr));
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<!-- ── Header (rendered by admin-header.js) ─────────────────────────────── -->
|
|
<div id="admin-header" data-active="/admin/cache"></div>
|
|
|
|
<main class="admin-main" style="padding-bottom:60px">
|
|
|
|
<!-- Page header -->
|
|
<div class="page-hd">
|
|
<div>
|
|
<div class="page-title" data-i18n="cache.pageHeading">缓存管理</div>
|
|
<div class="page-sub" data-i18n="cache.pageSubtitle">统一查看本地缓存文件与账户在线资产,并支持按类型清理。</div>
|
|
</div>
|
|
<div class="page-actions">
|
|
<button class="page-action-btn" onclick="refreshAll()">
|
|
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>
|
|
<span data-i18n="cache.refresh">刷新</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="section-head">
|
|
<div class="section-title" data-i18n="cache.sectionOverview">缓存概览</div>
|
|
</div>
|
|
|
|
<!-- Stats -->
|
|
<div class="stat-grid">
|
|
<div class="stat-cell">
|
|
<div class="stat-top">
|
|
<div class="stat-label" data-i18n="cache.statImageCount">图片缓存数量</div>
|
|
<span class="stat-icon" style="color:#2563eb">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>
|
|
</span>
|
|
</div>
|
|
<div class="stat-num" id="s-img-count" style="color:#2563eb">—</div>
|
|
</div>
|
|
<div class="stat-cell">
|
|
<div class="stat-top">
|
|
<div class="stat-label" data-i18n="cache.statImageSize">图片缓存大小</div>
|
|
<span class="stat-icon" style="color:#2563eb">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
|
|
</span>
|
|
</div>
|
|
<div class="stat-num" id="s-img-size" style="color:#2563eb">—</div>
|
|
</div>
|
|
<div class="stat-cell">
|
|
<div class="stat-top">
|
|
<div class="stat-label" data-i18n="cache.statVideoCount">视频缓存数量</div>
|
|
<span class="stat-icon" style="color:#7c3aed">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>
|
|
</span>
|
|
</div>
|
|
<div class="stat-num" id="s-vid-count" style="color:#7c3aed">—</div>
|
|
</div>
|
|
<div class="stat-cell">
|
|
<div class="stat-top">
|
|
<div class="stat-label" data-i18n="cache.statVideoSize">视频缓存大小</div>
|
|
<span class="stat-icon" style="color:#7c3aed">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>
|
|
</span>
|
|
</div>
|
|
<div class="stat-num" id="s-vid-size" style="color:#7c3aed">—</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="section-head">
|
|
<div class="section-title" data-i18n="cache.sectionDetails">缓存明细</div>
|
|
<div class="sec-tabs">
|
|
<button class="sec-tab active" onclick="switchSection('local')" data-i18n="cache.tabLocal">本地缓存</button>
|
|
<button class="sec-tab" onclick="switchSection('online')" data-i18n="cache.tabOnline">在线资产</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- ── 本地缓存 panel ─────────────────────────────────────────── -->
|
|
<div class="sec-panel active" id="panel-local">
|
|
|
|
<div class="filter-bar">
|
|
<button class="ftab active" onclick="switchType('image')" id="ftab-image">
|
|
<span data-i18n="cache.typeImage">图片</span> <span class="ftab-n" id="ftab-img-n">0</span>
|
|
</button>
|
|
<button class="ftab" onclick="switchType('video')" id="ftab-video">
|
|
<span data-i18n="cache.typeVideo">视频</span> <span class="ftab-n" id="ftab-vid-n">0</span>
|
|
</button>
|
|
</div>
|
|
|
|
<div class="table-toolbar">
|
|
<div class="table-toolbar-left">
|
|
<button class="pagi-btn" onclick="prevPage()" id="btn-prev" disabled>
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 18 9 12 15 6"/></svg>
|
|
</button>
|
|
<div class="pagi-meta">
|
|
<span class="pagi-page" id="pagi-page">第 0 / 0 页</span>
|
|
<span class="pagi-dot" aria-hidden="true"></span>
|
|
<span class="pagi-info" id="pagi-info">共 0 个文件</span>
|
|
</div>
|
|
<button class="pagi-btn" onclick="nextPage()" id="btn-next" disabled>
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="9 18 15 12 9 6"/></svg>
|
|
</button>
|
|
</div>
|
|
<div class="table-toolbar-actions">
|
|
<button class="toolbar-action toolbar-action-danger" onclick="clearLocal()" id="btn-clear" data-i18n="cache.clearCurrent">清空当前缓存</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="table-card table-freeze-last">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th data-i18n="cache.colFileName">文件名</th>
|
|
<th style="width:100px" data-i18n="cache.colSize">大小</th>
|
|
<th style="width:160px" data-i18n="cache.colModified">修改时间</th>
|
|
<th style="width:64px" data-i18n="cache.colActions">操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="cache-tbody"></tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- ── 在线资产 panel ──────────────────────────────────────────── -->
|
|
<div class="sec-panel" id="panel-online">
|
|
<div class="asset-desc">
|
|
<strong data-i18n="cache.onlineAssetLead">在线资产</strong><span data-i18n="cache.onlineAssetDesc">指账户在 Grok 平台侧保存的图片与附件。清理仅影响远端文件,不会删除本地缓存。</span>
|
|
</div>
|
|
|
|
<div class="table-toolbar" style="margin-bottom:14px">
|
|
<div class="table-toolbar-left" style="gap:10px">
|
|
<button class="page-action-btn" onclick="loadAssets()" id="btn-load-assets">
|
|
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>
|
|
<span data-i18n="cache.loadAssets">加载资产列表</span>
|
|
</button>
|
|
<span id="asset-summary" style="font-size:12px;color:#aaa"></span>
|
|
</div>
|
|
<div class="table-toolbar-actions" style="gap:10px">
|
|
<button class="toolbar-action toolbar-action-danger" onclick="clearAllAssets()" id="btn-clear-all-assets" style="display:none">
|
|
<span data-i18n="cache.clearAll">清理全部</span>
|
|
</button>
|
|
<button class="toolbar-action toolbar-action-danger" onclick="cancelAssetClear()" id="btn-asset-cancel" style="display:none">
|
|
<span data-i18n="cache.cancel">取消</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="table-card table-freeze-last" id="assets-table-wrap" style="display:none">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th style="width:32px"></th>
|
|
<th data-i18n="cache.colToken">Token</th>
|
|
<th style="width:72px" data-i18n="cache.colFileCount">文件数</th>
|
|
<th style="width:100px" data-i18n="cache.colActions">操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="assets-tbody"></tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
</main>
|
|
|
|
<script src="/static/js/i18n.js"></script>
|
|
<script src="/static/js/auth.js"></script>
|
|
<script src="/static/js/admin-header.js"></script>
|
|
<script src="/static/js/toast.js"></script>
|
|
<script>
|
|
const REFRESH_ICON = '<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>';
|
|
const SPINNER_ICON = '<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" style="animation:spin .8s linear infinite"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>';
|
|
|
|
let _type = 'image';
|
|
let _page = 1;
|
|
let _pageSize = 100;
|
|
let _total = 0;
|
|
let _assetData = [];
|
|
let _assetTotal = 0;
|
|
let _expanded = {};
|
|
let _assetTaskId = null;
|
|
let _assetEs = null;
|
|
|
|
function tr(key, params, fallback) {
|
|
const value = t(key, params);
|
|
return value === key ? (fallback ?? key) : value;
|
|
}
|
|
|
|
function waitI18n() {
|
|
return new Promise((resolve) => I18n.onReady(resolve));
|
|
}
|
|
|
|
function getLocale() {
|
|
return I18n.getLang() === 'en' ? 'en-US' : 'zh-CN';
|
|
}
|
|
|
|
function getTypeNoun(type = _type) {
|
|
return tr(type === 'image' ? 'cache.typeImageNoun' : 'cache.typeVideoNoun', null, type === 'image' ? '图片' : '视频');
|
|
}
|
|
|
|
function setLoadAssetsButton(labelKey, fallback, spinning = false) {
|
|
const btn = document.getElementById('btn-load-assets');
|
|
if (!btn) return;
|
|
btn.innerHTML = `${spinning ? SPINNER_ICON : REFRESH_ICON}<span>${tr(labelKey, null, fallback)}</span>`;
|
|
}
|
|
|
|
function updateAssetSummary() {
|
|
const totalAssets = _assetTotal || _assetData.reduce((sum, row) => sum + (row.count || 0), 0);
|
|
document.getElementById('asset-summary').textContent = tr(
|
|
'cache.assetSummary',
|
|
{ accounts: _assetData.length, files: totalAssets },
|
|
`共 ${_assetData.length} 个账户 · ${totalAssets} 个文件`
|
|
);
|
|
document.getElementById('btn-clear-all-assets').style.display = _assetData.some((row) => row.count > 0) ? '' : 'none';
|
|
}
|
|
|
|
function applyCacheI18n() {
|
|
document.title = tr('cache.pageTitle', null, 'Grok2API - 缓存管理');
|
|
setLoadAssetsButton('cache.loadAssets', '加载资产列表');
|
|
_renderPagi();
|
|
if (_assetData.length) {
|
|
_renderAssets();
|
|
updateAssetSummary();
|
|
}
|
|
}
|
|
|
|
async function _api(method, path, body) {
|
|
const key = await adminKey.get();
|
|
const r = await fetch(ADMIN_API + path, {
|
|
method,
|
|
headers: { ...(body != null && { 'Content-Type': 'application/json' }), Authorization: `Bearer ${key}` },
|
|
...(body != null && { body: JSON.stringify(body) }),
|
|
});
|
|
if (!r.ok) {
|
|
const d = await r.json().catch(() => ({}));
|
|
throw new Error(d.detail || r.status);
|
|
}
|
|
return r.json();
|
|
}
|
|
|
|
async function loadStats() {
|
|
try {
|
|
const d = await _api('GET', '/cache');
|
|
const img = d.local_image || {};
|
|
const vid = d.local_video || {};
|
|
document.getElementById('s-img-count').textContent = img.count ?? 0;
|
|
document.getElementById('s-img-size').textContent = _fmtMb(img.size_mb);
|
|
document.getElementById('s-vid-count').textContent = vid.count ?? 0;
|
|
document.getElementById('s-vid-size').textContent = _fmtMb(vid.size_mb);
|
|
document.getElementById('ftab-img-n').textContent = img.count ?? 0;
|
|
document.getElementById('ftab-vid-n').textContent = vid.count ?? 0;
|
|
} catch (e) {
|
|
console.warn('stats error', e);
|
|
}
|
|
}
|
|
|
|
async function loadList() {
|
|
try {
|
|
const d = await _api('GET', `/cache/list?type=${_type}&page=${_page}&page_size=${_pageSize}`);
|
|
_total = d.total ?? 0;
|
|
_renderTable(d.items || []);
|
|
_renderPagi();
|
|
} catch (e) {
|
|
showToast(tr('cache.loadFailed', { message: e.message }, `加载失败: ${e.message}`), 'error');
|
|
}
|
|
}
|
|
|
|
function _renderTable(items) {
|
|
const tbody = document.getElementById('cache-tbody');
|
|
if (!items.length) {
|
|
tbody.innerHTML = `<tr><td colspan="4" class="table-empty">${_esc(tr('cache.emptyLocal', null, '暂无缓存文件'))}</td></tr>`;
|
|
return;
|
|
}
|
|
tbody.innerHTML = items.map((f) => `
|
|
<tr>
|
|
<td>
|
|
<div class="row-name">
|
|
<span class="row-icon" aria-hidden="true">
|
|
${_type === 'image'
|
|
? `<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="8.5" cy="9" r="1.5"/><path d="M21 15l-4.5-4.5L7 20"/></svg>`
|
|
: `<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><polygon points="16 12 10 8.5 10 15.5 16 12"/><rect x="4" y="5" width="16" height="14" rx="2"/></svg>`
|
|
}
|
|
</span>
|
|
<span class="row-value">${_esc(f.name)}</span>
|
|
</div>
|
|
</td>
|
|
<td>${_fmtBytes(f.size_bytes)}</td>
|
|
<td>${_fmtTime(f.modified_at)}</td>
|
|
<td><div class="row-actions"><button class="row-action row-action-danger" onclick="deleteFile('${_esc(f.name)}')">${_esc(tr('cache.actionDelete', null, '删除'))}</button></div></td>
|
|
</tr>`).join('');
|
|
}
|
|
|
|
function _renderPagi() {
|
|
const pages = Math.max(1, Math.ceil(_total / _pageSize));
|
|
document.getElementById('pagi-page').textContent = tr('cache.pageIndicator', { current: _page, total: pages }, `第 ${_page} / ${pages} 页`);
|
|
document.getElementById('pagi-info').textContent = tr('cache.fileCount', { n: _total }, `共 ${_total} 个文件`);
|
|
document.getElementById('btn-prev').disabled = _page <= 1;
|
|
document.getElementById('btn-next').disabled = _page >= pages;
|
|
}
|
|
|
|
function prevPage() {
|
|
if (_page > 1) {
|
|
_page -= 1;
|
|
loadList();
|
|
}
|
|
}
|
|
|
|
function nextPage() {
|
|
const pages = Math.max(1, Math.ceil(_total / _pageSize));
|
|
if (_page < pages) {
|
|
_page += 1;
|
|
loadList();
|
|
}
|
|
}
|
|
|
|
function switchSection(section) {
|
|
document.querySelectorAll('.sec-tab').forEach((el, i) => el.classList.toggle('active', ['local', 'online'][i] === section));
|
|
document.getElementById('panel-local').classList.toggle('active', section === 'local');
|
|
document.getElementById('panel-online').classList.toggle('active', section === 'online');
|
|
}
|
|
|
|
function switchType(type) {
|
|
_type = type;
|
|
_page = 1;
|
|
document.getElementById('ftab-image').classList.toggle('active', type === 'image');
|
|
document.getElementById('ftab-video').classList.toggle('active', type === 'video');
|
|
loadList();
|
|
}
|
|
|
|
function refreshAll() {
|
|
loadStats();
|
|
loadList();
|
|
if (
|
|
document.getElementById('panel-online').classList.contains('active') &&
|
|
document.getElementById('assets-table-wrap').style.display !== 'none'
|
|
) {
|
|
loadAssets();
|
|
}
|
|
}
|
|
|
|
async function clearLocal() {
|
|
if (!confirm(tr('cache.clearLocalConfirm', { type: getTypeNoun() }, `确定要清空所有本地${getTypeNoun()}缓存吗?`))) return;
|
|
try {
|
|
const d = await _api('POST', '/cache/clear', { type: _type });
|
|
const removed = d.result?.removed ?? 0;
|
|
showToast(tr('cache.clearLocalDone', { count: removed, type: getTypeNoun() }, `已清除 ${removed} 个${getTypeNoun()}文件`));
|
|
_page = 1;
|
|
loadStats();
|
|
loadList();
|
|
} catch (e) {
|
|
showToast(tr('cache.clearFailed', { message: e.message }, `清除失败: ${e.message}`), 'error');
|
|
}
|
|
}
|
|
|
|
async function deleteFile(name) {
|
|
try {
|
|
await _api('POST', '/cache/item/delete', { type: _type, name });
|
|
showToast(tr('cache.deleted', null, '已删除'));
|
|
loadStats();
|
|
loadList();
|
|
} catch (e) {
|
|
showToast(tr('cache.deleteFailed', { message: e.message }, `删除失败: ${e.message}`), 'error');
|
|
}
|
|
}
|
|
|
|
async function loadAssets() {
|
|
const btn = document.getElementById('btn-load-assets');
|
|
btn.disabled = true;
|
|
setLoadAssetsButton('cache.loading', '加载中…', true);
|
|
document.getElementById('asset-summary').textContent = '';
|
|
try {
|
|
const d = await _api('GET', '/assets');
|
|
_assetData = d.tokens || [];
|
|
_assetTotal = d.total_assets ?? _assetData.reduce((sum, row) => sum + (row.count || 0), 0);
|
|
_expanded = {};
|
|
_renderAssets();
|
|
updateAssetSummary();
|
|
document.getElementById('assets-table-wrap').style.display = '';
|
|
} catch (e) {
|
|
showToast(tr('cache.loadFailed', { message: e.message }, `加载失败: ${e.message}`), 'error');
|
|
} finally {
|
|
btn.disabled = false;
|
|
setLoadAssetsButton('cache.refreshList', '刷新列表');
|
|
}
|
|
}
|
|
|
|
function _renderAssets() {
|
|
const tbody = document.getElementById('assets-tbody');
|
|
if (!_assetData.length) {
|
|
tbody.innerHTML = `<tr><td colspan="4" class="table-empty">${_esc(tr('cache.emptyAccounts', null, '暂无账户数据'))}</td></tr>`;
|
|
return;
|
|
}
|
|
|
|
const expandLabel = tr('cache.expand', null, '展开');
|
|
const collapseLabel = tr('cache.collapse', null, '收起');
|
|
const clearLabel = tr('cache.actionClear', null, '清理');
|
|
const deleteLabel = tr('cache.actionDelete', null, '删除');
|
|
const naLabel = tr('cache.na', null, '—');
|
|
|
|
const rows = [];
|
|
for (const row of _assetData) {
|
|
const isOpen = !!_expanded[row.masked];
|
|
const hasItems = row.count > 0;
|
|
rows.push(`
|
|
<tr>
|
|
<td style="padding:10px 8px 10px 14px">
|
|
${hasItems ? `<button class="expand-btn${isOpen ? ' open' : ''}" onclick="toggleExpand('${_esc(row.masked)}')" title="${_esc(isOpen ? collapseLabel : expandLabel)}">
|
|
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="9 18 15 12 9 6"/></svg>
|
|
</button>` : '<span style="width:22px;display:inline-block"></span>'}
|
|
</td>
|
|
<td>
|
|
<div class="row-name">
|
|
<span class="row-icon" aria-hidden="true">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><path d="M7 10V8a5 5 0 0 1 10 0v2"/><rect x="4" y="10" width="16" height="10" rx="2"/><circle cx="12" cy="15" r="1"/></svg>
|
|
</span>
|
|
<span class="row-value">${_esc(row.masked)}</span>
|
|
</div>
|
|
${row.error ? `<span class="asset-err" title="${_esc(row.error)}">⚠ ${_esc(row.error)}</span>` : ''}
|
|
</td>
|
|
<td>
|
|
<span class="asset-count-badge${hasItems ? ' has-items' : ''}">${row.count}</span>
|
|
</td>
|
|
<td>
|
|
${hasItems ? `<div class="row-actions"><button class="row-action row-action-danger" onclick="clearTokenAssets('${_esc(row.token)}','${_esc(row.masked)}')">${_esc(clearLabel)}</button></div>` : ''}
|
|
</td>
|
|
</tr>`);
|
|
|
|
if (isOpen && hasItems) {
|
|
for (const asset of row.assets) {
|
|
rows.push(`
|
|
<tr class="sub-row">
|
|
<td></td>
|
|
<td>
|
|
<div class="row-name">
|
|
<span class="row-icon" aria-hidden="true">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke-width="1.8"><path d="M8 3h6l5 5v13H8a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/><path d="M14 3v5h5"/></svg>
|
|
</span>
|
|
<span class="row-value" style="font-size:11px">${_esc(asset.name || asset.id)}</span>
|
|
${asset.content_type ? `<span style="color:#aaa;font-size:11px">${_esc(asset.content_type)}</span>` : ''}
|
|
</div>
|
|
</td>
|
|
<td style="color:#aaa;font-size:11px">${asset.size ? _fmtBytes(asset.size) : _esc(naLabel)}</td>
|
|
<td>
|
|
<div class="row-actions"><button class="row-action row-action-danger" onclick="deleteAssetItem('${_esc(row.token)}','${_esc(row.masked)}','${_esc(asset.id)}')">${_esc(deleteLabel)}</button></div>
|
|
</td>
|
|
</tr>`);
|
|
}
|
|
}
|
|
}
|
|
tbody.innerHTML = rows.join('');
|
|
}
|
|
|
|
function toggleExpand(masked) {
|
|
_expanded[masked] = !_expanded[masked];
|
|
_renderAssets();
|
|
}
|
|
|
|
async function clearTokenAssets(token, masked) {
|
|
if (!confirm(tr('cache.clearTokenConfirm', { token: masked }, `确定要清理 ${masked} 的全部在线资产吗?`))) return;
|
|
try {
|
|
const d = await _api('POST', '/assets/clear-token', { token });
|
|
showToast(tr('cache.clearTokenDone', { count: d.deleted ?? 0 }, `已删除 ${d.deleted ?? 0} 个文件`));
|
|
await loadAssets();
|
|
} catch (e) {
|
|
showToast(tr('cache.clearFailed', { message: e.message }, `清理失败: ${e.message}`), 'error');
|
|
}
|
|
}
|
|
|
|
async function deleteAssetItem(token, masked, assetId) {
|
|
try {
|
|
await _api('POST', '/assets/delete-item', { token, asset_id: assetId });
|
|
showToast(tr('cache.deleted', null, '已删除'));
|
|
const row = _assetData.find((item) => item.token === token);
|
|
if (row) {
|
|
row.assets = row.assets.filter((asset) => asset.id !== assetId);
|
|
row.count = row.assets.length;
|
|
}
|
|
_assetTotal = _assetData.reduce((sum, item) => sum + (item.count || 0), 0);
|
|
_renderAssets();
|
|
updateAssetSummary();
|
|
} catch (e) {
|
|
showToast(tr('cache.deleteFailed', { message: e.message }, `删除失败: ${e.message}`), 'error');
|
|
}
|
|
}
|
|
|
|
async function clearAllAssets() {
|
|
const withAssets = _assetData.filter((row) => row.count > 0);
|
|
if (!withAssets.length) return;
|
|
if (!confirm(tr('cache.clearAllConfirm', { n: withAssets.length }, `确定要清理全部 ${withAssets.length} 个账户的在线资产吗?此操作不可撤销。`))) return;
|
|
|
|
const tokens = withAssets.map((row) => row.token);
|
|
const btnClear = document.getElementById('btn-clear-all-assets');
|
|
const btnCancel = document.getElementById('btn-asset-cancel');
|
|
|
|
btnClear.style.display = 'none';
|
|
btnCancel.style.display = '';
|
|
const progress = showProgressToast(tr('cache.clearingAssets', null, '正在清理在线资产…'));
|
|
let finalReceived = false;
|
|
|
|
function cleanup() {
|
|
btnCancel.style.display = 'none';
|
|
}
|
|
|
|
function done(es) {
|
|
finalReceived = true;
|
|
es.close();
|
|
_assetEs = null;
|
|
cleanup();
|
|
}
|
|
|
|
try {
|
|
const d = await _api('POST', '/batch/cache-clear?async=true', { tokens });
|
|
_assetTaskId = d.task_id;
|
|
const total = d.total || tokens.length;
|
|
const key = await adminKey.get();
|
|
const es = new EventSource(`${ADMIN_API}/batch/${_assetTaskId}/stream?app_key=${encodeURIComponent(key)}`);
|
|
_assetEs = es;
|
|
|
|
es.onmessage = (event) => {
|
|
const ev = JSON.parse(event.data);
|
|
if (ev.type === 'snapshot' || ev.type === 'progress') {
|
|
progress.update(ev.processed || 0, total);
|
|
}
|
|
if (['done', 'error', 'cancelled'].includes(ev.type)) {
|
|
done(es);
|
|
if (ev.type === 'done') {
|
|
const summary = ev.result?.summary || {};
|
|
progress.finish(
|
|
tr('cache.clearAllDone', { ok: summary.ok ?? 0, fail: summary.fail ?? 0 }, `清理完成:成功 ${summary.ok ?? 0},失败 ${summary.fail ?? 0}`),
|
|
(summary.fail ?? 0) > 0 ? 'error' : 'success'
|
|
);
|
|
loadAssets();
|
|
} else if (ev.type === 'cancelled') {
|
|
progress.finish(tr('cache.cancelled', null, '已取消'), 'error');
|
|
btnClear.style.display = '';
|
|
} else {
|
|
progress.finish(ev.error || tr('cache.clearFailedPlain', null, '清理失败'), 'error');
|
|
btnClear.style.display = '';
|
|
}
|
|
}
|
|
};
|
|
|
|
es.onerror = function() {
|
|
if (finalReceived) {
|
|
_assetEs = null;
|
|
return;
|
|
}
|
|
if (this.readyState === EventSource.CLOSED) {
|
|
_assetEs = null;
|
|
cleanup();
|
|
btnClear.style.display = '';
|
|
progress.finish(tr('cache.connectionInterrupted', null, '连接中断'), 'error');
|
|
}
|
|
};
|
|
} catch (e) {
|
|
cleanup();
|
|
btnClear.style.display = '';
|
|
progress.finish(tr('cache.startFailed', { message: e.message }, `启动失败: ${e.message}`), 'error');
|
|
}
|
|
}
|
|
|
|
async function cancelAssetClear() {
|
|
if (!_assetTaskId) return;
|
|
showToast(tr('cache.cancelInProgress', null, '已暂停,请耐心等待本批次完成…'), 'info');
|
|
try {
|
|
await _api('POST', `/batch/${_assetTaskId}/cancel`);
|
|
} catch {}
|
|
}
|
|
|
|
function _fmtMb(mb) {
|
|
if (mb == null) return tr('cache.na', null, '—');
|
|
if (mb < 1) return `${Math.round(mb * 1024)} KB`;
|
|
return `${mb.toFixed(1)} MB`;
|
|
}
|
|
|
|
function _fmtBytes(bytes) {
|
|
if (!bytes) return '0 B';
|
|
if (bytes < 1024) return `${bytes} B`;
|
|
if (bytes < 1048576) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
return `${(bytes / 1048576).toFixed(1)} MB`;
|
|
}
|
|
|
|
function _fmtTime(ts) {
|
|
if (!ts) return tr('cache.na', null, '—');
|
|
return new Date(ts * 1000).toLocaleString(getLocale(), { hour12: false });
|
|
}
|
|
|
|
function _maskToken(token) {
|
|
return token.length > 16 ? `${token.slice(0, 8)}…${token.slice(-4)}` : token;
|
|
}
|
|
|
|
function _esc(value) {
|
|
return String(value).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
}
|
|
|
|
(async () => {
|
|
await waitI18n();
|
|
applyCacheI18n();
|
|
await renderAdminHeader?.();
|
|
const key = await adminKey.get();
|
|
if (!key || !await verifyKey(ADMIN_API + '/verify', key).catch(() => false)) {
|
|
location.href = '/admin/login';
|
|
return;
|
|
}
|
|
loadStats();
|
|
loadList();
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|