Files
YG_FT_Platform/web/pages/dataset-preview.html
WIN-JHFT4D3SIVT\caoxiaozhu 513e96082c 重构了main.html的主函数
重构了大量的页面的sidebar
优化了代码结构
2026-02-02 09:22:52 +08:00

314 lines
13 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据管理-文件预览 - 远光软件微调平台</title>
<script src="../lib/tailwindcss/tailwind.js"></script>
<script>
if (typeof console !== 'undefined' && console.warn) {
const originalWarn = console.warn;
console.warn = function(...args) {
if (args[0] && args[0].includes && args[0].includes('cdn.tailwindcss.com')) {
return;
}
originalWarn.apply(console, args);
};
}
</script>
<script>
// 设置当前页面,供侧边栏高亮使用
window.sidebarCurrentPage = 'dataset-manage';
</script>
<link href="../lib/font-awesome/css/font-awesome.min.css" rel="stylesheet">
<!-- 侧边栏加载器 -->
<script src="../js/components/sidebar-loader.js"></script>
<style>
.text-primary { color: #1890ff; }
.bg-primary { background-color: #1890ff; }
.text-danger { color: #f5222d; }
</style>
</head>
<body class="antialiased bg-gray-50 flex h-screen overflow-hidden">
<!-- 侧边栏容器 -->
<div id="sidebar-container"></div>
<div class="flex-1 flex flex-col overflow-hidden">
<!-- 顶部导航 -->
<header class="bg-white border-b border-gray-200 shadow-sm">
<div class="flex items-center justify-between px-6 h-14">
<div class="flex items-center space-x-6">
<a href="#" onclick="goBack()" class="text-gray-500 hover:text-gray-700 flex items-center px-3 py-1.5 rounded hover:bg-gray-100 transition-colors">
<i class="fa fa-arrow-left"></i>
<span class="ml-1">返回</span>
</a>
</div>
<div class="flex items-center space-x-4">
<div class="relative group">
<img src="https://picsum.photos/id/1005/32/32" class="w-8 h-8 rounded-full cursor-pointer" alt="用户头像">
<div class="absolute right-0 top-full mt-1 bg-white rounded shadow-lg py-1 hidden group-hover:block border border-gray-100 min-w-[140px] z-50">
<a href="login.html" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 whitespace-nowrap">
<i class="fa fa-sign-out mr-1"></i>退出登录
</a>
</div>
</div>
</div>
</div>
</header>
<!-- 内容区域 - 文件预览 -->
<main class="flex-1 overflow-y-auto p-6 bg-gray-50">
<div id="page-content">
<!-- 文件预览卡片 -->
<div class="bg-white rounded-lg shadow-sm">
<!-- 面包屑导航 -->
<div class="px-4 py-3 border-b border-gray-100">
<div class="flex items-center text-sm">
<span class="text-primary cursor-pointer hover:underline" onclick="goBack()">数据集管理</span>
<span class="mx-2 text-gray-300">/</span>
<span class="text-gray-800 font-medium">文件预览</span>
</div>
</div>
<!-- 头部 -->
<div class="flex items-center justify-between p-4 border-b border-gray-100">
<div class="flex items-center">
<h2 class="text-lg font-medium">文件预览</h2>
<span id="fileNameDisplay" class="ml-3 text-sm text-gray-500"></span>
</div>
<div class="flex items-center space-x-3">
<button onclick="downloadFile()" class="px-3 py-1.5 bg-primary text-white rounded text-sm hover:bg-primary/90 transition-colors">
<i class="fa fa-download mr-1"></i>导出
</button>
<button onclick="deleteDataset()" class="px-3 py-1.5 border border-red-200 text-red-600 rounded text-sm hover:bg-red-50 transition-colors">
<i class="fa fa-trash mr-1"></i>删除
</button>
</div>
</div>
<!-- 状态栏 -->
<div id="fileStatus" class="px-4 py-3 bg-gray-50 border-b border-gray-100 flex items-center gap-6 text-sm text-gray-600 flex-wrap">
<!-- 动态生成 -->
</div>
<!-- 左侧文件列表 + 右侧预览区域 -->
<div class="flex" style="min-height: 500px;">
<!-- 右侧预览区域 -->
<div class="flex-1 flex flex-col">
<div class="p-3 border-b border-gray-100 bg-gray-50/30">
<h3 class="text-sm font-medium text-gray-700">预览内容</h3>
</div>
<div class="flex-1 p-4 overflow-auto">
<pre id="codePreview" class="text-sm text-gray-700 whitespace-pre-wrap font-mono bg-gray-50 rounded p-4" style="min-height: 400px;">加载中...</pre>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
<script>
// 全局返回函数
function goBack() {
window.location.href = 'main.html?page=dataset-manage';
}
// 使用 IIFE 避免全局变量污染
(function() {
// API 基础地址 - 优先使用 main.html 中定义的全局变量
const getApiBase = () => {
const protocol = window.location.protocol;
const hostname = window.location.hostname;
return `${protocol}//${hostname}:7861/api`;
};
const API_BASE = typeof window.API_BASE !== 'undefined' ? window.API_BASE : getApiBase();
// 获取URL参数
const urlParams = new URLSearchParams(window.location.search);
const datasetId = urlParams.get('id');
const fileId = urlParams.get('fileId');
let currentDataset = null;
let selectedFileId = null;
// 返回上一页面
function goBack() {
window.location.href = 'main.html?page=dataset-manage';
}
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
// 绑定导航点击事件
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const page = this.dataset.page;
window.location.href = `main.html?page=${page}`;
});
});
// 设置侧边栏当前页高亮
const currentPage = 'dataset-manage';
document.querySelectorAll('.nav-link').forEach(link => {
if (link.dataset.page === currentPage) {
link.classList.add('bg-[#1890ff]/10', 'text-[#1890ff]');
link.classList.remove('hover:bg-[#001529]/20', 'transition-colors');
}
});
updateSidebarSlider();
// 加载数据集信息
loadDataset();
});
// 更新侧边栏滑块位置
function updateSidebarSlider() {
const slider = document.getElementById('sidebar-slider');
if (!slider) return;
const activeLink = document.querySelector('.nav-link.bg-\\[\\#1890ff\\]\\/10');
if (activeLink) {
const wrapper = activeLink.closest('.nav-item-wrapper');
if (wrapper) {
slider.style.top = wrapper.offsetTop + 'px';
slider.style.height = wrapper.offsetHeight + 'px';
}
}
}
// 加载数据集信息
async function loadDataset() {
if (!datasetId) {
document.getElementById('codePreview').textContent = '缺少数据集ID参数';
return;
}
try {
const response = await fetch(`${API_BASE}/dataset-manage/${datasetId}`);
const result = await response.json();
if (result.code !== 0) {
document.getElementById('codePreview').textContent = '获取数据集信息失败: ' + (result.message || '未知错误');
return;
}
currentDataset = result.data;
updateUI();
} catch (error) {
document.getElementById('codePreview').textContent = '加载失败: ' + error.message;
}
}
// 更新UI
function updateUI() {
const dataset = currentDataset;
// 更新状态栏
const statusTag = dataset.storage_type === 'local'
? '<span class="px-2 py-0.5 rounded text-xs bg-blue-100 text-blue-700">本地存储</span>'
: '<span class="px-2 py-0.5 rounded text-xs bg-gray-100 text-gray-700">云存储</span>';
const count = dataset.count || 0;
const type = dataset.type || 'JSON/JSONL';
const createTime = dataset.create_time
? new Date(dataset.create_time).toLocaleString('zh-CN')
: '-';
document.getElementById('fileStatus').innerHTML = `
<span class="flex items-center">${statusTag}</span>
<span class="flex items-center">数据量:<span class="text-gray-800 ml-1">${count} 条</span></span>
<span class="flex items-center">数据类型:<span class="text-gray-800 ml-1">${type}</span></span>
<span class="flex items-center">创建时间:<span class="text-gray-800 ml-1">${createTime}</span></span>
`;
// 如果有文件ID选择该文件否则选择第一个文件
const targetFileId = fileId || (dataset.files && dataset.files.length > 0 ? dataset.files[0].id : null);
if (targetFileId) {
selectFile(targetFileId);
}
}
// 选择文件
async function selectFile(id) {
selectedFileId = id;
const previewEl = document.getElementById('codePreview');
previewEl.textContent = '加载中...';
try {
const response = await fetch(`${API_BASE}/dataset-manage/preview/${id}`);
const result = await response.json();
if (result.code !== 0) {
previewEl.textContent = '获取文件预览失败: ' + (result.message || '未知错误');
return;
}
const fileData = result.data;
document.getElementById('fileNameDisplay').textContent = fileData.file_name || '';
// 处理内容限制显示前100条记录
let content = fileData.content || '文件内容为空';
if (content && content.trim()) {
try {
// 尝试解析JSONL格式每行一个JSON对象
const lines = content.split('\n').filter(line => line.trim());
if (lines.length > 100) {
// 限制显示前100条
const limitedLines = lines.slice(0, 100);
content = limitedLines.join('\n') + '\n\n... 共 ' + lines.length + ' 条记录,已显示前 100 条 ...';
}
} catch (e) {
// 如果解析失败,直接显示原内容
}
}
previewEl.textContent = content;
} catch (error) {
previewEl.textContent = '加载失败: ' + error.message;
}
}
// 下载文件
function downloadFile() {
if (!selectedFileId) {
alert('请先选择一个文件');
return;
}
const protocol = window.location.protocol;
const hostname = window.location.hostname;
window.open(`${protocol}//${hostname}:7861/api/dataset-manage/download/${datasetId}/${selectedFileId}`, '_blank');
}
// 删除数据集
function deleteDataset() {
if (!datasetId) {
alert('缺少数据集ID');
return;
}
if (!confirm('确定要删除这个数据集吗?此操作不可恢复。')) {
return;
}
fetch(`${API_BASE}/dataset-manage/${datasetId}`, {
method: 'DELETE'
})
.then(response => response.json())
.then(result => {
if (result.code === 0) {
alert('删除成功');
window.location.href = 'main.html?page=dataset-manage';
} else {
alert('删除失败: ' + (result.message || '未知错误'));
}
})
.catch(error => {
alert('删除失败: ' + error.message);
});
}
})();
</script>
</body>
</html>