From fb47fbbb2acafb7434fd210031da5eacf8bd9334 Mon Sep 17 00:00:00 2001 From: LiuEnder Date: Fri, 13 Mar 2026 17:16:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=B9=E6=AE=8A=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E9=80=BB=E8=BE=91=EF=BC=9B=E4=BF=AE=E6=AD=A3=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=8E=A8=E9=80=81=E4=BD=BF=E7=94=A8=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GoldPrice.js | 98 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 37 deletions(-) diff --git a/GoldPrice.js b/GoldPrice.js index 70aee36..f397955 100644 --- a/GoldPrice.js +++ b/GoldPrice.js @@ -294,17 +294,50 @@ function checkSpecialTargets(data, lastPrice, currentPrice) { return null; } - // 穿过监测:上一笔和当前价格在目标价两侧,就认为“穿过”该价格 + // 穿过监测:上一笔与当前价覆盖到目标价就算“穿过” + // 说明:用 <= / >= 包含“刚好等于目标价”的边界,避免 prev===t 时离开 t 不触发的问题 for (const t of targets) { const prev = lastPrice; const curr = currentPrice; - if ((prev < t && curr >= t) || (prev > t && curr <= t)) { + if ((prev <= t && curr >= t) || (prev >= t && curr <= t)) { return t; } } return null; } +function buildPushPayload({ + currentPrice, + delta1h, + change7, + trend7, + trend14, + trend28, + yesterdayClosingPrice, + todayOpeningPrice, + isSpecial, + isTest, +}) { + const title = isSpecial + ? `特殊提醒:黄金价格达到${formatNumber(currentPrice)}元/克` + : (isTest ? '黄金价格监控(测试)' : '黄金价格监控'); + + const tags = isTest ? '黄金价格|TEST' : '黄金价格'; + + const parts = []; + if (isSpecial) parts.push(`特殊提醒:黄金价格达到${formatNumber(currentPrice)}元/克;七日价格趋势:${trend7}`); + parts.push(`- 当前黄金价格为:${formatNumber(currentPrice)}元/克`); + parts.push(`- 一小时变化量为:${delta1h !== null ? formatNumber(delta1h) : '数据不足'}元/克`); + parts.push(`- 七日前价格为:${change7.price7DaysAgo !== null ? formatNumber(change7.price7DaysAgo) : '数据不足'}元/克;七日价格变化量为:${change7.change !== null ? formatNumber(change7.change) : '数据不足'}元/克`); + parts.push(`- 七日价格趋势为:${trend7}`); + parts.push(`- 十四日价格趋势为:${trend14}`); + parts.push(`- 二十八日价格趋势为:${trend28}`); + parts.push(`- 昨日收盘价格为:${yesterdayClosingPrice !== null ? formatNumber(yesterdayClosingPrice) : '数据不足'}元/克`); + parts.push(`- 今日开盘价格为:${todayOpeningPrice !== null ? formatNumber(todayOpeningPrice) : '数据不足'}元/克`); + + return { title, desp: parts.join(';\n'), tags }; +} + async function runCycle() { console.log('[gold] service starting...'); console.log('[gold] config:', { @@ -394,29 +427,20 @@ async function runCycle() { // 测试强制推送:无视触发条件与间隔限制,推送一次后退出 if (CONFIG.forcePushTest) { console.log('[gold] forcePushTest enabled: will push once and exit.'); - const title = `黄金价格监控(测试)`; - const parts = []; - parts.push(`[TEST] 当前黄金价格为:${formatNumber(currentPrice)}元/克`); - const oneHourAgoForTest = findDeltaWithin(data.priceHistory, CONFIG.changeWindowMinutes); - const delta1hForTest = oneHourAgoForTest ? Number((currentPrice - oneHourAgoForTest.price).toFixed(CONFIG.pricePrecisionDigits)) : null; - parts.push(`[TEST] 一小时变化量为:${delta1hForTest !== null ? formatNumber(delta1hForTest) : '数据不足'}元/克`); - const seriesForTest = extractSeries(data); - const change7ForTest = computeChangeAmount(seriesForTest, 7); - const trend7ForTest = seriesForTest.length >= 7 ? computeTrendType(seriesForTest.slice(-7)) : '数据不足'; - const trend14ForTest = seriesForTest.length >= 14 ? computeTrendType(seriesForTest.slice(-14)) : '数据不足'; - const trend28ForTest = seriesForTest.length >= 28 ? computeTrendType(seriesForTest.slice(-28)) : '数据不足'; - parts.push(`[TEST] 七日前价格为:${change7ForTest.price7DaysAgo !== null ? formatNumber(change7ForTest.price7DaysAgo) : '数据不足'}元/克`); - parts.push(`[TEST] 七日价格变化量为:${change7ForTest.change !== null ? formatNumber(change7ForTest.change) : '数据不足'}元/克`); - parts.push(`[TEST] 七日价格趋势为:${trend7ForTest}`); - parts.push(`[TEST] 十四日价格趋势为:${trend14ForTest}`); - parts.push(`[TEST] 二十八日价格趋势为:${trend28ForTest}`); - const yesterdayClosingPriceForTest = getYesterdayClosingPrice(data); - const todayOpeningPriceForTest = getTodayOpeningPrice(data); - parts.push(`[TEST] 昨日收盘价格为:${yesterdayClosingPriceForTest !== null ? formatNumber(yesterdayClosingPriceForTest) : '数据不足'}元/克`); - parts.push(`[TEST] 今日开盘价格为:${todayOpeningPriceForTest !== null ? formatNumber(todayOpeningPriceForTest) : '数据不足'}元/克`); - const desp = parts.join(';\n'); + const payload = buildPushPayload({ + currentPrice, + delta1h, + change7, + trend7, + trend14, + trend28, + yesterdayClosingPrice, + todayOpeningPrice, + isSpecial: false, + isTest: true, + }); try { - const resp = await sendServerChan(title, desp, '黄金价格|TEST'); + const resp = await sendServerChan(payload.title, payload.desp, payload.tags); console.log('[gold] [TEST] push sent. status:', resp && resp.statusCode); data.lastPushTs = nowTs(); writeData(data); @@ -454,20 +478,20 @@ async function runCycle() { const anyTrigger = shouldPushByDelta || shouldPushBySpecial || shouldPushByClosing; if (anyTrigger && (canPushByInterval || bypassIntervalBySpecial) && (!inDisabledWindow || bypassDisabledBySpecial)) { - const title = shouldPushBySpecial ? `特殊提醒:黄金价格达到${formatNumber(currentPrice)}元/克` : `黄金价格监控`; - const parts = []; - parts.push(`- 当前黄金价格为:${formatNumber(currentPrice)}元/克`); - parts.push(`- 一小时变化量为:${delta1h !== null ? formatNumber(delta1h) : '数据不足'}元/克`); - parts.push(`- 七日前价格为:${change7.price7DaysAgo !== null ? formatNumber(change7.price7DaysAgo) : '数据不足'}元/克;七日价格变化量为:${change7.change !== null ? formatNumber(change7.change) : '数据不足'}元/克`); - parts.push(`- 七日价格趋势为:${trend7}`); - parts.push(`- 十四日价格趋势为:${trend14}`); - parts.push(`- 二十八日价格趋势为:${trend28}`); - parts.push(`- 昨日收盘价格为:${yesterdayClosingPrice !== null ? formatNumber(yesterdayClosingPrice) : '数据不足'}元/克`); - parts.push(`- 今日开盘价格为:${todayOpeningPrice !== null ? formatNumber(todayOpeningPrice) : '数据不足'}元/克`); - if (shouldPushBySpecial) parts.unshift(`特殊提醒:黄金价格达到${formatNumber(currentPrice)}元/克;七日价格趋势:${trend7}`); - const desp = parts.join(';\n'); + const payload = buildPushPayload({ + currentPrice, + delta1h, + change7, + trend7, + trend14, + trend28, + yesterdayClosingPrice, + todayOpeningPrice, + isSpecial: shouldPushBySpecial, + isTest: false, + }); try { - const resp = await sendServerChan(title, desp, '黄金价格'); + const resp = await sendServerChan(payload.title, payload.desp, payload.tags); console.log('[gold] push sent. status:', resp && resp.statusCode); // 更新最后推送时间:特殊推送不更新普通推送时间戳 data.lastPushTs = now;