Update public-api.js via upload script - 2026-10-22 15:32:38
This commit is contained in:
parent
68964700e9
commit
f316df32be
|
|
@ -44,7 +44,7 @@ const FIELDS = {
|
||||||
youtube: 'youtubeUrl', // YouTube 链接,输出时只保留视频 ID
|
youtube: 'youtubeUrl', // YouTube 链接,输出时只保留视频 ID
|
||||||
description: 'webDescription', // { en, es?, ru? }:只需填 en,其他语言自动翻译;手动填了的语言优先用手动的
|
description: 'webDescription', // { en, es?, ru? }:只需填 en,其他语言自动翻译;手动填了的语言优先用手动的
|
||||||
sale: 'webSale',
|
sale: 'webSale',
|
||||||
pack: 'webPack', // 箱规 { per, nw, gw, l, w, h },网站按数量生成箱单 // { mode: 'group'|'stock', stock, minPrice, maxPrice }
|
// 箱规:产品关联的 Packing List(webPackId → crm:webPackSpecs);早期直接填在产品里的 webPack 作后备,见 toPublic // { mode: 'group'|'stock', stock, minPrice, maxPrice }
|
||||||
// —— 离线式 / 在线互动式 UPS 的参数(迷你UPS 一般不填,空的不输出)——
|
// —— 离线式 / 在线互动式 UPS 的参数(迷你UPS 一般不填,空的不输出)——
|
||||||
acInputVoltage: 'inputVoltage', // 离线式UPS 的输入电压(迷你UPS 用上面的 inputVoltage ← voltage)
|
acInputVoltage: 'inputVoltage', // 离线式UPS 的输入电压(迷你UPS 用上面的 inputVoltage ← voltage)
|
||||||
inputVoltageRange: 'inputVoltageRange',
|
inputVoltageRange: 'inputVoltageRange',
|
||||||
|
|
@ -394,7 +394,8 @@ const imagesOf = (p) => {
|
||||||
};
|
};
|
||||||
const detailsOf = (p) => (Array.isArray(p[DETAIL_FIELD]) ? p[DETAIL_FIELD] : []).filter(Boolean).slice(0, 20);
|
const detailsOf = (p) => (Array.isArray(p[DETAIL_FIELD]) ? p[DETAIL_FIELD] : []).filter(Boolean).slice(0, 20);
|
||||||
|
|
||||||
function toPublic(p, templates = []) {
|
const PACK_KEY = 'crm:webPackSpecs'; // 箱规:[{ id, name, per, nw, gw, l, w, h }]
|
||||||
|
function toPublic(p, templates = [], packSpecs = []) {
|
||||||
const slug = slugify(p.name);
|
const slug = slugify(p.name);
|
||||||
const out = { slug };
|
const out = { slug };
|
||||||
for (const [pub, src] of Object.entries(FIELDS)) {
|
for (const [pub, src] of Object.entries(FIELDS)) {
|
||||||
|
|
@ -403,6 +404,10 @@ function toPublic(p, templates = []) {
|
||||||
out[pub] = v;
|
out[pub] = v;
|
||||||
}
|
}
|
||||||
if (!out.ratedPower) delete out.ratedPowerUnit;
|
if (!out.ratedPower) delete out.ratedPowerUnit;
|
||||||
|
{
|
||||||
|
const sp = (p.webPackId && packSpecs.find((x) => x.id === p.webPackId)) || p.webPack;
|
||||||
|
if (sp && sp.per && sp.gw) out.pack = { per: sp.per, nw: sp.nw || '', gw: sp.gw, l: sp.l, w: sp.w, h: sp.h };
|
||||||
|
}
|
||||||
// 卖点:英文、每行一条(含中文的行不对外)
|
// 卖点:英文、每行一条(含中文的行不对外)
|
||||||
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);
|
||||||
|
|
@ -441,7 +446,8 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
||||||
app.get('/api/public/products', checkKey, async (req, res) => {
|
app.get('/api/public/products', checkKey, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const templates = readTemplates(dataFile);
|
const templates = readTemplates(dataFile);
|
||||||
res.json(await translateProducts(ai, published().map((p) => toPublic(p, templates))));
|
const packSpecs = readKey(dataFile, PACK_KEY);
|
||||||
|
res.json(await translateProducts(ai, published().map((p) => toPublic(p, templates, packSpecs))));
|
||||||
}
|
}
|
||||||
catch (e) { res.status(500).json({ error: e.message }); }
|
catch (e) { res.status(500).json({ error: e.message }); }
|
||||||
});
|
});
|
||||||
|
|
@ -450,7 +456,7 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
||||||
app.get('/api/public/products/:slug', checkKey, (req, res) => {
|
app.get('/api/public/products/:slug', checkKey, (req, res) => {
|
||||||
const p = findBySlug(req.params.slug);
|
const p = findBySlug(req.params.slug);
|
||||||
if (!p) return res.status(404).json({ error: 'not found' });
|
if (!p) return res.status(404).json({ error: 'not found' });
|
||||||
translateProducts(ai, [toPublic(p, readTemplates(dataFile))]).then(([x]) => res.json(x), (e) => res.status(500).json({ error: e.message }));
|
translateProducts(ai, [toPublic(p, readTemplates(dataFile), readKey(dataFile, PACK_KEY))]).then(([x]) => res.json(x), (e) => res.status(500).json({ error: e.message }));
|
||||||
});
|
});
|
||||||
|
|
||||||
// 产品图片:只允许读取"已发布产品自己的图片",防止借这个接口读到别的文件
|
// 产品图片:只允许读取"已发布产品自己的图片",防止借这个接口读到别的文件
|
||||||
|
|
@ -511,7 +517,7 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
||||||
try {
|
try {
|
||||||
const p = readProducts(dataFile).find((x) => String(x.id) === String(req.params.id));
|
const p = readProducts(dataFile).find((x) => String(x.id) === String(req.params.id));
|
||||||
if (!p) return res.status(404).json({ error: '产品不存在(新建的产品先保存一次)' });
|
if (!p) return res.status(404).json({ error: '产品不存在(新建的产品先保存一次)' });
|
||||||
const pub = toPublic(p, readTemplates(dataFile));
|
const pub = toPublic(p, readTemplates(dataFile), readKey(dataFile, PACK_KEY));
|
||||||
pub.description = {}; // 预览时忽略手动介绍,看 AI 会写什么
|
pub.description = {}; // 预览时忽略手动介绍,看 AI 会写什么
|
||||||
await translateSpecs(ai, [pub]);
|
await translateSpecs(ai, [pub]);
|
||||||
await generateDescriptions(ai, [pub]);
|
await generateDescriptions(ai, [pub]);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue