Sfoglia il codice sorgente

feat: 深采管理添加入库按钮,支持选择知识库提交到样本中心

kinglee 6 giorni fa
parent
commit
8d39dc2d82
2 ha cambiato i file con 157 aggiunte e 0 eliminazioni
  1. 61 0
      app/routes/deep_routes.py
  2. 96 0
      app/templates/deep_management.html

+ 61 - 0
app/routes/deep_routes.py

@@ -267,6 +267,67 @@ def deep_collect():
         'status': 'started'
         'status': 'started'
     })
     })
 
 
+@bp.route('/api/knowledge-bases')
+@login_required
+def list_knowledge_bases_proxy():
+    """代理请求到样本中心获取知识库列表。"""
+    from app.sample_center_client import SampleCenterClient
+    from flask import current_app
+
+    client = SampleCenterClient(
+        base_url=current_app.config['SAMPLE_CENTER_BASE_URL'],
+        app_id=current_app.config['SAMPLE_CENTER_APP_ID'],
+        app_secret=current_app.config['SAMPLE_CENTER_APP_SECRET'],
+    )
+
+    result = client.list_knowledge_bases(page=1, page_size=100)
+    return jsonify(result)
+
+
+@bp.route('/api/import', methods=['POST'])
+@login_required
+def import_to_knowledge():
+    """将深采数据提交到样本中心入库。"""
+    import uuid
+    from app.sample_center_client import SampleCenterClient
+    from flask import current_app
+
+    data = request.json
+    deep_id = data.get('deep_id')
+    kb_id = data.get('kb_id')
+    kb_name = data.get('kb_name', '')
+
+    item = DeepCollection.query.get(deep_id)
+    if not item:
+        return jsonify({'error': '深采数据不存在'}), 404
+
+    parents = [{
+        'title': item.title or '',
+        'url': item.url,
+    }]
+    children = [{
+        'title': item.title or '',
+        'content': item.summary or item.content or '',
+    }]
+
+    task_no = f"deep_{uuid.uuid4().hex[:16]}"
+
+    client = SampleCenterClient(
+        base_url=current_app.config['SAMPLE_CENTER_BASE_URL'],
+        app_id=current_app.config['SAMPLE_CENTER_APP_ID'],
+        app_secret=current_app.config['SAMPLE_CENTER_APP_SECRET'],
+    )
+
+    result = client.batch_import(
+        kb_id=kb_id,
+        task_no=task_no,
+        parents=parents,
+        children=children,
+    )
+
+    return jsonify(result)
+
+
 @bp.route('/api/status/<int:id>', methods=['GET'])
 @bp.route('/api/status/<int:id>', methods=['GET'])
 @login_required
 @login_required
 def check_status(id):
 def check_status(id):

+ 96 - 0
app/templates/deep_management.html

@@ -120,6 +120,25 @@
     </div>
     </div>
 </div>
 </div>
 
 
+<!-- Import Modal -->
+<div id="import-modal" class="fixed inset-0 z-50 hidden">
+    <div class="absolute inset-0 bg-black/60 backdrop-blur-sm" onclick="closeImportModal()"></div>
+    <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-[480px] p-6 animate-fade-in-up">
+        <h3 class="text-xl font-bold text-white mb-1">数据入库</h3>
+        <p class="text-sm text-gray-400 mb-4" id="import-item-title"></p>
+        <label class="text-sm text-gray-300 mb-1 block">选择知识库</label>
+        <select id="import-kb-select" class="w-full bg-gray-700 text-white rounded p-2 border border-gray-600 focus:outline-none focus:border-blue-500 mb-4">
+            <option value="">加载中...</option>
+        </select>
+        <div class="flex justify-end gap-3">
+            <button onclick="closeImportModal()" class="px-4 py-2 rounded text-gray-400 hover:text-white hover:bg-gray-700 transition-colors">取消</button>
+            <button id="import-submit-btn" class="px-4 py-2 rounded bg-green-600 text-white hover:bg-green-700 transition-colors shadow-lg">
+                <i class="fas fa-upload"></i> 提交入库
+            </button>
+        </div>
+    </div>
+</div>
+
 <script>
 <script>
     let currentPage = 1;
     let currentPage = 1;
     let currentKeyword = '';
     let currentKeyword = '';
@@ -210,6 +229,9 @@
                             <button onclick="viewDeepItem(${item.id})" class="text-blue-400 hover:text-blue-300 p-1" title="查看详情">
                             <button onclick="viewDeepItem(${item.id})" class="text-blue-400 hover:text-blue-300 p-1" title="查看详情">
                                 <i class="fas fa-eye"></i>
                                 <i class="fas fa-eye"></i>
                             </button>
                             </button>
+                            <button onclick="openImportModal(${item.id})" class="text-green-400 hover:text-green-300 p-1" title="入库">
+                                <i class="fas fa-upload"></i>
+                            </button>
                             <button onclick="deleteItem(${item.id})" class="text-red-400 hover:text-red-300 p-1" title="删除">
                             <button onclick="deleteItem(${item.id})" class="text-red-400 hover:text-red-300 p-1" title="删除">
                                 <i class="fas fa-trash-alt"></i>
                                 <i class="fas fa-trash-alt"></i>
                             </button>
                             </button>
@@ -349,5 +371,79 @@
         if (confirmCallback) confirmCallback();
         if (confirmCallback) confirmCallback();
         closeConfirm();
         closeConfirm();
     });
     });
+
+    // --- Import Modal ---
+    let currentImportId = null;
+
+    function openImportModal(id) {
+        currentImportId = id;
+        // 获取当前行标题
+        const row = $(`.row-checkbox[value="${id}"]`).closest('tr');
+        const title = row.find('a[title]').attr('title') || `ID: ${id}`;
+        $('#import-item-title').text(title);
+
+        // 加载知识库列表
+        const select = $('#import-kb-select');
+        select.html('<option value="">加载中...</option>');
+
+        $.get('/deep/api/knowledge-bases', function(response) {
+            select.empty();
+            const items = response.items || response.data?.items || [];
+            if (items.length === 0) {
+                select.html('<option value="">暂无知识库</option>');
+                return;
+            }
+            items.forEach(kb => {
+                select.append(`<option value="${kb.id}">${kb.name}</option>`);
+            });
+        }).fail(function() {
+            select.html('<option value="">加载失败</option>');
+            showToast('知识库列表加载失败', 'error');
+        });
+
+        $('#import-modal').removeClass('hidden');
+    }
+
+    function closeImportModal() {
+        $('#import-modal').addClass('hidden');
+        currentImportId = null;
+    }
+
+    $('#import-submit-btn').click(function() {
+        const kbId = $('#import-kb-select').val();
+        const kbName = $('#import-kb-select option:selected').text();
+
+        if (!kbId) {
+            showToast('请选择知识库', 'info');
+            return;
+        }
+
+        $(this).prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 提交中...');
+
+        $.ajax({
+            url: '/deep/api/import',
+            type: 'POST',
+            contentType: 'application/json',
+            data: JSON.stringify({
+                deep_id: currentImportId,
+                kb_id: kbId,
+                kb_name: kbName,
+            }),
+            success: function(response) {
+                closeImportModal();
+                if (response.code === '000000') {
+                    showToast('入库任务已提交,请在样本中心查看进度', 'success');
+                } else {
+                    showToast('入库失败: ' + (response.message || '未知错误'), 'error');
+                }
+            },
+            error: function() {
+                showToast('入库请求失败', 'error');
+            },
+            complete: function() {
+                $('#import-submit-btn').prop('disabled', false).html('<i class="fas fa-upload"></i> 提交入库');
+            }
+        });
+    });
 </script>
 </script>
 {% endblock %}
 {% endblock %}