From 53a8300c49863cd8edf4e8feaebda8e1fc80fe6e Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 4 Nov 2026 09:48:27 +0800 Subject: [PATCH] Update public-api.js via upload script - 2026-11-04 09:48:22 --- public-api.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 6 deletions(-) diff --git a/public-api.js b/public-api.js index 9f39d4c..8f79e55 100644 --- a/public-api.js +++ b/public-api.js @@ -84,6 +84,62 @@ const FIELDS = { upsModuleDimensions: 'upsModuleDimensions', upsModuleWeight: 'upsModuleWeight', sockets: 'webSockets', // 网站展示的默认插座配置 [{ type: 'SCHUKO'|'NEMA'|'UK'|'UNIVERSAL'|'IEC'|'AU', count }] + // —— 光伏逆变器(混网 / 并网 / 离网)—— + batteryType: 'batteryType', + maxChargingVoltage: 'maxChargingVoltage', + maxChargeDischargeCurrent: 'maxChargeDischargeCurrent', + maxDcInputPower: 'maxDcInputPower', + maxDcInputVoltage: 'maxDcInputVoltage', + mpptVoltageRange: 'mpptVoltageRange', + startingVoltage: 'startingVoltage', + maxInputCurrent: 'maxInputCurrent', + mpptNumber: 'mpptNumber', + onGridMaxApparentPower: 'onGridMaxApparentPower', + ratedOutputFrequency: 'ratedOutputFrequency', + onGridMaxOutputCurrent: 'onGridMaxOutputCurrent', + offGridRatedApparentPower: 'offGridRatedApparentPower', + offGridMaxApparentPower: 'offGridMaxApparentPower', + offGridMaxOutputCurrent: 'offGridMaxOutputCurrent', + efficiencyMppt: 'efficiencyMppt', + efficiencyMax: 'efficiencyMax', + efficiencyEurope: 'efficiencyEurope', + protectionFunctions: 'protectionFunctions', + operatingTemp: 'operatingTemp', + storageTemp: 'storageTemp', + protectionClass: 'protectionClass', + cooling: 'cooling', + topology: 'topology', + altitude: 'altitude', + safetyStandard: 'safetyStandard', + gridStandard: 'gridStandard', + otherStandard: 'otherStandard', + // —— 储能电池(机架式 / 壁挂式锂电、铅酸、家庭储能系统)—— + moduleType: 'moduleType', + cellType: 'cellType', + cellConfiguration: 'cellConfiguration', + moduleVoltage: 'moduleVoltage', + moduleCapacity: 'moduleCapacity', + moduleMaxEnergy: 'moduleMaxEnergy', + moduleOutputPower: 'moduleOutputPower', + moduleDimensions: 'moduleDimensions', + moduleWeight: 'moduleWeight', + cycleLife: 'cycleLife', + selfDischargeRate: 'selfDischargeRate', + chargeRate: 'chargeRate', + dischargeRate: 'dischargeRate', + chargingEfficiency: 'chargingEfficiency', + dischargingEfficiency: 'dischargingEfficiency', + recommendedChargingVoltage: 'recommendedChargingVoltage', + recommendedDischargeCurrent: 'recommendedDischargeCurrent', + maxChargingCurrent: 'maxChargingCurrent', + maxDischargeCurrent: 'maxDischargeCurrent', + dischargeCutoffVoltage: 'dischargeCutoffVoltage', + cabinetMaxEnergy: 'cabinetMaxEnergy', + cabinetRatedVoltage: 'cabinetRatedVoltage', + cabinetDimensions: 'cabinetDimensions', + cabinetWeight: 'cabinetWeight', + commInterface: 'commInterface', + color: 'color', }; const PUBLISH_FLAG = 'publishToWeb'; // 新增:"发布到网站"开关 const IMAGES_FIELD = 'webImages'; // 新增:主图文件名数组,最多 6 张;为空时用现有的 productImageFileName @@ -166,7 +222,17 @@ const SPEC_TRANSLATE_FIELDS = ['inputVoltage', 'acInputVoltage', 'inputVoltageRa 'frequencyRangeBatteryMode', 'batteryVoltage', 'chargeCurrent', 'chargeTime', 'lcdDisplay', 'ledDisplay', 'fullProtection', 'alarmInfo', 'interfacePort', 'humidity', 'noiseLevel', 'outputPower', 'powerFactor', 'batterySpec', 'dimensions', 'netWeight', 'phase', 'inputPowerFactor', 'transferTimeAcToBattery', 'harmonicDistortion', 'efficiencyAcMode', 'efficiencyBatteryMode', 'chargingVoltage', - 'optionalSnmp', 'outputPowerFactor', 'parallelUnits', 'overload']; + 'optionalSnmp', 'outputPowerFactor', 'parallelUnits', 'overload', + // 光伏逆变器 + 'batteryType', 'maxChargingVoltage', 'maxChargeDischargeCurrent', 'maxDcInputPower', 'maxDcInputVoltage', 'mpptVoltageRange', 'startingVoltage', + 'maxInputCurrent', 'mpptNumber', 'onGridMaxApparentPower', 'ratedOutputFrequency', 'onGridMaxOutputCurrent', 'offGridRatedApparentPower', + 'offGridMaxApparentPower', 'offGridMaxOutputCurrent', 'efficiencyMppt', 'efficiencyMax', 'efficiencyEurope', 'protectionFunctions', 'operatingTemp', + 'storageTemp', 'protectionClass', 'cooling', 'topology', 'altitude', 'safetyStandard', 'gridStandard', 'otherStandard', + // 储能电池 + 'moduleType', 'cellType', 'cellConfiguration', 'moduleVoltage', 'moduleCapacity', 'moduleMaxEnergy', 'moduleOutputPower', 'moduleDimensions', 'moduleWeight', + 'cycleLife', 'selfDischargeRate', 'chargeRate', 'dischargeRate', 'chargingEfficiency', 'dischargingEfficiency', 'recommendedChargingVoltage', + 'recommendedDischargeCurrent', 'maxChargingCurrent', 'maxDischargeCurrent', 'dischargeCutoffVoltage', 'cabinetMaxEnergy', 'cabinetRatedVoltage', + 'cabinetDimensions', 'cabinetWeight', 'commInterface', 'color']; const ZH_TARGETS = { en: 'English', ...WEB_TRANSLATE_LANGS }; const hasZh = (s) => /[\u4e00-\u9fff]/.test(String(s || '')); async function translateSpecs(ai, products) { @@ -256,12 +322,19 @@ const tplHtmlListFor = (t, p) => { else if (pk.html === false || (pk.html !== true && exclOf(t, p).html)) list = []; return list.map((h) => ({ id: h.id, html: cleanTplHtml(h.html) })).filter((h) => h.html); }; +// HTML 片段里的图片:CRM 里插入的图片存成模板目录下的文件(uploads/<模板id>/<文件名>),HTML 里写 /uploads/<模板id>/<文件名>。 +// 对外输出时换成 /api/public/tplmedia/<模板id>/<文件名>,网站构建时下载到网站自己的 /media/,HTML 里再换成网站地址。 +// (复制模板时 HTML 可能引用原模板目录里的图片,所以不限定是当前模板的 id) +const HTML_IMG_RE = /\/uploads\/([A-Za-z0-9_-]+)\/([A-Za-z0-9_.-]+)/g; +const htmlImgRefs = (h) => [...String(h || '').matchAll(HTML_IMG_RE)].map((m) => `${m[1]}/${m[2]}`); +const tplHtmlRefs = (t) => [...tplDetailBlocks(t).flatMap((b) => htmlImgRefs(b.html)), ...tplHtmlsOf(t).flatMap((h) => htmlImgRefs(h.html))]; +const publicHtmlImgs = (h) => String(h).replace(HTML_IMG_RE, (m, id, f) => `${conf('PUBLIC_BASE_URL')}/api/public/tplmedia/${id}/${f}`); // 详情图片 + HTML 片段的顺序:片段位置按模板里的原始图片序号算,去掉的图片 / 片段跳过 function tplDetailSeq(t, p) { const all = tplImagesOf(t), keep = tplImagesFor(t, p), blocks = tplBlocksFor(t, p); const out = []; for (let i = 0; i <= all.length; i++) { - for (const b of blocks) if (Math.min(Math.max(0, Math.round(+b.pos || 0)), all.length) === i) out.push({ html: String(b.html).replace(/\s*\[cite:[^\]]*\]/gi, '') }); + for (const b of blocks) if (Math.min(Math.max(0, Math.round(+b.pos || 0)), all.length) === i) out.push({ html: publicHtmlImgs(String(b.html).replace(/\s*\[cite:[^\]]*\]/gi, '')) }); if (i < all.length && keep.includes(all[i])) out.push({ img: all[i] }); } return out; @@ -307,7 +380,7 @@ async function translateTemplateHtml(ai, products) { // ===== 网站产品介绍:没手动填英文介绍的产品,由 AI 根据参数自动写一段 ===== // 参数变了会重新生成(缓存按参数内容算);手动填了英文介绍就一直用手动的。 -const TYPE_EN = { '迷你UPS': 'Mini DC UPS', '离线式UPS': 'Offline UPS', '离线式锂电UPS': 'Lithium-ion offline UPS', '在线互动式UPS': 'Line-interactive UPS', '机架式UPS': 'Rack-mount online double-conversion UPS', '机架式锂电UPS': 'Lithium-ion rack-mount online double-conversion UPS', '模块化UPS': 'Modular online UPS with hot-swappable power modules', '机架式模块化UPS': 'Rack-mount modular online UPS with hot-swappable power modules', '工频UPS': 'Industrial-frequency (transformer-based) online UPS with output isolation transformer', '在线式高频UPS': 'Online double-conversion high-frequency tower UPS' }; +const TYPE_EN = { '迷你UPS': 'Mini DC UPS', '离线式UPS': 'Offline UPS', '离线式锂电UPS': 'Lithium-ion offline UPS', '在线互动式UPS': 'Line-interactive UPS', '机架式UPS': 'Rack-mount online double-conversion UPS', '机架式锂电UPS': 'Lithium-ion rack-mount online double-conversion UPS', '模块化UPS': 'Modular online UPS with hot-swappable power modules', '机架式模块化UPS': 'Rack-mount modular online UPS with hot-swappable power modules', '工频UPS': 'Industrial-frequency (transformer-based) online UPS with output isolation transformer', '在线式高频UPS': 'Online double-conversion high-frequency tower UPS', '太阳能混网逆变器': 'Hybrid solar inverter (solar + battery + grid, with backup output)', '并网/离网逆变器': 'Solar inverter (grid-tied / off-grid)', '机架式磷酸铁锂电池': 'Rack-mount LiFePO4 lithium battery module (19-inch, for solar energy storage and telecom)', '机架式锂电池': 'Rack-mount LiFePO4 lithium battery module (19-inch, for solar energy storage and telecom)' }; const DESC_SKIP = new Set(['model', 'brand', 'category', 'type', 'machineVariant', 'description', 'sale', 'youtube', 'sellingPoints', 'sockets', 'extraSpecs', 'pack']); const SOCKET_EN = { SCHUKO: 'Schuko', NEMA: 'NEMA 5-15', UK: 'BS 1363 (UK)', UNIVERSAL: 'universal', IEC: 'IEC C13', AU: 'Type I (AU/CN)' }; function descFacts(p) { @@ -572,9 +645,14 @@ module.exports = function mountPublicApi(app, { dataFile, uploadsDir, requireAut 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 || !tplFilesOf(t).includes(file) || !/^[A-Za-z0-9_-]+$/.test(tplId)) return res.status(404).end(); + if (!/^[A-Za-z0-9_-]+$/.test(tplId)) return res.status(404).end(); + const templates = readTemplates(dataFile); + const usedIds = new Set(published().flatMap((p) => (Array.isArray(p[TEMPLATE_IDS_FIELD]) ? p[TEMPLATE_IDS_FIELD] : []))); + const t = templates.find((x) => x.id === tplId); + // 模板自己的主图 / 详情图片;或者被已发布产品用到的模板,在 HTML 片段里引用了这个文件 + const ownFile = t && usedIds.has(tplId) && tplFilesOf(t).includes(file); + const inHtml = !ownFile && templates.some((x) => usedIds.has(x.id) && tplHtmlRefs(x).includes(`${tplId}/${file}`)); + if (!ownFile && !inHtml) return res.status(404).end(); res.sendFile(path.resolve(uploadsDir, tplId, file), (err) => { if (err) res.status(404).end(); }); });