feat(代理): 多套餐余量预警与用户级 jikip 套餐配置

平台自费池由单个 planId/userId 改为 planId:userId 列表逐套餐探测,
套餐余量或账户余额低于阈值时推管理员预警;用户可在密钥设置里为自己
配置多个套餐,巡检跟随密钥检查按用户查余量并推站内通知,欠费状态在
客户端右上角常驻提示(V160 新建用户级套餐表)。

同时把测试里写死的真实代理 key 换成占位串,避免凭证随仓库扩散。
This commit is contained in:
2026-10-21 13:32:56 +08:00
parent 19a291ec39
commit f6aa04d0f2
31 changed files with 1544 additions and 52 deletions
@@ -64,6 +64,9 @@ public class NotificationScanScheduler {
private static final DateTimeFormatter DAY_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd");
/** 「jikip 套餐信息未配置」的日志日期(按天只提示一次,避免每 5 分钟刷一条)。 */
private String jikipUnconfiguredWarnDay;
/**
* 任务模块类型 → 中文名:直接取模块注册表(G6),新增模块只需在
* {@code TaskModuleRegistry} 追加一行,不再需要同步这份表。
@@ -238,25 +241,102 @@ public class NotificationScanScheduler {
}
}
/** jikip 探测:复用余额查询接口;未配置套餐信息时跳过(返回 null 不告警)。 */
/** jikip 探测:逐套餐复用余额查询接口;未配置任何套餐时跳过(返回 null 不告警)。 */
private ProbeOutcome probeJikip() {
try {
ProxyBalancePort.ProxyBalanceSnapshot balance = proxyBalancePort.fetchBalance();
if (balance.available()) {
return ProbeOutcome.alive("余额接口可用 surplus=" + balance.surplus()
+ " balance=" + balance.balance());
}
String message = balance.message() == null ? "" : balance.message();
if (message.contains("未配置")) {
log.info("[notification-scan] jikip 探测跳过:{}", message);
List<ProxyBalancePort.ProxyBalanceEntry> entries = proxyBalancePort.fetchBalances();
if (entries.isEmpty()) {
warnJikipUnconfiguredOncePerDay();
return null;
}
return ProbeOutcome.down(message.isEmpty() ? "余额接口不可用" : message);
List<String> alive = new ArrayList<>();
List<String> failures = new ArrayList<>();
for (ProxyBalancePort.ProxyBalanceEntry entry : entries) {
ProxyBalancePort.ProxyBalanceSnapshot balance = entry.snapshot();
String message = balance.message() == null ? "" : balance.message();
if (balance.available()) {
warnIfLowBalance(entry.planId(), balance);
alive.add("套餐" + entry.planId() + " surplus=" + balance.surplus()
+ " balance=" + balance.balance());
continue;
}
if (!message.contains("未配置")) {
failures.add("套餐" + entry.planId() + ":"
+ (message.isEmpty() ? "余额接口不可用" : message));
}
}
if (!alive.isEmpty()) {
if (!failures.isEmpty()) {
log.warn("[notification-scan] jikip 部分套餐余量查询失败: {}",
String.join(";", failures));
}
return ProbeOutcome.alive(String.join(";", alive));
}
if (failures.isEmpty()) {
warnJikipUnconfiguredOncePerDay();
return null;
}
return ProbeOutcome.down(String.join(";", failures));
} catch (Exception ex) {
return ProbeOutcome.down(rootCauseMessage(ex));
}
}
/**
* 余额/套餐余量低于阈值时推管理员预警——这是「欠费导致代理全盘停摆」唯一的事前信号。
* 每个套餐独立判定、独立去重:一个套餐见底不影响另一个套餐的判定。
*/
void warnIfLowBalance(String planId, ProxyBalancePort.ProxyBalanceSnapshot balance) {
warnBelowThreshold(planId, balance.surplus(), properties.getJikipLowSurplusThreshold(),
"jikip 代理套餐余量不足", "套餐剩余 IP 余量", "jikip_low_surplus");
warnBelowThreshold(planId, balance.balance(), properties.getJikipLowBalanceThreshold(),
"jikip 代理账户余额不足", "账户余额", "jikip_low_balance");
}
private void warnBelowThreshold(String planId, String raw, long threshold, String title,
String label, String dedupePrefix) {
if (threshold <= 0) {
return;
}
Long value = firstNumber(raw);
if (value == null || value > threshold) {
return;
}
log.warn("[notification-scan] jikip 预警 planId={} {}={} 阈值={},耗尽后平台自费代理将停摆",
planId, label, value, threshold);
notificationDispatchService.pushToAdmins(NotificationService.SCENE_SERVICE_DOWN,
NotificationService.LEVEL_WARNING, title,
"套餐 " + planId + " 的" + label + "仅剩 " + value + "(预警阈值 " + threshold
+ ")。耗尽后平台自费代理会全盘停摆,请立即续费或补一把独立账号的钥匙。",
dedupePrefix + ":" + planId + ":" + dayOf(LocalDateTime.now()), null);
}
/** 供应商字段可能是 "123" 或 "123次":取首个整数;纯文本(如"充足")返回 null。 */
static Long firstNumber(String raw) {
if (raw == null) {
return null;
}
java.util.regex.Matcher matcher = java.util.regex.Pattern.compile("\\d+").matcher(raw);
if (!matcher.find()) {
return null;
}
String digits = matcher.group();
// 18 位以内必在 long 范围内;再长说明不是余量数字(不抛异常,静默 catch 会涨棘轮)
return digits.length() <= 18 ? Long.parseLong(digits) : null;
}
/** 套餐信息未配置 = 余量预警根本没生效:日志按天提示一次,别让「静默跳过」看起来像没事。 */
private void warnJikipUnconfiguredOncePerDay() {
String today = dayOf(LocalDateTime.now());
if (today.equals(jikipUnconfiguredWarnDay)) {
return;
}
jikipUnconfiguredWarnDay = today;
log.warn("[notification-scan] jikip 余量预警未启用:未配置代理套餐信息"
+ "(格式 planId:userId;planId:userId,多个套餐用分号分隔;"
+ "未配置时只能在任务停滞事后发现)");
}
/** 探测结果上报:正常记 info;故障按自然日去重推管理员通知;outcome=null 表示跳过。 */
private void reportProbe(String serviceKey, String serviceLabel, ProbeOutcome outcome) {
if (outcome == null) {
@@ -1,5 +1,7 @@
package com.nanri.aiimage.modules.notification.spi;
import java.util.List;
/**
* 代理余额探测(2026-09 边界收敛:notification → usersecret 的类依赖改为端口)。
*
@@ -9,11 +11,15 @@ package com.nanri.aiimage.modules.notification.spi;
public interface ProxyBalancePort {
/**
* 查询代理服务商余额。
* 逐套餐查询代理服务商余额;未配置任何套餐时返回空列表。
*
* @return 余额快照;调用异常由实现方抛出(调用方负责降级)
* @return 每个套餐一条快照;单条失败由实现方降级成 {@code available=false},不中断其余套餐
*/
ProxyBalanceSnapshot fetchBalance();
List<ProxyBalanceEntry> fetchBalances();
/** 套餐 id + 该套餐的余额快照。 */
record ProxyBalanceEntry(String planId, ProxyBalanceSnapshot snapshot) {
}
/**
* @param available 余额接口是否可用