Update public-api.js via upload script - 2026-10-20 09:37:55

This commit is contained in:
mike 2026-10-20 09:37:55 +08:00
parent 3d1a4db16a
commit 33be1dc44a
1 changed files with 54 additions and 2 deletions

View File

@ -68,7 +68,7 @@ const FIELDS = {
const PUBLISH_FLAG = 'publishToWeb'; // 新增:"发布到网站"开关 const PUBLISH_FLAG = 'publishToWeb'; // 新增:"发布到网站"开关
const IMAGES_FIELD = 'webImages'; // 新增:主图文件名数组,最多 6 张;为空时用现有的 productImageFileName const IMAGES_FIELD = 'webImages'; // 新增:主图文件名数组,最多 6 张;为空时用现有的 productImageFileName
const DETAIL_FIELD = 'webDetailImages'; // 新增:详情图文件名数组,最多 20 张,显示在规格表下方 const DETAIL_FIELD = 'webDetailImages'; // 新增:详情图文件名数组,最多 20 张,显示在规格表下方
const TEMPLATES_KEY = 'crm:webDetailTemplates'; // 详情模板:[{ id, name, images: [文件名] }],图片在 uploads/<模板id>/ const TEMPLATES_KEY = 'crm:webDetailTemplates'; // 详情模板:[{ id, name, kind: 'images'|'html', images: [文件名], html }],图片在 uploads/<模板id>/
const TEMPLATE_IDS_FIELD = 'webDetailTemplateIds'; // 产品引用的模板id,按顺序显示在详情图下方 const TEMPLATE_IDS_FIELD = 'webDetailTemplateIds'; // 产品引用的模板id,按顺序显示在详情图下方
// —— 网站管理(非产品内容)—— // —— 网站管理(非产品内容)——
const SITE_ASSETS_KEY = 'crm:webAssets'; // { 图片位id: [文件名 或 { file, caption:{en,es?,ru?} }] },文件在 uploads/site_assets/ const SITE_ASSETS_KEY = 'crm:webAssets'; // { 图片位id: [文件名 或 { file, caption:{en,es?,ru?} }] },文件在 uploads/site_assets/
@ -177,9 +177,58 @@ async function translateSpecs(ai, products) {
} }
} }
// ===== HTML 详情模板 =====
// 模板 kind = 'html' 时,html 字段是一段 HTML(通常是系列参数表)。这里只做基础清理 + 翻译文字;
// 网站构建时会重新解析成表格 / 标题 / 列表,丢掉原文件的所有样式和不安全标签,统一用网站样式显示。
const HTML_ENTITIES = { nbsp: ' ', amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", plusmn: '±', deg: '°', times: '×', middot: '·', ndash: '–', mdash: '—', le: '≤', ge: '≥', micro: 'µ', ohm: 'Ω', hellip: '…', bull: '•', reg: '®', copy: '©', trade: '™' };
function decodeEntities(t) {
return String(t).replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (m, e) => {
if (e[0] === '#') { const n = e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10); return Number.isFinite(n) && n > 0 && n < 0x110000 ? String.fromCodePoint(n) : m; }
const k = e.toLowerCase();
return k in HTML_ENTITIES && k !== 'amp' && k !== 'lt' && k !== 'gt' ? HTML_ENTITIES[k] : m;
});
}
function cleanTplHtml(html) {
let h = String(html || '');
const body = h.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
if (body) h = body[1];
h = h.replace(/<!--[\s\S]*?-->/g, '')
.replace(/<(script|style|head|title|iframe|object|embed|noscript|template)[\s\S]*?<\/\1>/gi, '')
.replace(/<(script|style|link|meta|iframe|object|embed|base)[^>]*>/gi, '')
.replace(/\s*\[cite:[^\]]*\]/gi, ''); // AI 生成文件里常见的引用标记
return decodeEntities(h).trim();
}
// 标签之间的文字片段;纯型号 / 数值(有数字、没有小写字母,如 GS400BI MAX、12V7AH*2)不翻译
const shouldTranslateSeg = (t) => /[A-Za-z]{2,}/.test(t) && !(/\d/.test(t) && !/[a-z]/.test(t));
function htmlSegments(h) {
const out = [];
String(h).replace(/>([^<]+)</g, (m, t) => { const x = t.trim(); if (x && shouldTranslateSeg(x)) out.push(x); return m; });
return out;
}
const tplHtmlOf = (t) => (t && t.kind === 'html' && String(t.html || '').trim() ? cleanTplHtml(t.html) : '');
async function translateTemplateHtml(ai, products) {
const segs = [];
for (const p of products) for (const t of p.templateHtml || []) segs.push(...htmlSegments(t.html.en));
if (!segs.length) return;
const tr = ai ? await translateStrings(ai, segs) : {};
for (const p of products) {
for (const t of p.templateHtml || []) {
for (const lang of Object.keys(WEB_TRANSLATE_LANGS)) {
const dict = tr[lang] || {};
t.html[lang] = t.html.en.replace(/>([^<]+)</g, (m, raw) => {
const x = raw.trim();
const v = x && dict[x];
return v ? '>' + raw.replace(x, v.replace(/</g, '‹').replace(/>/g, '›')) + '<' : m;
});
}
}
}
}
// 给一批产品补齐各语言的介绍和卖点。翻译失败不影响构建:那一项网站上显示英文。 // 给一批产品补齐各语言的介绍和卖点。翻译失败不影响构建:那一项网站上显示英文。
async function translateProducts(ai, products) { async function translateProducts(ai, products) {
try { await translateSpecs(ai, products); } catch (e) { console.warn('[public-api] 参数翻译失败:', e.message); } try { await translateSpecs(ai, products); } catch (e) { console.warn('[public-api] 参数翻译失败:', e.message); }
try { await translateTemplateHtml(ai, products); } catch (e) { console.warn('[public-api] HTML 模板翻译失败:', e.message); }
if (!ai) { if (!ai) {
for (const p of products) { if (p._points.length) p.sellingPointsI18n = { en: p._points }; delete p._points; } for (const p of products) { if (p._points.length) p.sellingPointsI18n = { en: p._points }; delete p._points; }
return products; return products;
@ -265,7 +314,7 @@ function siteAssetFiles(dataFile) {
for (const [k, items] of Object.entries(siteAssetItems(dataFile))) out[k] = items.map((x) => x.file); for (const [k, items] of Object.entries(siteAssetItems(dataFile))) out[k] = items.map((x) => x.file);
return out; return out;
} }
const tplImagesOf = (t) => (Array.isArray(t && t.images) ? t.images : []).filter(Boolean).slice(0, 20); const tplImagesOf = (t) => (t && t.kind === 'html' ? [] : (Array.isArray(t && t.images) ? t.images : [])).filter(Boolean).slice(0, 20);
// 产品引用的模板(按产品里选的顺序;已删除的模板自动忽略) // 产品引用的模板(按产品里选的顺序;已删除的模板自动忽略)
const templatesOf = (p, templates) => (Array.isArray(p[TEMPLATE_IDS_FIELD]) ? p[TEMPLATE_IDS_FIELD] : []) const templatesOf = (p, templates) => (Array.isArray(p[TEMPLATE_IDS_FIELD]) ? p[TEMPLATE_IDS_FIELD] : [])
.map((id) => templates.find((t) => t.id === id)).filter(Boolean); .map((id) => templates.find((t) => t.id === id)).filter(Boolean);
@ -301,6 +350,9 @@ function toPublic(p, templates = []) {
const tplUrls = templatesOf(p, templates).flatMap((t) => tplImagesOf(t) const tplUrls = templatesOf(p, templates).flatMap((t) => tplImagesOf(t)
.map((f) => `${base}/api/public/tplmedia/${encodeURIComponent(t.id)}/${encodeURIComponent(f)}`)); .map((f) => `${base}/api/public/tplmedia/${encodeURIComponent(t.id)}/${encodeURIComponent(f)}`));
if (tplUrls.length) out.templateImages = tplUrls; if (tplUrls.length) out.templateImages = tplUrls;
// HTML 模板(系列参数表等):显示在详情图下方
const tplHtml = templatesOf(p, templates).map((t) => ({ id: t.id, html: { en: tplHtmlOf(t) } })).filter((x) => x.html.en);
if (tplHtml.length) out.templateHtml = tplHtml;
return out; return out;
} }