BadIntentionOpServiceImpl.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.cyksj.service.user.impl;
  2. import com.cyksj.common.exception.BusinessRuntimeException;
  3. import com.cyksj.redis.RedisService;
  4. import com.cyksj.service.user.BadIntentionOpService;
  5. import lombok.RequiredArgsConstructor;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. @RequiredArgsConstructor
  10. @Slf4j
  11. public class BadIntentionOpServiceImpl implements BadIntentionOpService {
  12. private final RedisService redisService;
  13. private static final int PHONE_CODE_GET_NUM_DAY_LIMIT = 30;
  14. private static final int PHONE_CODE_GET_NUM_DAY_MAX_BLACK = 50;
  15. @Override
  16. public void checkHandleBadUserOp(String phone) {
  17. if ("13662979985".equals(phone)) {
  18. throw BusinessRuntimeException.getInstance("异常手机号");
  19. }
  20. String dayKey = RedisService.key.PHONE_CODE_USER_NUM_KEY_DAY.getName() + phone;
  21. if (redisService.hasKey(dayKey)) {
  22. Long incr = redisService.incr(dayKey, 1l);
  23. if (incr.intValue() >= PHONE_CODE_GET_NUM_DAY_MAX_BLACK) {
  24. log.error("近一天内手机号:{}获取验证码超过50次,移入黑名单", phone);
  25. redisService.set(RedisService.key.PHONE_CODE_BALCK_KEY.getName() + phone, phone);
  26. }
  27. if (incr.intValue() > PHONE_CODE_GET_NUM_DAY_LIMIT) {
  28. log.error("手机号:{}频繁获取短信验证码", phone);
  29. throw BusinessRuntimeException.getInstance("请勿频繁获取短信验证码");
  30. }
  31. return;
  32. }
  33. redisService.set(dayKey, 1, RedisService.key.PHONE_CODE_USER_NUM_KEY_DAY.getTimeout());
  34. }
  35. }