diff --git a/models/database.py b/models/database.py index 89cb3a0..1c770da 100644 --- a/models/database.py +++ b/models/database.py @@ -32,6 +32,7 @@ class Database: CREATE TABLE IF NOT EXISTS articles ( id INTEGER PRIMARY KEY AUTOINCREMENT, product_names TEXT NOT NULL, + search_title TEXT, category TEXT, keywords TEXT, summary TEXT, @@ -44,6 +45,12 @@ class Database: ) ''') + # 为旧表添加字段(如果不存在) + try: + cursor.execute('ALTER TABLE articles ADD COLUMN search_title TEXT') + except: + pass + # 待处理产品列表 cursor.execute(''' CREATE TABLE IF NOT EXISTS pending_products ( @@ -138,15 +145,15 @@ class Database: conn.commit() # ========== 内容库操作 ========== - def add_article(self, product_names, category, keywords, summary, content, source, url=None): + def add_article(self, product_names, category, keywords, summary, content, source, url=None, search_title=None): """添加文章到内容库""" with self.get_connection() as conn: cursor = conn.cursor() cursor.execute(''' - INSERT INTO articles (product_names, category, keywords, summary, content, source, url) - VALUES (?, ?, ?, ?, ?, ?, ?) - ''', (json.dumps(product_names, ensure_ascii=False), category, - json.dumps(keywords, ensure_ascii=False), summary, content, source, url)) + INSERT INTO articles (product_names, search_title, category, keywords, summary, content, source, url) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) + ''', (json.dumps(product_names, ensure_ascii=False), search_title, + category, json.dumps(keywords, ensure_ascii=False), summary, content, source, url)) conn.commit() return cursor.lastrowid diff --git a/routes/articles.py b/routes/articles.py index a166480..44467ba 100644 --- a/routes/articles.py +++ b/routes/articles.py @@ -108,6 +108,7 @@ def fetch_article(): """从URL抓取文章""" data = request.get_json() url = data.get('url') + search_title = data.get('search_title') # 搜索结果的标题 if not url: return jsonify({'error': '请提供URL'}), 400 @@ -116,6 +117,7 @@ def fetch_article(): if result and result.get('success'): # 自动保存到内容库 + # product_names 使用抓取到的标题,search_title 使用搜索结果的标题 article_id = search_service.save_to_articles( product_names=data.get('product_names', [result['title']]), category=data.get('category'), @@ -123,7 +125,8 @@ def fetch_article(): summary=result.get('description', ''), content=result['content'], source=url, - url=url + url=url, + search_title=search_title ) return jsonify({ diff --git a/services/search_service.py b/services/search_service.py index 63c45eb..c046305 100644 --- a/services/search_service.py +++ b/services/search_service.py @@ -390,9 +390,9 @@ class SearchService: return results - def save_to_articles(self, product_names, category, keywords, summary, content, source, url=None): + def save_to_articles(self, product_names, category, keywords, summary, content, source, url=None, search_title=None): """保存搜索结果到内容库""" - return db.add_article(product_names, category, keywords, summary, content, source, url) + return db.add_article(product_names, category, keywords, summary, content, source, url, search_title) # 全局搜索服务实例 search_service = SearchService() \ No newline at end of file diff --git a/static/css/library.css b/static/css/library.css index aa3cdb1..c7bba5c 100644 --- a/static/css/library.css +++ b/static/css/library.css @@ -228,6 +228,19 @@ font-size: 12px; } +.article-search-title { + font-size: 13px; + color: #666; + margin-bottom: 10px; + padding: 4px 8px; + background: #f0f4ff; + border-radius: 4px; +} + +.article-search-title i { + color: #667eea; +} + .article-summary { font-size: 14px; color: #666; diff --git a/static/css/search.css b/static/css/search.css index 8f7b021..7664b3c 100644 --- a/static/css/search.css +++ b/static/css/search.css @@ -268,6 +268,19 @@ color: #667eea; } +.result-page-title { + font-size: 13px; + color: #666; + margin-bottom: 5px; + padding: 4px 8px; + background: #f0f4ff; + border-radius: 4px; +} + +.result-page-title i { + color: #667eea; +} + .result-title .saved-icon { color: #10b981; } diff --git a/static/js/library.js b/static/js/library.js index 0a42130..f4741da 100644 --- a/static/js/library.js +++ b/static/js/library.js @@ -118,6 +118,11 @@ function displayArticles() { + ${article.search_title && article.search_title !== productNames.join(', ') ? ` +
+ 搜索标题: ${escapeHtml(article.search_title)} +
+ ` : ''}
${article.category ? `${escapeHtml(article.category)}` : ''} ${escapeHtml(article.source || '未知来源')} diff --git a/static/js/search.js b/static/js/search.js index 9d6f5a2..37ef771 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -135,6 +135,11 @@ function displayResults() { ${r.saved ? '' : ''} ${escapeHtml(r.title)}
+ ${r.pageTitle && r.pageTitle !== r.title ? ` +
+ 网页标题: ${escapeHtml(r.pageTitle)} +
+ ` : ''}
@@ -207,7 +212,8 @@ async function fetchResult(index) { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: result.url, - product_names: [result.title] + product_names: [result.title], + search_title: result.title // 搜索结果的标题 }) }); @@ -216,7 +222,7 @@ async function fetchResult(index) { if (data.success) { searchResults[index].fetched = true; searchResults[index].content = data.data.content; - searchResults[index].title = data.data.title || result.title; + searchResults[index].pageTitle = data.data.title || result.title; // 抓取到的标题 btn.innerHTML = ' 已抓取';