Update public-api.js via upload script - 2026-10-15 15:08:15
This commit is contained in:
parent
f655d6c449
commit
0b2efc532e
|
|
@ -48,6 +48,8 @@ const FIELDS = {
|
|||
const PUBLISH_FLAG = 'publishToWeb'; // 新增:"发布到网站"开关
|
||||
const IMAGES_FIELD = 'webImages'; // 新增:主图文件名数组,最多 6 张;为空时用现有的 productImageFileName
|
||||
const DETAIL_FIELD = 'webDetailImages'; // 新增:详情图文件名数组,最多 20 张,显示在规格表下方
|
||||
const TEMPLATES_KEY = 'crm:webDetailTemplates'; // 详情模板:[{ id, name, images: [文件名] }],图片在 uploads/<模板id>/
|
||||
const TEMPLATE_IDS_FIELD = 'webDetailTemplateIds'; // 产品引用的模板id,按顺序显示在详情图下方
|
||||
|
||||
// 配置:优先读环境变量,没有就读同目录下的 site-config.json(不用改进程启动方式)
|
||||
// { "SITE_BUILD_KEY": "...", "CF_DEPLOY_HOOK": "...", "PUBLIC_BASE_URL": "https://b2b.google-helper.com" }
|
||||
|
|
@ -141,12 +143,18 @@ async function translateProducts(ai, products) {
|
|||
const slugify =(s) => String(s || '').trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
||||
const parse = (v) => { try { return typeof v === 'string' ? JSON.parse(v) : v; } catch { return v; } };
|
||||
|
||||
function readProducts(dataFile) {
|
||||
function readKey(dataFile, key) {
|
||||
const data = JSON.parse(fs.readFileSync(dataFile, 'utf8'));
|
||||
const raw = data[PRODUCTS_KEY];
|
||||
const raw = data[key];
|
||||
const list = parse(raw && raw.value !== undefined ? raw.value : raw);
|
||||
return Array.isArray(list) ? list : [];
|
||||
}
|
||||
const readProducts = (dataFile) => readKey(dataFile, PRODUCTS_KEY);
|
||||
const readTemplates = (dataFile) => readKey(dataFile, TEMPLATES_KEY);
|
||||
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] : [])
|
||||
.map((id) => templates.find((t) => t.id === id)).filter(Boolean);
|
||||
|
||||
const youtubeId = (url) => {
|
||||
const m = String(url || '').match(/(?:youtu\.be\/|v=|embed\/|shorts\/)([\w-]{11})/);
|
||||
|
|
@ -160,7 +168,7 @@ const imagesOf = (p) => {
|
|||
};
|
||||
const detailsOf = (p) => (Array.isArray(p[DETAIL_FIELD]) ? p[DETAIL_FIELD] : []).filter(Boolean).slice(0, 20);
|
||||
|
||||
function toPublic(p) {
|
||||
function toPublic(p, templates = []) {
|
||||
const slug = slugify(p.name);
|
||||
const out = { slug };
|
||||
for (const [pub, src] of Object.entries(FIELDS)) {
|
||||
|
|
@ -175,6 +183,10 @@ function toPublic(p) {
|
|||
const base = conf('PUBLIC_BASE_URL');
|
||||
out.images = imagesOf(p).map((f) => `${base}/api/public/media/${slug}/${encodeURIComponent(f)}`);
|
||||
out.detailImages = detailsOf(p).map((f) => `${base}/api/public/media/${slug}/${encodeURIComponent(f)}`);
|
||||
// 模板图片:同一个模板被多个产品引用时 URL 相同,网站构建时只下载一次
|
||||
const tplUrls = templatesOf(p, templates).flatMap((t) => tplImagesOf(t)
|
||||
.map((f) => `${base}/api/public/tplmedia/${encodeURIComponent(t.id)}/${encodeURIComponent(f)}`));
|
||||
if (tplUrls.length) out.templateImages = tplUrls;
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
@ -193,7 +205,10 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
|||
|
||||
// 网站构建时调用:全部已发布产品
|
||||
app.get('/api/public/products', checkKey, async (req, res) => {
|
||||
try { res.json(await translateProducts(ai, published().map(toPublic))); }
|
||||
try {
|
||||
const templates = readTemplates(dataFile);
|
||||
res.json(await translateProducts(ai, published().map((p) => toPublic(p, templates))));
|
||||
}
|
||||
catch (e) { res.status(500).json({ error: e.message }); }
|
||||
});
|
||||
|
||||
|
|
@ -201,7 +216,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)]).then(([x]) => res.json(x), (e) => res.status(500).json({ error: e.message }));
|
||||
translateProducts(ai, [toPublic(p, readTemplates(dataFile))]).then(([x]) => res.json(x), (e) => res.status(500).json({ error: e.message }));
|
||||
});
|
||||
|
||||
// 产品图片:只允许读取"已发布产品自己的图片",防止借这个接口读到别的文件
|
||||
|
|
@ -213,6 +228,16 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
|||
res.sendFile(path.resolve(uploadsDir, path.basename(String(p.id)), file), (err) => { if (err) res.status(404).end(); });
|
||||
});
|
||||
|
||||
// 模板图片:只允许读取"被已发布产品引用的模板"里的图片
|
||||
app.get('/api/public/tplmedia/:tplId/:file', checkKey, (req, res) => {
|
||||
const tplId = String(req.params.tplId);
|
||||
const file = path.basename(req.params.file);
|
||||
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));
|
||||
if (!used || !tplImagesOf(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(); });
|
||||
});
|
||||
|
||||
// 后台"发布到网站"按钮:触发 Cloudflare Pages 重新构建(需要后台登录)
|
||||
app.post('/api/site/deploy', requireAuth, async (req, res) => {
|
||||
if (!conf('CF_DEPLOY_HOOK')) return res.status(500).json({ error: '未配置 CF_DEPLOY_HOOK' });
|
||||
|
|
|
|||
Loading…
Reference in New Issue