Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2dd45854c1 | |||
| 72de68e1ff | |||
| 6844d73c9d | |||
| 40b04ae9c1 | |||
| c8d46f6f99 | |||
| f2bb5dd2e8 | |||
| a3448ed5fe | |||
| 1a65d408f7 | |||
| 643a934c83 |
152
app.py
152
app.py
@@ -101,11 +101,87 @@ def save_data(file_path, data):
|
||||
|
||||
# ============ 大模型智能解析 ============
|
||||
|
||||
def parse_with_llm(text, category_type, images=None, category_id=None, subcategory_id=None):
|
||||
def get_parse_prompt_template(category_type, category_id=None, subcategory_id=None):
|
||||
"""
|
||||
获取解析 prompt 模板(供前端显示和编辑)
|
||||
"""
|
||||
# 从类别配置中获取字段定义
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
|
||||
# 确定类别ID
|
||||
if category_id:
|
||||
cat = next((c for c in categories if c['id'] == category_id), None)
|
||||
else:
|
||||
type_to_cat_id = {'model': 'ai-models', 'gpu': 'gpus', 'cpu': 'cpus', 'dynamic': None}
|
||||
cat_id = type_to_cat_id.get(category_type)
|
||||
cat = next((c for c in categories if c['id'] == cat_id), None)
|
||||
|
||||
# 构建字段模板
|
||||
fields = {}
|
||||
|
||||
if cat and 'fields' in cat:
|
||||
for field in cat['fields']:
|
||||
field_desc = field['label']
|
||||
if field.get('input_style') == 'long':
|
||||
field_desc += '(长文本)'
|
||||
else:
|
||||
field_desc += '(文本)'
|
||||
if field.get('description'):
|
||||
field_desc += f" - {field['description']}"
|
||||
fields[field['key']] = field_desc
|
||||
|
||||
if subcategory_id:
|
||||
subcat = next((s for s in cat.get('subcategories', []) if s['id'] == subcategory_id), None)
|
||||
if subcat and 'extra_fields' in subcat:
|
||||
for field in subcat['extra_fields']:
|
||||
field_desc = field['label']
|
||||
if field.get('input_style') == 'long':
|
||||
field_desc += '(长文本)'
|
||||
else:
|
||||
field_desc += '(文本)'
|
||||
if field.get('description'):
|
||||
field_desc += f" - {field['description']}"
|
||||
fields[field['key']] = field_desc
|
||||
else:
|
||||
fields = {
|
||||
'name': '名称',
|
||||
'brand': '品牌',
|
||||
'price': '价格(数字)',
|
||||
'year': '年份(数字)',
|
||||
'specs': '规格参数(JSON对象)',
|
||||
'description': '简介描述',
|
||||
}
|
||||
|
||||
fields_json = json.dumps(fields, ensure_ascii=False, indent=2)
|
||||
|
||||
# 图片解析 prompt
|
||||
image_prompt = """请分析图片中的产品参数信息,提取结构化数据。
|
||||
|
||||
需要提取的字段:
|
||||
""" + fields_json + """
|
||||
|
||||
重要要求:
|
||||
1. 图片中可能包含1个或多个产品,请识别所有产品
|
||||
2. 如果是多张图片,请综合分析所有图片内容
|
||||
3. **提取数据时保留原始单位**:字段标签中如有单位标注(如($)、(GB)、(MHz)等),提取时请带上对应单位,保持数据完整性
|
||||
4. 如果某字段没有提及,返回null
|
||||
5. 返回格式:如果识别到多个产品,返回数组 [对象列表]; 如果只有一个产品,返回单个对象
|
||||
6. 只返回JSON数据,不要其他内容"""
|
||||
|
||||
return {
|
||||
'fields': fields,
|
||||
'fields_json': fields_json,
|
||||
'image_prompt': image_prompt,
|
||||
'category_name': cat.get('name', '') if cat else ''
|
||||
}
|
||||
|
||||
|
||||
def parse_with_llm(text, category_type, images=None, category_id=None, subcategory_id=None, custom_prompt=None):
|
||||
"""
|
||||
使用大模型解析文本/图片,提取结构化数据
|
||||
支持多张图片输入,可能解析出多个产品
|
||||
根据类别配置的参数字段进行解析
|
||||
支持自定义 prompt(优先使用自定义)
|
||||
"""
|
||||
|
||||
# 从类别配置中获取字段定义
|
||||
@@ -127,14 +203,11 @@ def parse_with_llm(text, category_type, images=None, category_id=None, subcatego
|
||||
# 使用类别配置的字段
|
||||
for field in cat['fields']:
|
||||
field_desc = field['label']
|
||||
if field['type'] == 'number':
|
||||
field_desc += '(数字)'
|
||||
elif field['type'] == 'boolean':
|
||||
field_desc += '(true/false)'
|
||||
elif field['type'] == 'date':
|
||||
field_desc += '(日期格式YYYY-MM-DD)'
|
||||
elif field['type'] == 'json':
|
||||
field_desc += '(JSON对象)'
|
||||
# 所有字段都是文本类型
|
||||
if field.get('input_style') == 'long':
|
||||
field_desc += '(长文本)'
|
||||
else:
|
||||
field_desc += '(文本)'
|
||||
if field.get('description'):
|
||||
field_desc += f" - {field['description']}"
|
||||
fields[field['key']] = field_desc
|
||||
@@ -145,12 +218,10 @@ def parse_with_llm(text, category_type, images=None, category_id=None, subcatego
|
||||
if subcat and 'extra_fields' in subcat:
|
||||
for field in subcat['extra_fields']:
|
||||
field_desc = field['label']
|
||||
if field['type'] == 'number':
|
||||
field_desc += '(数字)'
|
||||
elif field['type'] == 'boolean':
|
||||
field_desc += '(true/false)'
|
||||
elif field['type'] == 'date':
|
||||
field_desc += '(日期格式YYYY-MM-DD)'
|
||||
if field.get('input_style') == 'long':
|
||||
field_desc += '(长文本)'
|
||||
else:
|
||||
field_desc += '(文本)'
|
||||
if field.get('description'):
|
||||
field_desc += f" - {field['description']}"
|
||||
fields[field['key']] = field_desc
|
||||
@@ -172,7 +243,11 @@ def parse_with_llm(text, category_type, images=None, category_id=None, subcatego
|
||||
|
||||
# 如果有图片,添加图片内容
|
||||
if images and len(images) > 0:
|
||||
prompt_text = """请分析图片中的产品参数信息,提取结构化数据。
|
||||
# 优先使用自定义 prompt,否则使用默认
|
||||
if custom_prompt and custom_prompt.strip():
|
||||
prompt_text = custom_prompt
|
||||
else:
|
||||
prompt_text = """请分析图片中的产品参数信息,提取结构化数据。
|
||||
|
||||
需要提取的字段:
|
||||
""" + fields_json + """
|
||||
@@ -180,7 +255,7 @@ def parse_with_llm(text, category_type, images=None, category_id=None, subcatego
|
||||
重要要求:
|
||||
1. 图片中可能包含1个或多个产品,请识别所有产品
|
||||
2. 如果是多张图片,请综合分析所有图片内容
|
||||
3. 数字字段只返回数字,不带单位
|
||||
3. **提取数据时保留原始单位**:字段标签中如有单位标注(如($)、(GB)、(MHz)等),提取时请带上对应单位,保持数据完整性
|
||||
4. 如果某字段没有提及,返回null
|
||||
5. 返回格式:如果识别到多个产品,返回数组 [对象列表]; 如果只有一个产品,返回单个对象
|
||||
6. 只返回JSON数据,不要其他内容"""
|
||||
@@ -232,7 +307,7 @@ def parse_with_llm(text, category_type, images=None, category_id=None, subcatego
|
||||
|
||||
要求:
|
||||
1. 根据文本内容智能提取各个字段的值
|
||||
2. 数字字段只返回数字,不带单位
|
||||
2. **提取数据时保留原始单位**:字段标签中如有单位标注(如($)、(GB)、(MHz)等),提取时请带上对应单位,保持数据完整性
|
||||
3. 如果某字段在文本中没有提及,返回null
|
||||
4. 返回JSON格式,不要包含任何其他内容
|
||||
|
||||
@@ -497,6 +572,25 @@ def api_toggle_model_visible(model_id):
|
||||
|
||||
return jsonify({'success': True, 'visible': model['visible']})
|
||||
|
||||
# ============ 获取解析Prompt模板API ============
|
||||
|
||||
@app.route('/api/parse-prompt', methods=['POST'])
|
||||
def api_get_parse_prompt():
|
||||
"""
|
||||
获取智能解析的 prompt 模板(供前端显示和编辑)
|
||||
"""
|
||||
data = request.get_json()
|
||||
category_type = data.get('category_type', 'dynamic')
|
||||
category_id = data.get('category_id')
|
||||
subcategory_id = data.get('subcategory_id')
|
||||
|
||||
template = get_parse_prompt_template(category_type, category_id, subcategory_id)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'template': template
|
||||
})
|
||||
|
||||
# ============ 图片解析API(预览) ============
|
||||
|
||||
@app.route('/api/parse-images', methods=['POST'])
|
||||
@@ -505,12 +599,14 @@ def api_parse_images():
|
||||
解析图片中的产品参数(预览模式,不保存)
|
||||
支持多张图片,可能返回多个产品
|
||||
根据类别配置的参数字段进行解析
|
||||
支持自定义 prompt(可选)
|
||||
"""
|
||||
data = request.get_json()
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
category_type = data.get('category_type', 'dynamic')
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
@@ -522,8 +618,8 @@ def api_parse_images():
|
||||
type_to_cat_id = {'model': 'ai-models', 'gpu': 'gpus', 'cpu': 'cpus', 'dynamic': None}
|
||||
category_id = type_to_cat_id.get(category_type)
|
||||
|
||||
# 调用大模型解析(根据类别字段配置)
|
||||
parsed_list = parse_with_llm(text, category_type, images, category_id=category_id, subcategory_id=subcategory_id)
|
||||
# 调用大模型解析(根据类别字段配置,支持自定义 prompt)
|
||||
parsed_list = parse_with_llm(text, category_type, images, category_id=category_id, subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
@@ -733,12 +829,13 @@ def api_smart_add_model():
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '') # 子类别ID
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
# 大模型解析(根据类别字段配置)
|
||||
parsed_list = parse_with_llm(text, 'model', images, category_id='ai-models', subcategory_id=subcategory_id)
|
||||
# 大模型解析(根据类别字段配置,支持自定义 prompt)
|
||||
parsed_list = parse_with_llm(text, 'model', images, category_id='ai-models', subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
# 处理多个产品
|
||||
results = []
|
||||
@@ -778,11 +875,12 @@ def api_smart_add_gpu():
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
parsed_list = parse_with_llm(text, 'gpu', images, category_id='gpus', subcategory_id=subcategory_id)
|
||||
parsed_list = parse_with_llm(text, 'gpu', images, category_id='gpus', subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
results = []
|
||||
gpus = load_data(GPUS_FILE)
|
||||
@@ -819,11 +917,12 @@ def api_smart_add_cpu():
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
parsed_list = parse_with_llm(text, 'cpu', images, category_id='cpus', subcategory_id=subcategory_id)
|
||||
parsed_list = parse_with_llm(text, 'cpu', images, category_id='cpus', subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
results = []
|
||||
cpus = load_data(CPUS_FILE)
|
||||
@@ -860,12 +959,13 @@ def api_smart_add_item(category_id):
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
# 使用类别配置的字段解析
|
||||
parsed_list = parse_with_llm(text, 'dynamic', images, category_id=category_id, subcategory_id=subcategory_id)
|
||||
# 使用类别配置的字段解析,支持自定义 prompt
|
||||
parsed_list = parse_with_llm(text, 'dynamic', images, category_id=category_id, subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
results = []
|
||||
items_file = DATA_DIR / f'items_{category_id}.json'
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -54,49 +54,30 @@
|
||||
"name": "EOS R7",
|
||||
"brand": "佳能 (Canon)",
|
||||
"year": 2022,
|
||||
"specs": {
|
||||
"品牌": "佳能 (Canon)",
|
||||
"商品编号": "10090975539899",
|
||||
"店铺": "佳能 (Canon) 数码旗舰店",
|
||||
"外接电源": "支持外接电源",
|
||||
"电池类型": "锂离子电池",
|
||||
"接口": "Wi-Fi 蓝牙 HDMI",
|
||||
"高清摄像": "4K超高清视频",
|
||||
"焦点数量": "5915个",
|
||||
"型号": "EOS R7",
|
||||
"有效像素": "3250万",
|
||||
"传感器类型": "CMOS",
|
||||
"上市时间": "2022-06",
|
||||
"取景器类型": "电子取景器",
|
||||
"液晶屏像素": "162万",
|
||||
"液晶屏尺寸": "3.2英寸",
|
||||
"液晶屏类型": "侧翻屏 旋转屏",
|
||||
"最大光圈": "F3.5",
|
||||
"标准ISO感光度": "ISO 100-32000",
|
||||
"连拍速度": "电子最高约30张/秒,机械最高约15张/秒",
|
||||
"存储介质": "SD卡 SDHC卡 SDXC卡",
|
||||
"功能": "Wi-Fi 4K视频 5轴防抖 高速连拍 翻转自拍",
|
||||
"滤镜直径": "55mm",
|
||||
"视频拍摄能力": "4K 60P",
|
||||
"传感器尺寸": "APS-C",
|
||||
"视频采样": "4:2:2",
|
||||
"像素": "3000-4000万",
|
||||
"镜头卡口": "佳能RF卡口",
|
||||
"RAW照片输出": "14bit",
|
||||
"适用对象": "入门级",
|
||||
"类型": "机身"
|
||||
},
|
||||
"specs": "{\n \"RAW照片输出\": \"14bit\",\n \"上市时间\": \"2022-06\",\n \"传感器尺寸\": \"APS-C\",\n \"传感器类型\": \"CMOS\",\n \"像素\": \"3000-4000万\",\n \"功能\": \"Wi-Fi 4K视频 5轴防抖 高速连拍 翻转自拍\",\n \"取景器类型\": \"电子取景器\",\n \"品牌\": \"佳能 (Canon)\",\n \"商品编号\": \"10090975539899\",\n \"型号\": \"EOS R7\",\n \"外接电源\": \"支持外接电源\",\n \"存储介质\": \"SD卡 SDHC卡 SDXC卡\",\n \"店铺\": \"佳能 (Canon) 数码旗舰店\",\n \"接口\": \"Wi-Fi 蓝牙 HDMI\",\n \"最大光圈\": \"F3.5\",\n \"有效像素\": \"3250万\",\n \"标准ISO感光度\": \"ISO 100-32000\",\n \"液晶屏像素\": \"162万\",\n \"液晶屏尺寸\": \"3.2英寸\",\n \"液晶屏类型\": \"侧翻屏 旋转屏\",\n \"滤镜直径\": \"55mm\",\n \"焦点数量\": \"5915个\",\n \"电池类型\": \"锂离子电池\",\n \"类型\": \"机身\",\n \"视频拍摄能力\": \"4K 60P\",\n \"视频采样\": \"4:2:2\",\n \"连拍速度\": \"电子最高约30张/秒,机械最高约15张/秒\",\n \"适用对象\": \"入门级\",\n \"镜头卡口\": \"佳能RF卡口\",\n \"高清摄像\": \"4K超高清视频\"\n}",
|
||||
"description": "入门级机身",
|
||||
"id": "c8c3f124b2ce",
|
||||
"category_id": "71fa2b4d818f",
|
||||
"created_at": "2026-04-28 16:38:03",
|
||||
"visible": true,
|
||||
"raw_text": "",
|
||||
"images": [
|
||||
"/static/uploads/9703a1d16424_1777365365.png"
|
||||
],
|
||||
"publish_date": "2022-01-01",
|
||||
"images": [],
|
||||
"publish_date": "2022-06-01",
|
||||
"views": 0,
|
||||
"is_pinned": false
|
||||
"is_pinned": false,
|
||||
"updated_at": "2026-04-29 00:24:06",
|
||||
"parse_sources": [
|
||||
{
|
||||
"type": "smart_update",
|
||||
"timestamp": "2026-04-28 23:32:40",
|
||||
"images": [
|
||||
"/static/uploads/deca243eff98_1777390343.png"
|
||||
],
|
||||
"text": "",
|
||||
"updated_fields": []
|
||||
}
|
||||
],
|
||||
"subcategory_id": "dslr",
|
||||
"megapixels": "3000"
|
||||
}
|
||||
]
|
||||
3109
logs/app.log
3109
logs/app.log
File diff suppressed because it is too large
Load Diff
BIN
static/uploads/deca243eff98_1777390343.png
Normal file
BIN
static/uploads/deca243eff98_1777390343.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 52 KiB |
@@ -297,7 +297,7 @@
|
||||
<!-- 子类别选择 -->
|
||||
<div class="mb-4" id="smartAddSubcategoryArea">
|
||||
<label class="text-sm text-gray-600 mb-2 block">选择子类别(可选,用于匹配特定参数字段)</label>
|
||||
<select id="smartAddSubcategory" class="w-full px-3 py-2 border rounded-lg">
|
||||
<select id="smartAddSubcategory" class="w-full px-3 py-2 border rounded-lg" onchange="loadSmartAddPrompt()">
|
||||
<option value="">请选择子类别</option>
|
||||
</select>
|
||||
</div>
|
||||
@@ -306,6 +306,35 @@
|
||||
<p class="text-sm text-orange-700"><i class="ri-information-line mr-1"></i>上传<strong>产品参数截图</strong>或<strong>参数文本</strong>,AI将根据类别字段配置自动解析参数。支持多次解析,每次来源都会被记录。</p>
|
||||
</div>
|
||||
|
||||
<!-- Prompt 编辑区域(可折叠) -->
|
||||
<div class="mb-4 border rounded-lg">
|
||||
<div class="flex items-center justify-between p-3 bg-gray-50 cursor-pointer" onclick="togglePromptEditor()">
|
||||
<div class="flex items-center gap-2">
|
||||
<i class="ri-code-line text-gray-500"></i>
|
||||
<span class="text-sm font-medium text-gray-700">查看/编辑解析 Prompt</span>
|
||||
</div>
|
||||
<i class="ri-arrow-down-s-line text-gray-400 transition-transform" id="promptToggleIcon"></i>
|
||||
</div>
|
||||
<div id="promptEditorArea" class="hidden p-3 border-t">
|
||||
<div class="text-xs text-gray-500 mb-2">
|
||||
<i class="ri-information-line mr-1"></i>
|
||||
Prompt 包含字段配置,AI 将根据此指令解析参数。可自定义修改以优化解析效果。
|
||||
</div>
|
||||
<div class="bg-gray-100 rounded p-2 mb-2 text-xs text-gray-600" id="promptFieldsInfo">
|
||||
<!-- 字段信息显示 -->
|
||||
</div>
|
||||
<textarea id="smartAddPrompt" rows="8" class="w-full p-3 border rounded-lg text-sm text-gray-700 font-mono focus:outline-none focus:border-orange-400" placeholder="加载中..."></textarea>
|
||||
<div class="flex gap-2 mt-2">
|
||||
<button onclick="resetSmartAddPrompt()" class="px-3 py-1 bg-gray-100 text-gray-600 rounded text-xs hover:bg-gray-200">
|
||||
<i class="ri-refresh-line mr-1"></i>重置为默认
|
||||
</button>
|
||||
<button onclick="loadSmartAddPrompt()" class="px-3 py-1 bg-blue-100 text-blue-600 rounded text-xs hover:bg-blue-200">
|
||||
<i class="ri-download-line mr-1"></i>刷新字段配置
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-6">
|
||||
<p class="text-sm text-gray-500 mb-3">上传参数截图(规格表、参数页面等),AI将识别并解析参数数据</p>
|
||||
<div class="flex flex-wrap gap-3 mb-3" id="smartImagePreviewArea">
|
||||
@@ -505,9 +534,21 @@
|
||||
container.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
container.innerHTML = cat.subcategories.map(sub => `
|
||||
<button onclick="${filterFunction}('${sub.id}')" class="px-3 py-1 rounded text-sm bg-white border text-gray-600 hover:bg-gray-100">
|
||||
<i class="${sub.icon} mr-1"></i>${sub.name}
|
||||
|
||||
// 获取当前筛选值
|
||||
let currentFilter = '';
|
||||
if (categoryId === 'ai-models') currentFilter = modelSubcategoryFilter;
|
||||
else if (categoryId === 'gpus') currentFilter = gpuSubcategoryFilter;
|
||||
else if (categoryId === 'cpus') currentFilter = cpuSubcategoryFilter;
|
||||
else if (categoryId === dynamicCategoryId) currentFilter = dynamicSubcategoryFilter;
|
||||
|
||||
container.innerHTML = `
|
||||
<button onclick="${filterFunction}('')" class="px-3 py-1 rounded text-sm ${currentFilter === '' ? 'bg-indigo-600 text-white' : 'bg-white border text-gray-600 hover:bg-gray-100'}">
|
||||
<i class="ri-apps-line mr-1"></i>全部
|
||||
</button>
|
||||
` + cat.subcategories.map(sub => `
|
||||
<button onclick="${filterFunction}('${sub.id}')" class="px-3 py-1 rounded text-sm ${currentFilter === sub.id ? 'bg-indigo-600 text-white' : 'bg-white border text-gray-600 hover:bg-gray-100'}">
|
||||
<i class="${sub.icon || 'ri-folder-line'} mr-1"></i>${sub.name}
|
||||
</button>
|
||||
`).join('');
|
||||
}
|
||||
@@ -1510,7 +1551,7 @@
|
||||
<div class="border-t pt-4">
|
||||
<div class="flex justify-between items-center mb-3">
|
||||
<label class="text-sm font-medium text-gray-700"><i class="ri-list-settings-line mr-1"></i>参数字段管理</label>
|
||||
<button onclick="openFieldAddModal('category')" class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg text-sm hover:bg-indigo-700">
|
||||
<button type="button" onclick="openFieldAddModal('category')" class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg text-sm hover:bg-indigo-700">
|
||||
<i class="ri-add-line mr-1"></i>添加字段
|
||||
</button>
|
||||
</div>
|
||||
@@ -1524,7 +1565,7 @@
|
||||
<div class="border-t pt-4">
|
||||
<div class="flex justify-between items-center mb-3">
|
||||
<label class="text-sm font-medium text-gray-700"><i class="ri-folder-line mr-1"></i>子类别管理</label>
|
||||
<button onclick="openSubcategoryAddModal()" class="px-3 py-1.5 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700">
|
||||
<button type="button" onclick="openSubcategoryAddModal()" class="px-3 py-1.5 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700">
|
||||
<i class="ri-add-line mr-1"></i>添加子类别
|
||||
</button>
|
||||
</div>
|
||||
@@ -1566,7 +1607,7 @@
|
||||
<div class="border-t pt-4">
|
||||
<div class="flex justify-between items-center mb-3">
|
||||
<label class="text-sm font-medium text-gray-700"><i class="ri-list-settings-line mr-1"></i>参数字段管理(基础字段,所有子类别共享)</label>
|
||||
<button onclick="openFieldAddModal('category')" class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg text-sm hover:bg-indigo-700">
|
||||
<button type="button" onclick="openFieldAddModal('category')" class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg text-sm hover:bg-indigo-700">
|
||||
<i class="ri-add-line mr-1"></i>添加字段
|
||||
</button>
|
||||
</div>
|
||||
@@ -1580,7 +1621,7 @@
|
||||
<div class="border-t pt-4">
|
||||
<div class="flex justify-between items-center mb-3">
|
||||
<label class="text-sm font-medium text-gray-700"><i class="ri-folder-line mr-1"></i>子类别管理</label>
|
||||
<button onclick="openSubcategoryAddModal()" class="px-3 py-1.5 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700">
|
||||
<button type="button" onclick="openSubcategoryAddModal()" class="px-3 py-1.5 bg-green-600 text-white rounded-lg text-sm hover:bg-green-700">
|
||||
<i class="ri-add-line mr-1"></i>添加子类别
|
||||
</button>
|
||||
</div>
|
||||
@@ -1794,7 +1835,7 @@
|
||||
<div class="border-t pt-4">
|
||||
<div class="flex justify-between items-center mb-3">
|
||||
<label class="text-sm font-medium text-gray-700"><i class="ri-list-settings-line mr-1"></i>额外参数字段(子类别特有,继承父类别字段)</label>
|
||||
<button onclick="openSubcategoryFieldAddModal()" class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg text-sm hover:bg-indigo-700">
|
||||
<button type="button" onclick="openSubcategoryFieldAddModal()" class="px-3 py-1.5 bg-indigo-600 text-white rounded-lg text-sm hover:bg-indigo-700">
|
||||
<i class="ri-add-line mr-1"></i>添加字段
|
||||
</button>
|
||||
</div>
|
||||
@@ -1904,7 +1945,7 @@
|
||||
if (!cat) return '';
|
||||
|
||||
const fields = cat.fields || [];
|
||||
const fixedFields = ['id', 'subcategory_id', 'images', 'visible', 'created_at', 'updated_at', 'raw_text', 'is_pinned', 'views'];
|
||||
const fixedFields = ['id', 'subcategory_id', 'images', 'visible', 'created_at', 'updated_at', 'raw_text', 'is_pinned', 'views', 'parse_sources', 'product_images'];
|
||||
|
||||
// 合并子类别额外字段
|
||||
let allFields = fields;
|
||||
@@ -1922,22 +1963,14 @@
|
||||
const required = field.required ? 'required' : '';
|
||||
const requiredMark = field.required ? '<span class="text-red-500">*</span>' : '';
|
||||
const desc = field.description ? `<p class="text-xs text-gray-400 mt-1">${field.description}</p>` : '';
|
||||
const inputStyle = field.input_style || 'normal';
|
||||
|
||||
let inputHtml = '';
|
||||
if (field.type === 'boolean') {
|
||||
inputHtml = `<select name="${field.key}" class="w-full px-3 py-2 border rounded-lg" ${required}>
|
||||
<option value="true" ${value === true ? 'selected' : ''}>是</option>
|
||||
<option value="false" ${value === false || !value ? 'selected' : ''}>否</option>
|
||||
</select>`;
|
||||
} else if (field.type === 'number') {
|
||||
inputHtml = `<input type="number" name="${field.key}" value="${value}" step="any" class="w-full px-3 py-2 border rounded-lg" ${required}>`;
|
||||
} else if (field.type === 'date') {
|
||||
inputHtml = `<input type="date" name="${field.key}" value="${value}" class="w-full px-3 py-2 border rounded-lg" ${required}>`;
|
||||
} else if (field.type === 'json') {
|
||||
inputHtml = `<textarea name="${field.key}" rows="3" class="w-full px-3 py-2 border rounded-lg font-mono text-sm" ${required}>${typeof value === 'object' ? JSON.stringify(value, null, 2) : value}</textarea>`;
|
||||
} else if (field.type === 'url') {
|
||||
inputHtml = `<input type="url" name="${field.key}" value="${value}" class="w-full px-3 py-2 border rounded-lg" ${required}>`;
|
||||
if (inputStyle === 'long') {
|
||||
// 长文本用 textarea
|
||||
inputHtml = `<textarea name="${field.key}" rows="4" class="w-full px-3 py-2 border rounded-lg" ${required}>${value}</textarea>`;
|
||||
} else {
|
||||
// 普通文本用 input
|
||||
inputHtml = `<input type="text" name="${field.key}" value="${value}" class="w-full px-3 py-2 border rounded-lg" ${required}>`;
|
||||
}
|
||||
|
||||
@@ -2013,6 +2046,7 @@
|
||||
|
||||
let smartAddType = '';
|
||||
let smartAddImages = []; // 智能添加的图片列表
|
||||
let defaultPrompt = ''; // 保存默认 prompt
|
||||
|
||||
function openSmartAddModal(type) {
|
||||
smartAddType = type;
|
||||
@@ -2021,10 +2055,15 @@
|
||||
document.getElementById('smartAddPreview').classList.add('hidden');
|
||||
document.getElementById('smartImagePreviewArea').innerHTML = '';
|
||||
document.getElementById('smartImageCount').textContent = '0';
|
||||
document.getElementById('promptEditorArea').classList.add('hidden');
|
||||
document.getElementById('promptToggleIcon').style.transform = '';
|
||||
|
||||
// 加载子类别选项
|
||||
loadSmartAddSubcategories(type);
|
||||
|
||||
// 加载默认 prompt
|
||||
loadSmartAddPrompt();
|
||||
|
||||
document.getElementById('smartAddModal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
@@ -2052,6 +2091,62 @@
|
||||
}
|
||||
}
|
||||
|
||||
// 加载 prompt 模板
|
||||
async function loadSmartAddPrompt() {
|
||||
const subcategoryId = document.getElementById('smartAddSubcategory').value;
|
||||
let categoryId;
|
||||
|
||||
if (smartAddType === 'model') categoryId = 'ai-models';
|
||||
else if (smartAddType === 'gpu') categoryId = 'gpus';
|
||||
else if (smartAddType === 'cpu') categoryId = 'cpus';
|
||||
else if (smartAddType === 'dynamic') categoryId = dynamicCategoryId;
|
||||
else categoryId = null;
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/parse-prompt', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
category_type: smartAddType,
|
||||
category_id: categoryId,
|
||||
subcategory_id: subcategoryId
|
||||
})
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
if (data.success) {
|
||||
defaultPrompt = data.template.image_prompt;
|
||||
document.getElementById('smartAddPrompt').value = defaultPrompt;
|
||||
|
||||
// 显示字段信息
|
||||
const fieldsInfo = Object.entries(data.template.fields)
|
||||
.map(([k, v]) => `<span class="inline-block bg-gray-200 px-2 py-1 rounded mr-1 mb-1">${k}: ${v}</span>`)
|
||||
.join('');
|
||||
document.getElementById('promptFieldsInfo').innerHTML = `<strong>字段配置:</strong>${fieldsInfo}`;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('加载 prompt 失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 重置为默认 prompt
|
||||
function resetSmartAddPrompt() {
|
||||
document.getElementById('smartAddPrompt').value = defaultPrompt;
|
||||
}
|
||||
|
||||
// 切换 prompt 编辑器显示
|
||||
function togglePromptEditor() {
|
||||
const area = document.getElementById('promptEditorArea');
|
||||
const icon = document.getElementById('promptToggleIcon');
|
||||
if (area.classList.contains('hidden')) {
|
||||
area.classList.remove('hidden');
|
||||
icon.style.transform = 'rotate(180deg)';
|
||||
} else {
|
||||
area.classList.add('hidden');
|
||||
icon.style.transform = '';
|
||||
}
|
||||
}
|
||||
|
||||
function closeSmartAddModal() {
|
||||
document.getElementById('smartAddModal').classList.add('hidden');
|
||||
}
|
||||
@@ -2163,6 +2258,7 @@
|
||||
async function previewSmartParse() {
|
||||
const text = document.getElementById('smartAddText').value.trim();
|
||||
const subcategoryId = document.getElementById('smartAddSubcategory').value;
|
||||
const customPrompt = document.getElementById('smartAddPrompt').value.trim();
|
||||
|
||||
if (!text && smartAddImages.length === 0) {
|
||||
alert('请上传图片或输入文本');
|
||||
@@ -2181,7 +2277,8 @@
|
||||
text: text,
|
||||
images: smartAddImages,
|
||||
category_type: smartAddType,
|
||||
subcategory_id: subcategoryId
|
||||
subcategory_id: subcategoryId,
|
||||
custom_prompt: customPrompt
|
||||
})
|
||||
});
|
||||
|
||||
@@ -2221,6 +2318,7 @@
|
||||
async function smartAddSubmit() {
|
||||
const text = document.getElementById('smartAddText').value.trim();
|
||||
const subcategoryId = document.getElementById('smartAddSubcategory').value;
|
||||
const customPrompt = document.getElementById('smartAddPrompt').value.trim();
|
||||
|
||||
if (!text && smartAddImages.length === 0) {
|
||||
alert('请上传图片或输入文本');
|
||||
@@ -2244,7 +2342,8 @@
|
||||
body: JSON.stringify({
|
||||
text: text,
|
||||
images: smartAddImages,
|
||||
subcategory_id: subcategoryId
|
||||
subcategory_id: subcategoryId,
|
||||
custom_prompt: customPrompt
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
<i class="ri-search-line absolute left-4 top-1/2 -translate-y-1/2 text-gray-400"></i>
|
||||
<input type="text" id="searchInput" placeholder="搜索..."
|
||||
class="w-full pl-12 pr-4 py-2 border border-gray-200 rounded-lg focus:outline-none focus:border-indigo-400"
|
||||
onkeyup="filterItems()">
|
||||
oninput="filterItems()">
|
||||
</div>
|
||||
<select id="sortBy" onchange="loadItems()" class="px-4 py-2 border border-gray-200 rounded-lg focus:outline-none">
|
||||
<option value="default">默认排序(置顶优先)</option>
|
||||
@@ -65,14 +65,32 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 数据列表 -->
|
||||
<div class="bg-white rounded-xl shadow-sm p-6">
|
||||
<div id="itemsList" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
<div class="text-center text-gray-400 py-8">加载中...</div>
|
||||
</div>
|
||||
<!-- 数据表格 -->
|
||||
<div class="bg-white rounded-xl shadow-sm overflow-hidden">
|
||||
<table class="w-full">
|
||||
<thead class="bg-gray-50 border-b" id="tableHead">
|
||||
<!-- 动态生成 -->
|
||||
</thead>
|
||||
<tbody id="itemsTable">
|
||||
<tr><td colspan="10" class="text-center text-gray-400 py-8">加载中...</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- 详情弹窗 -->
|
||||
<div id="detailModal" class="fixed inset-0 bg-black/50 z-50 hidden flex items-center justify-center">
|
||||
<div class="bg-white rounded-xl max-w-2xl w-full mx-4 max-h-[80vh] overflow-auto">
|
||||
<div class="p-6 border-b flex justify-between items-center sticky top-0 bg-white z-10">
|
||||
<h2 class="text-xl font-bold text-gray-800" id="modalTitle">详情</h2>
|
||||
<button onclick="closeModal()" class="text-gray-400 hover:text-gray-600">
|
||||
<i class="ri-close-line text-2xl"></i>
|
||||
</button>
|
||||
</div>
|
||||
<div id="modalContent" class="p-6"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 页脚 -->
|
||||
<footer class="bg-white border-t mt-8 py-6 text-center text-gray-500 text-sm">
|
||||
ParamHub - 参数百科
|
||||
@@ -82,12 +100,23 @@
|
||||
const categoryId = '{{ category.id }}';
|
||||
let allItems = [];
|
||||
let categories = [];
|
||||
let currentCategory = null;
|
||||
let displayFields = []; // 显示的字段列表
|
||||
|
||||
// 加载导航
|
||||
async function loadNav() {
|
||||
const res = await fetch('/api/categories');
|
||||
categories = await res.json();
|
||||
|
||||
// 获取当前类别
|
||||
currentCategory = categories.find(c => c.id === categoryId);
|
||||
if (currentCategory && currentCategory.fields) {
|
||||
// 过滤要显示的字段(排除id、created_at等内部字段)
|
||||
displayFields = currentCategory.fields
|
||||
.filter(f => !['id', 'category_id', 'created_at', 'updated_at', 'visible', 'is_pinned', 'views', 'publish_date', 'subcategory_id', 'parse_sources', 'product_images'].includes(f.key))
|
||||
.slice(0, 6); // 最多显示6个字段
|
||||
}
|
||||
|
||||
// 内置页面映射
|
||||
const builtinPages = [
|
||||
{id: 'home', name: '首页', href: '/'},
|
||||
@@ -129,50 +158,78 @@
|
||||
allItems = await res.json();
|
||||
|
||||
document.getElementById('itemCount').textContent = allItems.length;
|
||||
renderTableHead();
|
||||
renderItems(allItems);
|
||||
}
|
||||
|
||||
// 渲染数据
|
||||
// 渲染表格头部
|
||||
function renderTableHead() {
|
||||
let html = '<tr>';
|
||||
|
||||
// 名称列
|
||||
html += '<th class="px-4 py-3 text-left text-sm font-medium text-gray-600">名称</th>';
|
||||
|
||||
// 动态字段列
|
||||
displayFields.forEach(f => {
|
||||
html += `<th class="px-4 py-3 text-left text-sm font-medium text-gray-600">${f.label}</th>`;
|
||||
});
|
||||
|
||||
// 发布日期列
|
||||
html += '<th class="px-4 py-3 text-left text-sm font-medium text-gray-600">发布日期</th>';
|
||||
|
||||
// 操作列
|
||||
html += '<th class="px-4 py-3 text-center text-sm font-medium text-gray-600">操作</th>';
|
||||
|
||||
html += '</tr>';
|
||||
document.getElementById('tableHead').innerHTML = html;
|
||||
}
|
||||
|
||||
// 渲染数据表格
|
||||
function renderItems(items) {
|
||||
if (items.length === 0) {
|
||||
document.getElementById('itemsList').innerHTML = `
|
||||
<div class="col-span-3 text-center py-12">
|
||||
const colCount = displayFields.length + 3; // 名称 + 字段 + 日期 + 操作
|
||||
document.getElementById('itemsTable').innerHTML = `
|
||||
<tr><td colspan="${colCount}" class="text-center py-12">
|
||||
<i class="ri-inbox-line text-4xl text-gray-300 mb-4 block"></i>
|
||||
<p class="text-gray-400">暂无数据</p>
|
||||
</div>
|
||||
</td></tr>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('itemsList').innerHTML = items.map(item => {
|
||||
const fields = Object.entries(item)
|
||||
.filter(([key, val]) => !['id', 'category_id', 'created_at', 'updated_at', 'visible', 'is_pinned', 'views', 'publish_date'].includes(key) && val)
|
||||
.slice(0, 5)
|
||||
.map(([key, val]) => `<span class="text-gray-500 text-sm">${key}: ${val}</span>`)
|
||||
.join('<br>');
|
||||
|
||||
return `
|
||||
<div class="border border-gray-200 rounded-lg p-4 hover:shadow-md transition group ${item.is_pinned ? 'bg-yellow-50 border-yellow-300' : ''}">
|
||||
<div class="flex items-start justify-between">
|
||||
<div>
|
||||
<h3 class="font-medium text-gray-800 group-hover:text-indigo-600 flex items-center gap-2">
|
||||
${item.is_pinned ? '<i class="ri-pushpin-fill text-yellow-500" title="置顶"></i>' : ''}
|
||||
${item.name || item.title || '未命名'}
|
||||
</h3>
|
||||
<div class="mt-2 space-y-1">
|
||||
${fields}
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-right">
|
||||
<div class="text-xs text-gray-400">
|
||||
${item.publish_date || (item.created_at ? item.created_at.split(' ')[0] : '')}
|
||||
</div>
|
||||
${item.views ? `<div class="text-xs text-gray-400 mt-1"><i class="ri-eye-line"></i> ${item.views}</div>` : ''}
|
||||
</div>
|
||||
</div>
|
||||
const html = items.map(item => {
|
||||
let row = `<tr class="border-b hover:bg-gray-50 transition ${item.is_pinned ? 'bg-yellow-50' : ''}">`;
|
||||
|
||||
// 名称列
|
||||
row += `<td class="px-4 py-3">
|
||||
<div class="font-medium text-gray-800 flex items-center gap-2">
|
||||
${item.is_pinned ? '<i class="ri-pushpin-fill text-yellow-500" title="置顶"></i>' : ''}
|
||||
${item.name || item.title || '未命名'}
|
||||
</div>
|
||||
`;
|
||||
${item.views ? `<div class="text-xs text-gray-400 mt-1"><i class="ri-eye-line mr-1"></i>${item.views}</div>` : ''}
|
||||
</td>`;
|
||||
|
||||
// 动态字段列
|
||||
displayFields.forEach(f => {
|
||||
const value = item[f.key] || '-';
|
||||
row += `<td class="px-4 py-3 text-gray-600 text-sm">${value}</td>`;
|
||||
});
|
||||
|
||||
// 发布日期列
|
||||
row += `<td class="px-4 py-3 text-gray-500 text-sm">${item.publish_date || (item.created_at ? item.created_at.split(' ')[0] : '-')}</td>`;
|
||||
|
||||
// 操作列
|
||||
row += `<td class="px-4 py-3 text-center">
|
||||
<button onclick="showDetail('${item.id}')" class="text-indigo-600 hover:text-indigo-800 text-sm">
|
||||
<i class="ri-eye-line mr-1"></i>详情
|
||||
</button>
|
||||
</td>`;
|
||||
|
||||
row += '</tr>';
|
||||
return row;
|
||||
}).join('');
|
||||
|
||||
document.getElementById('itemsTable').innerHTML = html;
|
||||
}
|
||||
|
||||
// 搜索过滤
|
||||
@@ -191,6 +248,56 @@
|
||||
renderItems(filtered);
|
||||
}
|
||||
|
||||
// 显示详情
|
||||
function showDetail(id) {
|
||||
const item = allItems.find(i => i.id === id);
|
||||
if (!item) return;
|
||||
|
||||
document.getElementById('modalTitle').textContent = item.name || '详情';
|
||||
|
||||
let html = '<div class="space-y-3">';
|
||||
|
||||
// 按字段顺序显示
|
||||
if (currentCategory && currentCategory.fields) {
|
||||
currentCategory.fields.forEach(f => {
|
||||
if (item[f.key]) {
|
||||
const value = item[f.key];
|
||||
html += `
|
||||
<div class="flex justify-between py-2 border-b">
|
||||
<span class="text-gray-500">${f.label}</span>
|
||||
<span class="text-gray-800 ${f.input_style === 'long' ? 'text-right max-w-xs' : ''}">${value}</span>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 添加统计信息
|
||||
if (item.views) {
|
||||
html += `
|
||||
<div class="flex justify-between py-2 border-b">
|
||||
<span class="text-gray-500">热度</span>
|
||||
<span class="text-gray-800"><i class="ri-eye-line mr-1"></i>${item.views}</span>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
html += '</div>';
|
||||
|
||||
document.getElementById('modalContent').innerHTML = html;
|
||||
document.getElementById('detailModal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
// 关闭弹窗
|
||||
function closeModal() {
|
||||
document.getElementById('detailModal').classList.add('hidden');
|
||||
}
|
||||
|
||||
// 点击弹窗外部关闭
|
||||
document.getElementById('detailModal').addEventListener('click', function(e) {
|
||||
if (e.target === this) closeModal();
|
||||
});
|
||||
|
||||
// 初始化
|
||||
loadNav();
|
||||
loadItems();
|
||||
|
||||
@@ -283,7 +283,7 @@
|
||||
</tr>
|
||||
`;
|
||||
|
||||
document.querySelector('#modelsTable thead').innerHTML = headerHtml;
|
||||
document.querySelector('table thead').innerHTML = headerHtml;
|
||||
|
||||
// 动态内容
|
||||
const html = models.map(m => {
|
||||
|
||||
Reference in New Issue
Block a user