From 322ca28cb404821df3285b06d048b8dce32f96e1 Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Tue, 14 Jul 2026 00:37:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AF=8F=E9=A1=B5=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E9=80=89=E6=8B=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 前端搜索时传递limit参数 - 后端搜索API支持limit参数 --- routes/articles.py | 5 +++++ static/js/library.js | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/routes/articles.py b/routes/articles.py index 44467ba..1477042 100644 --- a/routes/articles.py +++ b/routes/articles.py @@ -31,12 +31,17 @@ def search_articles(): """搜索文章""" keyword = request.args.get('q', '') category = request.args.get('category') + limit = request.args.get('limit', type=int) # 支持limit参数 if not keyword: return jsonify({'error': '请提供搜索关键词'}), 400 articles = db.search_articles(keyword, category) + # 如果有limit参数,截取 + if limit: + articles = articles[:limit] + # 解析JSON字段 for article in articles: article['product_names'] = __import__('json').loads(article.get('product_names', '[]')) diff --git a/static/js/library.js b/static/js/library.js index bf9cbe4..252087c 100644 --- a/static/js/library.js +++ b/static/js/library.js @@ -49,7 +49,12 @@ async function loadArticles() { }); if (keyword) { - const response = await fetch(`${API_BASE}/api/articles/search?q=${encodeURIComponent(keyword)}${category ? '&category=' + encodeURIComponent(category) : ''}`); + // 搜索时也传递 limit 参数 + let searchUrl = `${API_BASE}/api/articles/search?q=${encodeURIComponent(keyword)}`; + if (category) searchUrl += `&category=${encodeURIComponent(category)}`; + searchUrl += `&limit=${pageSize}`; + + const response = await fetch(searchUrl); const data = await response.json(); if (data.success) {