1) 分片上传(routes/chunked.py) - POST /api/upload/init 建会话,校验表单/字段/扩展名/体积 - POST /api/upload/chunk 单片上传,幂等可重传,http body 直写磁盘 - GET /api/upload/status 查询已收分片 - POST /api/upload/complete 按序合并 + sha256 校验 + 清理分片 - /api/submit 新增 field fileTokens 挂载已上传文件(零拷贝 os.replace) - 过期会话(24h)由定时任务清理 2) 弱网下的样式可靠性 - Tailwind Play CDN / GSAP 改为本地自托管(static/js/vendor/) - vendor 资源 1 年 immutable 缓存,自家 css/js 1 小时 - CSS link 失败自动带 ?r= 重试一次 - status 页加关键内联样式兜底,极端情况下仍可读 3) 大文件落盘沿用流式写入(无 400MB 内存峰值)
1892 lines
74 KiB
HTML
1892 lines
74 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN" class="light">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
<title>数据库管理</title>
|
|
|
|
<!-- Tailwind CSS -->
|
|
<script src="/static/js/vendor/tailwind-play.js"></script>
|
|
<script>
|
|
tailwind.config = {
|
|
darkMode: 'class',
|
|
theme: {
|
|
extend: {
|
|
colors: {
|
|
neutral: {
|
|
50: '#fafafa',
|
|
100: '#f5f5f5',
|
|
200: '#e5e5e5',
|
|
300: '#d4d4d4',
|
|
400: '#a3a3a3',
|
|
500: '#737373',
|
|
600: '#525252',
|
|
700: '#404040',
|
|
800: '#262626',
|
|
900: '#171717',
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<!-- ECharts for data visualization -->
|
|
<script src="/static/js/echart.js"></script>
|
|
<!-- GSAP for animations -->
|
|
<script src="/static/js/vendor/gsap.min.js"></script>
|
|
<script src="/static/js/particles.js"></script>
|
|
<link rel="stylesheet" href="/static/css/toast.css" onerror="this.onerror=null;this.href='/static/css/toast.css?r='+Date.now()">
|
|
<script src="/static/js/toast.js"></script>
|
|
<link rel="stylesheet" href="/static/css/basic.css" onerror="this.onerror=null;this.href='/static/css/basic.css?r='+Date.now()">
|
|
<link rel="stylesheet" href="/static/css/common.css" onerror="this.onerror=null;this.href='/static/css/common.css?r='+Date.now()">
|
|
<script src="/static/js/common.js"></script>
|
|
|
|
<style>
|
|
/* ── db_manage 页面专属样式 ── */
|
|
|
|
/* Card Styles */
|
|
.stat-card {
|
|
background: var(--glass-bg);
|
|
backdrop-filter: blur(20px);
|
|
-webkit-backdrop-filter: blur(20px);
|
|
border: 1px solid var(--glass-border);
|
|
border-radius: 20px;
|
|
padding: 24px;
|
|
transition: all 0.4s cubic-bezier(0.2, 0, 0, 1);
|
|
}
|
|
|
|
.stat-card:hover {
|
|
transform: translateY(-4px);
|
|
box-shadow: 0 20px 40px -15px var(--shadow-color);
|
|
background: var(--glass-hover);
|
|
}
|
|
|
|
/* Chart Container */
|
|
.chart-container {
|
|
background: var(--glass-bg);
|
|
border: 1px solid var(--glass-border);
|
|
border-radius: 20px;
|
|
padding: 20px;
|
|
height: 350px;
|
|
}
|
|
|
|
.btn-success {
|
|
background-color: var(--success-bg);
|
|
color: var(--success-text);
|
|
border: 1px solid var(--success-border);
|
|
}
|
|
|
|
.btn-success:hover {
|
|
opacity: 0.8;
|
|
transform: translateY(-2px);
|
|
}
|
|
|
|
/* SQL Editor */
|
|
.sql-editor {
|
|
font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
|
|
background: var(--bg-secondary);
|
|
border: 1px solid var(--border-color);
|
|
border-radius: 12px;
|
|
padding: 16px;
|
|
width: 100%;
|
|
min-height: 120px;
|
|
resize: vertical;
|
|
color: var(--text-primary);
|
|
font-size: 14px;
|
|
line-height: 1.6;
|
|
}
|
|
|
|
.sql-editor:focus {
|
|
outline: none;
|
|
border-color: var(--border-focus);
|
|
}
|
|
|
|
/* SQL Result Table */
|
|
.sql-result {
|
|
overflow-x: auto;
|
|
max-height: 400px;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.sql-result table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.sql-result th,
|
|
.sql-result td {
|
|
padding: 10px 12px;
|
|
text-align: left;
|
|
border-bottom: 1px solid var(--border-color);
|
|
}
|
|
|
|
.sql-result th {
|
|
background: var(--bg-primary);
|
|
font-weight: 600;
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 10;
|
|
}
|
|
|
|
.sql-result tr:hover {
|
|
background: var(--glass-hover);
|
|
}
|
|
|
|
/* Table Editor */
|
|
.table-editor {
|
|
max-height: 500px;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.table-editor table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.table-editor th,
|
|
.table-editor td {
|
|
padding: 12px;
|
|
text-align: left;
|
|
border-bottom: 1px solid var(--border-color);
|
|
}
|
|
|
|
.table-editor th {
|
|
background: var(--bg-primary);
|
|
font-weight: 600;
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 10;
|
|
}
|
|
|
|
.table-editor tr:hover {
|
|
background: var(--glass-hover);
|
|
}
|
|
|
|
.table-editor input {
|
|
background: transparent;
|
|
border: 1px solid var(--border-color);
|
|
border-radius: 6px;
|
|
padding: 6px 10px;
|
|
color: var(--text-primary);
|
|
width: 100%;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.table-editor input:focus {
|
|
outline: none;
|
|
border-color: var(--border-focus);
|
|
}
|
|
|
|
/* Tabs */
|
|
.tab-btn {
|
|
padding: 10px 20px;
|
|
border-radius: 10px;
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
transition: all 0.3s ease;
|
|
cursor: pointer;
|
|
border: none;
|
|
background: transparent;
|
|
color: var(--text-muted);
|
|
}
|
|
|
|
.tab-btn:hover {
|
|
background: var(--glass-hover);
|
|
color: var(--text-primary);
|
|
}
|
|
|
|
.tab-btn.active {
|
|
background: var(--accent-bg);
|
|
color: var(--accent-text);
|
|
}
|
|
|
|
.tab-content {
|
|
display: none;
|
|
}
|
|
|
|
.tab-content.active {
|
|
display: block;
|
|
}
|
|
|
|
/* Size Bar */
|
|
.size-bar {
|
|
height: 8px;
|
|
border-radius: 4px;
|
|
background: var(--border-color);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.size-bar-fill {
|
|
height: 100%;
|
|
border-radius: 4px;
|
|
transition: width 0.5s ease;
|
|
}
|
|
/* Cell Edit */
|
|
.cell-edit-input {
|
|
font-family: inherit;
|
|
}
|
|
|
|
/* Modal Animation */
|
|
.modal-enter {
|
|
animation: modalEnter 0.3s ease forwards;
|
|
}
|
|
|
|
@keyframes modalEnter {
|
|
from {
|
|
opacity: 0;
|
|
transform: scale(0.95);
|
|
}
|
|
to {
|
|
opacity: 1;
|
|
transform: scale(1);
|
|
}
|
|
}
|
|
|
|
/* Table Action Buttons */
|
|
.table-editor button {
|
|
transition: all 0.2s ease;
|
|
}
|
|
|
|
.table-editor button:hover {
|
|
transform: translateY(-1px);
|
|
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body class="antialiased min-h-screen relative">
|
|
|
|
<!-- Progress Bar -->
|
|
<div class="fixed top-0 left-0 w-full h-[2px] z-50 theme-transition" style="background-color: var(--border-color);">
|
|
<div id="scrollProgress" class="h-full w-0 transition-all duration-100 ease-out"></div>
|
|
</div>
|
|
|
|
<!-- Header -->
|
|
<header class="fixed top-0 w-full z-40 px-4 sm:px-6 py-4 sm:py-5 glass theme-transition border-b">
|
|
<div class="max-w-7xl mx-auto flex justify-between items-center">
|
|
<!-- 左侧:面包屑 -->
|
|
<div class="flex items-center gap-2">
|
|
<a href="/admin" class="nav-link flex items-center gap-1.5">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
|
|
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
<rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/>
|
|
<rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/>
|
|
</svg>
|
|
<span class="hidden sm:inline text-sm">管理后台</span>
|
|
</a>
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" viewBox="0 0 24 24" fill="none"
|
|
stroke="currentColor" stroke-width="2" style="color:var(--text-muted);opacity:0.4">
|
|
<path d="m9 18 6-6-6-6"/>
|
|
</svg>
|
|
<span class="text-sm font-medium" style="color:var(--text-primary)">数据库管理</span>
|
|
</div>
|
|
<!-- 右侧:返回 -->
|
|
<div class="flex items-center gap-2 sm:gap-3">
|
|
<a href="/admin" class="btn btn-secondary px-4 py-2.5 rounded-full text-xs sm:text-sm font-medium tracking-wide flex items-center gap-2">
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
<path d="M19 12H5M12 19l-7-7 7-7"/>
|
|
</svg>
|
|
<span class="hidden sm:inline">返回</span>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<!-- Main Content -->
|
|
<main class="relative pt-32 pb-24 px-6">
|
|
<div class="max-w-7xl mx-auto">
|
|
|
|
<!-- Header Section -->
|
|
<div class="mb-12 opacity-0" id="headerSection">
|
|
<h2 class="font-serif text-4xl sm:text-5xl leading-[1.1] font-medium tracking-tight mb-4 theme-transition"
|
|
style="color: var(--text-primary);">
|
|
数据库管理
|
|
</h2>
|
|
<p class="text-sm leading-relaxed max-w-md tracking-wide font-light theme-transition"
|
|
style="color: var(--text-secondary);">
|
|
实时查看数据库状态,执行SQL命令,管理表结构,优化数据库性能。
|
|
</p>
|
|
</div>
|
|
|
|
<!-- Loading State -->
|
|
<div id="loadingState" class="flex justify-center items-center h-64">
|
|
<div class="loading-spinner"></div>
|
|
</div>
|
|
|
|
<!-- Content (hidden initially) -->
|
|
<div id="contentArea" class="hidden space-y-8">
|
|
|
|
<!-- Overview Stats -->
|
|
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4">
|
|
<div class="stat-card stagger-item">
|
|
<div class="text-[10px] tracking-[0.2em] uppercase mb-2 theme-transition" style="color: var(--text-muted);">
|
|
数据库大小</div>
|
|
<div class="text-3xl font-semibold theme-transition" style="color: var(--text-primary);" id="dbSize">0
|
|
</div>
|
|
<div class="text-xs mt-1 theme-transition" style="color: var(--text-muted);" id="dbSizeBytes">0 bytes</div>
|
|
</div>
|
|
<div class="stat-card stagger-item">
|
|
<div class="text-[10px] tracking-[0.2em] uppercase mb-2 theme-transition" style="color: var(--text-muted);">
|
|
表数量</div>
|
|
<div class="text-3xl font-semibold theme-transition" style="color: var(--info-text);" id="tableCount">0
|
|
</div>
|
|
</div>
|
|
<div class="stat-card stagger-item">
|
|
<div class="text-[10px] tracking-[0.2em] uppercase mb-2 theme-transition" style="color: var(--text-muted);">
|
|
总记录数</div>
|
|
<div class="text-3xl font-semibold theme-transition" style="color: var(--success-text);" id="totalRecords">0
|
|
</div>
|
|
</div>
|
|
<div class="stat-card stagger-item">
|
|
<div class="text-[10px] tracking-[0.2em] uppercase mb-2 theme-transition" style="color: var(--text-muted);">
|
|
文件存储</div>
|
|
<div class="text-3xl font-semibold theme-transition" style="color: var(--warning-text);" id="fileStorage">0
|
|
</div>
|
|
</div>
|
|
<div class="stat-card stagger-item cursor-pointer hover:scale-105" id="tempZipStatsCard" onclick="showTempZipModal()">
|
|
<div class="text-[10px] tracking-[0.2em] uppercase mb-2 theme-transition" style="color: var(--text-muted);">
|
|
临时ZIP文件
|
|
</div>
|
|
<div class="flex items-center gap-2 mb-1">
|
|
<div class="text-3xl font-semibold theme-transition" style="color: var(--text-primary);">
|
|
0
|
|
</div>
|
|
<div class="text-xs" style="color: var(--text-muted);">
|
|
个文件
|
|
</div>
|
|
</div>
|
|
<div class="text-xs mb-2 theme-transition" style="color: var(--text-muted);">
|
|
总大小: 0 B
|
|
</div>
|
|
<div class="text-xs theme-transition" style="color: var(--text-muted);">
|
|
加载中...
|
|
</div>
|
|
${dbStats.temp_zip_stats && dbStats.temp_zip_stats.count > 0 ? `
|
|
<div class="mt-3 text-xs theme-transition" style="color: var(--accent-color);">
|
|
点击查看详情 →
|
|
</div>
|
|
` : ''}
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Charts Row -->
|
|
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
|
<div class="chart-container stagger-item">
|
|
<h3 class="text-sm font-semibold mb-4 theme-transition" style="color: var(--text-primary);">存储空间分布</h3>
|
|
<div id="storageChart" style="height: 280px;"></div>
|
|
</div>
|
|
<div class="chart-container stagger-item">
|
|
<h3 class="text-sm font-semibold mb-4 theme-transition" style="color: var(--text-primary);">各表记录数量</h3>
|
|
<div id="recordsChart" style="height: 280px;"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Form Storage Details -->
|
|
<div class="glass rounded-2xl p-6 stagger-item">
|
|
<div class="flex justify-between items-center mb-6">
|
|
<h3 class="text-lg font-semibold theme-transition" style="color: var(--text-primary);">表单存储详情</h3>
|
|
<span class="text-xs theme-transition" style="color: var(--text-muted);" id="formCount">0 个表单</span>
|
|
</div>
|
|
<div id="formStorageList" class="space-y-4">
|
|
<!-- Form storage items will be inserted here -->
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Action Buttons -->
|
|
<div class="flex flex-wrap gap-3 stagger-item">
|
|
<button onclick="runVacuum()" class="btn btn-success px-6 py-3 rounded-xl text-sm font-medium flex items-center gap-2">
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
<path d="M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4M4.93 19.07l2.83-2.83M16.24 7.76l2.83-2.83"/>
|
|
</svg>
|
|
优化数据库 (VACUUM)
|
|
</button>
|
|
<button onclick="refreshData()" class="btn btn-secondary px-6 py-3 rounded-xl text-sm font-medium flex items-center gap-2">
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
<polyline points="23 4 23 10 17 10"></polyline>
|
|
<path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path>
|
|
</svg>
|
|
刷新数据
|
|
</button>
|
|
</div>
|
|
|
|
<!-- Tabs Section -->
|
|
<div class="glass rounded-2xl overflow-hidden stagger-item">
|
|
<!-- Tab Buttons -->
|
|
<div class="flex border-b theme-transition" style="border-color: var(--glass-border);">
|
|
<button class="tab-btn active" onclick="switchTab('sql')">SQL 控制台</button>
|
|
<button class="tab-btn" onclick="switchTab('tables')">表结构管理</button>
|
|
<button class="tab-btn" onclick="switchTab('data')">数据浏览</button>
|
|
</div>
|
|
|
|
<!-- SQL Console Tab -->
|
|
<div id="sqlTab" class="tab-content active p-6">
|
|
<div class="mb-4">
|
|
<label class="text-sm font-medium mb-2 block theme-transition" style="color: var(--text-secondary);">
|
|
执行 SQL 命令
|
|
</label>
|
|
<textarea id="sqlInput" class="sql-editor" placeholder="-- 输入 SQL 命令,例如:
|
|
SELECT * FROM forms LIMIT 10;"></textarea>
|
|
</div>
|
|
<div class="flex gap-3 mb-6">
|
|
<button onclick="executeSql()" class="btn btn-primary px-5 py-2.5 rounded-xl text-sm font-medium">
|
|
执行
|
|
</button>
|
|
<button onclick="clearSql()" class="btn btn-secondary px-5 py-2.5 rounded-xl text-sm font-medium">
|
|
清空
|
|
</button>
|
|
</div>
|
|
<div id="sqlResult" class="hidden">
|
|
<div class="text-sm font-medium mb-3 theme-transition" style="color: var(--text-secondary);">
|
|
执行结果
|
|
<span id="sqlStatus" class="ml-2 text-xs"></span>
|
|
</div>
|
|
<div class="sql-result glass rounded-xl p-4" id="sqlResultContent">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Tables Tab -->
|
|
<div id="tablesTab" class="tab-content p-6">
|
|
<div class="flex justify-between items-center mb-4">
|
|
<h3 class="text-sm font-medium theme-transition" style="color: var(--text-secondary);">数据库表结构</h3>
|
|
<button onclick="loadTables()" class="text-xs px-3 py-1.5 rounded-lg border theme-transition hover:opacity-70" style="border-color: var(--border-color); color: var(--text-muted);">
|
|
刷新
|
|
</button>
|
|
</div>
|
|
<div id="tablesList" class="space-y-4">
|
|
<!-- Tables will be inserted here -->
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Data Browser Tab -->
|
|
<div id="dataTab" class="tab-content p-6">
|
|
<div class="flex flex-wrap gap-3 mb-4">
|
|
<select id="tableSelect" class="px-4 py-2.5 rounded-xl border theme-transition text-sm" style="border-color: var(--border-color); background: var(--bg-secondary); color: var(--text-primary);">
|
|
<option value="">选择表...</option>
|
|
</select>
|
|
<input type="number" id="limitInput" value="50" min="1" max="1000" class="px-4 py-2.5 rounded-xl border theme-transition text-sm w-24" style="border-color: var(--border-color); background: var(--bg-secondary); color: var(--text-primary);" placeholder="条数">
|
|
<button onclick="browseTable()" class="btn btn-primary px-5 py-2.5 rounded-xl text-sm font-medium">
|
|
浏览
|
|
</button>
|
|
<button onclick="openInsertModal()" class="btn btn-success px-5 py-2.5 rounded-xl text-sm font-medium flex items-center gap-2">
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
<line x1="12" y1="5" x2="12" y2="19"></line>
|
|
<line x1="5" y1="12" x2="19" y2="12"></line>
|
|
</svg>
|
|
新增
|
|
</button>
|
|
</div>
|
|
<div class="text-xs mb-2 theme-transition" style="color: var(--text-muted);">
|
|
提示:点击表格中的单元格可直接编辑,右键点击行可删除
|
|
</div>
|
|
<div id="tableData" class="table-editor glass rounded-xl">
|
|
<div class="p-8 text-center text-sm" style="color: var(--text-muted);">
|
|
请选择表并点击浏览
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<!-- VACUUM Result Modal -->
|
|
<div id="vacuumModal" class="fixed inset-0 z-50 hidden">
|
|
<div class="absolute inset-0 bg-black/50 backdrop-blur-sm transition-opacity" onclick="closeVacuumModal()"></div>
|
|
<div class="absolute inset-0 flex items-center justify-center p-4">
|
|
<div class="glass rounded-2xl w-full max-w-md shadow-2xl transform transition-all scale-95 opacity-0" id="vacuumModalContent">
|
|
<div class="p-6">
|
|
<div class="flex items-center gap-3 mb-4">
|
|
<div class="w-12 h-12 rounded-full flex items-center justify-center" style="background: var(--success-bg);">
|
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="color: var(--success-text);">
|
|
<polyline points="20 6 9 17 4 12"></polyline>
|
|
</svg>
|
|
</div>
|
|
<div>
|
|
<h3 class="text-lg font-semibold theme-transition" style="color: var(--text-primary);">数据库优化完成</h3>
|
|
<p class="text-xs theme-transition" style="color: var(--text-muted);">VACUUM 命令执行成功</p>
|
|
</div>
|
|
</div>
|
|
<div class="space-y-3 mb-6">
|
|
<div class="flex justify-between py-2 border-b theme-transition" style="border-color: var(--glass-border);">
|
|
<span class="text-sm theme-transition" style="color: var(--text-secondary);">优化前大小</span>
|
|
<span class="text-sm font-medium theme-transition" style="color: var(--text-primary);" id="vacuumBefore">-</span>
|
|
</div>
|
|
<div class="flex justify-between py-2 border-b theme-transition" style="border-color: var(--glass-border);">
|
|
<span class="text-sm theme-transition" style="color: var(--text-secondary);">优化后大小</span>
|
|
<span class="text-sm font-medium theme-transition" style="color: var(--text-primary);" id="vacuumAfter">-</span>
|
|
</div>
|
|
<div class="flex justify-between py-2">
|
|
<span class="text-sm theme-transition" style="color: var(--text-secondary);">释放空间</span>
|
|
<span class="text-sm font-semibold theme-transition" style="color: var(--success-text);" id="vacuumSaved">-</span>
|
|
</div>
|
|
</div>
|
|
<button onclick="closeVacuumModal(); refreshData();" class="btn btn-primary w-full py-3 rounded-xl text-sm font-medium">
|
|
确定
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Insert Modal -->
|
|
<div id="insertModal" class="fixed inset-0 z-50 hidden">
|
|
<div class="absolute inset-0 bg-black/50 backdrop-blur-sm transition-opacity" onclick="closeInsertModal()"></div>
|
|
<div class="absolute inset-0 flex items-center justify-center p-4">
|
|
<div class="glass rounded-2xl w-full max-w-2xl shadow-2xl transform transition-all scale-95 opacity-0 max-h-[90vh] overflow-hidden flex flex-col" id="insertModalContent">
|
|
<div class="p-6 border-b theme-transition" style="border-color: var(--glass-border);">
|
|
<div class="flex justify-between items-center">
|
|
<div>
|
|
<h3 class="text-lg font-semibold theme-transition" style="color: var(--text-primary);">新增条目</h3>
|
|
<p class="text-xs theme-transition" style="color: var(--text-muted);" id="insertTableName">表名: -</p>
|
|
</div>
|
|
<button onclick="closeInsertModal()" class="p-2 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors">
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="color: var(--text-muted);">
|
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="p-6 overflow-y-auto flex-1">
|
|
<div id="insertFormFields" class="space-y-4">
|
|
<!-- Fields will be inserted here -->
|
|
</div>
|
|
</div>
|
|
<div class="p-6 border-t theme-transition" style="border-color: var(--glass-border);">
|
|
<div class="flex gap-3">
|
|
<button onclick="executeInsert()" class="btn btn-success flex-1 py-3 rounded-xl text-sm font-medium">
|
|
确认新增
|
|
</button>
|
|
<button onclick="closeInsertModal()" class="btn btn-secondary px-6 py-3 rounded-xl text-sm font-medium">
|
|
取消
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Modal -->
|
|
<div id="editModal" class="fixed inset-0 z-50 hidden">
|
|
<div class="absolute inset-0 bg-black/50 backdrop-blur-sm transition-opacity" onclick="closeEditModal()"></div>
|
|
<div class="absolute inset-0 flex items-center justify-center p-4">
|
|
<div class="glass rounded-2xl w-full max-w-2xl shadow-2xl transform transition-all scale-95 opacity-0 max-h-[90vh] overflow-hidden flex flex-col" id="editModalContent">
|
|
<div class="p-6 border-b theme-transition" style="border-color: var(--glass-border);">
|
|
<div class="flex justify-between items-center">
|
|
<div>
|
|
<h3 class="text-lg font-semibold theme-transition" style="color: var(--text-primary);">修改条目</h3>
|
|
<p class="text-xs theme-transition" style="color: var(--text-muted);" id="editTableName">表名: -</p>
|
|
</div>
|
|
<button onclick="closeEditModal()" class="p-2 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors">
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="color: var(--text-muted);">
|
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="p-6 overflow-y-auto flex-1">
|
|
<div id="editFormFields" class="space-y-4">
|
|
<!-- Fields will be inserted here -->
|
|
</div>
|
|
</div>
|
|
<div class="p-6 border-t theme-transition" style="border-color: var(--glass-border);">
|
|
<div class="flex gap-3">
|
|
<button onclick="executeUpdate()" class="btn btn-primary flex-1 py-3 rounded-xl text-sm font-medium">
|
|
确认修改
|
|
</button>
|
|
<button onclick="closeEditModal()" class="btn btn-secondary px-6 py-3 rounded-xl text-sm font-medium">
|
|
取消
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Delete Confirm Modal -->
|
|
<div id="deleteModal" class="fixed inset-0 z-50 hidden">
|
|
<div class="absolute inset-0 bg-black/50 backdrop-blur-sm transition-opacity" onclick="closeDeleteModal()"></div>
|
|
<div class="absolute inset-0 flex items-center justify-center p-4">
|
|
<div class="glass rounded-2xl w-full max-w-md shadow-2xl transform transition-all scale-95 opacity-0" id="deleteModalContent">
|
|
<div class="p-6">
|
|
<div class="flex items-center gap-3 mb-4">
|
|
<div class="w-12 h-12 rounded-full flex items-center justify-center" style="background: var(--error-bg);">
|
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="color: var(--error-text);">
|
|
<polyline points="3 6 5 6 21 6"></polyline>
|
|
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
|
|
</svg>
|
|
</div>
|
|
<div>
|
|
<h3 class="text-lg font-semibold theme-transition" style="color: var(--text-primary);">确认删除</h3>
|
|
<p class="text-xs theme-transition" style="color: var(--text-muted);">此操作不可恢复</p>
|
|
</div>
|
|
</div>
|
|
<p class="text-sm mb-6 theme-transition" style="color: var(--text-secondary);" id="deleteConfirmText">
|
|
确定要删除这条记录吗?
|
|
</p>
|
|
<div class="flex gap-3">
|
|
<button onclick="executeDelete()" class="btn btn-danger flex-1 py-3 rounded-xl text-sm font-medium">
|
|
确认删除
|
|
</button>
|
|
<button onclick="closeDeleteModal()" class="btn btn-secondary px-6 py-3 rounded-xl text-sm font-medium">
|
|
取消
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Temp ZIP Detail Modal -->
|
|
<div id="tempZipModal" class="fixed inset-0 z-50 hidden">
|
|
<div class="absolute inset-0 bg-black/50 backdrop-blur-sm transition-opacity" onclick="closeTempZipModal()"></div>
|
|
<div class="absolute inset-0 flex items-center justify-center p-4">
|
|
<div class="glass rounded-2xl w-full max-w-4xl shadow-2xl transform transition-all scale-95 opacity-0 max-h-[85vh] overflow-hidden flex flex-col" id="tempZipModalContent">
|
|
<div class="p-6 border-b theme-transition" style="border-color: var(--glass-border);">
|
|
<div class="flex justify-between items-center">
|
|
<div>
|
|
<h3 class="text-lg font-semibold theme-transition" style="color: var(--text-primary);">临时ZIP文件详情</h3>
|
|
<p class="text-xs theme-transition" style="color: var(--text-muted);" id="tempZipModalSummary">共 0 个文件</p>
|
|
</div>
|
|
<button onclick="closeTempZipModal()" class="p-2 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors">
|
|
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="color: var(--text-muted);">
|
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="p-6 overflow-y-auto flex-1">
|
|
<div id="tempZipFileList" class="space-y-3">
|
|
<!-- File items will be inserted here -->
|
|
</div>
|
|
<div id="tempZipEmptyState" class="text-center py-12 text-sm" style="color: var(--text-muted); display: none;">
|
|
暂无临时ZIP文件
|
|
</div>
|
|
</div>
|
|
<div class="p-6 border-t theme-transition" style="border-color: var(--glass-border);">
|
|
<div class="flex gap-3">
|
|
<button onclick="cleanupAllTempZipsFromModal()" class="btn btn-danger flex-1 py-3 rounded-xl text-sm font-medium flex items-center justify-center gap-2">
|
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
<polyline points="3 6 5 6 21 6"></polyline>
|
|
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
|
|
</svg>
|
|
清理所有临时ZIP
|
|
</button>
|
|
<button onclick="closeTempZipModal()" class="btn btn-secondary px-6 py-3 rounded-xl text-sm font-medium">
|
|
关闭
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
|
|
// Unicode 转义解码
|
|
function decodeUnicode(str) {
|
|
if (typeof str !== 'string') return str;
|
|
try {
|
|
// 处理 \uXXXX 格式
|
|
return str.replace(/\\u([0-9a-fA-F]{4})/g, function(match, grp) {
|
|
return String.fromCharCode(parseInt(grp, 16));
|
|
});
|
|
} catch (e) {
|
|
return str;
|
|
}
|
|
}
|
|
|
|
// 深度解码对象中的所有字符串
|
|
function deepDecodeUnicode(obj) {
|
|
if (typeof obj === 'string') {
|
|
return decodeUnicode(obj);
|
|
}
|
|
if (Array.isArray(obj)) {
|
|
return obj.map(deepDecodeUnicode);
|
|
}
|
|
if (obj && typeof obj === 'object') {
|
|
const decoded = {};
|
|
for (const key in obj) {
|
|
decoded[key] = deepDecodeUnicode(obj[key]);
|
|
}
|
|
return decoded;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
// Global variables
|
|
let dbStats = null;
|
|
let storageChart = null;
|
|
let recordsChart = null;
|
|
|
|
// 图表主题监听:系统主题变化时重绘图表
|
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
|
|
if (dbStats) { initCharts(); }
|
|
});
|
|
|
|
// Load data
|
|
async function loadData() {
|
|
try {
|
|
const response = await fetch('/api/db/stats');
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.error || `HTTP ${response.status}`);
|
|
}
|
|
|
|
dbStats = data;
|
|
|
|
// 同时加载临时ZIP统计
|
|
const tempZipResponse = await fetch('/api/db/temp-zip-stats');
|
|
if (tempZipResponse.ok) {
|
|
const tempZipData = await tempZipResponse.json();
|
|
dbStats.temp_zip_stats = tempZipData;
|
|
}
|
|
|
|
renderData();
|
|
} catch (error) {
|
|
console.error('加载数据失败:', error);
|
|
document.getElementById('loadingState').innerHTML =
|
|
`<div class="text-center">
|
|
<p class="text-red-500 mb-2">加载失败</p>
|
|
<p class="text-sm text-gray-500">${error.message}</p>
|
|
<button onclick="location.reload(true)" class="mt-4 px-4 py-2 bg-gray-100 rounded-lg text-sm hover:bg-gray-200">重试</button>
|
|
</div>`;
|
|
showToast('加载数据失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
function renderData() {
|
|
// 解码所有数据
|
|
dbStats = deepDecodeUnicode(dbStats);
|
|
|
|
// Hide loading, show content
|
|
document.getElementById('loadingState').style.display = 'none';
|
|
document.getElementById('contentArea').classList.remove('hidden');
|
|
|
|
// Update stats
|
|
document.getElementById('dbSize').textContent = formatBytes(dbStats.db_size);
|
|
document.getElementById('dbSizeBytes').textContent = dbStats.db_size.toLocaleString() + ' bytes';
|
|
document.getElementById('tableCount').textContent = dbStats.tables.length;
|
|
document.getElementById('totalRecords').textContent = dbStats.total_records.toLocaleString();
|
|
document.getElementById('fileStorage').textContent = formatBytes(dbStats.file_storage);
|
|
document.getElementById('formCount').textContent = `${dbStats.forms.length} 个表单`;
|
|
|
|
// Render temp zip stats
|
|
if (dbStats.temp_zip_stats) {
|
|
renderTempZipStats(dbStats.temp_zip_stats);
|
|
}
|
|
|
|
// Render form storage list
|
|
renderFormStorageList();
|
|
|
|
// Initialize charts
|
|
initCharts();
|
|
|
|
// Load tables
|
|
loadTables();
|
|
|
|
// Animate elements
|
|
gsap.to("#headerSection", { opacity: 1, y: 0, duration: 0.6, ease: "power2.out" });
|
|
gsap.to(".stagger-item", {
|
|
opacity: 1,
|
|
y: 0,
|
|
duration: 0.5,
|
|
stagger: 0.1,
|
|
ease: "power2.out",
|
|
delay: 0.2
|
|
});
|
|
}
|
|
|
|
function renderFormStorageList() {
|
|
const container = document.getElementById('formStorageList');
|
|
const forms = dbStats.forms || [];
|
|
|
|
if (forms.length === 0) {
|
|
container.innerHTML = '<div class="text-center py-8 text-sm" style="color: var(--text-muted);">暂无表单数据</div>';
|
|
return;
|
|
}
|
|
|
|
// Sort by total size descending
|
|
forms.sort((a, b) => b.total_size - a.total_size);
|
|
|
|
const maxSize = forms[0].total_size;
|
|
|
|
container.innerHTML = forms.map(form => {
|
|
const filePercent = form.total_size > 0 ? (form.file_size / form.total_size * 100) : 0;
|
|
const dataPercent = form.total_size > 0 ? (form.data_size / form.total_size * 100) : 0;
|
|
const barWidth = maxSize > 0 ? (form.total_size / maxSize * 100) : 0;
|
|
|
|
return `
|
|
<div class="flex items-center gap-4 p-4 rounded-xl theme-transition" style="background: var(--bg-primary);">
|
|
<div class="flex-1 min-w-0">
|
|
<div class="flex items-center gap-2 mb-2">
|
|
<span class="font-medium text-sm truncate" style="color: var(--text-primary);">${form.title}</span>
|
|
<span class="text-xs px-2 py-0.5 rounded-full" style="background: var(--info-bg); color: var(--info-text);">${form.id}</span>
|
|
</div>
|
|
<div class="size-bar mb-2">
|
|
<div class="size-bar-fill" style="width: ${barWidth}%; background: linear-gradient(90deg, #22c55e, #3b82f6);"></div>
|
|
</div>
|
|
<div class="flex items-center gap-4 text-xs" style="color: var(--text-muted);">
|
|
<span>文件: ${formatBytes(form.file_size)}</span>
|
|
<span>数据: ${formatBytes(form.data_size)}</span>
|
|
<span>提交: ${form.submission_count} 条</span>
|
|
</div>
|
|
</div>
|
|
<div class="text-right">
|
|
<div class="text-lg font-semibold" style="color: var(--text-primary);">${formatBytes(form.total_size)}</div>
|
|
<div class="text-xs" style="color: var(--text-muted);">总计</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}).join('');
|
|
}
|
|
|
|
function renderTempZipStats(stats) {
|
|
const container = document.getElementById('tempZipStatsCard');
|
|
if (!container) return;
|
|
|
|
const count = stats.count || 0;
|
|
const totalSize = stats.total_size || 0;
|
|
const isExpired = stats.is_expired || false;
|
|
const remainingMinutes = stats.earliest_expires_minutes;
|
|
|
|
let statusText = '';
|
|
let statusClass = '';
|
|
|
|
if (count === 0) {
|
|
statusText = '无临时文件';
|
|
statusClass = 'text-xs' + ' ' + 'theme-transition';
|
|
statusClass += '; color: var(--text-muted);';
|
|
} else if (isExpired) {
|
|
statusText = '已过期';
|
|
statusClass = 'text-xs px-2 py-1 rounded-full' + ' ' + 'theme-transition';
|
|
statusClass += '; background: var(--error-bg); color: var(--error-text);';
|
|
} else if (remainingMinutes !== null && remainingMinutes >= 0) {
|
|
statusText = `${remainingMinutes}分钟后过期`;
|
|
if (remainingMinutes <= 5) {
|
|
statusClass = 'text-xs px-2 py-1 rounded-full' + ' ' + 'theme-transition';
|
|
statusClass += '; background: var(--warning-bg); color: var(--warning-text);';
|
|
} else {
|
|
statusClass = 'text-xs px-2 py-1 rounded-full' + ' ' + 'theme-transition';
|
|
statusClass += '; background: var(--info-bg); color: var(--info-text);';
|
|
}
|
|
} else {
|
|
statusText = '未知状态';
|
|
statusClass = 'text-xs' + ' ' + 'theme-transition';
|
|
statusClass += '; color: var(--text-muted);';
|
|
}
|
|
|
|
container.innerHTML = `
|
|
<div class="text-[10px] tracking-[0.2em] uppercase mb-2 theme-transition" style="color: var(--text-muted);">
|
|
临时ZIP文件
|
|
</div>
|
|
<div class="flex items-center gap-2 mb-1">
|
|
<div class="text-3xl font-semibold theme-transition" style="color: var(--text-primary);">
|
|
${count}
|
|
</div>
|
|
<div class="text-xs" style="color: var(--text-muted);">
|
|
个文件
|
|
</div>
|
|
</div>
|
|
<div class="text-xs mb-2 theme-transition" style="color: var(--text-muted);">
|
|
总大小: ${formatBytes(totalSize)}
|
|
</div>
|
|
<div class="${statusClass}">
|
|
${statusText}
|
|
</div>
|
|
${count > 0 ? `
|
|
<div class="mt-3 text-xs theme-transition" style="color: var(--accent-color);">
|
|
点击查看详情 →
|
|
</div>
|
|
` : ''}
|
|
`;
|
|
}
|
|
|
|
// 清理临时ZIP文件
|
|
async function cleanupTempZips() {
|
|
if (!confirm('确定要清理所有临时ZIP文件吗?此操作不可恢复。')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
showToast('正在清理...', 'info');
|
|
const response = await fetch('/api/db/temp-zip-cleanup', {
|
|
method: 'POST'
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
showToast(`成功清理 ${data.deleted_count} 个文件,释放 ${formatBytes(data.freed_size)} 空间`, 'success');
|
|
// 刷新数据
|
|
refreshData();
|
|
} else {
|
|
showToast('清理失败: ' + (data.error || '未知错误'), 'error');
|
|
}
|
|
} catch (error) {
|
|
showToast('清理失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
function initCharts() {
|
|
const isDark = document.documentElement.classList.contains('dark');
|
|
const textColor = isDark ? '#fafafa' : '#171717';
|
|
const gridColor = isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)';
|
|
|
|
// Storage Distribution Chart (Pie)
|
|
const storageChartEl = document.getElementById('storageChart');
|
|
if (storageChart) storageChart.dispose();
|
|
storageChart = echarts.init(storageChartEl);
|
|
|
|
const storageData = [
|
|
{ value: dbStats.file_storage, name: '文件存储', itemStyle: { color: '#22c55e' } },
|
|
{ value: dbStats.data_storage, name: '数据存储', itemStyle: { color: '#3b82f6' } },
|
|
{ value: dbStats.db_size - dbStats.file_storage - dbStats.data_storage, name: '其他', itemStyle: { color: '#9ca3af' } }
|
|
].filter(d => d.value > 0);
|
|
|
|
storageChart.setOption({
|
|
tooltip: {
|
|
trigger: 'item',
|
|
formatter: '{b}: {c} ({d}%)',
|
|
backgroundColor: isDark ? 'rgba(23,23,23,0.9)' : 'rgba(255,255,255,0.9)',
|
|
borderColor: isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)',
|
|
textStyle: { color: textColor }
|
|
},
|
|
legend: {
|
|
orient: 'vertical',
|
|
right: '5%',
|
|
top: 'center',
|
|
textStyle: { color: textColor }
|
|
},
|
|
series: [{
|
|
type: 'pie',
|
|
radius: ['40%', '70%'],
|
|
center: ['35%', '50%'],
|
|
avoidLabelOverlap: false,
|
|
itemStyle: {
|
|
borderRadius: 8,
|
|
borderColor: isDark ? '#171717' : '#ffffff',
|
|
borderWidth: 2
|
|
},
|
|
label: { show: false },
|
|
emphasis: {
|
|
label: {
|
|
show: true,
|
|
fontSize: 14,
|
|
fontWeight: 'bold',
|
|
color: textColor
|
|
}
|
|
},
|
|
data: storageData
|
|
}]
|
|
});
|
|
|
|
// Records Chart (Bar)
|
|
const recordsChartEl = document.getElementById('recordsChart');
|
|
if (recordsChart) recordsChart.dispose();
|
|
recordsChart = echarts.init(recordsChartEl);
|
|
|
|
const tableNames = dbStats.tables.map(t => t.name);
|
|
const tableRecords = dbStats.tables.map(t => t.record_count);
|
|
|
|
recordsChart.setOption({
|
|
tooltip: {
|
|
trigger: 'axis',
|
|
axisPointer: { type: 'shadow' },
|
|
backgroundColor: isDark ? 'rgba(23,23,23,0.9)' : 'rgba(255,255,255,0.9)',
|
|
borderColor: isDark ? 'rgba(255,255,255,0.1)' : 'rgba(0,0,0,0.1)',
|
|
textStyle: { color: textColor }
|
|
},
|
|
grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
|
|
xAxis: {
|
|
type: 'category',
|
|
data: tableNames,
|
|
axisLine: { lineStyle: { color: gridColor } },
|
|
axisLabel: { color: textColor, rotate: 30 }
|
|
},
|
|
yAxis: {
|
|
type: 'value',
|
|
axisLine: { lineStyle: { color: gridColor } },
|
|
axisLabel: { color: textColor },
|
|
splitLine: { lineStyle: { color: gridColor } }
|
|
},
|
|
series: [{
|
|
data: tableRecords,
|
|
type: 'bar',
|
|
barWidth: '50%',
|
|
itemStyle: {
|
|
borderRadius: [4, 4, 0, 0],
|
|
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
|
|
{ offset: 0, color: '#6366f1' },
|
|
{ offset: 1, color: '#8b5cf6' }
|
|
])
|
|
}
|
|
}]
|
|
});
|
|
|
|
// Handle resize
|
|
if (!window._chartResizeHandler) {
|
|
window._chartResizeHandler = () => {
|
|
if (storageChart) storageChart.resize();
|
|
if (recordsChart) recordsChart.resize();
|
|
};
|
|
window.addEventListener('resize', window._chartResizeHandler);
|
|
}
|
|
}
|
|
|
|
// Tab switching
|
|
function switchTab(tab) {
|
|
document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
|
|
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
|
|
|
|
event.target.classList.add('active');
|
|
document.getElementById(tab + 'Tab').classList.add('active');
|
|
}
|
|
|
|
// SQL Execution
|
|
async function executeSql() {
|
|
const sql = document.getElementById('sqlInput').value.trim();
|
|
if (!sql) {
|
|
showToast('请输入 SQL 命令', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
showToast('执行中...', 'info');
|
|
const response = await fetch('/api/db/execute', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ sql })
|
|
});
|
|
|
|
const data = await response.json();
|
|
const resultDiv = document.getElementById('sqlResult');
|
|
const resultContent = document.getElementById('sqlResultContent');
|
|
const statusSpan = document.getElementById('sqlStatus');
|
|
|
|
resultDiv.classList.remove('hidden');
|
|
|
|
if (data.success) {
|
|
// 解码结果数据
|
|
if (data.rows) {
|
|
data.rows = deepDecodeUnicode(data.rows);
|
|
}
|
|
|
|
statusSpan.innerHTML = `<span style="color: var(--success-text);">✓ 执行成功 (${data.execution_time}ms)</span>`;
|
|
|
|
if (data.rows && data.rows.length > 0) {
|
|
// Render table
|
|
const columns = Object.keys(data.rows[0]);
|
|
resultContent.innerHTML = `
|
|
<table>
|
|
<thead>
|
|
<tr>${columns.map(c => `<th>${c}</th>`).join('')}</tr>
|
|
</thead>
|
|
<tbody>
|
|
${data.rows.map(row =>
|
|
`<tr>${columns.map(c => `<td style="white-space: pre-wrap; max-width: 300px;">${row[c] !== null ? row[c] : '<span style="opacity:0.5;">NULL</span>'}</td>`).join('')}</tr>`
|
|
).join('')}
|
|
</tbody>
|
|
</table>
|
|
`;
|
|
} else if (data.affected_rows !== undefined) {
|
|
resultContent.innerHTML = `<div class="text-sm" style="color: var(--text-secondary);">影响行数: ${data.affected_rows}</div>`;
|
|
} else {
|
|
resultContent.innerHTML = `<div class="text-sm" style="color: var(--text-secondary);">执行成功,无返回数据</div>`;
|
|
}
|
|
} else {
|
|
statusSpan.innerHTML = `<span style="color: var(--error-text);">✗ 执行失败</span>`;
|
|
resultContent.innerHTML = `<div class="text-sm" style="color: var(--error-text);">${data.error}</div>`;
|
|
}
|
|
} catch (error) {
|
|
showToast('执行失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
function clearSql() {
|
|
document.getElementById('sqlInput').value = '';
|
|
document.getElementById('sqlResult').classList.add('hidden');
|
|
}
|
|
|
|
// Load tables
|
|
async function loadTables() {
|
|
try {
|
|
const response = await fetch('/api/db/tables');
|
|
const data = await response.json();
|
|
|
|
if (data.tables) {
|
|
// Update table select
|
|
const select = document.getElementById('tableSelect');
|
|
select.innerHTML = '<option value="">选择表...</option>' +
|
|
data.tables.map(t => `<option value="${t.name}">${t.name} (${t.record_count} 条)</option>`).join('');
|
|
|
|
// Render tables list
|
|
const container = document.getElementById('tablesList');
|
|
container.innerHTML = data.tables.map(table => `
|
|
<div class="p-4 rounded-xl theme-transition" style="background: var(--bg-primary);">
|
|
<div class="flex justify-between items-center mb-3">
|
|
<div class="flex items-center gap-3">
|
|
<span class="font-medium" style="color: var(--text-primary);">${table.name}</span>
|
|
<span class="text-xs px-2 py-0.5 rounded-full" style="background: var(--info-bg); color: var(--info-text);">${table.record_count} 条</span>
|
|
</div>
|
|
<button onclick="showTableStructure('${table.name}')" class="text-xs px-3 py-1.5 rounded-lg border theme-transition hover:opacity-70" style="border-color: var(--border-color); color: var(--text-muted);">
|
|
查看结构
|
|
</button>
|
|
</div>
|
|
<div class="text-xs" style="color: var(--text-muted);">
|
|
${table.columns.map(c => `<span class="inline-block mr-3">${c.name} <span style="opacity:0.6;">${c.type}</span></span>`).join('')}
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
}
|
|
} catch (error) {
|
|
console.error('加载表失败:', error);
|
|
}
|
|
}
|
|
|
|
function showTableStructure(tableName) {
|
|
// Switch to SQL tab and show structure
|
|
document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
|
|
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
|
|
document.querySelector('.tab-btn:nth-child(1)').classList.add('active');
|
|
document.getElementById('sqlTab').classList.add('active');
|
|
|
|
document.getElementById('sqlInput').value = `PRAGMA table_info(${tableName});`;
|
|
executeSql();
|
|
}
|
|
|
|
// Browse table data
|
|
async function browseTable() {
|
|
const tableName = document.getElementById('tableSelect').value;
|
|
const limit = document.getElementById('limitInput').value;
|
|
|
|
if (!tableName) {
|
|
showToast('请选择表', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`/api/db/browse?table=${tableName}&limit=${limit}`);
|
|
const data = await response.json();
|
|
|
|
// 解码数据
|
|
if (data.rows) {
|
|
data.rows = deepDecodeUnicode(data.rows);
|
|
}
|
|
|
|
const container = document.getElementById('tableData');
|
|
|
|
if (data.rows && data.rows.length > 0) {
|
|
const columns = Object.keys(data.rows[0]);
|
|
container.innerHTML = `
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
${columns.map(c => `<th>${c}</th>`).join('')}
|
|
<th style="width: 120px; text-align: center;">操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
${data.rows.map((row, index) =>
|
|
`<tr>
|
|
${columns.map(c => `<td onclick="cellClick(this, '${c}', ${index})" style="cursor: pointer; white-space: pre-wrap; max-width: 300px;">${row[c] !== null ? escapeHtml(row[c]) : '<span style="opacity:0.5;">NULL</span>'}</td>`).join('')}
|
|
<td style="text-align: center;">
|
|
<button onclick='openEditModal(${JSON.stringify(row).replace(/'/g, "'")})' class="btn btn-primary px-2 py-1 rounded text-xs mr-1" style="padding: 4px 8px;">编辑</button>
|
|
<button onclick='openDeleteModal(${JSON.stringify(row).replace(/'/g, "'")})' class="btn btn-danger px-2 py-1 rounded text-xs" style="padding: 4px 8px;">删除</button>
|
|
</td>
|
|
</tr>`
|
|
).join('')}
|
|
</tbody>
|
|
</table>
|
|
`;
|
|
} else {
|
|
container.innerHTML = '<div class="p-8 text-center text-sm" style="color: var(--text-muted);">暂无数据</div>';
|
|
}
|
|
} catch (error) {
|
|
showToast('浏览失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
// VACUUM
|
|
async function runVacuum() {
|
|
if (!confirm('确定要执行 VACUUM 优化吗?这将重建数据库文件以释放空间。')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
showToast('正在优化数据库...', 'info');
|
|
const response = await fetch('/api/db/vacuum', { method: 'POST' });
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
document.getElementById('vacuumBefore').textContent = formatBytes(data.before_size);
|
|
document.getElementById('vacuumAfter').textContent = formatBytes(data.after_size);
|
|
document.getElementById('vacuumSaved').textContent = formatBytes(data.freed_space);
|
|
|
|
document.getElementById('vacuumModal').classList.remove('hidden');
|
|
setTimeout(() => {
|
|
document.getElementById('vacuumModalContent').classList.add('scale-100', 'opacity-100');
|
|
document.getElementById('vacuumModalContent').classList.remove('scale-95', 'opacity-0');
|
|
}, 10);
|
|
} else {
|
|
showToast('优化失败: ' + data.error, 'error');
|
|
}
|
|
} catch (error) {
|
|
showToast('优化失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
function closeVacuumModal() {
|
|
document.getElementById('vacuumModalContent').classList.remove('scale-100', 'opacity-100');
|
|
document.getElementById('vacuumModalContent').classList.add('scale-95', 'opacity-0');
|
|
setTimeout(() => {
|
|
document.getElementById('vacuumModal').classList.add('hidden');
|
|
}, 300);
|
|
}
|
|
|
|
// Refresh data
|
|
async function refreshData() {
|
|
document.getElementById('loadingState').style.display = 'flex';
|
|
document.getElementById('contentArea').classList.add('hidden');
|
|
await loadData();
|
|
showToast('数据已刷新', 'success');
|
|
}
|
|
|
|
// ==================== CRUD Operations ====================
|
|
|
|
let currentTableStructure = null;
|
|
let currentEditingRow = null;
|
|
let currentDeletingRow = null;
|
|
let currentTableName = '';
|
|
|
|
// 获取表结构
|
|
async function getTableStructure(tableName) {
|
|
try {
|
|
const response = await fetch('/api/db/execute', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ sql: `PRAGMA table_info(${tableName});` })
|
|
});
|
|
const data = await response.json();
|
|
if (data.success && data.rows) {
|
|
return deepDecodeUnicode(data.rows);
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
console.error('获取表结构失败:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 打开新增模态框
|
|
async function openInsertModal() {
|
|
const tableName = document.getElementById('tableSelect').value;
|
|
if (!tableName) {
|
|
showToast('请先选择表', 'error');
|
|
return;
|
|
}
|
|
|
|
currentTableName = tableName;
|
|
const structure = await getTableStructure(tableName);
|
|
if (!structure) {
|
|
showToast('获取表结构失败', 'error');
|
|
return;
|
|
}
|
|
|
|
currentTableStructure = structure;
|
|
document.getElementById('insertTableName').textContent = `表名: ${tableName}`;
|
|
|
|
const container = document.getElementById('insertFormFields');
|
|
container.innerHTML = structure.map(col => {
|
|
const isPrimaryKey = col.pk === 1;
|
|
const isRequired = col.notnull === 1;
|
|
const inputType = getInputType(col.type);
|
|
const isTextarea = inputType === 'textarea';
|
|
|
|
return `
|
|
<div class="space-y-1">
|
|
<label class="text-sm font-medium flex items-center gap-2" style="color: var(--text-secondary);">
|
|
${col.name}
|
|
${isPrimaryKey ? '<span class="text-xs px-1.5 py-0.5 rounded" style="background: var(--warning-bg); color: var(--warning-text);">主键</span>' : ''}
|
|
${isRequired ? '<span class="text-xs" style="color: var(--error-text);">*</span>' : ''}
|
|
<span class="text-xs opacity-60">${col.type}</span>
|
|
</label>
|
|
${isTextarea ? `
|
|
<textarea id="insert_${col.name}"
|
|
class="w-full px-4 py-2.5 rounded-xl border theme-transition text-sm"
|
|
style="border-color: var(--border-color); background: var(--bg-secondary); color: var(--text-primary); min-height: 80px; resize: vertical; font-family: inherit;"
|
|
placeholder="${isPrimaryKey ? '自动填充或手动输入' : '请输入...'}"></textarea>
|
|
` : `
|
|
<input type="${inputType}"
|
|
id="insert_${col.name}"
|
|
class="w-full px-4 py-2.5 rounded-xl border theme-transition text-sm"
|
|
style="border-color: var(--border-color); background: var(--bg-secondary); color: var(--text-primary);"
|
|
placeholder="${isPrimaryKey ? '自动填充或手动输入' : '请输入...'}"
|
|
${isPrimaryKey ? '' : ''}>
|
|
`}
|
|
</div>
|
|
`;
|
|
}).join('');
|
|
|
|
document.getElementById('insertModal').classList.remove('hidden');
|
|
setTimeout(() => {
|
|
document.getElementById('insertModalContent').classList.add('scale-100', 'opacity-100');
|
|
document.getElementById('insertModalContent').classList.remove('scale-95', 'opacity-0');
|
|
}, 10);
|
|
}
|
|
|
|
function closeInsertModal() {
|
|
document.getElementById('insertModalContent').classList.remove('scale-100', 'opacity-100');
|
|
document.getElementById('insertModalContent').classList.add('scale-95', 'opacity-0');
|
|
setTimeout(() => {
|
|
document.getElementById('insertModal').classList.add('hidden');
|
|
}, 300);
|
|
}
|
|
|
|
// 执行新增
|
|
async function executeInsert() {
|
|
const tableName = currentTableName;
|
|
const columns = [];
|
|
const values = [];
|
|
|
|
currentTableStructure.forEach(col => {
|
|
const value = document.getElementById(`insert_${col.name}`).value;
|
|
if (value !== '' || col.notnull === 1) {
|
|
columns.push(col.name);
|
|
values.push(formatSqlValue(value, col.type));
|
|
}
|
|
});
|
|
|
|
if (columns.length === 0) {
|
|
showToast('请至少填写一个字段', 'error');
|
|
return;
|
|
}
|
|
|
|
const sql = `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${values.join(', ')});`;
|
|
|
|
try {
|
|
showToast('正在执行...', 'info');
|
|
const response = await fetch('/api/db/execute', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ sql })
|
|
});
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
showToast('新增成功', 'success');
|
|
closeInsertModal();
|
|
browseTable(); // 刷新表格
|
|
} else {
|
|
showToast('新增失败: ' + data.error, 'error');
|
|
}
|
|
} catch (error) {
|
|
showToast('执行失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
// 打开编辑模态框
|
|
async function openEditModal(rowData) {
|
|
const tableName = document.getElementById('tableSelect').value;
|
|
if (!tableName) return;
|
|
|
|
currentTableName = tableName;
|
|
currentEditingRow = rowData;
|
|
|
|
const structure = await getTableStructure(tableName);
|
|
if (!structure) {
|
|
showToast('获取表结构失败', 'error');
|
|
return;
|
|
}
|
|
|
|
currentTableStructure = structure;
|
|
document.getElementById('editTableName').textContent = `表名: ${tableName}`;
|
|
|
|
const container = document.getElementById('editFormFields');
|
|
container.innerHTML = structure.map(col => {
|
|
const isPrimaryKey = col.pk === 1;
|
|
const value = rowData[col.name] !== null && rowData[col.name] !== undefined ? rowData[col.name] : '';
|
|
const inputType = getInputType(col.type);
|
|
const isTextarea = inputType === 'textarea';
|
|
|
|
return `
|
|
<div class="space-y-1">
|
|
<label class="text-sm font-medium flex items-center gap-2" style="color: var(--text-secondary);">
|
|
${col.name}
|
|
${isPrimaryKey ? '<span class="text-xs px-1.5 py-0.5 rounded" style="background: var(--warning-bg); color: var(--warning-text);">主键</span>' : ''}
|
|
<span class="text-xs opacity-60">${col.type}</span>
|
|
</label>
|
|
${isTextarea ? `
|
|
<textarea id="edit_${col.name}"
|
|
class="w-full px-4 py-2.5 rounded-xl border theme-transition text-sm"
|
|
style="border-color: var(--border-color); background: var(--bg-secondary); color: var(--text-primary); min-height: 80px; resize: vertical; font-family: inherit;"
|
|
${isPrimaryKey ? 'readonly' : ''}>${escapeHtml(value)}</textarea>
|
|
` : `
|
|
<input type="${inputType}"
|
|
id="edit_${col.name}"
|
|
class="w-full px-4 py-2.5 rounded-xl border theme-transition text-sm"
|
|
style="border-color: var(--border-color); background: var(--bg-secondary); color: var(--text-primary);"
|
|
value="${escapeHtml(value)}"
|
|
${isPrimaryKey ? 'readonly' : ''}>
|
|
`}
|
|
</div>
|
|
`;
|
|
}).join('');
|
|
|
|
document.getElementById('editModal').classList.remove('hidden');
|
|
setTimeout(() => {
|
|
document.getElementById('editModalContent').classList.add('scale-100', 'opacity-100');
|
|
document.getElementById('editModalContent').classList.remove('scale-95', 'opacity-0');
|
|
}, 10);
|
|
}
|
|
|
|
function closeEditModal() {
|
|
document.getElementById('editModalContent').classList.remove('scale-100', 'opacity-100');
|
|
document.getElementById('editModalContent').classList.add('scale-95', 'opacity-0');
|
|
setTimeout(() => {
|
|
document.getElementById('editModal').classList.add('hidden');
|
|
currentEditingRow = null;
|
|
}, 300);
|
|
}
|
|
|
|
// 执行更新
|
|
async function executeUpdate() {
|
|
const tableName = currentTableName;
|
|
const updates = [];
|
|
let primaryKeyCol = null;
|
|
let primaryKeyValue = null;
|
|
|
|
currentTableStructure.forEach(col => {
|
|
const value = document.getElementById(`edit_${col.name}`).value;
|
|
|
|
if (col.pk === 1) {
|
|
primaryKeyCol = col.name;
|
|
primaryKeyValue = currentEditingRow[col.name];
|
|
} else {
|
|
updates.push(`${col.name} = ${formatSqlValue(value, col.type)}`);
|
|
}
|
|
});
|
|
|
|
if (!primaryKeyCol) {
|
|
showToast('未找到主键列,无法更新', 'error');
|
|
return;
|
|
}
|
|
|
|
if (updates.length === 0) {
|
|
showToast('没有需要更新的字段', 'error');
|
|
return;
|
|
}
|
|
|
|
const sql = `UPDATE ${tableName} SET ${updates.join(', ')} WHERE ${primaryKeyCol} = ${formatSqlValue(primaryKeyValue, 'INTEGER')};`;
|
|
|
|
try {
|
|
showToast('正在执行...', 'info');
|
|
const response = await fetch('/api/db/execute', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ sql })
|
|
});
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
showToast('更新成功', 'success');
|
|
closeEditModal();
|
|
browseTable(); // 刷新表格
|
|
} else {
|
|
showToast('更新失败: ' + data.error, 'error');
|
|
}
|
|
} catch (error) {
|
|
showToast('执行失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
// 打开删除确认框
|
|
function openDeleteModal(rowData) {
|
|
const tableName = document.getElementById('tableSelect').value;
|
|
currentTableName = tableName;
|
|
currentDeletingRow = rowData;
|
|
|
|
// 查找主键列
|
|
const primaryKey = Object.keys(rowData)[0];
|
|
const displayText = `确定要删除 ${tableName} 表中 ${primaryKey} = ${rowData[primaryKey]} 的记录吗?`;
|
|
|
|
document.getElementById('deleteConfirmText').textContent = displayText;
|
|
document.getElementById('deleteModal').classList.remove('hidden');
|
|
setTimeout(() => {
|
|
document.getElementById('deleteModalContent').classList.add('scale-100', 'opacity-100');
|
|
document.getElementById('deleteModalContent').classList.remove('scale-95', 'opacity-0');
|
|
}, 10);
|
|
}
|
|
|
|
function closeDeleteModal() {
|
|
document.getElementById('deleteModalContent').classList.remove('scale-100', 'opacity-100');
|
|
document.getElementById('deleteModalContent').classList.add('scale-95', 'opacity-0');
|
|
setTimeout(() => {
|
|
document.getElementById('deleteModal').classList.add('hidden');
|
|
currentDeletingRow = null;
|
|
}, 300);
|
|
}
|
|
|
|
// 执行删除
|
|
async function executeDelete() {
|
|
const tableName = currentTableName;
|
|
const rowData = currentDeletingRow;
|
|
|
|
// 查找主键列(通常是第一列或名为 id 的列)
|
|
let primaryKeyCol = Object.keys(rowData).find(k => k.toLowerCase() === 'id') || Object.keys(rowData)[0];
|
|
let primaryKeyValue = rowData[primaryKeyCol];
|
|
|
|
const sql = `DELETE FROM ${tableName} WHERE ${primaryKeyCol} = ${formatSqlValue(primaryKeyValue, 'INTEGER')};`;
|
|
|
|
try {
|
|
showToast('正在执行...', 'info');
|
|
const response = await fetch('/api/db/execute', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ sql })
|
|
});
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
showToast('删除成功', 'success');
|
|
closeDeleteModal();
|
|
browseTable(); // 刷新表格
|
|
} else {
|
|
showToast('删除失败: ' + data.error, 'error');
|
|
}
|
|
} catch (error) {
|
|
showToast('执行失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
// 辅助函数:根据类型获取输入类型
|
|
function getInputType(sqlType) {
|
|
if (!sqlType) return 'text';
|
|
const type = sqlType.toUpperCase();
|
|
if (type.includes('INT')) return 'number';
|
|
if (type.includes('REAL') || type.includes('FLOAT') || type.includes('DOUBLE')) return 'number';
|
|
if (type.includes('DATE') || type.includes('TIME')) return 'datetime-local';
|
|
if (type.includes('BOOLEAN')) return 'checkbox';
|
|
if (type.includes('TEXT')) return 'textarea';
|
|
return 'text';
|
|
}
|
|
|
|
// 辅助函数:格式化 SQL 值
|
|
function formatSqlValue(value, type) {
|
|
if (value === '' || value === null || value === undefined) {
|
|
return 'NULL';
|
|
}
|
|
|
|
const upperType = (type || '').toUpperCase();
|
|
|
|
// 数字类型
|
|
if (upperType.includes('INT') || upperType.includes('REAL') || upperType.includes('FLOAT') || upperType.includes('DOUBLE') || upperType.includes('NUMERIC')) {
|
|
if (value === '' || isNaN(value)) {
|
|
return 'NULL';
|
|
}
|
|
return value;
|
|
}
|
|
|
|
// 布尔类型
|
|
if (upperType.includes('BOOLEAN')) {
|
|
return value === 'true' || value === true || value === '1' || value === 1 ? '1' : '0';
|
|
}
|
|
|
|
// 字符串类型需要转义
|
|
const escaped = String(value)
|
|
.replace(/'/g, "''") // SQLite 转义单引号
|
|
.replace(/\\/g, '\\\\'); // 转义反斜杠
|
|
return `'${escaped}'`;
|
|
}
|
|
|
|
// 辅助函数:HTML 转义
|
|
function escapeHtml(text) {
|
|
if (text === null || text === undefined) return '';
|
|
const div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
|
|
// ==================== End CRUD Operations ====================
|
|
// 单元格点击编辑
|
|
let editingCell = null;
|
|
let editingCellOriginalValue = null;
|
|
|
|
function cellClick(td, columnName, rowIndex) {
|
|
// 如果正在编辑其他单元格,先保存
|
|
if (editingCell && editingCell !== td) {
|
|
saveCellEdit();
|
|
}
|
|
|
|
if (editingCell === td) return;
|
|
|
|
const originalValue = td.textContent === 'NULL' ? '' : td.textContent;
|
|
editingCellOriginalValue = originalValue;
|
|
editingCell = td;
|
|
|
|
td.innerHTML = `<textarea class="cell-edit-input" onblur="saveCellEdit()" onkeydown="cellKeydown(event)" style="width: 100%; background: transparent; border: 1px solid var(--border-focus); border-radius: 4px; padding: 4px; color: var(--text-primary); font-size: inherit; min-height: 60px; resize: vertical; font-family: inherit; white-space: pre-wrap;">${escapeHtml(originalValue)}</textarea>`;
|
|
td.querySelector('textarea').focus();
|
|
}
|
|
|
|
function cellKeydown(event) {
|
|
if (event.key === 'Enter') {
|
|
saveCellEdit();
|
|
} else if (event.key === 'Escape') {
|
|
cancelCellEdit();
|
|
}
|
|
}
|
|
|
|
async function saveCellEdit() {
|
|
if (!editingCell) return;
|
|
|
|
const input = editingCell.querySelector('textarea') || editingCell.querySelector('input');
|
|
const newValue = input.value;
|
|
|
|
if (newValue !== editingCellOriginalValue) {
|
|
// 获取行数据
|
|
const tableName = document.getElementById('tableSelect').value;
|
|
const row = editingCell.parentElement;
|
|
const cells = row.querySelectorAll('td');
|
|
const headers = document.querySelectorAll('#tableData th');
|
|
|
|
// 构建行数据对象
|
|
const rowData = {};
|
|
cells.forEach((cell, index) => {
|
|
if (index < headers.length - 1) { // 排除操作列
|
|
const colName = headers[index].textContent;
|
|
rowData[colName] = cell.textContent;
|
|
}
|
|
});
|
|
|
|
// 更新当前单元格的值
|
|
const colName = headers[[...cells].indexOf(editingCell)].textContent;
|
|
rowData[colName] = newValue;
|
|
|
|
// 执行更新
|
|
const structure = await getTableStructure(tableName);
|
|
const primaryKeyCol = structure.find(c => c.pk === 1);
|
|
|
|
if (primaryKeyCol) {
|
|
const colInfo = structure.find(c => c.name === colName);
|
|
const sql = `UPDATE ${tableName} SET ${colName} = ${formatSqlValue(newValue, colInfo ? colInfo.type : 'TEXT')} WHERE ${primaryKeyCol.name} = ${formatSqlValue(rowData[primaryKeyCol.name], primaryKeyCol.type)};`;
|
|
|
|
try {
|
|
const response = await fetch('/api/db/execute', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ sql })
|
|
});
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
showToast('更新成功', 'success');
|
|
editingCell.textContent = newValue || 'NULL';
|
|
} else {
|
|
showToast('更新失败: ' + data.error, 'error');
|
|
editingCell.textContent = editingCellOriginalValue || 'NULL';
|
|
}
|
|
} catch (error) {
|
|
showToast('执行失败: ' + error.message, 'error');
|
|
editingCell.textContent = editingCellOriginalValue || 'NULL';
|
|
}
|
|
}
|
|
} else {
|
|
editingCell.textContent = newValue || 'NULL';
|
|
}
|
|
|
|
editingCell = null;
|
|
editingCellOriginalValue = null;
|
|
}
|
|
|
|
function cancelCellEdit() {
|
|
if (editingCell) {
|
|
editingCell.textContent = editingCellOriginalValue || 'NULL';
|
|
editingCell = null;
|
|
editingCellOriginalValue = null;
|
|
}
|
|
}
|
|
|
|
// ==================== End Cell Edit ====================
|
|
|
|
// ==================== Temp ZIP Modal ====================
|
|
|
|
let tempZipData = null;
|
|
|
|
async function showTempZipModal() {
|
|
try {
|
|
showToast('加载中...', 'info');
|
|
const response = await fetch('/api/db/temp-zip-stats');
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.error || `HTTP ${response.status}`);
|
|
}
|
|
|
|
tempZipData = data;
|
|
renderTempZipModal();
|
|
|
|
document.getElementById('tempZipModal').classList.remove('hidden');
|
|
setTimeout(() => {
|
|
document.getElementById('tempZipModalContent').classList.add('scale-100', 'opacity-100');
|
|
document.getElementById('tempZipModalContent').classList.remove('scale-95', 'opacity-0');
|
|
}, 10);
|
|
|
|
} catch (error) {
|
|
showToast('加载失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
function closeTempZipModal() {
|
|
document.getElementById('tempZipModalContent').classList.remove('scale-100', 'opacity-100');
|
|
document.getElementById('tempZipModalContent').classList.add('scale-95', 'opacity-0');
|
|
setTimeout(() => {
|
|
document.getElementById('tempZipModal').classList.add('hidden');
|
|
}, 300);
|
|
}
|
|
|
|
function renderTempZipModal() {
|
|
if (!tempZipData) return;
|
|
|
|
const summary = document.getElementById('tempZipModalSummary');
|
|
const fileList = document.getElementById('tempZipFileList');
|
|
const emptyState = document.getElementById('tempZipEmptyState');
|
|
|
|
if (tempZipData.count === 0) {
|
|
summary.textContent = '暂无临时ZIP文件';
|
|
fileList.innerHTML = '';
|
|
emptyState.style.display = 'block';
|
|
return;
|
|
}
|
|
|
|
summary.textContent = `共 ${tempZipData.count} 个文件, 总大小: ${formatBytes(tempZipData.total_size)}`;
|
|
emptyState.style.display = 'none';
|
|
|
|
fileList.innerHTML = tempZipData.files.map(file => {
|
|
const isExpired = file.remaining_minutes <= 0;
|
|
const isExpiringSoon = file.remaining_minutes > 0 && file.remaining_minutes <= 5;
|
|
|
|
let statusClass = '';
|
|
let statusText = '';
|
|
|
|
if (isExpired) {
|
|
statusClass = 'px-2 py-1 rounded-full text-xs';
|
|
statusClass += '; background: var(--error-bg); color: var(--error-text);';
|
|
statusText = '已过期';
|
|
} else if (isExpiringSoon) {
|
|
statusClass = 'px-2 py-1 rounded-full text-xs';
|
|
statusClass += '; background: var(--warning-bg); color: var(--warning-text);';
|
|
statusText = `${file.remaining_minutes}分钟后过期`;
|
|
} else {
|
|
statusClass = 'px-2 py-1 rounded-full text-xs';
|
|
statusClass += '; background: var(--info-bg); color: var(--info-text);';
|
|
statusText = `${file.remaining_minutes}分钟后过期`;
|
|
}
|
|
|
|
const uploadedTime = new Date(file.uploaded_at).toLocaleString('zh-CN');
|
|
|
|
return `
|
|
<div class="p-4 rounded-xl theme-transition" style="background: var(--bg-primary);">
|
|
<div class="flex justify-between items-start mb-3">
|
|
<div class="flex-1 min-w-0 pr-4">
|
|
<div class="font-medium text-sm mb-1 theme-transition" style="color: var(--text-primary); word-break: break-all;">
|
|
${escapeHtml(file.filename)}
|
|
</div>
|
|
<div class="text-xs theme-transition" style="color: var(--text-muted);">
|
|
<div class="mb-1">
|
|
<span class="inline-block mr-3">
|
|
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="inline mr-1">
|
|
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
|
|
<polyline points="14 2 14 8 20 8"></polyline>
|
|
</svg>
|
|
表单: ${escapeHtml(file.form_title || '未知表单')}
|
|
</span>
|
|
<span class="inline-block mr-3" style="color: var(--accent-color);">
|
|
ID: ${escapeHtml(file.form_id || 'N/A')}
|
|
</span>
|
|
</div>
|
|
<div class="mb-1">
|
|
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="inline mr-1">
|
|
<circle cx="12" cy="12" r="10"></circle>
|
|
<polyline points="12 6 12 12 16 14"></polyline>
|
|
</svg>
|
|
创建时间: ${uploadedTime}
|
|
</div>
|
|
<div>
|
|
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="inline mr-1">
|
|
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
|
|
<polyline points="7 10 12 15 17 10"></polyline>
|
|
<line x1="12" y1="15" x2="12" y2="3"></line>
|
|
</svg>
|
|
大小: ${formatBytes(file.file_size)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="text-right">
|
|
<div class="${statusClass}">
|
|
${statusText}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
${isExpired ? `
|
|
<div class="text-xs theme-transition mt-3 px-3 py-2 rounded-lg" style="background: var(--error-bg); color: var(--error-text);">
|
|
⚠️ 此文件已过期,建议清理
|
|
</div>
|
|
` : ''}
|
|
</div>
|
|
`;
|
|
}).join('');
|
|
}
|
|
|
|
async function cleanupAllTempZipsFromModal() {
|
|
if (!tempZipData || tempZipData.count === 0) {
|
|
showToast('没有可清理的文件', 'warning');
|
|
return;
|
|
}
|
|
|
|
if (!confirm(`确定要清理所有 ${tempZipData.count} 个临时ZIP文件吗?\n此操作不可恢复。`)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
showToast('正在清理...', 'info');
|
|
const response = await fetch('/api/db/temp-zip-cleanup', {
|
|
method: 'POST'
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
showToast(`成功清理 ${data.deleted_count} 个文件,释放 ${formatBytes(data.freed_size)} 空间`, 'success');
|
|
|
|
// 关闭弹窗
|
|
closeTempZipModal();
|
|
|
|
// 刷新主页面数据
|
|
refreshData();
|
|
} else {
|
|
showToast('清理失败: ' + (data.error || '未知错误'), 'error');
|
|
}
|
|
} catch (error) {
|
|
showToast('清理失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
// ==================== End Temp ZIP Modal ====================
|
|
|
|
|
|
// Initialize
|
|
window.addEventListener('load', () => {
|
|
loadData();
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html> |