修复每页数量选择功能
- 前端搜索时传递limit参数 - 后端搜索API支持limit参数
This commit is contained in:
@@ -31,12 +31,17 @@ def search_articles():
|
|||||||
"""搜索文章"""
|
"""搜索文章"""
|
||||||
keyword = request.args.get('q', '')
|
keyword = request.args.get('q', '')
|
||||||
category = request.args.get('category')
|
category = request.args.get('category')
|
||||||
|
limit = request.args.get('limit', type=int) # 支持limit参数
|
||||||
|
|
||||||
if not keyword:
|
if not keyword:
|
||||||
return jsonify({'error': '请提供搜索关键词'}), 400
|
return jsonify({'error': '请提供搜索关键词'}), 400
|
||||||
|
|
||||||
articles = db.search_articles(keyword, category)
|
articles = db.search_articles(keyword, category)
|
||||||
|
|
||||||
|
# 如果有limit参数,截取
|
||||||
|
if limit:
|
||||||
|
articles = articles[:limit]
|
||||||
|
|
||||||
# 解析JSON字段
|
# 解析JSON字段
|
||||||
for article in articles:
|
for article in articles:
|
||||||
article['product_names'] = __import__('json').loads(article.get('product_names', '[]'))
|
article['product_names'] = __import__('json').loads(article.get('product_names', '[]'))
|
||||||
|
|||||||
@@ -49,7 +49,12 @@ async function loadArticles() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (keyword) {
|
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();
|
const data = await response.json();
|
||||||
|
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
|
|||||||
Reference in New Issue
Block a user