修改特殊推送逻辑;修正测试推送使用函数。

This commit is contained in:
LiuEnder
2026-03-13 17:16:41 +08:00
parent 72e032980b
commit fb47fbbb2a

View File

@@ -294,17 +294,50 @@ function checkSpecialTargets(data, lastPrice, currentPrice) {
return null; return null;
} }
// 穿过监测:上一笔当前价格在目标价两侧,就认为“穿过”该价格 // 穿过监测:上一笔当前价覆盖到目标价就算“穿过”
// 说明:用 <= / >= 包含“刚好等于目标价”的边界,避免 prev===t 时离开 t 不触发的问题
for (const t of targets) { for (const t of targets) {
const prev = lastPrice; const prev = lastPrice;
const curr = currentPrice; const curr = currentPrice;
if ((prev < t && curr >= t) || (prev > t && curr <= t)) { if ((prev <= t && curr >= t) || (prev >= t && curr <= t)) {
return t; return t;
} }
} }
return null; 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() { async function runCycle() {
console.log('[gold] service starting...'); console.log('[gold] service starting...');
console.log('[gold] config:', { console.log('[gold] config:', {
@@ -394,29 +427,20 @@ async function runCycle() {
// 测试强制推送:无视触发条件与间隔限制,推送一次后退出 // 测试强制推送:无视触发条件与间隔限制,推送一次后退出
if (CONFIG.forcePushTest) { if (CONFIG.forcePushTest) {
console.log('[gold] forcePushTest enabled: will push once and exit.'); console.log('[gold] forcePushTest enabled: will push once and exit.');
const title = `黄金价格监控(测试)`; const payload = buildPushPayload({
const parts = []; currentPrice,
parts.push(`[TEST] 当前黄金价格为:${formatNumber(currentPrice)}元/克`); delta1h,
const oneHourAgoForTest = findDeltaWithin(data.priceHistory, CONFIG.changeWindowMinutes); change7,
const delta1hForTest = oneHourAgoForTest ? Number((currentPrice - oneHourAgoForTest.price).toFixed(CONFIG.pricePrecisionDigits)) : null; trend7,
parts.push(`[TEST] 一小时变化量为:${delta1hForTest !== null ? formatNumber(delta1hForTest) : '数据不足'}元/克`); trend14,
const seriesForTest = extractSeries(data); trend28,
const change7ForTest = computeChangeAmount(seriesForTest, 7); yesterdayClosingPrice,
const trend7ForTest = seriesForTest.length >= 7 ? computeTrendType(seriesForTest.slice(-7)) : '数据不足'; todayOpeningPrice,
const trend14ForTest = seriesForTest.length >= 14 ? computeTrendType(seriesForTest.slice(-14)) : '数据不足'; isSpecial: false,
const trend28ForTest = seriesForTest.length >= 28 ? computeTrendType(seriesForTest.slice(-28)) : '数据不足'; isTest: true,
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');
try { 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); console.log('[gold] [TEST] push sent. status:', resp && resp.statusCode);
data.lastPushTs = nowTs(); data.lastPushTs = nowTs();
writeData(data); writeData(data);
@@ -454,20 +478,20 @@ async function runCycle() {
const anyTrigger = shouldPushByDelta || shouldPushBySpecial || shouldPushByClosing; const anyTrigger = shouldPushByDelta || shouldPushBySpecial || shouldPushByClosing;
if (anyTrigger && (canPushByInterval || bypassIntervalBySpecial) && (!inDisabledWindow || bypassDisabledBySpecial)) { if (anyTrigger && (canPushByInterval || bypassIntervalBySpecial) && (!inDisabledWindow || bypassDisabledBySpecial)) {
const title = shouldPushBySpecial ? `特殊提醒:黄金价格达到${formatNumber(currentPrice)}元/克` : `黄金价格监控`; const payload = buildPushPayload({
const parts = []; currentPrice,
parts.push(`- 当前黄金价格为:${formatNumber(currentPrice)}元/克`); delta1h,
parts.push(`- 一小时变化量为:${delta1h !== null ? formatNumber(delta1h) : '数据不足'}元/克`); change7,
parts.push(`- 七日前价格为:${change7.price7DaysAgo !== null ? formatNumber(change7.price7DaysAgo) : '数据不足'}元/克;七日价格变化量为:${change7.change !== null ? formatNumber(change7.change) : '数据不足'}元/克`); trend7,
parts.push(`- 七日价格趋势为:${trend7}`); trend14,
parts.push(`- 十四日价格趋势为:${trend14}`); trend28,
parts.push(`- 二十八日价格趋势为:${trend28}`); yesterdayClosingPrice,
parts.push(`- 昨日收盘价格为:${yesterdayClosingPrice !== null ? formatNumber(yesterdayClosingPrice) : '数据不足'}元/克`); todayOpeningPrice,
parts.push(`- 今日开盘价格为:${todayOpeningPrice !== null ? formatNumber(todayOpeningPrice) : '数据不足'}元/克`); isSpecial: shouldPushBySpecial,
if (shouldPushBySpecial) parts.unshift(`特殊提醒:黄金价格达到${formatNumber(currentPrice)}元/克;七日价格趋势:${trend7}`); isTest: false,
const desp = parts.join(';\n'); });
try { 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); console.log('[gold] push sent. status:', resp && resp.statusCode);
// 更新最后推送时间:特殊推送不更新普通推送时间戳 // 更新最后推送时间:特殊推送不更新普通推送时间戳
data.lastPushTs = now; data.lastPushTs = now;