Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
981b3c9c5c | ||
|
|
127654a558 | ||
|
|
322ca28cb4 |
@@ -198,6 +198,14 @@ class Database:
|
||||
cursor.execute('DELETE FROM articles WHERE id = ?', (article_id,))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def get_articles_count(self):
|
||||
"""获取文章总数"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT COUNT(*) as count FROM articles')
|
||||
row = cursor.fetchone()
|
||||
return row['count'] if row else 0
|
||||
|
||||
# ========== 待处理产品操作 ==========
|
||||
def add_pending_product(self, product_name, category=None, subcategory=None, priority=0, source='manual'):
|
||||
|
||||
+13
-3
@@ -14,6 +14,7 @@ def list_articles():
|
||||
offset = request.args.get('offset', 0, type=int)
|
||||
|
||||
articles = db.get_all_articles(limit=limit, offset=offset)
|
||||
total_count = db.get_articles_count() # 获取总数
|
||||
|
||||
# 解析JSON字段
|
||||
for article in articles:
|
||||
@@ -23,7 +24,7 @@ def list_articles():
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'articles': articles,
|
||||
'count': len(articles)
|
||||
'count': total_count # 返回总数
|
||||
})
|
||||
|
||||
@bp.route('/search', methods=['GET'])
|
||||
@@ -31,11 +32,20 @@ def search_articles():
|
||||
"""搜索文章"""
|
||||
keyword = request.args.get('q', '')
|
||||
category = request.args.get('category')
|
||||
limit = request.args.get('limit', type=int)
|
||||
offset = request.args.get('offset', 0, type=int)
|
||||
|
||||
if not keyword:
|
||||
return jsonify({'error': '请提供搜索关键词'}), 400
|
||||
|
||||
articles = db.search_articles(keyword, category)
|
||||
all_articles = db.search_articles(keyword, category)
|
||||
total_count = len(all_articles)
|
||||
|
||||
# 分页截取
|
||||
if limit:
|
||||
articles = all_articles[offset:offset + limit]
|
||||
else:
|
||||
articles = all_articles
|
||||
|
||||
# 解析JSON字段
|
||||
for article in articles:
|
||||
@@ -45,7 +55,7 @@ def search_articles():
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'articles': articles,
|
||||
'count': len(articles)
|
||||
'count': total_count # 返回总数,用于分页
|
||||
})
|
||||
|
||||
@bp.route('/<int:article_id>', methods=['GET'])
|
||||
|
||||
@@ -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 和 offset 参数
|
||||
let searchUrl = `${API_BASE}/api/articles/search?q=${encodeURIComponent(keyword)}`;
|
||||
if (category) searchUrl += `&category=${encodeURIComponent(category)}`;
|
||||
searchUrl += `&limit=${pageSize}&offset=${(currentPage - 1) * pageSize}`;
|
||||
|
||||
const response = await fetch(searchUrl);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
|
||||
Reference in New Issue
Block a user