first commit
This commit is contained in:
commit
a2fda685ec
2 files changed
+228
No files matched your search
+177
@@ -0,0 +1,177 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>商品信息生成器</title>
|
||||||
|
<style>
|
||||||
|
body { max-width: 1200px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif; }
|
||||||
|
.container { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
|
||||||
|
textarea { width: 100%; height: 300px; margin: 10px 0; }
|
||||||
|
button { padding: 10px 20px; margin: 10px 0; }
|
||||||
|
.error { color: red; }
|
||||||
|
.field-info {
|
||||||
|
background: #f5f5f5;
|
||||||
|
padding: 10px;
|
||||||
|
margin: 5px 0;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>商品信息生成器</h1>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<div>
|
||||||
|
<h2>1. 输入样例JSON创建模板</h2>
|
||||||
|
<textarea id="sampleJson" placeholder="粘贴示例商品JSON..."></textarea>
|
||||||
|
<button onclick="generateTemplate()">生成模板</button>
|
||||||
|
<div id="templateError" class="error"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<h2>2. 生成的模板和Schema</h2>
|
||||||
|
<textarea id="template" readonly></textarea>
|
||||||
|
<h3>Schema定义:</h3>
|
||||||
|
<div id="schemaDisplay"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<h2>3. 输入新商品数据</h2>
|
||||||
|
<textarea id="productData" placeholder="输入要替换的数据,每行一个值..."></textarea>
|
||||||
|
<button onclick="generateProduct()">生成商品信息</button>
|
||||||
|
<div id="productError" class="error"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<h2>4. 生成的商品信息</h2>
|
||||||
|
<textarea id="output" readonly></textarea>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
let currentTemplate = null;
|
||||||
|
let templateFields = [];
|
||||||
|
let schemaInfo = {};
|
||||||
|
|
||||||
|
function generateTemplate() {
|
||||||
|
const sampleJson = document.getElementById('sampleJson').value;
|
||||||
|
const templateError = document.getElementById('templateError');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const sample = JSON.parse(sampleJson);
|
||||||
|
currentTemplate = sample;
|
||||||
|
templateFields = extractFields(sample);
|
||||||
|
generateSchema(sample);
|
||||||
|
|
||||||
|
document.getElementById('template').value = JSON.stringify(sample, null, 2);
|
||||||
|
displaySchema();
|
||||||
|
templateError.textContent = `模板已生成,需要填充的字段:\n${templateFields.join(', ')}`;
|
||||||
|
templateError.style.color = 'green';
|
||||||
|
} catch (e) {
|
||||||
|
templateError.textContent = '无效的JSON格式';
|
||||||
|
templateError.style.color = 'red';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractFields(obj, prefix = '', fields = []) {
|
||||||
|
for (let key in obj) {
|
||||||
|
const value = obj[key];
|
||||||
|
const fullPath = prefix ? `${prefix}.${key}` : key;
|
||||||
|
|
||||||
|
if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
|
||||||
|
fields.push(value.slice(2, -2).trim());
|
||||||
|
} else if (typeof value === 'object' && value !== null) {
|
||||||
|
extractFields(value, fullPath, fields);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateSchema(obj, path = '') {
|
||||||
|
for (let key in obj) {
|
||||||
|
const value = obj[key];
|
||||||
|
const currentPath = path ? `${path}.${key}` : key;
|
||||||
|
|
||||||
|
if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
|
||||||
|
const fieldName = value.slice(2, -2).trim();
|
||||||
|
schemaInfo[fieldName] = {
|
||||||
|
path: currentPath,
|
||||||
|
type: inferType(key),
|
||||||
|
description: `${key}的值`
|
||||||
|
};
|
||||||
|
} else if (typeof value === 'object' && value !== null) {
|
||||||
|
generateSchema(value, currentPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function inferType(key) {
|
||||||
|
const pricePattern = /(price|费用|价格|金额)/i;
|
||||||
|
const numberPattern = /(count|数量|个数|数字|number)/i;
|
||||||
|
const datePattern = /(date|时间|日期)/i;
|
||||||
|
|
||||||
|
if (pricePattern.test(key)) return 'price';
|
||||||
|
if (numberPattern.test(key)) return 'number';
|
||||||
|
if (datePattern.test(key)) return 'date';
|
||||||
|
return 'string';
|
||||||
|
}
|
||||||
|
|
||||||
|
function displaySchema() {
|
||||||
|
const schemaDisplay = document.getElementById('schemaDisplay');
|
||||||
|
schemaDisplay.innerHTML = '';
|
||||||
|
|
||||||
|
for (const [field, info] of Object.entries(schemaInfo)) {
|
||||||
|
const fieldDiv = document.createElement('div');
|
||||||
|
fieldDiv.className = 'field-info';
|
||||||
|
fieldDiv.innerHTML = `
|
||||||
|
<strong>字段名:</strong> ${field}<br>
|
||||||
|
<strong>路径:</strong> ${info.path}<br>
|
||||||
|
<strong>类型:</strong> ${info.type}<br>
|
||||||
|
<strong>描述:</strong> ${info.description}
|
||||||
|
`;
|
||||||
|
schemaDisplay.appendChild(fieldDiv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateProduct() {
|
||||||
|
if (!currentTemplate) {
|
||||||
|
document.getElementById('productError').textContent = '请先生成模板';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const productData = document.getElementById('productData').value;
|
||||||
|
const values = productData.trim().split('\n');
|
||||||
|
|
||||||
|
if (values.length !== templateFields.length) {
|
||||||
|
document.getElementById('productError').textContent =
|
||||||
|
`需要 ${templateFields.length} 个值,但提供了 ${values.length} 个`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fieldValues = {};
|
||||||
|
templateFields.forEach((field, index) => {
|
||||||
|
fieldValues[field] = values[index];
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = JSON.parse(JSON.stringify(currentTemplate));
|
||||||
|
fillTemplate(result, fieldValues);
|
||||||
|
|
||||||
|
document.getElementById('output').value = JSON.stringify(result, null, 2);
|
||||||
|
document.getElementById('productError').textContent = '商品信息已生成';
|
||||||
|
document.getElementById('productError').style.color = 'green';
|
||||||
|
}
|
||||||
|
|
||||||
|
function fillTemplate(obj, values) {
|
||||||
|
for (let key in obj) {
|
||||||
|
const value = obj[key];
|
||||||
|
if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
|
||||||
|
const field = value.slice(2, -2).trim();
|
||||||
|
obj[key] = values[field];
|
||||||
|
} else if (typeof value === 'object' && value !== null) {
|
||||||
|
fillTemplate(value, values);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
# 自动生成商品信息
|
||||||
|
|
||||||
|
## 背景
|
||||||
|
获取一个模板,基于模板创建创建商品信息,替换字段,上架
|
||||||
|
|
||||||
|
|
||||||
|
## 功能
|
||||||
|
|
||||||
|
- 单页面 html 程序
|
||||||
|
- 根据模板生成商品信息
|
||||||
|
|
||||||
|
## 使用说明
|
||||||
|
|
||||||
|
|
||||||
|
1. 打开 index.html 文件
|
||||||
|
2. 在第一个文本框中粘贴示例商品JSON,使用 {{字段名}} 标记需要替换的字段
|
||||||
|
3. 点击"生成模板"按钮生成模板
|
||||||
|
4. 在第三个文本框中输入要替换的值(每行一个值,顺序要与模板中的字段对应)
|
||||||
|
5. 点击"生成商品信息"按钮生成新的商品信息
|
||||||
|
|
||||||
|
示例模板:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"name": "{{商品名}}",
|
||||||
|
"price": "{{价格}}",
|
||||||
|
"description": "{{描述}}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
输入数据:
|
||||||
|
```
|
||||||
|
测试商品
|
||||||
|
99.99
|
||||||
|
这是一个测试商品
|
||||||
|
```
|
||||||
|
|
||||||
|
## Schema生成
|
||||||
|
|
||||||
|
|
||||||
|
系统会自动为模板生成Schema定义,包含以下信息:
|
||||||
|
- 字段名:模板中的变量名
|
||||||
|
- 路径:字段在JSON中的完整路径
|
||||||
|
- 类型:自动推断的数据类型(价格/数字/日期/字符串)
|
||||||
|
- 描述:字段的基本描述
|
||||||
|
|
||||||
|
系统会根据字段名自动推断数据类型:
|
||||||
|
- 包含 price/费用/价格/金额 的字段识别为价格类型
|
||||||
|
- 包含 count/数量/个数/数字/number 的字段识别为数字类型
|
||||||
|
- 包含 date/时间/日期 的字段识别为日期类型
|
||||||
|
- 其他字段默认为字符串类型
|
||||||
|
|
||||||
Reference in new issue
Block a user