Forráskód Böngészése

feature 增加时间

xiahan 3 hete
szülő
commit
4f142ce611

+ 2 - 0
qhse-common/src/main/java/com/rongwei/bscommon/system/service/RedisService.java

@@ -23,4 +23,6 @@ public interface RedisService {
 
     void  updateValueNoUpdateExpireTime(String key, Object value);
 
+    long getExpireTime(String key);
+
 }

+ 10 - 10
qhse-common/src/main/java/com/rongwei/bscommon/system/service/impl/QHSELoginServiceImpl.java

@@ -8,7 +8,6 @@ import com.rongwei.rwadmincommon.system.domain.SysUserDo;
 import com.rongwei.rwadmincommon.system.domain.UserLoginVo;
 import com.rongwei.rwcommon.base.BaseDo;
 import com.rongwei.rwcommon.base.R;
-import com.rongwei.rwcommon.utils.DateUtils;
 import com.rongwei.rwcommon.utils.SecurityUtil;
 import com.rongwei.rwcommon.utils.StringUtils;
 import org.slf4j.Logger;
@@ -23,8 +22,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Random;
 
-import static com.rongwei.bscommon.system.utils.QHSEConstant.ERROR_LOWER_CASE;
-
 /**
  * QHSELoginServiceImpl class
  *
@@ -62,14 +59,16 @@ public class QHSELoginServiceImpl implements QHSELoginService {
         String username = userLogin.getUsername();
         // 错误次数
         int errorNum = 0;
-        if (redisService.hasKey("smscode-error-" + username)) {
-            errorNum = Integer.parseInt(redisService.getRedisCatchObj("smscode-error-" + username).toString());
+        String smsErrorKey = "smscode-error-" + username;
+        if (redisService.hasKey(smsErrorKey)) {
+            errorNum = Integer.parseInt(redisService.getRedisCatchObj(smsErrorKey).toString());
         }
         if (errorNum > 10) {
             log.error("用户:{},密码错误次数为:{}", username, errorNum + 1);
-            throw new RuntimeException("密码错误次数已超过限制");
+            long expireTime = redisService.getExpireTime(smsErrorKey);
+            throw new RuntimeException("密码错误次数已超过限制!请" + expireTime + "秒后再试");
         }
-        String smsErrorKey = "smscode-error-" + username;
+
         List<SysUserDo> sysUserDos = qhseUserService.list(new LambdaQueryWrapper<SysUserDo>()
                 .eq(BaseDo::getDeleted, "0").eq(SysUserDo::getAccount, username));
         if (sysUserDos.isEmpty()) {
@@ -98,7 +97,8 @@ public class QHSELoginServiceImpl implements QHSELoginService {
 
             if (errorNum >= 10) {
                 log.error("用户:{},密码错误次数为:{}", username, errorNum);
-                throw new RuntimeException("密码错误次数已超过限制");
+                long expireTime = redisService.getExpireTime(smsErrorKey);
+                throw new RuntimeException("密码错误次数已超过限制!请" + expireTime + "秒后再试");
             }
             throw new RuntimeException("密码错误");
         }
@@ -114,7 +114,7 @@ public class QHSELoginServiceImpl implements QHSELoginService {
             Map<String, Object> templateParamMap = new HashMap<>();
             templateParamMap.put("code", smsCode);
 
-            aliyunSmsService.sendSms(smsCodeConfig.getTempId(),templateParamMap, mobile);
+            aliyunSmsService.sendSms(smsCodeConfig.getTempId(), templateParamMap, mobile);
         } catch (Exception e) {
             log.error("验证码发送失败原因为");
             throw new RuntimeException(e.getMessage());
@@ -126,7 +126,7 @@ public class QHSELoginServiceImpl implements QHSELoginService {
     }
 
     public int generateSms() {
-        return  100000 +  new Random().nextInt(900000);
+        return 100000 + new Random().nextInt(900000);
     }
 
 

+ 13 - 4
qhse-common/src/main/java/com/rongwei/bscommon/system/service/impl/RedisServiceImpl.java

@@ -11,7 +11,6 @@ import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.core.ValueOperations;
 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
 import org.springframework.data.redis.serializer.RedisSerializer;
 import org.springframework.data.redis.serializer.StringRedisSerializer;
@@ -30,7 +29,6 @@ public class RedisServiceImpl implements RedisService {
     private RedisTemplate redisTemplate;
 
 
-
     @Override
     public void redisCatchInit(String key, Object obj, int expireTime) {
         if (expireTime > 0) {
@@ -58,11 +56,20 @@ public class RedisServiceImpl implements RedisService {
     @Override
     public void updateValueNoUpdateExpireTime(String key, Object value) {
 
-        redisTemplate.opsForValue().set(key,value,0);
+        redisTemplate.opsForValue().set(key, value, 0);
+    }
+
+    @Override
+    public long getExpireTime(String key) {
+        if (!redisTemplate.hasKey(key)) {
+            return 0;
+        }
+        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
     }
 
     /**
      * redis配置项初始化
+     *
      * @param redisTemplate
      */
     @Autowired(required = false)
@@ -87,6 +94,7 @@ public class RedisServiceImpl implements RedisService {
         redisTemplate.afterPropertiesSet();
         this.redisTemplate = redisTemplate;
     }
+
     /**
      * 根据 pattern 模糊查询,查所有传入 "*" 即可
      *
@@ -96,8 +104,9 @@ public class RedisServiceImpl implements RedisService {
     public Set<String> getPatternKey(String pattern) {
         return redisTemplate.keys(pattern);
     }
+
     @Override
     public SysUserVo getLoginUser(String token) {
-        return (SysUserVo)redisTemplate.opsForValue().get("token-"+token);
+        return (SysUserVo) redisTemplate.opsForValue().get("token-" + token);
     }
 }