refactor(代理): 用户余量巡检与长样式出件,压回可读性棘轮

推送闸门抓到 UserApiSecretService 1278→1408、两枚 Vue 超过 800 行:
把用户级余量查询/缓存/巡检拆到 UserProxyBalanceService,两端组件的大段
样式移到同名 .css 由 scoped src 挂载。行为与选择器不变,只为让长文件棘轮
回到基线以内(不调基线、不整份回写)。
This commit is contained in:
2026-10-21 14:26:03 +08:00
parent 86d78160fd
commit 6096e85d32
10 changed files with 1237 additions and 1199 deletions
@@ -97,7 +97,7 @@ class UserApiSecretServiceTest {
valueOps = mockValueOps();
when(stringRedisTemplate.opsForValue()).thenReturn(valueOps);
return new UserApiSecretService(
mapper, userProxyPlanService, crypto, checkService, jikipProxyClient, adminUserMapper, adminAuthSupport,
mapper, crypto, checkService, adminUserMapper, adminAuthSupport,
adminGroupMapper, userDataScopeSupport, notificationDispatchService, userSecretProperties,
stringRedisTemplate, objectMapper);
}
@@ -1103,13 +1103,13 @@ class UserApiSecretServiceTest {
/** 用户填了自己的套餐后,余量必须查自己的 planId/userId,不能回落到平台套餐。 */
@Test
void proxyBalanceUsesUserPlanAndFlagsLowSurplus() {
UserApiSecretService service = newService();
UserProxyBalanceService service = newBalanceService();
UserProxyPlanEntity plan = proxyPlan(7L, "20261010593613392231", "626710989815");
when(userProxyPlanService.getEntity(7L)).thenReturn(plan);
JikipProxyClient.PlanRef ref = new JikipProxyClient.PlanRef(plan.getPlanId(), plan.getJikipUserId());
when(jikipProxyClient.fetchBalance(ref)).thenReturn(proxyBalance("80", "10"));
UserApiSecretBalanceVo result = service.proxyBalance(7L);
UserApiSecretBalanceVo result = service.balance(7L);
assertThat(result.getScope()).isEqualTo("user");
assertThat(result.getLow()).isTrue();
@@ -1120,11 +1120,11 @@ class UserApiSecretServiceTest {
/** 没配套餐的用户看平台余量,但不参与用户级报警(平台告警归管理员巡检)。 */
@Test
void proxyBalanceFallsBackToPlatformWithoutUserPlan() {
UserApiSecretService service = newService();
UserProxyBalanceService service = newBalanceService();
when(userProxyPlanService.getEntity(7L)).thenReturn(null);
when(jikipProxyClient.fetchBalance()).thenReturn(proxyBalance("5000", "500"));
UserApiSecretBalanceVo result = service.proxyBalance(7L);
UserApiSecretBalanceVo result = service.balance(7L);
assertThat(result.getScope()).isEqualTo("platform");
assertThat(result.getLow()).isFalse();
@@ -1134,14 +1134,14 @@ class UserApiSecretServiceTest {
/** 余额耗尽必须报警,哪怕管理员把余额预警阈值配成 0(默认关闭)——「欠费了要一直警告」是需求原话。 */
@Test
void proxyBalanceFlagsExhaustedBalanceEvenWhenThresholdDisabled() {
UserApiSecretService service = newService();
UserProxyBalanceService service = newBalanceService();
userSecretProperties.setProxyBalanceLowBalanceThreshold(0);
UserProxyPlanEntity plan = proxyPlan(7L, "p-1", "u-1");
when(userProxyPlanService.getEntity(7L)).thenReturn(plan);
when(jikipProxyClient.fetchBalance(new JikipProxyClient.PlanRef("p-1", "u-1")))
.thenReturn(proxyBalance(null, "0"));
UserApiSecretBalanceVo result = service.proxyBalance(7L);
UserApiSecretBalanceVo result = service.balance(7L);
assertThat(result.getLow()).isTrue();
assertThat(result.getLowReason()).contains("已耗尽");
@@ -1150,29 +1150,29 @@ class UserApiSecretServiceTest {
/** 客户端会周期性轮询:5 分钟内复用缓存;改/清套餐后主动失效,下一次立即按新配置查。 */
@Test
void proxyBalanceCachesPerUserAndInvalidatesOnPlanChange() {
UserApiSecretService service = newService();
UserProxyBalanceService service = newBalanceService();
UserProxyPlanEntity plan = proxyPlan(7L, "p-1", "u-1");
when(userProxyPlanService.getEntity(7L)).thenReturn(plan);
JikipProxyClient.PlanRef ref = new JikipProxyClient.PlanRef("p-1", "u-1");
when(jikipProxyClient.fetchBalance(ref)).thenReturn(proxyBalance("5000", "500"));
service.proxyBalance(7L);
service.proxyBalance(7L);
service.balance(7L);
service.balance(7L);
verify(jikipProxyClient, times(1)).fetchBalance(ref);
service.invalidateProxyBalance(7L);
service.proxyBalance(7L);
service.invalidate(7L);
service.balance(7L);
verify(jikipProxyClient, times(2)).fetchBalance(ref);
}
/** 站内通知总开关关闭时连查都不查:余量巡检的唯一产出就是通知,查了也没处送。 */
@Test
void scheduledProxyBalanceSkipsWhenNotificationsDisabled() {
UserApiSecretService service = newService();
UserProxyBalanceService service = newBalanceService();
userSecretProperties.setNotifyEnabled(false);
UserApiSecretService.ProxyBalanceCheckSummary summary =
service.checkUserProxyBalancesForScheduler(2000, 10);
UserProxyBalanceService.Summary summary =
service.checkAll(2000, 10);
assertThat(summary.users()).isZero();
assertThat(summary.checked()).isZero();
@@ -1182,7 +1182,7 @@ class UserApiSecretServiceTest {
/** 巡检按用户逐个查:只给低余量用户推通知,正常用户不打扰,去重键按用户+自然日。 */
@Test
void scheduledProxyBalanceNotifiesOnlyLowUsers() {
UserApiSecretService service = newService();
UserProxyBalanceService service = newBalanceService();
when(userProxyPlanService.listAll(2000)).thenReturn(List.of(
proxyPlan(7L, "p-low", "u-low"), proxyPlan(8L, "p-ok", "u-ok")));
when(jikipProxyClient.fetchBalance(new JikipProxyClient.PlanRef("p-low", "u-low")))
@@ -1192,8 +1192,8 @@ class UserApiSecretServiceTest {
when(notificationDispatchService.pushToUser(anyLong(), anyString(), anyString(), anyString(),
anyString(), anyString())).thenReturn(true);
UserApiSecretService.ProxyBalanceCheckSummary summary =
service.checkUserProxyBalancesForScheduler(2000, 10);
UserProxyBalanceService.Summary summary =
service.checkAll(2000, 10);
assertThat(summary.users()).isEqualTo(2);
assertThat(summary.checked()).isEqualTo(2);
@@ -1207,6 +1207,11 @@ class UserApiSecretServiceTest {
anyString(), anyString(), anyString());
}
private UserProxyBalanceService newBalanceService() {
return new UserProxyBalanceService(userProxyPlanService, jikipProxyClient,
notificationDispatchService, userSecretProperties);
}
private UserProxyPlanEntity proxyPlan(long userId, String planId, String jikipUserId) {
UserProxyPlanEntity plan = new UserProxyPlanEntity();
plan.setUserId(userId);