AppService.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.common.constant.AppConstant;
  4. import com.fdkankan.common.constant.CameraConstant;
  5. import com.fdkankan.common.exception.BusinessException;
  6. import com.fdkankan.common.util.JwtUtil;
  7. import com.fdkankan.common.util.RandomUtil;
  8. import com.fdkankan.common.util.SecurityUtil;
  9. import com.fdkankan.redis.constant.RedisKey;
  10. import com.fdkankan.redis.util.RedisUtil;
  11. import com.fdkankan.ucenter.common.RedisKeyUtil;
  12. import com.fdkankan.ucenter.constant.LoginConstant;
  13. import com.fdkankan.ucenter.entity.Camera;
  14. import com.fdkankan.ucenter.entity.CameraDetail;
  15. import com.fdkankan.ucenter.entity.User;
  16. import com.fdkankan.ucenter.service.*;
  17. import com.fdkankan.ucenter.vo.request.AppLoginParam;
  18. import com.fdkankan.ucenter.vo.request.LoginParam;
  19. import com.fdkankan.ucenter.vo.response.LoginVo;
  20. import com.fdkankan.ucenter.vo.response.UserVo;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.BeanUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import java.util.List;
  29. @Service
  30. public class AppService {
  31. @Autowired
  32. private IUserService userService;
  33. @Autowired
  34. private RedisUtil redisUtil;
  35. @Autowired
  36. private ICameraDetailService cameraDetailService;
  37. @Autowired
  38. ISceneProService sceneProService;
  39. @Autowired
  40. IScenePlusService scenePlusService;
  41. @Autowired
  42. ICameraService cameraService;
  43. @Autowired
  44. LoginService loginService;
  45. public LoginVo login(LoginParam param) {
  46. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
  47. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  48. }
  49. String passwordCode = SecurityUtil.MD5(param.getPassword());
  50. User user = userService.getByUserName(param.getPhoneNum());
  51. if(user == null){
  52. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  53. }
  54. if(!user.getPassword().equals(passwordCode)){
  55. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  56. }
  57. String token = this.redisLogin(user.getUserName(), JSONObject.toJSONString(user),21800L);
  58. LoginVo loginVo = commonLogin(user, param, token);
  59. loginVo.getUser().setPassword(param.getPassword());
  60. return loginVo;
  61. }
  62. private LoginVo commonLogin(User user,LoginParam param,String token){
  63. Long count = cameraDetailService.getCountByUserId(user.getId(),null);
  64. if(param.getCameraType() == null){
  65. param.setCameraType( 4);
  66. }
  67. List<Integer> resourceList = new ArrayList<>();
  68. if(param.getCameraType() == 4){
  69. resourceList = Arrays.asList(1,2,12,13,14);
  70. }else {
  71. resourceList = Collections.singletonList(3);
  72. }
  73. Long sceneProCount = sceneProService.getCountByUserId(user.getId(),resourceList);
  74. Long scenePlusCount = scenePlusService.getCountByUserId(user.getId(),resourceList);
  75. UserVo userVo = new UserVo();
  76. userVo.setCameraCount(count);
  77. userVo.setSceneCount(sceneProCount + scenePlusCount);
  78. BeanUtils.copyProperties(user,userVo);
  79. LoginVo vo = new LoginVo();
  80. vo.setToken(token);
  81. vo.setUser(userVo);
  82. return vo;
  83. }
  84. public String redisLogin(String userName,String value,Long time){
  85. String token = JwtUtil.createJWT(-1,userName,"app");
  86. String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ userName;
  87. redisUtil.set(redisKey, token,time);
  88. //设置官网token
  89. String redisKey2 = String.format(RedisKey.TOKEN_V3 , token );
  90. redisUtil.set(redisKey2, value,time);
  91. return token;
  92. }
  93. public void appLogin(AppLoginParam param) {
  94. if(StringUtils.isEmpty(param.getAppUserName()) || StringUtils.isEmpty(param.getAppPassword())
  95. || StringUtils.isEmpty(param.getUuid())){
  96. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  97. }
  98. commonCheckCamera(param);
  99. redisUtil.set(param.getUuid(),param.getAppUserName(),60 * 5);
  100. }
  101. private Long commonCheckCamera(AppLoginParam param){
  102. Camera camera = cameraService.getBySnCodeAndPassword(param.getAppUserName(),param.getAppPassword());
  103. if(camera == null){
  104. throw new BusinessException(CameraConstant.FAILURE_6003);
  105. }
  106. CameraDetail detail = cameraDetailService.getByCameraId(camera.getId());
  107. if (detail == null ){
  108. throw new BusinessException(AppConstant.FAILURE_CODE_4012, AppConstant.FAILURE_MSG_4012);
  109. }
  110. return detail.getUserId();
  111. }
  112. public JSONObject login2(AppLoginParam param) {
  113. if(StringUtils.isEmpty(param.getAppUserName()) || StringUtils.isEmpty(param.getAppPassword())){
  114. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  115. }
  116. Long userId = commonCheckCamera(param);
  117. String redisValue = "";
  118. if(userId!= null){
  119. User user = userService.getById(userId);
  120. redisValue = JSONObject.toJSONString(user);
  121. }
  122. String token = redisLogin(param.getAppUserName(),redisValue, 604800L);
  123. JSONObject obj = new JSONObject();
  124. obj.put("token", token);
  125. return obj;
  126. }
  127. public LoginVo quickLogin(LoginParam param) {
  128. if(StringUtils.isEmpty(param.getPhoneNum()) || StringUtils.isEmpty(param.getMsgAuthCode())){
  129. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  130. }
  131. loginService.checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
  132. User user = userService.getByUserName(param.getPhoneNum());
  133. if(user == null){
  134. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  135. }
  136. String token = redisLogin(param.getPhoneNum(), JSONObject.toJSONString(user),21600L);
  137. return commonLogin(user,param,token);
  138. }
  139. public void logout(String token) {
  140. String username = JwtUtil.getUsername(token);
  141. String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ username;
  142. if(redisUtil.hasKey(redisKey)){
  143. redisUtil.del(redisKey);
  144. }
  145. }
  146. public JSONObject getNickName() {
  147. String nickName = null;
  148. Long count = 0L;
  149. do {
  150. nickName = "mob" + RandomUtil.generateShortUuid();
  151. count = userService.getCountByNickName(nickName);
  152. } while (count > 0);
  153. JSONObject jsonObject = new JSONObject();
  154. jsonObject.put("nickName",nickName);
  155. return jsonObject;
  156. }
  157. }