1.冻结用户,将数据存入Redis,并设置key的失效时间。
2.解冻用户,删除Redis数据
3.探花系统在用户登录,评论,发布动态时判断其冻结状态,如果被冻结抛出异常
用户状态枚举
public enum UserStatusEnum { FREEZE_LOGIN("1","冻结登录"), FREEZE_FANYAN("2","冻结评论"), FREEZE_DONGTAI("3","冻结发布动态"); private String code; private String msg; UserStatusEnum(String code, String msg) { this.code = code; this.msg = msg; } public String getCode() { return code; } public String getMsg() { return msg; } }
后台代码
/** * 冻结用户 * @param map * @return */ public HashMap<String, String> freezeUser(Map map) { // 1.构造key String userId = map.get("userId").toString(); String key = Constants.FREEZE_USER + userId; // 2.获取冻结时间,设置redis的存活时间 int day=0; Integer freezingTime = Integer.parseInt(map.get("freezingTime").toString()) ; if (freezingTime==1){ day=3; } if (freezingTime==2){ day=7; } // 3.存入redis String value = JSON.toJSONString(map); if (day>0){ redisTemplate.opsForValue().set(key,value,day,TimeUnit.DAYS); }else { redisTemplate.opsForValue().set(key,value); } HashMap<String, String> msg = new HashMap<>(); msg.put("message","冻结用户成功"); return msg; } /** * 解冻用户 * @param map * @return */ public HashMap<Object, Object> unfreezeUser(Map map) { String userId = map.get("userId").toString(); String key = Constants.FREEZE_USER + userId; redisTemplate.delete(key); HashMap<Object, Object> res = new HashMap<>(); res.put("message","解冻用户成功"); return res; }
前台代码
@Service public class UserFreezeService { @Autowired private RedisTemplate redisTemplate; public void checkStatus(String freezeRange, Long userId) { // 取出value,判断封禁类型 String key = Constants.FREEZE_USER + userId.toString(); String vlaue = (String) redisTemplate.opsForValue().get(key); if (!StringUtils.isEmpty(vlaue)) { Map map = JSON.parseObject(vlaue, Map.class); String range = (String) map.get("freezingRange"); // 取出封禁类型进行判断 if (freezeRange.equals(range)) { throw new BusinessException(ErrorResult.countError()); } } } }
本文摘自 :https://www.cnblogs.com/