From 37d6fc727302d60b54da27f76b462fb182ac49af Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Thu, 19 Mar 2026 12:19:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=92=E8=A1=8C=E6=A6=9C=E9=9A=90=E8=BA=AB?= =?UTF-8?q?=E6=88=91=E7=9A=84=E6=8E=92=E5=90=8D=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=94=A8=E6=88=B7ID=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GiftsReceivedLeaderboardQryExe.java | 2 + .../activity/GiftsSendLeaderboardQryExe.java | 2 + .../ActivityLeaderboardServiceImpl.java | 7 +++- .../activity/ActivityLeaderboardCO.java | 5 +++ .../user/impl/RankCacheServiceImpl.java | 2 +- .../other/infra/utils/UserIdEncryptUtils.java | 39 +++++++++++++++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/UserIdEncryptUtils.java diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsReceivedLeaderboardQryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsReceivedLeaderboardQryExe.java index b6de40ee..ae4f8e13 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsReceivedLeaderboardQryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsReceivedLeaderboardQryExe.java @@ -10,6 +10,7 @@ import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheSe import com.red.circle.other.infra.database.mongo.entity.activity.RankQueenCount; import com.red.circle.other.infra.database.mongo.entity.activity.RankQueenType; import com.red.circle.other.infra.database.mongo.service.activity.RankCountService; +import com.red.circle.other.infra.utils.UserIdEncryptUtils; import com.red.circle.other.inner.enums.config.EnumConfigKey; import com.red.circle.other.inner.model.dto.user.UserProfileDTO; import com.red.circle.tool.core.collection.CollectionUtils; @@ -104,6 +105,7 @@ public class GiftsReceivedLeaderboardQryExe { .setQuantityFormat(NumUtils.formatLong(count.getQuantity())); if (invisibleUserIds.contains(count.getBusinessId())) { co.setId(null); + co.setEncryptedId(UserIdEncryptUtils.encrypt(userProfile.getId())); co.setNickname("****"); co.setAvatar(enumConfigCacheService.getValue(EnumConfigKey.USER_YINSHEN_COVER, userProfile.getSysOriginChild())); } else { diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsSendLeaderboardQryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsSendLeaderboardQryExe.java index 91a6fd24..1095ac17 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsSendLeaderboardQryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/GiftsSendLeaderboardQryExe.java @@ -10,6 +10,7 @@ import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheSe import com.red.circle.other.infra.database.mongo.entity.activity.RankQueenCount; import com.red.circle.other.infra.database.mongo.entity.activity.RankQueenType; import com.red.circle.other.infra.database.mongo.service.activity.RankCountService; +import com.red.circle.other.infra.utils.UserIdEncryptUtils; import com.red.circle.other.inner.enums.config.EnumConfigKey; import com.red.circle.other.inner.model.dto.user.UserProfileDTO; import com.red.circle.tool.core.collection.CollectionUtils; @@ -116,6 +117,7 @@ public class GiftsSendLeaderboardQryExe { .setQuantityFormat(NumUtils.formatLong(count.getQuantity())); if (invisibleUserIds.contains(count.getBusinessId())) { co.setId(null); + co.setEncryptedId(UserIdEncryptUtils.encrypt(userProfile.getId())); co.setNickname("****"); co.setAvatar(enumConfigCacheService.getValue(EnumConfigKey.USER_YINSHEN_COVER, userProfile.getSysOriginChild())); } else { diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/ActivityLeaderboardServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/ActivityLeaderboardServiceImpl.java index 721f1843..6d317ede 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/ActivityLeaderboardServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/ActivityLeaderboardServiceImpl.java @@ -16,6 +16,7 @@ import com.red.circle.other.infra.database.cache.key.RankKey; import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService; import com.red.circle.other.infra.database.cache.service.user.RankCacheService; import com.red.circle.other.inner.enums.config.EnumConfigKey; +import com.red.circle.other.infra.utils.UserIdEncryptUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -236,7 +237,11 @@ public class ActivityLeaderboardServiceImpl implements ActivityLeaderboardServic // 查找用户排名 for (int i = 0; i < rankList.size(); i++) { ActivityLeaderboardCO.LeaderboardUserCO userCO = rankList.get(i); - if (Objects.equals(userCO.getId(), userId)) { + boolean matched = Objects.equals(userCO.getId(), userId) + || (userCO.getId() == null + && userCO.getEncryptedId() != null + && Objects.equals(UserIdEncryptUtils.decrypt(userCO.getEncryptedId()), userId)); + if (matched) { userCO.setRank(i + 1); return userCO; } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/ActivityLeaderboardCO.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/ActivityLeaderboardCO.java index bb23e33f..19b360db 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/ActivityLeaderboardCO.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/ActivityLeaderboardCO.java @@ -67,6 +67,11 @@ public class ActivityLeaderboardCO extends ClientObject { @JsonSerialize(using = ToStringSerializer.class) private Long id; + /** + * 加密后的用户ID(隐身用户使用,用于myRank匹配). + */ + private String encryptedId; + /** * 头像. */ diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/RankCacheServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/RankCacheServiceImpl.java index 351abdb2..55f4be17 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/RankCacheServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/RankCacheServiceImpl.java @@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit; public class RankCacheServiceImpl implements RankCacheService { private final RedisService redisService; - private static final long CACHE_TIME = 5; + private static final long CACHE_TIME = 2; @Override public void remove(String sysOrigin, String type) { diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/UserIdEncryptUtils.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/UserIdEncryptUtils.java new file mode 100644 index 00000000..a8304b43 --- /dev/null +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/UserIdEncryptUtils.java @@ -0,0 +1,39 @@ +package com.red.circle.other.infra.utils; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +/** + * 用户ID加密工具(用于排行榜隐身场景). + * + * @author tf + */ +public class UserIdEncryptUtils { + + private static final String ALGORITHM = "AES/ECB/PKCS5Padding"; + private static final byte[] KEY = "likei_rank_2026!".getBytes(StandardCharsets.UTF_8); // 16字节 + + public static String encrypt(Long userId) { + try { + Cipher cipher = Cipher.getInstance(ALGORITHM); + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES")); + byte[] encrypted = cipher.doFinal(userId.toString().getBytes(StandardCharsets.UTF_8)); + return Base64.getUrlEncoder().withoutPadding().encodeToString(encrypted); + } catch (Exception e) { + throw new RuntimeException("userId encrypt error", e); + } + } + + public static Long decrypt(String token) { + try { + Cipher cipher = Cipher.getInstance(ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "AES")); + byte[] decoded = Base64.getUrlDecoder().decode(token); + return Long.parseLong(new String(cipher.doFinal(decoded), StandardCharsets.UTF_8)); + } catch (Exception e) { + throw new RuntimeException("userId decrypt error", e); + } + } +}