LoginService.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.fdkankan.app.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.app.service.*;
  4. import com.fdkankan.common.constant.ConstantFilePath;
  5. import com.fdkankan.common.constant.ConstantRegex;
  6. import com.fdkankan.common.exception.BusinessException;
  7. import com.fdkankan.common.util.*;
  8. import com.fdkankan.redis.constant.RedisKey;
  9. import com.fdkankan.redis.util.RedisUtil;
  10. import com.fdkankan.sms.SendMailAcceUtils;
  11. import com.fdkankan.sms.SmsService;
  12. import com.fdkankan.app.common.RedisKeyUtil;
  13. import com.fdkankan.app.constant.LoginConstant;
  14. import com.fdkankan.app.entity.Camera;
  15. import com.fdkankan.app.entity.CameraDetail;
  16. import com.fdkankan.app.entity.User;
  17. import com.fdkankan.app.vo.request.LoginParam;
  18. import com.fdkankan.app.vo.response.LoginVo;
  19. import com.fdkankan.app.vo.response.UserVo;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.omg.PortableInterceptor.INACTIVE;
  22. import org.springframework.beans.BeanUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.stereotype.Service;
  26. import java.util.*;
  27. @Service
  28. public class LoginService {
  29. @Autowired
  30. private IUserService userService;
  31. @Autowired
  32. private RedisUtil redisUtil;
  33. @Autowired
  34. private ICameraService cameraService;
  35. @Autowired
  36. private ICameraDetailService cameraDetailService;
  37. @Autowired
  38. ISceneProService sceneProService;
  39. @Autowired
  40. IScenePlusService scenePlusService;
  41. public LoginVo login(LoginParam param) {
  42. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
  43. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  44. }
  45. String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
  46. String passwordCode = SecurityUtil.MD5(password);
  47. User user = userService.getByUserName(param.getPhoneNum());
  48. if(user == null){
  49. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  50. }
  51. if(!user.getPassword().equals(passwordCode)){
  52. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  53. }
  54. String token = this.redisLogin(user.getUserName(),JSONObject.toJSONString(user));
  55. Long count = cameraDetailService.getCountByUserId(user.getId());
  56. if(param.getCameraType() == null){
  57. param.setCameraType( 4);
  58. }
  59. List<Integer> resourceList = new ArrayList<>();
  60. if(param.getCameraType() == 4){
  61. resourceList = Arrays.asList(1,2,12,13,14);
  62. }else {
  63. resourceList = Collections.singletonList(3);
  64. }
  65. Long sceneProCount = sceneProService.getCountByUserId(user.getId(),resourceList);
  66. Long scenePlusCount = scenePlusService.getCountByUserId(user.getId(),resourceList);
  67. UserVo userVo = new UserVo();
  68. userVo.setCameraCount(count);
  69. userVo.setSceneCount(sceneProCount + scenePlusCount);
  70. BeanUtils.copyProperties(user,userVo);
  71. LoginVo vo = new LoginVo();
  72. vo.setToken(token);
  73. vo.setUser(userVo);
  74. return vo;
  75. }
  76. public void logout(String userName) {
  77. String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ userName;
  78. redisUtil.del(redisKey);
  79. }
  80. public String redisLogin(String userName,String value){
  81. String token = JwtUtil.createJWT(-1,userName,"app");
  82. String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ userName;
  83. redisUtil.set(redisKey, token,21600);
  84. return token;
  85. }
  86. }