Update public-api.js via upload script - 2026-10-20 11:02:56
This commit is contained in:
parent
15493c080c
commit
93a8fb90f0
|
|
@ -314,7 +314,10 @@ 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) => (t && t.kind === 'html' ? [] : (Array.isArray(t && t.images) ? t.images : [])).filter(Boolean).slice(0, 20);
|
// 模板三种:images = 详情图片组(详情图下方);html = 系列参数表;main = 主图模板(同系列共用的主图,接在产品自己的主图后面)
|
||||||
|
const tplFilesOf = (t) => (t && t.kind !== 'html' && Array.isArray(t.images) ? t.images : []).filter(Boolean).slice(0, 20);
|
||||||
|
const tplImagesOf = (t) => (t && (t.kind || 'images') === 'images' ? tplFilesOf(t) : []);
|
||||||
|
const tplMainOf = (t) => (t && t.kind === 'main' ? tplFilesOf(t).slice(0, 6) : []);
|
||||||
// 产品引用的模板(按产品里选的顺序;已删除的模板自动忽略)
|
// 产品引用的模板(按产品里选的顺序;已删除的模板自动忽略)
|
||||||
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);
|
||||||
|
|
@ -344,7 +347,12 @@ function toPublic(p, templates = []) {
|
||||||
out._points = String(p.sellingPoints || '').split('\n').map((l) => l.trim()).filter((l) => l && !/[\u4e00-\u9fff]/.test(l)); // 没填额定功率时,单位单独出现没有意义
|
out._points = String(p.sellingPoints || '').split('\n').map((l) => l.trim()).filter((l) => l && !/[\u4e00-\u9fff]/.test(l)); // 没填额定功率时,单位单独出现没有意义
|
||||||
if (out.youtube) out.youtube = youtubeId(out.youtube);
|
if (out.youtube) out.youtube = youtubeId(out.youtube);
|
||||||
const base = conf('PUBLIC_BASE_URL');
|
const base = conf('PUBLIC_BASE_URL');
|
||||||
out.images = imagesOf(p).map((f) => `${base}/api/public/media/${slug}/${encodeURIComponent(f)}`);
|
// 主图:产品自己的网站主图在前,主图模板的图片接在后面,最多 6 张;
|
||||||
|
// 产品没有自己的网站主图、也没用主图模板时,才退回用产品档案里的"产品主图"
|
||||||
|
const mainTpl = templatesOf(p, templates).flatMap((t) => tplMainOf(t)
|
||||||
|
.map((f) => `${base}/api/public/tplmedia/${encodeURIComponent(t.id)}/${encodeURIComponent(f)}`));
|
||||||
|
const own = (Array.isArray(p[IMAGES_FIELD]) && p[IMAGES_FIELD].length) || !mainTpl.length ? imagesOf(p) : [];
|
||||||
|
out.images = [...own.map((f) => `${base}/api/public/media/${slug}/${encodeURIComponent(f)}`), ...mainTpl].slice(0, 6);
|
||||||
out.detailImages = detailsOf(p).map((f) => `${base}/api/public/media/${slug}/${encodeURIComponent(f)}`);
|
out.detailImages = detailsOf(p).map((f) => `${base}/api/public/media/${slug}/${encodeURIComponent(f)}`);
|
||||||
// 模板图片:同一个模板被多个产品引用时 URL 相同,网站构建时只下载一次
|
// 模板图片:同一个模板被多个产品引用时 URL 相同,网站构建时只下载一次
|
||||||
const tplUrls = templatesOf(p, templates).flatMap((t) => tplImagesOf(t)
|
const tplUrls = templatesOf(p, templates).flatMap((t) => tplImagesOf(t)
|
||||||
|
|
@ -400,7 +408,7 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
||||||
const file = path.basename(req.params.file);
|
const file = path.basename(req.params.file);
|
||||||
const t = readTemplates(dataFile).find((x) => x.id === tplId);
|
const t = readTemplates(dataFile).find((x) => x.id === tplId);
|
||||||
const used = t && published().some((p) => Array.isArray(p[TEMPLATE_IDS_FIELD]) && p[TEMPLATE_IDS_FIELD].includes(tplId));
|
const used = t && published().some((p) => Array.isArray(p[TEMPLATE_IDS_FIELD]) && p[TEMPLATE_IDS_FIELD].includes(tplId));
|
||||||
if (!used || !tplImagesOf(t).includes(file) || !/^[A-Za-z0-9_-]+$/.test(tplId)) return res.status(404).end();
|
if (!used || !tplFilesOf(t).includes(file) || !/^[A-Za-z0-9_-]+$/.test(tplId)) return res.status(404).end();
|
||||||
res.sendFile(path.resolve(uploadsDir, tplId, file), (err) => { if (err) res.status(404).end(); });
|
res.sendFile(path.resolve(uploadsDir, tplId, file), (err) => { if (err) res.status(404).end(); });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue