feat(账号到期+公司超管): 级联过期判定、过期停任务巡检与两端续费提醒弹窗

需求两条:站点超管可开设其他公司的超级管理员(站点超管看全站、公司超管只看本公司、
组内管理员仍只看本组);账号要有过期时间,管理员过期后名下普通账户一并停用、
正在跑的任务也要停掉。落库只有 users.expires_at(V146,NULL=永不过期),级联一律现算,
不写"上级过期就把下级置成过期"那种续费也救不回来的数据。

- AccountExpiryService:生效到期 = min(自身, 本公司公司超管, 成员表组长, 创建人)。
  登录拦 403(文案给续费提示)、每次请求鉴权拦 401(前端按下线踢回登录页),super_admin
  永不适用。级联口径与数据可见范围同源(成员表基本未用、实际按 created_by_id 派生)。
- ExpiredAccountTaskStopService:每分钟把过期账号(含级联成员)名下 biz_file_task
  PENDING/SCHEDULED/RUNNING → FAILED,含未领取的 PENDING(防内部通道继续派发)。原因文案
  刻意不以「客户端异常中断」开头,否则会被 TaskResumeService 自动复活。品牌采集与跟价循环
  不在那张表上,经 ExpiredAccountTaskStopSpi 由各自模块停:品牌 pending/running→cancelled
  且 UPDATE 的 WHERE 二次核对状态(抢到行后被客户端推进成完成的行不得误杀),跟价循环只置
  stop_requested=1、沿用用户点「停止循环」那条收尾路径,绝不动 status。任务心跳接口保持匿名
  不走 requireUser,所以过期账号的客户端仍能拿到 alive=false 协作停止。
- 角色 company_super_admin:由站点超管开设(唯一要显式选公司的建号场景),建号/授菜单/改用户/
  看数据四条链路都按"本公司"收口;公司超管之间、以及公司超管与站点超管互相碰不得。
- 续费提醒:剩余 <5 天在数富后台与用户端弹窗,只能收起不能关闭(无关闭按钮,收起态是右下角
  浮标),续费到 ≥5 天自动消失。后台读 /api/admin/current-user 的 expiresAt/daysToExpire;
  用户端无 Pinia,走 localStorage 的 account_expires_at(登录/check_login 落键、被顶下线清键,
  换账号不残留)。只反映账号自身到期,弹窗不做任何级联推断。
- V146 撞掉了 V145 那批的一条护栏:它按"db/ 里不许出现 V146__ 前缀"编死号位,被一条正常的
  前向迁移占用就红。改成钉动作——剥注释后的可执行 SQL 不得写 flyway_schema_history(回滚
  一律是扫描路径外的人工补丁),并把盘点文档改 V146 时丢掉的 V145 文件名补回去。

验证(本会话亲跑,不是转述):后端定向 14 类 123/0(同一轮 mvn 里连批次③的 4 类 53 例、以及既有 ShopDataCrawl/SimilarAsin 两条回归对照 21 例一起跑,合计 197 例 0 红);admin-frontend-vue 本批定向 44/0(弹窗 7 +
公司建号/列表/入口/日期默认 21 + 角色 task-48/task-251 16),全量 1726 例里那 1 支红是另一条
会话刻意留的「公司 tab 接线缺口」尺子(点名 admin_task_center 等 6 页,与本批无关);
frontend-vue 弹窗 9/0、全量 710/0。变异自证两批共 34 点:弹窗语义 16 点全杀(阈值 5、只能收起
不能关闭、挂载点含"注释掉挂载"、落键/清键链路),本批 SPI+迁移护栏 17 点全杀(含 G2 反向:
前向迁移只在注释里提 flyway_schema_history 必须照样绿),注入文件还原 dirty=0、探针残留 0。
This commit is contained in:
2026-10-05 11:51:33 +08:00
parent 265f31681f
commit 95e2318c42
62 changed files with 2866 additions and 112 deletions
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.admin.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.security.AccountExpiryService;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.company.service.CompanyService;
@@ -64,17 +65,22 @@ public class AdminConsoleController {
}
@GetMapping("/api/admin/current-user")
@Operation(summary = "当前登录管理员概要")
@Operation(summary = "获取当前管理员信息")
public ApiResponse<Map<String, Object>> currentUser(HttpServletRequest request) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
Map<String, Object> item = new LinkedHashMap<>();
item.put("id", operator.getId());
item.put("username", operator.getUsername() == null ? "" : operator.getUsername());
item.put("role", adminAuthSupport.currentRole(operator));
// 键名固定 camelCase:session.ts 把 item 原样当 AdminUser 用,没有字段映射层,
// 拼错只会让顶栏静默少一行字(前端 || '' 兜底),不会报错。
// 这里刻意用 camelCase:session.ts 把 item 当作 AdminUser 用,不再经解析器,
// 所以这些键要与既有 companyId/companyName 的形状一致(前端 || '' 兜底)
item.put("companyId", operator.getCompanyId());
item.put("companyName", companyService.nameOrEmpty(operator.getCompanyId()));
// 到期续费提醒(V146):只反映自身 expires_at,不级联——续费是账号自己的事。
// expiresAt 为 yyyy-MM-dd HH:mm 或 null;daysToExpire 为剩余整天数或 null。
item.put("expiresAt", operator.getExpiresAt() == null ? null
: operator.getExpiresAt().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
item.put("daysToExpire", AccountExpiryService.ownDaysToExpire(operator.getExpiresAt()));
return ApiResponse.success(Map.of("item", item));
}
@@ -11,5 +11,10 @@ public class AdminUserCreateRequest {
private String role;
private Long createdById;
private Long companyId;
/**
* 到期时间(V146):支持 yyyy-MM-dd / yyyy-MM-dd HH:mm / yyyy-MM-dd HH:mm:ss。
* 空/不传 = 永不过期;super_admin 目标不存在本字段语义(超管建不出超管)。
*/
private String expiresAt;
private List<Long> columnIds;
}
@@ -11,4 +11,9 @@ public class AdminUserUpdateRequest {
private List<Long> columnIds;
/** 移人目标公司(V143 §2.2,仅超管可用);null 或与账号现公司相同都表示「不动」。 */
private Long companyId;
/**
* 到期时间(V146):null=不动;空串=清除(恢复永不过期);否则为
* yyyy-MM-dd[ HH:mm[:ss]],允许过去时间(立即到期 = 手工停用)。
*/
private String expiresAt;
}
@@ -22,6 +22,9 @@ public class AdminUserItemVo {
private String createdAt;
@JsonProperty("updated_at")
private String updatedAt;
/** 到期时间(yyyy-MM-dd HH:mm);空串=永不过期,前端按此渲染。 */
@JsonProperty("expires_at")
private String expiresAt;
@JsonProperty("pinyin_abbr")
private String pinyinAbbr;
}
@@ -72,8 +72,9 @@ public class AdminUserService {
query.eq(AdminUserEntity::getRole, roleFilterValue);
}
boolean isSuperAdmin = "super_admin".equals(role);
boolean isCompanySuperAdmin = "company_super_admin".equals(role);
if (companyIdFilter != null && !isSuperAdmin) {
// 公司内管理员:筛自己公司 = 不加条件(他的可见范围本来就只有本公司),
// 公司内管理员/公司超管:筛自己公司 = 不加条件(可见范围本来就只有本公司),
// 筛别家公司 = 直接空页。这里故意不报错也不越权(设计文档 §2.3)。
if (!companyIdFilter.equals(currentUser.getCompanyId())) {
return emptyListVo(currentUser, role, safePage, safeSize);
@@ -89,6 +90,13 @@ public class AdminUserService {
if (companyIdFilter != null) {
query.eq(AdminUserEntity::getCompanyId, companyIdFilter);
}
} else if (isCompanySuperAdmin) {
// 公司超管看本公司全部账号(站点超管账号除外——那是平台自己的身份)
query.eq(AdminUserEntity::getCompanyId, currentUser.getCompanyId())
.ne(AdminUserEntity::getRole, "super_admin");
if (!kw.isEmpty()) {
query.like(AdminUserEntity::getUsername, kw);
}
} else {
Long adminId = currentUser.getId();
query.and(w -> w
@@ -140,13 +148,13 @@ public class AdminUserService {
ensureAdminUsersMenuAccess(currentUser, List.of("admin_users"));
String username = request.getUsername() == null ? "" : request.getUsername().trim();
String password = request.getPassword() == null ? "" : request.getPassword();
String wantRole = request.getRole() == null ? "normal" : request.getRole().trim();
if (wantRole.isEmpty()) wantRole = "normal";
if (!"admin".equals(wantRole) && !"normal".equals(wantRole)) {
wantRole = "normal";
String wantRole = normalizeWantRole(request.getRole());
if ("admin".equals(wantRole) && "admin".equals(role)) {
throw new BusinessException("仅超级管理员或公司超级管理员可创建管理员");
}
if ("admin".equals(role) && "admin".equals(wantRole)) {
throw new BusinessException("仅超级管理员可创建管理员");
if ("company_super_admin".equals(wantRole) && !"super_admin".equals(role)) {
// 公司超管是「某一家公司的最高账号」,只能由站点超管开设,谁都不能自己造一级
throw new BusinessException(403, "只有超级管理员可以创建公司超级管理员");
}
if (username.isEmpty() || password.isEmpty()) {
throw new BusinessException("用户名和密码不能为空");
@@ -159,7 +167,8 @@ public class AdminUserService {
}
Long wantCreatedBy;
if ("admin".equals(wantRole)) {
if ("admin".equals(wantRole) || "company_super_admin".equals(wantRole)) {
// 组长/公司超管的创建人就是操作者本人:公司超管不能替别的主管“认领”管理员
wantCreatedBy = currentUser.getId();
} else if ("super_admin".equals(role)) {
wantCreatedBy = request.getCreatedById();
@@ -174,7 +183,8 @@ public class AdminUserService {
wantCreatedBy = currentUser.getId();
}
int isAdmin = ("super_admin".equals(wantRole) || "admin".equals(wantRole)) ? 1 : 0;
int isAdmin = ("super_admin".equals(wantRole) || "admin".equals(wantRole)
|| "company_super_admin".equals(wantRole)) ? 1 : 0;
AdminUserEntity entity = new AdminUserEntity();
entity.setUsername(username);
entity.setPasswordHash(passwordEncoder.hash(password));
@@ -182,6 +192,7 @@ public class AdminUserService {
entity.setRole(wantRole);
entity.setCreatedById(wantCreatedBy);
entity.setCompanyId(resolveTargetCompanyId(role, currentUser, request.getCompanyId()));
entity.setExpiresAt(parseExpiresAt(request.getExpiresAt()));
try {
adminUserMapper.insert(entity);
} catch (DuplicateKeyException e) {
@@ -191,6 +202,49 @@ public class AdminUserService {
return entity.getId();
}
/**
* 建号角色归一:白名单 admin / company_super_admin / normal,其余一律归 normal。 */
private static String normalizeWantRole(String rawRole) {
String wantRole = rawRole == null ? "normal" : rawRole.trim();
if (wantRole.isEmpty()) {
wantRole = "normal";
}
if (!"admin".equals(wantRole) && !"normal".equals(wantRole)
&& !"company_super_admin".equals(wantRole)) {
wantRole = "normal";
}
return wantRole;
}
/**
* 解析请求里的到期时间:null/空白 = null(永不过期);支持 yyyy-MM-dd、
* yyyy-MM-dd HH:mm、yyyy-MM-dd HH:mm:ss 与 ISO(yyyy-MM-ddTHH:mm)。
* 格式不对直接报错,不静默归 null——那会把"想设到期"变成"设了永不过期"。
*/
private static LocalDateTime parseExpiresAt(String raw) {
if (raw == null || raw.trim().isEmpty()) {
return null;
}
String value = raw.trim().replace('T', ' ');
for (String pattern : new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd"}) {
try {
java.time.format.DateTimeFormatter formatter =
java.time.format.DateTimeFormatter.ofPattern(pattern);
java.time.temporal.TemporalAccessor parsed = formatter.parseBest(value,
LocalDateTime::from, java.time.LocalDate::from);
if (parsed instanceof LocalDateTime dateTime) {
return dateTime;
}
if (parsed instanceof java.time.LocalDate date) {
return date.atStartOfDay();
}
} catch (java.time.format.DateTimeParseException ignored) {
// 试下一个格式
}
}
throw new BusinessException("到期时间格式不正确,请使用 yyyy-MM-dd 或 yyyy-MM-dd HH:mm");
}
/**
* 建号落哪家公司(设计文档 §2.2,与 createdById 同构):超管可指定任意启用中的公司,
* 其余角色只能落自己所属公司——显式传别家即 403,不静默改写。
@@ -266,7 +320,8 @@ public class AdminUserService {
if ((password == null || password.isEmpty())
&& (wantRole == null || wantRole.isEmpty())
&& request.getColumnIds() == null
&& request.getCompanyId() == null) {
&& request.getCompanyId() == null
&& request.getExpiresAt() == null) {
throw new BusinessException("请提供要修改的内容");
}
@@ -282,13 +337,27 @@ public class AdminUserService {
}
wantRole = null;
} else {
boolean operatorIsSiteSuperAdmin = "super_admin".equals(role);
if ("super_admin".equals(targetRole)) {
throw new BusinessException("不能修改超级管理员");
}
if ("company_super_admin".equals(role)) {
// 公司超管只能编辑本公司管理员/普通用户;同公司的公司超管与站点超管都碰不得
if ("company_super_admin".equals(targetRole)) {
throw new BusinessException(403, "只有超级管理员可以修改公司超级管理员");
}
if (!currentUser.getCompanyId().equals(target.getCompanyId())) {
throw new BusinessException(403, "只能编辑本公司名下的账号");
}
}
if ("super_admin".equals(wantRole)) {
throw new BusinessException("不能将用户设为超级管理员");
}
if (wantRole != null && !"admin".equals(wantRole) && !"normal".equals(wantRole) && !wantRole.isEmpty()) {
if ("company_super_admin".equals(wantRole) && !operatorIsSiteSuperAdmin) {
throw new BusinessException(403, "只有超级管理员可以设置公司超级管理员");
}
if (wantRole != null && !"admin".equals(wantRole) && !"normal".equals(wantRole)
&& !"company_super_admin".equals(wantRole) && !wantRole.isEmpty()) {
wantRole = null;
}
}
@@ -314,11 +383,18 @@ public class AdminUserService {
dirty = true;
}
if (wantRole != null && !wantRole.isEmpty()) {
int isAdmin = "admin".equals(wantRole) ? 1 : 0;
int isAdmin = ("admin".equals(wantRole) || "company_super_admin".equals(wantRole)) ? 1 : 0;
update.set(AdminUserEntity::getIsAdmin, isAdmin);
update.set(AdminUserEntity::getRole, wantRole);
dirty = true;
}
if (request.getExpiresAt() != null) {
// null=不动;空串=清除(恢复永不过期);有值=改期(允许过去时间,等于立即停用)。
// super_admin / 同公司公司超管目标在上面的闸门里已拒绝,到不了这里。
update.set(AdminUserEntity::getExpiresAt,
request.getExpiresAt().trim().isEmpty() ? null : parseExpiresAt(request.getExpiresAt()));
dirty = true;
}
if (dirty) {
adminUserMapper.update(null, update);
}
@@ -387,6 +463,14 @@ public class AdminUserService {
if (!"normal".equals(targetRole) || !currentUser.getId().equals(target.getCreatedById())) {
throw new BusinessException(403, "只能删除自己创建的普通用户");
}
} else if ("company_super_admin".equals(role)) {
// 公司超管可清退本公司的管理员/普通用户;公司超管之间互删与站点超管一样被拒
if ("company_super_admin".equals(targetRole)) {
throw new BusinessException(403, "只有超级管理员可以删除公司超级管理员");
}
if (!currentUser.getCompanyId().equals(target.getCompanyId())) {
throw new BusinessException(403, "只能删除本公司名下的账号");
}
}
int affected = adminUserMapper.deleteById(uid);
if (affected == 0) {
@@ -459,6 +543,8 @@ public class AdminUserService {
vo.setCreatedAt(createdAt == null ? "" : createdAt.format(CREATED_AT_FORMATTER));
LocalDateTime updatedAt = entity.getUpdatedAt();
vo.setUpdatedAt(updatedAt == null ? "" : updatedAt.format(CREATED_AT_FORMATTER));
// 到期时间:null(永不过期)与空字符串在列表里都要有明确渲染,前端按空串判"永不过期"
vo.setExpiresAt(entity.getExpiresAt() == null ? "" : entity.getExpiresAt().format(CREATED_AT_FORMATTER));
vo.setPinyinAbbr(PinyinAbbrUtil.abbr(entity.getUsername()));
return vo;
}