|
@@ -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);
|
|
|
|
+ }
|
|
|
|
+}
|