lyhzzz hace 2 años
padre
commit
00d8b1f528

+ 1 - 0
src/main/java/com/fdkankan/agent/common/ResultCode.java

@@ -20,6 +20,7 @@ public enum ResultCode {
     INCREMENT_NUM_EMPTY(4017, "权益数量不足!"),
     DOWN_NUM_EMPTY(4018, "下载数量不足!"),
     CAMERA_SN_ERROR(4019, "sn码不存在!"),
+    LOGIN_NUM_MUCH(4020, "频繁登录失败,请五分钟后再次尝试登录!"),
 
 
     ;

+ 7 - 0
src/main/java/com/fdkankan/agent/common/util/RedisKeyUtil.java

@@ -0,0 +1,7 @@
+package com.fdkankan.agent.common.util;
+
+public class RedisKeyUtil {
+
+    public static final String loginNum= "agent:login:userName:%s:ip:%s";
+
+}

+ 10 - 32
src/main/java/com/fdkankan/agent/controller/LoginController.java

@@ -1,5 +1,6 @@
 package com.fdkankan.agent.controller;
 
+import cn.hutool.extra.servlet.ServletUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.agent.common.BaseController;
 import com.fdkankan.agent.common.ResultCode;
@@ -12,6 +13,7 @@ import com.fdkankan.agent.httpClient.response.FdkkLoginVo;
 import com.fdkankan.agent.httpClient.response.FdkkResponse;
 import com.fdkankan.agent.response.AgentNewVo;
 import com.fdkankan.agent.service.IAgentNewService;
+import com.fdkankan.agent.service.LoginService;
 import com.fdkankan.redis.constant.RedisKey;
 import com.fdkankan.redis.util.RedisUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -27,31 +29,18 @@ import java.util.List;
 @RequestMapping("/agent")
 public class LoginController extends BaseController {
 
-    @Resource
-    FdKKClient fdKKClient;
+
+    @Autowired
+    LoginService loginService;
     @Autowired
     RedisUtil redisUtil;
 
-    @Autowired
-    IAgentNewService iAgentNewService;
 
     @PostMapping("/fdLogin")
-    public ResultData fdLogin(@RequestBody FdkkLoginRequest request){
-        try {
-            FdkkResponse<FdkkLoginVo> fdkkLoginVo = fdKKClient.fdkkLogin(request);
-            if(fdkkLoginVo.getCode() == 0){
-                AgentNewVo agentNewVo = iAgentNewService.getByUserName(request.getPhoneNum());
-                if(agentNewVo == null){
-                    throw new BusinessException(ResultCode.AGENT_USER_EMPTY);
-                }
-                fdkkLoginVo.getData().put("agent",agentNewVo);
-                return ResultData.ok(fdkkLoginVo.getData());
-            }
-            return ResultData.error(fdkkLoginVo.getCode(),fdkkLoginVo.getMsg());
-        }catch (Exception e){
-            log.error("fd登录失败",e);
-        }
-        throw new BusinessException(ResultCode.FD_ERROR);
+    public ResultData fdLogin(@RequestBody FdkkLoginRequest param){
+        String clientIP = ServletUtil.getClientIP(request);
+        param.setIp(clientIP);
+        return loginService.login(param);
     }
 
     @GetMapping("/fdLogout")
@@ -66,17 +55,6 @@ public class LoginController extends BaseController {
      */
     @PostMapping("/getUserInfo")
     public ResultData getUserInfo(){
-        try {
-            JSONObject data = fdKKClient.getUserInfo(new JSONObject(), getToken()).getData();
-            AgentNewVo agentNewVo = iAgentNewService.getByUserName(data.getString("userName"));
-            if(agentNewVo == null){
-                throw new BusinessException(ResultCode.AGENT_USER_EMPTY);
-            }
-            data.put("agent",agentNewVo);
-            return ResultData.ok(data);
-        }catch (Exception e){
-            e.printStackTrace();
-        }
-        throw new BusinessException(ResultCode.USER_NOT_LOGIN);
+       return loginService.getUserInfo(getToken());
     }
 }

+ 1 - 0
src/main/java/com/fdkankan/agent/httpClient/request/FdkkLoginRequest.java

@@ -8,4 +8,5 @@ import lombok.Data;
 public class FdkkLoginRequest {
     private String phoneNum;
     private String password;
+    private String ip;
 }

+ 2 - 0
src/main/java/com/fdkankan/agent/httpClient/response/FdkkLoginVo.java

@@ -1,9 +1,11 @@
 package com.fdkankan.agent.httpClient.response;
 
+import com.fdkankan.agent.response.AgentNewVo;
 import lombok.Data;
 
 @Data
 public class FdkkLoginVo {
     private String token;
     private FdkkUserVo user;
+    private AgentNewVo agent;
 }

+ 1 - 1
src/main/java/com/fdkankan/agent/httpClient/response/FdkkResponse.java

@@ -7,5 +7,5 @@ import lombok.Data;
 public class FdkkResponse<T> {
     private Integer code;
     private String msg;
-    private JSONObject data;
+    private T data;
 }

+ 1 - 1
src/main/java/com/fdkankan/agent/httpClient/service/LaserService.java

@@ -52,7 +52,7 @@ public class LaserService {
             return PageInfo.PageInfo(voPage);
         }
         FdkkResponse response = laserClient.sceneList(laserSceneParam);
-        JSONObject jsonObject =response.getData();
+        JSONObject jsonObject = (JSONObject) response.getData();
         if(jsonObject == null){
             Page<SceneVo> voPage = new Page<>(param.getPageNum(),param.getPageSize());
             voPage.setRecords(new ArrayList<>());

+ 97 - 0
src/main/java/com/fdkankan/agent/service/LoginService.java

@@ -0,0 +1,97 @@
+package com.fdkankan.agent.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.fdkankan.agent.common.ResultCode;
+import com.fdkankan.agent.common.ResultData;
+import com.fdkankan.agent.common.util.RedisKeyUtil;
+import com.fdkankan.agent.exception.BusinessException;
+import com.fdkankan.agent.httpClient.client.FdKKClient;
+import com.fdkankan.agent.httpClient.request.FdkkLoginRequest;
+import com.fdkankan.agent.httpClient.response.FdkkLoginVo;
+import com.fdkankan.agent.httpClient.response.FdkkResponse;
+import com.fdkankan.agent.response.AgentNewVo;
+import com.fdkankan.redis.util.RedisUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+@Service
+@Slf4j
+public class LoginService {
+
+    @Autowired
+    RedisUtil redisUtil;
+    @Resource
+    FdKKClient fdKKClient;
+    @Autowired
+    IAgentNewService iAgentNewService;
+
+    public ResultData login(FdkkLoginRequest param) {
+        try {
+            this.checkLoginNum(param.getIp(),param.getPhoneNum());
+            FdkkResponse<FdkkLoginVo> fdkkLoginVo = fdKKClient.fdkkLogin(param);
+            if(fdkkLoginVo.getCode() == 0){
+                AgentNewVo agentNewVo = iAgentNewService.getByUserName(param.getPhoneNum());
+                if(agentNewVo == null){
+                    this.addLoginErrorNum(param.getIp(),param.getPhoneNum());
+                    throw new BusinessException(ResultCode.AGENT_USER_EMPTY);
+                }
+                fdkkLoginVo.getData().setAgent(agentNewVo);
+                return ResultData.ok(fdkkLoginVo.getData());
+            }
+            this.addLoginErrorNum(param.getIp(),param.getPhoneNum());
+            return ResultData.error(fdkkLoginVo.getCode(),fdkkLoginVo.getMsg());
+        }catch (Exception e){
+            log.error("fd登录失败",e);
+        }
+        this.addLoginErrorNum(param.getIp(),param.getPhoneNum());
+        throw new BusinessException(ResultCode.FD_ERROR);
+    }
+
+
+    public void checkLoginNum(String ip, String userName) {
+        if(StringUtils.isNotBlank(userName)){
+            String redisKey = String.format(RedisKeyUtil.loginNum,userName,ip);
+            if(redisUtil.hasKey(redisKey)){
+                String value = redisUtil.get(redisKey);
+                if("5".equals(value)){
+                    throw new BusinessException(ResultCode.LOGIN_NUM_MUCH);
+                }
+            }
+        }
+    }
+
+    public void addLoginErrorNum(String ip, String userName) {
+        if(StringUtils.isNotBlank(userName)){
+            String redisKey = String.format(RedisKeyUtil.loginNum,userName,ip);
+            if(redisUtil.hasKey(redisKey)){
+                String value = redisUtil.get(redisKey);
+                if("5".equals(value)){
+                    throw new BusinessException(ResultCode.LOGIN_NUM_MUCH);
+                }
+                Integer num = Integer.parseInt(value) + 1;
+                redisUtil.set(redisKey,num.toString(),60 * 5);
+            }else {
+                redisUtil.set(redisKey,"1",60 * 5);
+            }
+        }
+    }
+
+    public ResultData getUserInfo(String token) {
+        try {
+            JSONObject data = (JSONObject) fdKKClient.getUserInfo(new JSONObject(), token).getData();
+            AgentNewVo agentNewVo = iAgentNewService.getByUserName(data.getString("userName"));
+            if(agentNewVo == null){
+                throw new BusinessException(ResultCode.AGENT_USER_EMPTY);
+            }
+            data.put("agent",agentNewVo);
+            return ResultData.ok(data);
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+        throw new BusinessException(ResultCode.USER_NOT_LOGIN);
+    }
+}