Update public-api.js via upload script - 2026-11-04 11:53:47
This commit is contained in:
parent
a781ec76ad
commit
179af185a0
|
|
@ -635,7 +635,7 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
|||
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 });
|
||||
res.json({ status, deleted, siteChanged: siteChanged(ensureFp(data)), autoAt: autoSync.at || 0, autoRunning: autoSync.running });
|
||||
} catch (e) { res.status(500).json({ error: e.message }); }
|
||||
});
|
||||
// 把选中的模板同步到网站(复制到"网站模板");没选的保持网站上原来的版本。之后再调用 /api/site/deploy 重新构建网站
|
||||
|
|
@ -767,18 +767,71 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut
|
|||
scheduleRetry();
|
||||
}, 20000);
|
||||
}
|
||||
app.post('/api/site/deploy', requireAuth, async (req, res) => {
|
||||
if (!conf('CF_DEPLOY_HOOK')) return res.status(500).json({ error: '未配置 CF_DEPLOY_HOOK' });
|
||||
if (deployQ.pending) return res.json({ ok: true, queued: true, note: '已加入更新队列:当前这次构建结束后自动再更新一次,包含你刚才的修改,不用再点' });
|
||||
try {
|
||||
// 触发网站重新构建:上一次构建还没结束(304)就排队,结束后自动再构建一次
|
||||
async function requestDeploy() {
|
||||
if (!conf('CF_DEPLOY_HOOK')) throw new Error('未配置 CF_DEPLOY_HOOK');
|
||||
if (deployQ.pending) return { queued: true };
|
||||
const st = await hook();
|
||||
if (st === 304) {
|
||||
deployQ.pending = true; deployQ.tries = 0; deployQ.since = Date.now();
|
||||
scheduleRetry();
|
||||
return res.json({ ok: true, queued: true, note: '已加入更新队列:当前这次构建结束后自动再更新一次,包含你刚才的修改,不用再点' });
|
||||
}
|
||||
if (st === 304) { deployQ.pending = true; deployQ.tries = 0; deployQ.since = Date.now(); scheduleRetry(); return { queued: true }; }
|
||||
if (st < 200 || st >= 300) throw new Error(`Cloudflare 返回 HTTP ${st}`);
|
||||
res.json({ ok: true });
|
||||
} catch (e) { res.status(502).json({ error: e.message }); }
|
||||
return { queued: false };
|
||||
}
|
||||
app.post('/api/site/deploy', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const r = await requestDeploy();
|
||||
res.json(r.queued ? { ok: true, queued: true, note: '已加入更新队列:当前这次构建结束后自动再更新一次,包含你刚才的修改,不用再点' } : { ok: true });
|
||||
} catch (e) { res.status(e.message.includes('CF_DEPLOY_HOOK') ? 500 : 502).json({ error: e.message }); }
|
||||
});
|
||||
|
||||
// —— 模板自动同步到网站 ——
|
||||
// 后台保存模板后不马上同步:等模板停止修改 AUTO_SYNC_DELAY 之后,把所有改过的模板一次同步到网站并重新构建一次。
|
||||
// 连续改很多个模板也只构建一次;改到一半的内容不会马上出现在网站上。服务重启后如果还有没同步的,自动补上。
|
||||
const AUTO_SYNC_DELAY = 3 * 60 * 1000;
|
||||
const autoSync = { timer: null, at: 0, running: false };
|
||||
// 网站内容指纹:只看会出现在网站上的东西(已发布产品的对外字段和图片、箱规、网站图片、近期动态)。
|
||||
// 改了进价、备注这类只在后台用的字段,指纹不变,不会白白重新构建网站。
|
||||
const FP_KEY = '__siteFingerprint';
|
||||
const PUB_SRC = [...new Set([...Object.values(FIELDS), 'name', 'id', PUBLISH_FLAG, IMAGES_FIELD, DETAIL_FIELD, TEMPLATE_IDS_FIELD, 'productImageFileName', 'webPackId', 'webPack'])];
|
||||
function siteFingerprint(data) {
|
||||
const prods = listOf(data, PRODUCTS_KEY).filter((p) => p && p[PUBLISH_FLAG]).map((p) => PUB_SRC.map((k) => p[k]));
|
||||
const raw = (k) => { const v = data[k]; return v && v.value !== undefined ? v.value : v; };
|
||||
return crypto.createHash('sha256').update(JSON.stringify([prods, raw(PACK_KEY), raw(SITE_ASSETS_KEY), raw(SITE_NEWS_KEY)])).digest('hex');
|
||||
}
|
||||
function ensureFp(data) { if (!data[FP_KEY] && writeData) { data[FP_KEY] = siteFingerprint(data); writeData(data); } return data; }
|
||||
const siteChanged = (data) => !!data[FP_KEY] && data[FP_KEY] !== siteFingerprint(data);
|
||||
function unsyncedCount() {
|
||||
const data = ensureLive(loadAll());
|
||||
const work = listOf(data, TEMPLATES_KEY), live = listOf(data, LIVE_TEMPLATES_KEY);
|
||||
const liveMap = new Map(live.map((t) => [t.id, tplNorm(t)]));
|
||||
const workIds = new Set(work.map((t) => t.id));
|
||||
return work.filter((t) => liveMap.get(t.id) !== tplNorm(t)).length + live.filter((t) => !workIds.has(t.id)).length;
|
||||
}
|
||||
async function runAutoSync() {
|
||||
autoSync.timer = null; autoSync.at = 0;
|
||||
if (autoSync.running) { scheduleTemplateSync(); return; }
|
||||
autoSync.running = true;
|
||||
try {
|
||||
if (!writeData) return;
|
||||
const tplN = unsyncedCount();
|
||||
const data = ensureFp(ensureLive(loadAll()));
|
||||
const prodChanged = siteChanged(data);
|
||||
if (!tplN && !prodChanged) return;
|
||||
if (tplN) data[LIVE_TEMPLATES_KEY] = JSON.stringify(listOf(data, TEMPLATES_KEY)); // 网站模板 = 后台现在的全部模板(删掉的也去掉)
|
||||
data[FP_KEY] = siteFingerprint(data);
|
||||
writeData(data);
|
||||
const r = await requestDeploy();
|
||||
console.log(`[public-api] 自动更新网站:${tplN ? tplN + ' 个模板' : ''}${tplN && prodChanged ? ' + ' : ''}${prodChanged ? '产品 / 网站内容' : ''}${r.queued ? '(网站正在构建,排队等下一次)' : ''}`);
|
||||
} catch (e) {
|
||||
console.warn('[public-api] 模板自动同步失败,1 分钟后重试:', e.message);
|
||||
autoSync.at = Date.now() + 60000;
|
||||
autoSync.timer = setTimeout(runAutoSync, 60000);
|
||||
} finally { autoSync.running = false; }
|
||||
}
|
||||
function scheduleTemplateSync() {
|
||||
if (autoSync.timer) clearTimeout(autoSync.timer);
|
||||
autoSync.at = Date.now() + AUTO_SYNC_DELAY;
|
||||
autoSync.timer = setTimeout(runAutoSync, AUTO_SYNC_DELAY);
|
||||
}
|
||||
setTimeout(() => { try { const d = ensureFp(ensureLive(loadAll())); if (unsyncedCount() || siteChanged(d)) scheduleTemplateSync(); } catch (e) {} }, 10000);
|
||||
return { scheduleTemplateSync };
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue