diff --git a/src/main/java/com/bao/dating/util/UserBanUtil.java b/src/main/java/com/bao/dating/util/UserBanUtil.java new file mode 100644 index 0000000..13a0745 --- /dev/null +++ b/src/main/java/com/bao/dating/util/UserBanUtil.java @@ -0,0 +1,43 @@ +package com.bao.dating.util; + +import com.bao.dating.context.UserContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +/** + * 用户封禁验证工具类 + * 提供统一的用户封禁状态检查功能 + * + * @author KilLze + */ +@Component +public class UserBanUtil { + + @Autowired + private RedisTemplate redisTemplate; + + /** + * 验证指定用户是否被封禁 + * + * @param userId 用户ID + * @throws RuntimeException 如果用户被封禁则抛出异常 + */ + public void validateUserNotBanned(Long userId) { + String banKey = "user:ban:" + userId; + if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) { + String reason = (String) redisTemplate.opsForValue().get(banKey); + throw new RuntimeException("账号已被封禁:" + reason); + } + } + + /** + * 验证当前登录用户是否被封禁 + * + * @throws RuntimeException 如果用户被封禁则抛出异常 + */ + public void validateCurrentUserNotBanned() { + Long userId = UserContext.getUserId(); + validateUserNotBanned(userId); + } +} \ No newline at end of file