|
@@ -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(
|