feat: 阶梯提醒新增强制提醒幅度,偏离够大时忽略连续确认

- 阶梯规则新增 forceAlertValue:相对基准偏离 ≥ 该幅度就直接提醒,不再检查连续次数,
  避免大幅波动被「连续同向确认」挡掉;填 0 关闭,小于步长时按 max(步长, 幅度) 处理
- 触发判定拆成两条通道:常规(步长 + 连续确认)与强制(幅度),任一命中即提醒;
  冷却与生效时段对两条通道都生效
- 提醒正文区分触发方式:强制通道写明「已超过强制提醒幅度 X,忽略连续确认直接提醒」,
  并带出当前连续次数,便于事后回溯
- 配置归一化与 POST 接口接受 forceAlertValue(仅阶梯类型生效,其它类型固定 0)
- 弹窗新增「强制提醒幅度」字段(元/克 或 % 跟随口径切换)、0 值与负数校验,
  预览条件带「偏离 ≥ X 直接提醒(忽略连续)」,提示文案与 README 同步补充例子
This commit is contained in:
2026-11-25 15:13:26 +08:00
parent a0ab746487
commit d8788ae361
6 changed files with 81 additions and 28 deletions
+28 -4
View File
@@ -448,7 +448,13 @@ function ruleConditionText(rule) {
const dirText = rule.direction === 'down' ? '每下跌' : rule.direction === 'both' ? '每涨/跌' : '每上涨';
// 连续确认次数 > 1 时,把「还要连续几次同向」写进条件描述
const streakText = Number(rule.ladderStreak) > 1 ? `,需连续 ${Math.round(Number(rule.ladderStreak))} 次同向` : '';
return `${left.name} ${dirText} ${valueText} 提醒一次${streakText}(基价随提醒更新)${rangeSuffix}`;
// 强制提醒幅度:比步长大时才单独提一句(后端取 max(步长, 强制幅度))
const stepAbs = Math.abs(Number(rule.value) || 0);
const forceAbs = Math.abs(Number(rule.forceAlertValue) || 0);
const forceText = forceAbs > stepAbs
? `,偏离 ≥ ${isAmount ? `${fmt(forceAbs, left.digits)} ${left.unit}` : `${forceAbs.toFixed(2)}%`} 直接提醒(忽略连续)`
: '';
return `${left.name} ${dirText} ${valueText} 提醒一次${streakText}${forceText}(基价随提醒更新)${rangeSuffix}`;
}
if (rule.type === 'schedule') {
// 定时提醒:重复方式 + 时刻,必要时带上每周哪几天
@@ -935,8 +941,12 @@ function syncRuleFields() {
document.getElementById('ruleCooldownField').hidden = isSchedule || isFailure;
// 阶梯提醒按步长计算,没有「持续满足重复提醒」的概念(重复字段收起)
document.getElementById('ruleRepeatField').hidden = isSchedule || type === 'ladder';
// 阶梯提醒专用:连续同向确认次数
// 阶梯提醒专用:连续同向确认次数 / 强制提醒幅度
document.getElementById('ruleStreakField').hidden = type !== 'ladder';
document.getElementById('ruleForceField').hidden = type !== 'ladder';
document.getElementById('ruleForceLabel').textContent = isAmount
? `强制提醒幅度(${unit})`
: '强制提醒幅度(%)';
// 重复间隔文案:价格类规则是「持续满足」,失败告警是「持续失败」
document.getElementById('ruleRepeatLabel').textContent = isFailure
? '持续失败时每 N 秒重复提醒(0 只提醒一次)'
@@ -1045,8 +1055,9 @@ function updateRuleSummary() {
compareInstrumentId: document.getElementById('ruleCompare').value,
mode: document.getElementById('ruleMode').value,
changeBasis: document.getElementById('ruleBasis').value,
// 阶梯提醒的连续确认次数也进预览文案
// 阶梯提醒的连续确认次数、强制提醒幅度也进预览文案
ladderStreak: Number(document.getElementById('ruleStreak').value) || 1,
forceAlertValue: Number(document.getElementById('ruleForce').value) || 0,
direction: document.getElementById('ruleDirection').value,
value: Number(document.getElementById('ruleValue').value) || 0,
cooldownSec: Number(document.getElementById('ruleCooldown').value) || 0,
@@ -1101,8 +1112,9 @@ function openRuleModal(id) {
document.getElementById('ruleMode').value = rule?.mode || 'abs';
document.getElementById('ruleBasis').value = rule?.changeBasis === 'amount' ? 'amount' : 'percent';
document.getElementById('ruleValue').value = rule?.value ?? '';
// 阶梯提醒的连续确认次数,默认 1(只按步长判断)
// 阶梯提醒的连续确认次数,默认 1(只按步长判断);强制提醒幅度默认 0(不启用)
document.getElementById('ruleStreak').value = rule?.ladderStreak ?? 1;
document.getElementById('ruleForce').value = rule?.forceAlertValue ?? 0;
document.getElementById('ruleCooldown').value = rule?.cooldownSec ?? 1800;
document.getElementById('ruleRepeat').value = rule?.repeatIntervalSec ?? 0;
@@ -1198,6 +1210,12 @@ async function saveRule() {
setResult('ruleResult', '连续确认次数请填 1–20(填 1 表示只按步长判断)', false);
return;
}
// 强制提醒幅度:0 = 不启用;小于步长时后端按步长算,不报错
const force = Number(document.getElementById('ruleForce').value);
if (!Number.isFinite(force) || force < 0) {
setResult('ruleResult', '强制提醒幅度请填 0 或正数(0 表示不启用)', false);
return;
}
}
const inst = instruments.find((i) => i.id === instrumentId);
@@ -1233,6 +1251,8 @@ async function saveRule() {
changeBasis: document.getElementById('ruleBasis').value,
// 阶梯提醒的名称里带上连续确认次数,便于区分同一步长的多条规则
ladderStreak: Number(document.getElementById('ruleStreak').value) || 1,
// 名称里也带上强制幅度(>0 才算)
forceAlertValue: Number(document.getElementById('ruleForce').value) || 0,
windowSec, activeRange,
// 失败告警的名称里也带上重复间隔,避免多条同类规则名字撞车
repeatIntervalSec: Number(document.getElementById('ruleRepeat').value) || 0,
@@ -1248,6 +1268,8 @@ async function saveRule() {
changeBasis: (type === 'change' || type === 'ladder') ? document.getElementById('ruleBasis').value : 'percent',
// 阶梯提醒的连续确认次数(仅阶梯用,其它类型固定 1)
ladderStreak: type === 'ladder' ? (Number(document.getElementById('ruleStreak').value) || 1) : 1,
// 阶梯提醒的强制提醒幅度(仅阶梯用,0 = 不启用)
forceAlertValue: type === 'ladder' ? (Number(document.getElementById('ruleForce').value) || 0) : 0,
direction: type === 'schedule' ? 'abs' : direction,
value: type === 'schedule' ? 0 : value,
// 失败告警不用冷却,冷却置 0,节流交给「持续失败重复间隔」
@@ -1393,6 +1415,8 @@ function bindEvents() {
document.getElementById('ruleRepeat').addEventListener('input', updateRuleSummary);
// 阶梯提醒的连续确认次数改动同理
document.getElementById('ruleStreak').addEventListener('input', updateRuleSummary);
// 强制提醒幅度改动也要立刻反映到预览
document.getElementById('ruleForce').addEventListener('input', updateRuleSummary);
// 统计区间与生效时段:改动后同步字段显隐和预览
document.getElementById('ruleWindow').addEventListener('change', () => {
syncWindowCustom();
+7 -3
View File
@@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>金价提醒 · Gold Price Watcher</title>
<link rel="stylesheet" href="/styles.css?v=20261124y" />
<link rel="stylesheet" href="/styles.css?v=20261124z" />
</head>
<body>
<header class="topbar">
@@ -329,6 +329,10 @@
<label class="field" id="ruleStreakField" hidden><span>连续同向确认次数(次)</span>
<input type="number" id="ruleStreak" min="1" max="20" step="1" value="1" />
</label>
<!-- 阶梯提醒专用:强制提醒幅度(偏离超过它就一定提醒,忽略连续确认;0 = 不启用) -->
<label class="field" id="ruleForceField" hidden><span id="ruleForceLabel">强制提醒幅度(元/克)</span>
<input type="number" id="ruleForce" min="0" step="0.001" placeholder="0(不启用)" value="0" />
</label>
<!-- 4) 生效与提醒频率 -->
<!-- 生效时段:留空表示全天,支持跨零点(如 22:00 ~ 02:00) -->
<label class="field wide" id="ruleActiveRangeField"><span>生效时段(可选,留空表示全天)</span>
@@ -376,7 +380,7 @@
<p class="hint" id="ruleSummary"></p>
<p class="hint" id="ruleWindowHint">提示:统计区间选「日内」时,涨跌幅相对昨收计算;选「近 N 分钟」时,用本机缓存的行情点回算滚动区间,重启后会接着用已保存的历史。</p>
<p class="hint" id="ruleScheduleHint" hidden>提示:定时提醒会在指定时刻按当时的行情发一条通知,不受价格条件影响;同一时刻每天只响一次,程序当时没运行则不会补发。</p>
<p class="hint" id="ruleLadderHint" hidden>提示:阶梯提醒以「上次提醒时的价格」为基准,价格每偏离一个步长就提醒一次,随后基准更新为当前价。例如步长 2 元/克:782 提醒后基准变 782,涨到 784 再提醒;价格回落不会提醒,也不会重置基准。「连续同向确认次数」填 1 表示只按步长判断;填 3 则还要求连续 3 次刷新都是同向变动(每次刷新比上一次高/低),中途反向会重新计数,用来过滤一两笔跳动的假突破。</p>
<p class="hint" id="ruleLadderHint" hidden>提示:阶梯提醒以「上次提醒时的价格」为基准,价格每偏离一个步长就提醒一次,随后基准更新为当前价。例如步长 2 元/克:782 提醒后基准变 782,涨到 784 再提醒;价格回落不会提醒,也不会重置基准。「连续同向确认次数」填 1 表示只按步长判断;填 3 则还要求连续 3 次刷新都是同向变动(每次刷新比上一次高/低),中途反向会重新计数,用来过滤一两笔跳动的假突破。「强制提醒幅度」填一个比步长更大的值(如 3 元/克):只要偏离超过它,就直接提醒,不再管连续次数,避免大幅波动被连续确认挡住;填 0 表示不启用。冷却时间对两者都生效。</p>
<p class="hint" id="ruleFailureHint" hidden>提示:请求失败告警盯的是「数据源还能不能取到价」,与价格高低无关。连续查询失败达到设定次数时提醒一次;一旦某次抓取成功,失败计数清零,下次再连续失败达到次数还会重新提醒。数据源长时间不可用时,可在「持续失败时每 N 秒重复提醒」里填间隔,避免只响一次被忽略。</p>
</div>
<div class="modal-foot">
@@ -407,7 +411,7 @@
</div>
<div class="toast-wrap" id="toastWrap"></div>
<script src="/app.js?v=20261124y"></script>
<script src="/app.js?v=20261124z"></script>
</body>
</html>