Update public-api.js via upload script - 2026-11-04 10:50:37
This commit is contained in:
parent
1d8664a486
commit
ca7596e2da
|
|
@ -144,6 +144,9 @@ 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 张,显示在规格表下方
|
||||||
|
// 网站上用的是"已同步"的模板(crm:webTemplatesLive):后台改模板不会直接影响网站,
|
||||||
|
// 在模板管理里点"更新到网站"、勾选要同步的模板后,才把它们复制到这里。还没有这份数据时(第一次)用后台的模板。
|
||||||
|
const LIVE_TEMPLATES_KEY = 'crm:webTemplatesLive';
|
||||||
const TEMPLATES_KEY = 'crm:webDetailTemplates'; // 详情模板:[{ id, name, kind: 'images'|'html', images: [文件名], html }],图片在 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,按顺序显示在详情图下方
|
||||||
// —— 网站管理(非产品内容)——
|
// —— 网站管理(非产品内容)——
|
||||||
|
|
@ -496,7 +499,10 @@ function readKey(dataFile, key) {
|
||||||
return Array.isArray(list) ? list : [];
|
return Array.isArray(list) ? list : [];
|
||||||
}
|
}
|
||||||
const readProducts = (dataFile) => readKey(dataFile, PRODUCTS_KEY);
|
const readProducts = (dataFile) => readKey(dataFile, PRODUCTS_KEY);
|
||||||
const readTemplates = (dataFile) => readKey(dataFile, TEMPLATES_KEY);
|
const hasKey = (dataFile, key) => { try { return key in JSON.parse(fs.readFileSync(dataFile, 'utf8')); } catch { return false; } };
|
||||||
|
const readTemplates = (dataFile) => (hasKey(dataFile, LIVE_TEMPLATES_KEY) ? readKey(dataFile, LIVE_TEMPLATES_KEY) : readKey(dataFile, TEMPLATES_KEY));
|
||||||
|
// 比较"后台模板"和"网站模板"是否一样:kind 只是后台当前打开的是哪一栏(主图 / 详情 / HTML),不影响网站,不算改动
|
||||||
|
const tplNorm = (t) => JSON.stringify({ ...t, kind: t && !Array.isArray(t.mainImages) ? t.kind : undefined });
|
||||||
function readObj(dataFile, key) {
|
function readObj(dataFile, key) {
|
||||||
const data = JSON.parse(fs.readFileSync(dataFile, 'utf8'));
|
const data = JSON.parse(fs.readFileSync(dataFile, 'utf8'));
|
||||||
const raw = data[key];
|
const raw = data[key];
|
||||||
|
|
@ -611,7 +617,45 @@ function checkKey(req, res, next) {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAuth, ai }) {
|
module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAuth, ai, writeData }) {
|
||||||
|
// —— 模板同步状态 / 选择性同步 ——
|
||||||
|
const loadAll = () => JSON.parse(fs.readFileSync(dataFile, 'utf8'));
|
||||||
|
const listOf = (data, key) => { const raw = data[key]; const v = parse(raw && raw.value !== undefined ? raw.value : raw); return Array.isArray(v) ? v : []; };
|
||||||
|
function ensureLive(data) {
|
||||||
|
// 第一次:网站上现在用的就是后台的全部模板,把它们记成"已同步"
|
||||||
|
if (!(LIVE_TEMPLATES_KEY in data)) { data[LIVE_TEMPLATES_KEY] = JSON.stringify(listOf(data, TEMPLATES_KEY)); if (writeData) writeData(data); }
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
// 每个模板的状态:synced 已同步 / changed 改过没同步 / new 新建没同步;deleted = 后台已删除、网站上还在
|
||||||
|
app.get('/api/site/templates-status', requireAuth, (req, res) => {
|
||||||
|
try {
|
||||||
|
const data = ensureLive(loadAll());
|
||||||
|
const work = listOf(data, TEMPLATES_KEY), live = new Map(listOf(data, LIVE_TEMPLATES_KEY).map((t) => [t.id, t]));
|
||||||
|
const status = {};
|
||||||
|
for (const t of work) status[t.id] = !live.has(t.id) ? 'new' : tplNorm(live.get(t.id)) === tplNorm(t) ? 'synced' : 'changed';
|
||||||
|
const workIds = new Set(work.map((t) => t.id));
|
||||||
|
const deleted = [...live.values()].filter((t) => !workIds.has(t.id)).map((t) => ({ id: t.id, name: t.name || '' }));
|
||||||
|
res.json({ status, deleted });
|
||||||
|
} catch (e) { res.status(500).json({ error: e.message }); }
|
||||||
|
});
|
||||||
|
// 把选中的模板同步到网站(复制到"网站模板");没选的保持网站上原来的版本。之后再调用 /api/site/deploy 重新构建网站
|
||||||
|
app.post('/api/site/templates-publish', requireAuth, (req, res) => {
|
||||||
|
try {
|
||||||
|
if (!writeData) return res.status(500).json({ error: 'server.js 需要把 writeData 传给 public-api' });
|
||||||
|
const ids = new Set((Array.isArray(req.body && req.body.ids) ? req.body.ids : []).map(String));
|
||||||
|
const data = ensureLive(loadAll());
|
||||||
|
const work = listOf(data, TEMPLATES_KEY), oldLive = listOf(data, LIVE_TEMPLATES_KEY);
|
||||||
|
const live = new Map(oldLive.map((t) => [t.id, t]));
|
||||||
|
const next = [];
|
||||||
|
for (const t of work) { if (ids.has(t.id)) next.push(t); else if (live.has(t.id)) next.push(live.get(t.id)); }
|
||||||
|
const workIds = new Set(work.map((t) => t.id));
|
||||||
|
for (const t of oldLive) if (!workIds.has(t.id) && !ids.has(t.id)) next.push(t); // 后台删了但这次没选:网站上先保留
|
||||||
|
data[LIVE_TEMPLATES_KEY] = JSON.stringify(next);
|
||||||
|
writeData(data);
|
||||||
|
res.json({ ok: true, count: ids.size });
|
||||||
|
} catch (e) { res.status(500).json({ error: e.message }); }
|
||||||
|
});
|
||||||
|
|
||||||
const published = () => readProducts(dataFile).filter((p) => p[PUBLISH_FLAG]);
|
const published = () => readProducts(dataFile).filter((p) => p[PUBLISH_FLAG]);
|
||||||
const findBySlug = (slug) => published().find((p) => slugify(p.name) === slug);
|
const findBySlug = (slug) => published().find((p) => slugify(p.name) === slug);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue