data_management.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. {% extends "base.html" %}
  2. {% block content %}
  3. <div class="flex h-screen overflow-hidden" id="data-view">
  4. <!-- Sidebar -->
  5. {% include 'partials/sidebar.html' %}
  6. <!-- Main Content -->
  7. <div class="flex-1 flex flex-col min-w-0 overflow-hidden bg-gray-900/80 relative">
  8. <!-- Top Header -->
  9. <header class="h-16 flex items-center justify-between px-6 z-20 bg-gray-900/80 backdrop-blur-md border-b border-blue-900/30">
  10. <button class="md:hidden text-gray-300 focus:outline-none" id="open-sidebar">
  11. <i class="fas fa-bars text-xl"></i>
  12. </button>
  13. <h1 class="text-lg md:text-2xl font-bold tech-title truncate ml-2">数据管理</h1>
  14. <div class="flex items-center space-x-4">
  15. <div class="flex items-center space-x-2">
  16. <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold border border-cyan-400 shadow-md">A</div>
  17. <span class="hidden md:inline text-sm text-gray-300">{{ current_user.username }}</span>
  18. </div>
  19. </div>
  20. </header>
  21. <!-- Main Scrollable Area -->
  22. <main class="flex-1 overflow-y-auto p-4 md:p-6 scroll-smooth">
  23. <div class="tech-panel p-6 rounded-xl mb-6 min-h-[calc(100vh-140px)] flex flex-col">
  24. <!-- 1. Search Area -->
  25. <div class="flex flex-col md:flex-row gap-4 mb-6">
  26. <div class="flex-1 flex gap-2">
  27. <input type="text" id="search-keyword" class="flex-1 bg-gray-700 text-white rounded p-2 border border-gray-600 focus:outline-none focus:border-blue-500" placeholder="请输入标题、来源或摘要进行模糊查询...">
  28. <button onclick="searchData()" class="bg-blue-600 hover:bg-blue-700 text-white px-6 rounded font-medium transition-colors">
  29. <i class="fas fa-search"></i> 搜索
  30. </button>
  31. </div>
  32. </div>
  33. <!-- 2. Toolbar -->
  34. <div class="flex gap-3 mb-4 p-3 bg-gray-800/50 rounded-lg border border-gray-700">
  35. <button onclick="batchDelete()" class="bg-red-600/80 hover:bg-red-600 text-white px-4 py-2 rounded text-sm transition-colors flex items-center gap-2">
  36. <i class="fas fa-trash-alt"></i> 批量删除
  37. </button>
  38. <button onclick="batchDeepCollect()" class="bg-purple-600/80 hover:bg-purple-600 text-white px-4 py-2 rounded text-sm transition-colors flex items-center gap-2">
  39. <i class="fas fa-robot"></i> 批量AI深度采集
  40. </button>
  41. </div>
  42. <!-- 3. Data Table -->
  43. <div class="flex-1 overflow-x-auto">
  44. <table class="w-full text-left border-collapse">
  45. <thead>
  46. <tr class="text-gray-400 border-b border-gray-700 bg-gray-800/30">
  47. <th class="p-4 w-12 text-center">
  48. <input type="checkbox" id="select-all" onclick="toggleSelectAll()" class="rounded bg-gray-700 border-gray-600 text-blue-500 focus:ring-offset-gray-900">
  49. </th>
  50. <th class="p-4 w-20">ID</th>
  51. <th class="p-4 w-24">封面</th>
  52. <th class="p-4">标题</th>
  53. <th class="p-4 w-40">来源</th>
  54. <th class="p-4 w-48">采集时间</th>
  55. <th class="p-4 w-48 text-center">操作</th>
  56. </tr>
  57. </thead>
  58. <tbody id="data-table-body" class="text-gray-300 divide-y divide-gray-700/50">
  59. <!-- Rows will be injected here -->
  60. </tbody>
  61. </table>
  62. </div>
  63. <!-- 4. Pagination -->
  64. <div class="mt-6 flex justify-between items-center border-t border-gray-700 pt-4">
  65. <div class="text-sm text-gray-500">
  66. 共 <span id="total-count" class="text-white font-bold">0</span> 条数据
  67. </div>
  68. <div class="flex gap-2" id="pagination-controls">
  69. <!-- Pagination buttons -->
  70. </div>
  71. </div>
  72. </div>
  73. </main>
  74. </div>
  75. </div>
  76. <!-- Toast Notification -->
  77. <div id="toast" class="fixed top-5 left-1/2 transform -translate-x-1/2 z-50 hidden transition-all duration-300">
  78. <div class="bg-gray-800 border-l-4 border-blue-500 text-white px-6 py-3 rounded shadow-lg flex items-center gap-3">
  79. <i class="fas fa-info-circle text-blue-400" id="toast-icon"></i>
  80. <span id="toast-message">Notification</span>
  81. </div>
  82. </div>
  83. <!-- Confirm Modal -->
  84. <div id="confirm-modal" class="fixed inset-0 z-50 hidden">
  85. <div class="absolute inset-0 bg-black/60 backdrop-blur-sm" onclick="closeConfirm()"></div>
  86. <div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 bg-gray-800 rounded-xl shadow-2xl border border-gray-700 w-96 p-6 animate-fade-in-up">
  87. <h3 class="text-xl font-bold text-white mb-2">确认操作</h3>
  88. <p class="text-gray-400 mb-6" id="confirm-message">确定要执行此操作吗?</p>
  89. <div class="flex justify-end gap-3">
  90. <button onclick="closeConfirm()" class="px-4 py-2 rounded text-gray-400 hover:text-white hover:bg-gray-700 transition-colors">取消</button>
  91. <button id="confirm-btn" class="px-4 py-2 rounded bg-red-600 text-white hover:bg-red-700 transition-colors shadow-lg">确定</button>
  92. </div>
  93. </div>
  94. </div>
  95. <!-- Deep Collect Report Modal -->
  96. <div id="deep-collect-modal" class="fixed inset-0 z-50 hidden">
  97. <div class="absolute inset-0 bg-black/60 backdrop-blur-sm"></div>
  98. <div class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 bg-gray-800 rounded-xl shadow-2xl border border-gray-700 w-[500px] p-6 animate-fade-in-up">
  99. <h3 class="text-xl font-bold text-white mb-4"><i class="fas fa-robot text-purple-400 mr-2"></i>深度采集执行报告</h3>
  100. <div id="deep-progress-area" class="mb-6">
  101. <div class="flex justify-between text-sm text-gray-400 mb-2">
  102. <span id="deep-status-text">正在初始化...</span>
  103. <span id="deep-percent">0%</span>
  104. </div>
  105. <div class="w-full bg-gray-700 rounded-full h-2.5 overflow-hidden">
  106. <div id="deep-progress-bar" class="bg-purple-500 h-2.5 rounded-full transition-all duration-300" style="width: 0%"></div>
  107. </div>
  108. </div>
  109. <div id="deep-result-area" class="hidden mb-6 bg-gray-900/50 rounded p-4 border border-gray-700">
  110. <div class="flex items-center gap-2 mb-2">
  111. <i id="deep-result-icon" class="fas fa-check-circle text-green-500 text-lg"></i>
  112. <span id="deep-result-title" class="text-white font-bold">采集成功</span>
  113. </div>
  114. <p id="deep-result-msg" class="text-gray-400 text-sm mb-2">已成功采集并保存深度数据。</p>
  115. <div class="text-xs text-gray-500 bg-gray-800 p-2 rounded max-h-32 overflow-y-auto font-mono" id="deep-summary-preview">
  116. <!-- Summary -->
  117. </div>
  118. </div>
  119. <div class="flex justify-end gap-3">
  120. <button onclick="closeDeepModal()" class="px-4 py-2 rounded text-gray-400 hover:text-white hover:bg-gray-700 transition-colors" id="deep-close-btn">关闭</button>
  121. <a href="#" id="deep-view-btn" class="hidden px-4 py-2 rounded bg-blue-600 text-white hover:bg-blue-700 transition-colors shadow-lg">查看详情</a>
  122. </div>
  123. </div>
  124. </div>
  125. <script>
  126. let currentPage = 1;
  127. let currentKeyword = '';
  128. const pageSize = 10;
  129. $(document).ready(function() {
  130. loadData();
  131. // Bind Enter key for search
  132. $('#search-keyword').keypress(function(e) {
  133. if(e.which == 13) {
  134. searchData();
  135. }
  136. });
  137. // Sidebar Toggle
  138. $('#open-sidebar').click(function() {
  139. $('#sidebar').toggleClass('-translate-x-full');
  140. });
  141. });
  142. function searchData() {
  143. currentKeyword = $('#search-keyword').val().trim();
  144. currentPage = 1;
  145. loadData();
  146. }
  147. function loadData() {
  148. const tbody = $('#data-table-body');
  149. tbody.html('<tr><td colspan="7" class="text-center py-10 text-gray-500"><i class="fas fa-spinner fa-spin mr-2"></i>加载中...</td></tr>');
  150. $.get('/data/api/list', {
  151. page: currentPage,
  152. per_page: pageSize,
  153. keyword: currentKeyword
  154. }, function(response) {
  155. renderTable(response.items);
  156. renderPagination(response);
  157. $('#total-count').text(response.total);
  158. // Reset Select All
  159. $('#select-all').prop('checked', false);
  160. }).fail(function() {
  161. tbody.html('<tr><td colspan="7" class="text-center py-10 text-red-500">加载失败,请重试</td></tr>');
  162. });
  163. }
  164. function renderTable(items) {
  165. const tbody = $('#data-table-body');
  166. tbody.empty();
  167. if (items.length === 0) {
  168. tbody.html('<tr><td colspan="7" class="text-center py-10 text-gray-500">暂无数据</td></tr>');
  169. return;
  170. }
  171. items.forEach(item => {
  172. const html = `
  173. <tr class="hover:bg-gray-800/50 transition-colors group">
  174. <td class="p-4 text-center">
  175. <input type="checkbox" class="row-checkbox rounded bg-gray-700 border-gray-600 text-blue-500 focus:ring-offset-gray-900" value="${item.id}">
  176. </td>
  177. <td class="p-4 text-gray-500 text-sm">#${item.id}</td>
  178. <td class="p-4">
  179. <div class="w-12 h-8 bg-gray-700 rounded overflow-hidden flex items-center justify-center">
  180. ${item.cover ? `<img src="${item.cover}" class="w-full h-full object-cover" onerror="this.style.display='none';this.nextElementSibling.style.display='inline'">` : ''}
  181. <i class="fas fa-image text-gray-500 text-xs ${item.cover ? 'hidden' : ''}"></i>
  182. </div>
  183. </td>
  184. <td class="p-4">
  185. <div class="flex items-center">
  186. <a href="${item.link}" target="_blank" class="text-white font-medium hover:text-blue-400 line-clamp-1 mr-2" title="${item.title}">
  187. ${item.title || '无标题'}
  188. </a>
  189. ${item.has_deep_collection ?
  190. `<a href="/deep/dashboard?query=${encodeURIComponent(item.link)}" class="inline-flex items-center gap-1 text-[10px] bg-purple-900/50 text-purple-300 px-1.5 py-0.5 rounded border border-purple-700 hover:bg-purple-800 transition-colors whitespace-nowrap" title="已深度采集">
  191. <i class="fas fa-check"></i> Deep
  192. </a>`
  193. : ''}
  194. </div>
  195. </td>
  196. <td class="p-4 text-sm text-gray-400">${item.source || '-'}</td>
  197. <td class="p-4 text-sm text-gray-400 font-mono">${item.published_at || '-'}</td>
  198. <td class="p-4 text-sm text-gray-400 font-mono">${item.created_at}</td>
  199. <td class="p-4">
  200. <div class="flex justify-center gap-2">
  201. <button onclick="deepCollectItem(${item.id})" class="text-purple-400 hover:text-purple-300 p-1" title="AI深度采集">
  202. <i class="fas fa-robot"></i>
  203. </button>
  204. <button onclick="deleteItem(${item.id})" class="text-red-400 hover:text-red-300 p-1" title="删除">
  205. <i class="fas fa-trash-alt"></i>
  206. </button>
  207. </div>
  208. </td>
  209. </tr>
  210. `;
  211. tbody.append(html);
  212. });
  213. }
  214. let deepPollInterval = null;
  215. function deepCollectItem(id) {
  216. $('#deep-collect-modal').removeClass('hidden');
  217. $('#deep-progress-area').show();
  218. $('#deep-result-area').addClass('hidden');
  219. $('#deep-view-btn').addClass('hidden');
  220. $('#deep-close-btn').prop('disabled', true).text('执行中...');
  221. updateProgress(0, '正在初始化任务...');
  222. $.ajax({
  223. url: '/deep/api/collect',
  224. type: 'POST',
  225. contentType: 'application/json',
  226. data: JSON.stringify({ id: id }),
  227. success: function(res) {
  228. const taskId = res.task_id;
  229. startDeepPolling(taskId);
  230. },
  231. error: function(xhr) {
  232. updateProgress(100, '启动失败');
  233. showDeepResult(false, xhr.responseJSON);
  234. }
  235. });
  236. }
  237. function startDeepPolling(taskId) {
  238. if (deepPollInterval) clearInterval(deepPollInterval);
  239. deepPollInterval = setInterval(() => {
  240. $.get(`/deep/api/status/${taskId}`, function(res) {
  241. updateProgress(res.progress, res.progress_msg);
  242. if (res.status === 'completed') {
  243. clearInterval(deepPollInterval);
  244. setTimeout(() => {
  245. showDeepResult(true, res);
  246. }, 500);
  247. } else if (res.status === 'failed') {
  248. clearInterval(deepPollInterval);
  249. showDeepResult(false, { error: res.error });
  250. }
  251. }).fail(function() {
  252. clearInterval(deepPollInterval);
  253. showDeepResult(false, { error: '获取进度失败' });
  254. });
  255. }, 1000);
  256. }
  257. function updateProgress(percent, text) {
  258. $('#deep-progress-bar').css('width', percent + '%');
  259. $('#deep-percent').text(percent + '%');
  260. $('#deep-status-text').text(text);
  261. }
  262. function showDeepResult(success, data) {
  263. $('#deep-progress-area').hide();
  264. $('#deep-result-area').removeClass('hidden');
  265. $('#deep-close-btn').prop('disabled', false).text('关闭');
  266. if (success) {
  267. $('#deep-result-icon').attr('class', 'fas fa-check-circle text-green-500 text-lg');
  268. $('#deep-result-title').text('采集成功');
  269. $('#deep-result-msg').text('深度采集数据已成功保存。');
  270. $('#deep-summary-preview').text(data.summary || '无摘要生成');
  271. $('#deep-view-btn').removeClass('hidden').attr('href', '/deep/dashboard?query=' + encodeURIComponent(data.url));
  272. loadData(); // Refresh list to show flag
  273. } else {
  274. $('#deep-result-icon').attr('class', 'fas fa-times-circle text-red-500 text-lg');
  275. $('#deep-result-title').text('采集失败');
  276. $('#deep-result-msg').text(data?.error || '未知错误');
  277. $('#deep-summary-preview').text('');
  278. }
  279. }
  280. function closeDeepModal() {
  281. if (deepPollInterval) clearInterval(deepPollInterval);
  282. $('#deep-collect-modal').addClass('hidden');
  283. }
  284. function renderPagination(data) {
  285. const container = $('#pagination-controls');
  286. container.empty();
  287. if (data.pages <= 1) return;
  288. // Previous
  289. container.append(`
  290. <button onclick="changePage(${data.current_page - 1})"
  291. class="px-3 py-1 rounded bg-gray-700 text-gray-300 hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed"
  292. ${data.current_page === 1 ? 'disabled' : ''}>
  293. <i class="fas fa-chevron-left"></i>
  294. </button>
  295. `);
  296. // Page Numbers (Simple logic for now: show all or simplified range)
  297. // Let's show current - 2 to current + 2
  298. let start = Math.max(1, data.current_page - 2);
  299. let end = Math.min(data.pages, data.current_page + 2);
  300. if (start > 1) {
  301. container.append(`<span class="px-2 text-gray-500">...</span>`);
  302. }
  303. for (let i = start; i <= end; i++) {
  304. container.append(`
  305. <button onclick="changePage(${i})"
  306. class="px-3 py-1 rounded ${i === data.current_page ? 'bg-blue-600 text-white' : 'bg-gray-700 text-gray-300 hover:bg-gray-600'}">
  307. ${i}
  308. </button>
  309. `);
  310. }
  311. if (end < data.pages) {
  312. container.append(`<span class="px-2 text-gray-500">...</span>`);
  313. }
  314. // Next
  315. container.append(`
  316. <button onclick="changePage(${data.current_page + 1})"
  317. class="px-3 py-1 rounded bg-gray-700 text-gray-300 hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed"
  318. ${data.current_page === data.pages ? 'disabled' : ''}>
  319. <i class="fas fa-chevron-right"></i>
  320. </button>
  321. `);
  322. }
  323. function changePage(page) {
  324. if (page < 1) return;
  325. currentPage = page;
  326. loadData();
  327. }
  328. function toggleSelectAll() {
  329. const checked = $('#select-all').prop('checked');
  330. $('.row-checkbox').prop('checked', checked);
  331. }
  332. function getSelectedIds() {
  333. const ids = [];
  334. $('.row-checkbox:checked').each(function() {
  335. ids.push($(this).val());
  336. });
  337. return ids;
  338. }
  339. // --- UI Helpers ---
  340. function showToast(msg, type = 'info') {
  341. const toast = $('#toast');
  342. const icon = $('#toast-icon');
  343. const text = $('#toast-message');
  344. const container = toast.find('div');
  345. text.text(msg);
  346. toast.removeClass('hidden').addClass('flex');
  347. // Reset classes
  348. container.removeClass('border-blue-500 border-red-500 border-green-500');
  349. icon.removeClass('text-blue-400 text-red-400 text-green-400 fa-info-circle fa-check-circle fa-exclamation-circle');
  350. if (type === 'success') {
  351. container.addClass('border-green-500');
  352. icon.addClass('text-green-400 fa-check-circle');
  353. } else if (type === 'error') {
  354. container.addClass('border-red-500');
  355. icon.addClass('text-red-400 fa-exclamation-circle');
  356. } else {
  357. container.addClass('border-blue-500');
  358. icon.addClass('text-blue-400 fa-info-circle');
  359. }
  360. setTimeout(() => {
  361. toast.addClass('hidden').removeClass('flex');
  362. }, 3000);
  363. }
  364. let onConfirmAction = null;
  365. function showConfirm(msg, action) {
  366. $('#confirm-message').text(msg);
  367. $('#confirm-modal').removeClass('hidden');
  368. onConfirmAction = action;
  369. }
  370. function closeConfirm() {
  371. $('#confirm-modal').addClass('hidden');
  372. onConfirmAction = null;
  373. }
  374. // Initialize listeners
  375. $(document).ready(function() {
  376. $('#confirm-btn').click(function() {
  377. if (onConfirmAction) onConfirmAction();
  378. closeConfirm();
  379. });
  380. });
  381. // --- Actions ---
  382. function deleteItem(id) {
  383. showConfirm('确定要删除这条数据吗?', () => performDelete([id]));
  384. }
  385. function batchDelete() {
  386. const ids = getSelectedIds();
  387. if (ids.length === 0) {
  388. showToast('请先选择要删除的数据', 'info');
  389. return;
  390. }
  391. showConfirm(`确定要删除选中的 ${ids.length} 条数据吗?`, () => performDelete(ids));
  392. }
  393. function performDelete(ids) {
  394. $.ajax({
  395. url: '/data/api/delete',
  396. method: 'POST',
  397. contentType: 'application/json',
  398. data: JSON.stringify({ ids: ids }),
  399. success: function(response) {
  400. showToast('删除成功', 'success');
  401. loadData(); // Reload current page
  402. },
  403. error: function(xhr) {
  404. showToast('删除失败: ' + (xhr.responseJSON?.error || '未知错误'), 'error');
  405. }
  406. });
  407. }
  408. async function batchDeepCollect() {
  409. const ids = getSelectedIds();
  410. if (ids.length === 0) {
  411. showToast('请先选择要进行AI深度采集的数据', 'info');
  412. return;
  413. }
  414. $('#deep-collect-modal').removeClass('hidden');
  415. $('#deep-progress-area').show();
  416. $('#deep-result-area').addClass('hidden');
  417. $('#deep-view-btn').addClass('hidden');
  418. $('#deep-close-btn').prop('disabled', true).text('批量执行中...');
  419. let successCount = 0;
  420. let failCount = 0;
  421. for (let i = 0; i < ids.length; i++) {
  422. const id = ids[i];
  423. const percent = Math.round(((i) / ids.length) * 100);
  424. updateProgress(percent, `正在处理第 ${i+1}/${ids.length} 条数据...`);
  425. try {
  426. // 1. Start Task
  427. const res = await new Promise((resolve, reject) => {
  428. $.ajax({
  429. url: '/deep/api/collect',
  430. type: 'POST',
  431. contentType: 'application/json',
  432. data: JSON.stringify({ id: id }),
  433. success: resolve,
  434. error: reject
  435. });
  436. });
  437. const taskId = res.task_id;
  438. // 2. Poll until complete
  439. await new Promise((resolve) => {
  440. const poll = setInterval(() => {
  441. $.get(`/deep/api/status/${taskId}`, function(statusRes) {
  442. if (statusRes.status === 'completed') {
  443. clearInterval(poll);
  444. successCount++;
  445. resolve();
  446. } else if (statusRes.status === 'failed') {
  447. clearInterval(poll);
  448. failCount++;
  449. resolve();
  450. }
  451. }).fail(() => {
  452. clearInterval(poll);
  453. failCount++;
  454. resolve();
  455. });
  456. }, 1000);
  457. });
  458. } catch (e) {
  459. console.error(e);
  460. failCount++;
  461. }
  462. }
  463. updateProgress(100, '批量采集完成');
  464. $('#deep-progress-area').hide();
  465. $('#deep-result-area').removeClass('hidden');
  466. $('#deep-close-btn').prop('disabled', false).text('关闭');
  467. $('#deep-result-icon').attr('class', 'fas fa-info-circle text-blue-500 text-lg');
  468. $('#deep-result-title').text('批量任务结束');
  469. $('#deep-result-msg').text(`成功: ${successCount}, 失败: ${failCount}`);
  470. $('#deep-summary-preview').text('批量任务不提供摘要预览,请到深采管理查看详情。');
  471. loadData();
  472. }
  473. </script>
  474. {% endblock %}