From f742a339b8341ec9e8ee9afe538cb3fc0a8dc025 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 5 Nov 2026 11:36:03 +0800 Subject: [PATCH] Update public-api.js via upload script - 2026-11-05 11:35:54 --- public-api.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/public-api.js b/public-api.js index 17da697..7dc17d6 100644 --- a/public-api.js +++ b/public-api.js @@ -383,7 +383,7 @@ async function translateTemplateHtml(ai, products) { // ===== 网站产品介绍:没手动填英文介绍的产品,由 AI 根据参数自动写一段 ===== // 参数变了会重新生成(缓存按参数内容算);手动填了英文介绍就一直用手动的。 -const TYPE_EN = { '迷你UPS': 'Mini DC UPS', '离线式UPS': 'Offline UPS', '离线式锂电UPS': 'Lithium-ion offline UPS', '在线互动式UPS': 'Line-interactive UPS', '机架式UPS': 'Rack-mount online double-conversion UPS', '机架式锂电UPS': 'Lithium-ion rack-mount online double-conversion UPS', '模块化UPS': 'Modular online UPS with hot-swappable power modules', '机架式模块化UPS': 'Rack-mount modular online UPS with hot-swappable power modules', '工频UPS': 'Industrial-frequency (transformer-based) online UPS with output isolation transformer', '在线式高频UPS': 'Online double-conversion high-frequency tower UPS', '太阳能混网逆变器': 'Hybrid solar inverter (solar + battery + grid, with backup output)', '并网/离网逆变器': 'Solar inverter (grid-tied / off-grid)', '机架式磷酸铁锂电池': 'Rack-mount LiFePO4 lithium battery module (19-inch, for solar energy storage and telecom)', '机架式锂电池': 'Rack-mount LiFePO4 lithium battery module (19-inch, for solar energy storage and telecom)', '一体式锂电UPS': 'All-in-one online double-conversion UPS with built-in LiFePO4 lithium battery (choose the number of battery packs for the backup time)', '锂电一体式UPS': 'All-in-one online double-conversion UPS with built-in LiFePO4 lithium battery (choose the number of battery packs for the backup time)', '壁挂式锂电池': 'Wall-mount LiFePO4 home battery (powerwall type, for solar and backup)', '壁挂式磷酸铁锂电池': 'Wall-mount LiFePO4 home battery (powerwall type, for solar and backup)', '落地式锂电池': 'Floor-standing LiFePO4 home battery on wheels (high capacity, for solar and backup)', '锂电替换铅酸电池': 'LiFePO4 drop-in replacement for lead-acid batteries (12.8 V / 25.6 V / 51.2 V, same sizes as VRLA, for UPS, solar, RV and security)', '磷酸铁锂储能柜': 'High-voltage LiFePO4 battery cabinet (19-inch cabinet with master BMS, for online / industrial UPS and high-voltage hybrid inverters, replaces lead-acid battery strings)', '高压锂电池柜': 'High-voltage LiFePO4 battery cabinet (for UPS and high-voltage inverters)', '高压磷酸铁锂电池柜': 'High-voltage LiFePO4 battery cabinet (for UPS and high-voltage inverters)', '落地式磷酸铁锂电池': 'Floor-standing LiFePO4 home battery on wheels (high capacity, for solar and backup)' }; +const TYPE_EN = { '迷你UPS': 'Mini DC UPS', '离线式UPS': 'Offline UPS', '离线式锂电UPS': 'Lithium-ion offline UPS', '在线互动式UPS': 'Line-interactive UPS', '机架式UPS': 'Rack-mount online double-conversion UPS', '机架式锂电UPS': 'Lithium-ion rack-mount online double-conversion UPS', '模块化UPS': 'Modular online UPS with hot-swappable power modules', '机架式模块化UPS': 'Rack-mount modular online UPS with hot-swappable power modules', '工频UPS': 'Industrial-frequency (transformer-based) online UPS with output isolation transformer', '在线式高频UPS': 'Online double-conversion high-frequency tower UPS', '太阳能混网逆变器': 'Hybrid solar inverter (solar + battery + grid, with backup output)', '并网/离网逆变器': 'Solar inverter (grid-tied / off-grid)', '机架式磷酸铁锂电池': 'Rack-mount LiFePO4 lithium battery module (19-inch, for solar energy storage and telecom)', '机架式锂电池': 'Rack-mount LiFePO4 lithium battery module (19-inch, for solar energy storage and telecom)', '一体式锂电UPS': 'All-in-one online double-conversion UPS with built-in LiFePO4 lithium battery (choose the number of battery packs for the backup time)', '锂电一体式UPS': 'All-in-one online double-conversion UPS with built-in LiFePO4 lithium battery (choose the number of battery packs for the backup time)', '壁挂式锂电池': 'Wall-mount LiFePO4 home battery (powerwall type, for solar and backup)', '壁挂式磷酸铁锂电池': 'Wall-mount LiFePO4 home battery (powerwall type, for solar and backup)', '落地式锂电池': 'Floor-standing LiFePO4 home battery on wheels (high capacity, for solar and backup)', '壁挂式储能一体机': 'Wall-mounted all-in-one home energy storage system (LiFePO4 battery and hybrid inverter in one unit, PV and mains input, UPS function)', '落地式储能一体机': 'Floor-standing all-in-one home energy storage system (LiFePO4 battery and hybrid inverter in one unit, PV and mains input, UPS function)', '锂电替换铅酸电池': 'LiFePO4 drop-in replacement for lead-acid batteries (12.8 V / 25.6 V / 51.2 V, same sizes as VRLA, for UPS, solar, RV and security)', '磷酸铁锂储能柜': 'High-voltage LiFePO4 battery cabinet (19-inch cabinet with master BMS, for online / industrial UPS and high-voltage hybrid inverters, replaces lead-acid battery strings)', '高压锂电池柜': 'High-voltage LiFePO4 battery cabinet (for UPS and high-voltage inverters)', '高压磷酸铁锂电池柜': 'High-voltage LiFePO4 battery cabinet (for UPS and high-voltage inverters)', '落地式磷酸铁锂电池': 'Floor-standing LiFePO4 home battery on wheels (high capacity, for solar and backup)' }; const DESC_SKIP = new Set(['model', 'brand', 'category', 'type', 'machineVariant', 'description', 'sale', 'youtube', 'sellingPoints', 'sockets', 'extraSpecs', 'pack']); const SOCKET_EN = { SCHUKO: 'Schuko', NEMA: 'NEMA 5-15', UK: 'BS 1363 (UK)', UNIVERSAL: 'universal', IEC: 'IEC C13', AU: 'Type I (AU/CN)' }; function descFacts(p) { @@ -490,6 +490,26 @@ async function translateProducts(ai, products) { } const slugify =(s) => String(s || '').trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, ''); +// 网址里的产品标识(slug)按型号生成。两个已发布产品型号相同时(比如壁挂式和落地式储能一体机都叫"2.5kWh + 3kW"), +// 后面加上细分品类的英文后缀(-wall / -floor),没有对应后缀的按产品 id 顺序加 -2、-3……,保证每个产品的网址和图片目录不重复。 +const SLUG_TYPE_SUFFIX = { '壁挂式储能一体机': 'wall', '落地式储能一体机': 'floor' }; +function uniqueSlugs(list) { + const groups = new Map(); + for (const p of list) { const b = slugify(p.name); if (!groups.has(b)) groups.set(b, []); groups.get(b).push(p); } + const out = new Map(); + for (const [b, ps] of groups) { + if (ps.length === 1) { out.set(String(ps[0].id), b); continue; } + const used = new Set(); + const sorted = [...ps].sort((x, y) => String(x.id).localeCompare(String(y.id))); + sorted.forEach((p, i) => { + const sx = SLUG_TYPE_SUFFIX[String(p.type || '').trim()]; + let slug = sx && !used.has(`${b}-${sx}`) ? `${b}-${sx}` : i === 0 && !sx ? b : `${b}-${i + 1}`; + while (used.has(slug)) slug += 'x'; + used.add(slug); out.set(String(p.id), slug); + }); + } + return out; +} const parse = (v) => { try { return typeof v === 'string' ? JSON.parse(v) : v; } catch { return v; } }; function readKey(dataFile, key) { @@ -562,8 +582,8 @@ const imagesOf = (p) => { const detailsOf = (p) => (Array.isArray(p[DETAIL_FIELD]) ? p[DETAIL_FIELD] : []).filter(Boolean).slice(0, 20); const PACK_KEY = 'crm:webPackSpecs'; // 箱规:[{ id, name, per, nw, gw, l, w, h }] -function toPublic(p, templates = [], packSpecs = []) { - const slug = slugify(p.name); +function toPublic(p, templates = [], packSpecs = [], slugMap = null) { + const slug = (slugMap && slugMap.get(String(p.id))) || slugify(p.name); const out = { slug }; for (const [pub, src] of Object.entries(FIELDS)) { const v = p[src]; @@ -657,14 +677,15 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut }); const published = () => readProducts(dataFile).filter((p) => p[PUBLISH_FLAG]); - const findBySlug = (slug) => published().find((p) => slugify(p.name) === slug); + const findBySlug = (slug) => { const list = published(), m = uniqueSlugs(list); return list.find((p) => m.get(String(p.id)) === slug); }; // 网站构建时调用:全部已发布产品 app.get('/api/public/products', checkKey, async (req, res) => { try { const templates = readTemplates(dataFile); const packSpecs = readKey(dataFile, PACK_KEY); - res.json(await translateProducts(ai, published().map((p) => toPublic(p, templates, packSpecs)))); + const list = published(), slugMap = uniqueSlugs(list); + res.json(await translateProducts(ai, list.map((p) => toPublic(p, templates, packSpecs, slugMap)))); } catch (e) { res.status(500).json({ error: e.message }); } }); @@ -673,7 +694,7 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut app.get('/api/public/products/:slug', checkKey, (req, res) => { const p = findBySlug(req.params.slug); if (!p) return res.status(404).json({ error: 'not found' }); - translateProducts(ai, [toPublic(p, readTemplates(dataFile), readKey(dataFile, PACK_KEY))]).then(([x]) => res.json(x), (e) => res.status(500).json({ error: e.message })); + translateProducts(ai, [toPublic(p, readTemplates(dataFile), readKey(dataFile, PACK_KEY), uniqueSlugs(published()))]).then(([x]) => res.json(x), (e) => res.status(500).json({ error: e.message })); }); // 产品图片:只允许读取"已发布产品自己的图片",防止借这个接口读到别的文件