Browse Source

S-cfc-481: family.invite.memberJoin 重生成邀请实现使绿 (TDD-GREEN)

- FamilyInvitationService: 新增 regenerateInvitation(familyId,userId),先吊销有效旧令牌再签发新令牌;失效文案补齐「邀请已失效,请重新获取」
- FamilyInviteController: 新增 POST /api/family/invite/regenerate(userId 取 @RequestAttribute)
- API_REFERENCE.md / PROJECT-OVERVIEW.md 同步新接口
- 验证: FamilyInviteMemberJoinContractTest 7/7、FamilyInviteRegenerateContractRedTest 6/6 全绿
Agnes Agent 4 hours ago
parent
commit
dbf086d809

+ 33 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/family/FamilyInviteController.java

@@ -285,6 +285,39 @@ public class FamilyInviteController {
         return Result.success(result);
         return Result.success(result);
     }
     }
 
 
+    /**
+     * 重生成家庭邀请(SC-P-09:失效旧码 → 生成新码;旧码已加入成员不受影响)
+     */
+    @Operation(summary = "重生成家庭邀请")
+    @PostMapping("/regenerate")
+    public Result<InviteGenerateDTO> regenerateInvite(@RequestAttribute("userId") Long userId) {
+        if (userId == null) {
+            return Result.error("请先登录");
+        }
+
+        User user = userMapper.selectById(userId);
+        if (user == null || user.getFamilyId() == null) {
+            return Result.noFamily("请先创建或加入家庭");
+        }
+
+        String token;
+        try {
+            token = familyInvitationService.regenerateInvitation(user.getFamilyId(), userId);
+        } catch (Exception e) {
+            return Result.error(e.getMessage());
+        }
+
+        String qrBase64 = wechatService.generateWxacode(token, "pages/invite/join");
+        Family family = familyMapper.selectById(user.getFamilyId());
+
+        InviteGenerateDTO result = new InviteGenerateDTO();
+        result.setToken(token);
+        result.setQrCodeBase64(qrBase64);
+        result.setFamilyName(family != null ? family.getName() : null);
+        result.setFamilyId(user.getFamilyId());
+        return Result.success(result);
+    }
+
     /**
     /**
      * 校验邀请令牌有效性
      * 校验邀请令牌有效性
      */
      */

+ 67 - 1
cfc-backend/src/main/java/com/etotem/cfc/service/FamilyInvitationService.java

@@ -53,7 +53,8 @@ public class FamilyInvitationService {
                                 .or().gt(FamilyInvitation::getExpiresAt, new Date()))
                                 .or().gt(FamilyInvitation::getExpiresAt, new Date()))
         );
         );
         if (invitation == null) {
         if (invitation == null) {
-            throw new RuntimeException("邀请链接无效或已过期");
+            // SC-P-09(TC-P-02-E):失效旧码须引导用户重新获取新码
+            throw new RuntimeException("邀请已失效,请重新获取");
         }
         }
 
 
         if (invitation.getUsedCount() != null && invitation.getMaxUseCount() != null
         if (invitation.getUsedCount() != null && invitation.getMaxUseCount() != null
@@ -127,6 +128,71 @@ public class FamilyInvitationService {
         return invitation.getToken();
         return invitation.getToken();
     }
     }
 
 
+    /**
+     * 重生成家庭邀请令牌(SC-P-09「失效旧码 → 生成新码」)。
+     *
+     * <p>先吊销该用户在该家庭的有效旧令牌,再签发全新令牌;只改动邀请记录,
+     * 不触碰已加入成员/关系记录(旧码已加入的会员不受影响)。</p>
+     *
+     * @param familyId 目标家庭ID
+     * @param userId   邀请人用户ID(新令牌创建人/审批人)
+     * @return 新令牌,供二维码/分享使用
+     */
+    public String regenerateInvitation(Long familyId, Long userId) {
+        Family family = familyMapper.selectById(familyId);
+        if (family == null) {
+            throw new RuntimeException("家庭不存在");
+        }
+
+        Date now = new Date();
+        // 先吊销该家庭内该邀请人签发的有效旧令牌,确保旧码立即失效
+        List<FamilyInvitation> activeInvitations = familyInvitationMapper.selectList(
+                new LambdaQueryWrapper<FamilyInvitation>()
+                        .eq(FamilyInvitation::getFamilyId, familyId)
+                        .eq(FamilyInvitation::getCreatedBy, userId)
+                        .eq(FamilyInvitation::getStatus, "active")
+                        .and(w -> w.isNull(FamilyInvitation::getExpiresAt)
+                                .or().gt(FamilyInvitation::getExpiresAt, now))
+        );
+        if (activeInvitations != null) {
+            for (FamilyInvitation old : activeInvitations) {
+                old.setStatus("revoked");
+                old.setUpdatedAt(new Date());
+                familyInvitationMapper.updateById(old);
+            }
+        }
+
+        // 再签发新令牌:不能调用 generateInvitation(它会复用同家庭有效令牌)
+        String newToken;
+        boolean reused;
+        do {
+            newToken = UUID.randomUUID().toString().replace("-", "");
+            reused = false;
+            if (activeInvitations != null) {
+                for (FamilyInvitation old : activeInvitations) {
+                    if (newToken.equals(old.getToken())) {
+                        reused = true;
+                        break;
+                    }
+                }
+            }
+        } while (reused);
+
+        FamilyInvitation invitation = new FamilyInvitation();
+        invitation.setFamilyId(familyId);
+        invitation.setToken(newToken);
+        invitation.setCreatedBy(userId);
+        invitation.setStatus("active");
+        invitation.setExpiresAt(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000L));
+        invitation.setMaxUseCount(1);
+        invitation.setUsedCount(0);
+        invitation.setCreatedAt(new Date());
+        invitation.setUpdatedAt(new Date());
+        familyInvitationMapper.insert(invitation);
+
+        return invitation.getToken();
+    }
+
     public String acceptInvitation(Long userId, String token, String familyRole, Boolean mergeFamily) {
     public String acceptInvitation(Long userId, String token, String familyRole, Boolean mergeFamily) {
         // Step 1: 原子递增 usedCount(并发安全:只有 usedCount < maxUseCount 时才更新)
         // Step 1: 原子递增 usedCount(并发安全:只有 usedCount < maxUseCount 时才更新)
         int rows = familyInvitationMapper.update(
         int rows = familyInvitationMapper.update(

File diff suppressed because it is too large
+ 0 - 0
docs/superpowers/PROJECT-OVERVIEW.md


+ 1 - 0
docs/superpowers/api/API_REFERENCE.md

@@ -196,6 +196,7 @@ find cfc-backend/src/main/java -name "*XxxService.java" -o -name "*XxxController
 | `POST /api/family/invite/my-request` | 我的申请 | — |
 | `POST /api/family/invite/my-request` | 我的申请 | — |
 | `POST /api/family/invite/cancel-request` | 取消申请 | — |
 | `POST /api/family/invite/cancel-request` | 取消申请 | — |
 | `POST /api/family/invite/generate` | 生成邀请令牌(复用同家庭未过期未用尽的有效令牌;仅未过期记录计入 10 条额度) | — |
 | `POST /api/family/invite/generate` | 生成邀请令牌(复用同家庭未过期未用尽的有效令牌;仅未过期记录计入 10 条额度) | — |
+| `POST /api/family/invite/regenerate` | 重生成邀请令牌(先吊销该邀请人在该家庭的有效旧令牌,再签发新令牌;旧码已加入成员不受影响) | — |
 | `POST /api/family/invite/validate` | 验证邀请令牌 | — |
 | `POST /api/family/invite/validate` | 验证邀请令牌 | — |
 | `POST /api/family/invite/accept` | 接受邀请 | — |
 | `POST /api/family/invite/accept` | 接受邀请 | — |
 | `POST /api/family/invite/check-family` | 检查家庭状态 | — |
 | `POST /api/family/invite/check-family` | 检查家庭状态 | — |

Some files were not shown because too many files changed in this diff