Update public-api.js via upload script - 2026-10-16 17:22:04
This commit is contained in:
parent
6eaccaafec
commit
1ad3b6e823
|
|
@ -51,7 +51,7 @@ const DETAIL_FIELD = 'webDetailImages'; // 新增:详情图文件名数组,
|
||||||
const TEMPLATES_KEY = 'crm:webDetailTemplates'; // 详情模板:[{ id, name, images: [文件名] }],图片在 uploads/<模板id>/
|
const TEMPLATES_KEY = 'crm:webDetailTemplates'; // 详情模板:[{ id, name, images: [文件名] }],图片在 uploads/<模板id>/
|
||||||
const TEMPLATE_IDS_FIELD = 'webDetailTemplateIds'; // 产品引用的模板id,按顺序显示在详情图下方
|
const TEMPLATE_IDS_FIELD = 'webDetailTemplateIds'; // 产品引用的模板id,按顺序显示在详情图下方
|
||||||
// —— 网站管理(非产品内容)——
|
// —— 网站管理(非产品内容)——
|
||||||
const SITE_ASSETS_KEY = 'crm:webAssets'; // { 图片位id: [文件名] },文件在 uploads/site_assets/
|
const SITE_ASSETS_KEY = 'crm:webAssets'; // { 图片位id: [文件名 或 { file, caption:{en,es?,ru?} }] },文件在 uploads/site_assets/
|
||||||
const SITE_ASSETS_DIR = 'site_assets';
|
const SITE_ASSETS_DIR = 'site_assets';
|
||||||
const SITE_NEWS_KEY = 'crm:webNews'; // [{ id, date:'2025-12', title:{en,es?,ru?}, text:{en,es?,ru?}, youtubeUrl }]
|
const SITE_NEWS_KEY = 'crm:webNews'; // [{ id, date:'2025-12', title:{en,es?,ru?}, text:{en,es?,ru?}, youtubeUrl }]
|
||||||
|
|
||||||
|
|
@ -188,16 +188,25 @@ function readObj(dataFile, key) {
|
||||||
return v && typeof v === 'object' && !Array.isArray(v) ? v : {};
|
return v && typeof v === 'object' && !Array.isArray(v) ? v : {};
|
||||||
}
|
}
|
||||||
const safeSlot = (k) => /^[A-Za-z0-9_-]{1,40}$/.test(k);
|
const safeSlot = (k) => /^[A-Za-z0-9_-]{1,40}$/.test(k);
|
||||||
function siteAssetFiles(dataFile) {
|
// 图片位条目:普通图片位是文件名;图集(生产线、研发、仓库)是 { file, caption },可以有多张
|
||||||
|
function siteAssetItems(dataFile) {
|
||||||
const a = readObj(dataFile, SITE_ASSETS_KEY);
|
const a = readObj(dataFile, SITE_ASSETS_KEY);
|
||||||
const out = {};
|
const out = {};
|
||||||
for (const [k, list] of Object.entries(a)) {
|
for (const [k, list] of Object.entries(a)) {
|
||||||
if (!safeSlot(k) || !Array.isArray(list)) continue;
|
if (!safeSlot(k) || !Array.isArray(list)) continue;
|
||||||
const files = list.filter((f) => typeof f === 'string' && f && f === path.basename(f)).slice(0, 20);
|
const items = list
|
||||||
if (files.length) out[k] = files;
|
.map((x) => (typeof x === 'string' ? { file: x, caption: {} } : x && typeof x.file === 'string' ? { file: x.file, caption: x.caption || {} } : null))
|
||||||
|
.filter((x) => x && x.file && x.file === path.basename(x.file))
|
||||||
|
.slice(0, 20);
|
||||||
|
if (items.length) out[k] = items;
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
function siteAssetFiles(dataFile) {
|
||||||
|
const out = {};
|
||||||
|
for (const [k, items] of Object.entries(siteAssetItems(dataFile))) out[k] = items.map((x) => x.file);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
const tplImagesOf = (t) => (Array.isArray(t && t.images) ? t.images : []).filter(Boolean).slice(0, 10);
|
const tplImagesOf = (t) => (Array.isArray(t && t.images) ? t.images : []).filter(Boolean).slice(0, 10);
|
||||||
// 产品引用的模板(按产品里选的顺序;已删除的模板自动忽略)
|
// 产品引用的模板(按产品里选的顺序;已删除的模板自动忽略)
|
||||||
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] : [])
|
||||||
|
|
@ -289,17 +298,21 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
||||||
app.get('/api/public/site', checkKey, async (req, res) => {
|
app.get('/api/public/site', checkKey, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const base = conf('PUBLIC_BASE_URL');
|
const base = conf('PUBLIC_BASE_URL');
|
||||||
const files = siteAssetFiles(dataFile);
|
const items = siteAssetItems(dataFile);
|
||||||
const assets = {};
|
|
||||||
for (const [k, list] of Object.entries(files)) assets[k] = list.map((f) => `${base}/api/public/site-media/${encodeURIComponent(f)}`);
|
|
||||||
const rawNews = readKey(dataFile, SITE_NEWS_KEY).filter((n) => n && n.title && String(n.title.en || '').trim());
|
const rawNews = readKey(dataFile, SITE_NEWS_KEY).filter((n) => n && n.title && String(n.title.en || '').trim());
|
||||||
const tr = await translateStrings(ai, rawNews.flatMap((n) => [n.title.en, (n.text && n.text.en) || '']));
|
const captionsEn = Object.values(items).flat().map((x) => String((x.caption && x.caption.en) || '').trim());
|
||||||
|
const tr = await translateStrings(ai, [...rawNews.flatMap((n) => [n.title.en, (n.text && n.text.en) || '']), ...captionsEn]);
|
||||||
const i18n = (o) => {
|
const i18n = (o) => {
|
||||||
const en = String((o && o.en) || '').trim();
|
const en = String((o && o.en) || '').trim();
|
||||||
const r = { en };
|
const r = { en };
|
||||||
for (const lang of Object.keys(WEB_TRANSLATE_LANGS)) r[lang] = String((o && o[lang]) || '').trim() || (en && tr[lang][en]) || en;
|
for (const lang of Object.keys(WEB_TRANSLATE_LANGS)) r[lang] = String((o && o[lang]) || '').trim() || (en && tr[lang][en]) || en;
|
||||||
return r;
|
return r;
|
||||||
};
|
};
|
||||||
|
// 图片位:{ url, caption:{en,es,ru} },没写标题的 caption 为空
|
||||||
|
const assets = {};
|
||||||
|
for (const [k, list] of Object.entries(items)) {
|
||||||
|
assets[k] = list.map((x) => ({ url: `${base}/api/public/site-media/${encodeURIComponent(x.file)}`, caption: i18n(x.caption) }));
|
||||||
|
}
|
||||||
const news = rawNews
|
const news = rawNews
|
||||||
.map((n) => ({ date: String(n.date || '').slice(0, 10), title: i18n(n.title), text: i18n(n.text), youtube: youtubeId(n.youtubeUrl) || undefined }))
|
.map((n) => ({ date: String(n.date || '').slice(0, 10), title: i18n(n.title), text: i18n(n.text), youtube: youtubeId(n.youtubeUrl) || undefined }))
|
||||||
.sort((a, b) => (a.date < b.date ? 1 : a.date > b.date ? -1 : 0));
|
.sort((a, b) => (a.date < b.date ? 1 : a.date > b.date ? -1 : 0));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue