LoginService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.common.constant.ConstantRegex;
  4. import com.fdkankan.common.exception.BusinessException;
  5. import com.fdkankan.common.util.Base64Converter;
  6. import com.fdkankan.common.util.FileUtils;
  7. import com.fdkankan.common.util.JwtUtil;
  8. import com.fdkankan.common.util.NumberUtils;
  9. import com.fdkankan.common.util.SecurityUtil;
  10. import com.fdkankan.image.MatrixToImageWriterUtil;
  11. import com.fdkankan.model.constants.ConstantFilePath;
  12. import com.fdkankan.redis.constant.RedisKey;
  13. import com.fdkankan.redis.util.RedisUtil;
  14. import com.fdkankan.sms.SendMailAcceUtils;
  15. import com.fdkankan.sms.SmsService;
  16. import com.fdkankan.ucenter.common.RedisKeyUtil;
  17. import com.fdkankan.ucenter.constant.LoginConstant;
  18. import com.fdkankan.ucenter.constant.QrCodeFilePath;
  19. import com.fdkankan.ucenter.entity.Camera;
  20. import com.fdkankan.ucenter.entity.CameraDetail;
  21. import com.fdkankan.ucenter.entity.User;
  22. import com.fdkankan.ucenter.service.ICameraDetailService;
  23. import com.fdkankan.ucenter.service.ICameraService;
  24. import com.fdkankan.ucenter.service.IUserService;
  25. import com.fdkankan.ucenter.vo.request.LoginParam;
  26. import com.fdkankan.ucenter.vo.request.RegisterParam;
  27. import com.fdkankan.ucenter.vo.response.LoginVo;
  28. import com.fdkankan.ucenter.vo.response.UserVo;
  29. import java.util.Date;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.springframework.beans.BeanUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.beans.factory.annotation.Value;
  34. import org.springframework.stereotype.Service;
  35. @Service
  36. public class LoginService {
  37. @Autowired
  38. private IUserService userService;
  39. @Autowired
  40. private RedisUtil redisUtil;
  41. @Autowired
  42. private SmsService smsService;
  43. @Autowired
  44. private ICameraService cameraService;
  45. @Autowired
  46. private ICameraDetailService cameraDetailService;
  47. @Value("${phone.code.cn}")
  48. private String cnCode;
  49. @Value("${main.url}")
  50. private String mainUrl;
  51. public Object login(LoginParam param) {
  52. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
  53. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  54. }
  55. String password ;
  56. if(StringUtils.isNotBlank(param.getClear()) && param.getClear().equals("YES")){
  57. password = param.getPassword();
  58. }else {
  59. password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
  60. }
  61. String passwordCode = SecurityUtil.MD5(password);
  62. User user = userService.getByUserName(param.getPhoneNum());
  63. if(user == null){
  64. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  65. }
  66. if(!user.getPassword().equals(passwordCode)){
  67. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  68. }
  69. String token = this.redisLogin(user.getUserName(),JSONObject.toJSONString(user));
  70. UserVo userVo = new UserVo();
  71. BeanUtils.copyProperties(user,userVo);
  72. LoginVo vo = new LoginVo();
  73. vo.setToken(token);
  74. vo.setUser(userVo);
  75. return vo;
  76. }
  77. public void logout(String token) {
  78. String redisKey = String.format(RedisKey.TOKEN_V3,token);
  79. if(redisUtil.hasKey(redisKey)){
  80. redisUtil.del(redisKey);
  81. }
  82. }
  83. public void checkUser(String phoneNum,Boolean flg) {
  84. if(StringUtils.isBlank(phoneNum)){
  85. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  86. }
  87. User user = userService.getByUserName(phoneNum);
  88. if(user == null && flg){
  89. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  90. }
  91. if(user != null && !flg){
  92. throw new BusinessException(LoginConstant.FAILURE_CODE_3008, LoginConstant.FAILURE_MSG_3008);
  93. }
  94. }
  95. public void getMsgAuthCode(String areaNum, String phoneNum) {
  96. String redisKeyTime = RedisKeyUtil.PREFIX_MSG_NOT_CODE + phoneNum; //重发验证
  97. String redisKeyMsg = RedisKeyUtil.PREFIX_MSG_AUTH_CODE + phoneNum; //验证码code
  98. long value = redisUtil.getExpire(redisKeyTime);
  99. if(value != -2){
  100. throw new BusinessException(LoginConstant.FAILURE_CODE_3033, String.valueOf(value));
  101. }
  102. String code = String.valueOf((int)((Math.random()*9+1)*100000));
  103. if ("86".equals(areaNum)){
  104. String sendCode = null;
  105. try {
  106. sendCode = smsService.sendSms(phoneNum, "{\"code\":\"" + code + "\"}", cnCode);
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. if("isv.BUSINESS_LIMIT_CONTROL".equals(sendCode)){
  111. throw new BusinessException(LoginConstant.FAILURE_CODE_3023, LoginConstant.FAILURE_MSG_3023);
  112. }
  113. }else{
  114. try{
  115. smsService.sendSMSMessage(areaNum + phoneNum, code);
  116. }catch (Exception e){
  117. e.printStackTrace();
  118. throw new BusinessException(LoginConstant.FAILURE_CODE_3013, LoginConstant.FAILURE_MSG_3013);
  119. }
  120. }
  121. if(redisUtil.hasKey(redisKeyMsg)){
  122. redisUtil.del(redisKeyMsg);
  123. }
  124. redisUtil.set(redisKeyMsg,code,300);
  125. redisUtil.set(redisKeyTime,String.valueOf(new Date().getTime()),60);
  126. }
  127. public void register(RegisterParam param) {
  128. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum()) ||
  129. StringUtils.isEmpty(param.getMsgAuthCode()) || StringUtils.isEmpty(param.getCountry()) || StringUtils.isEmpty(param.getConfirmPwd())){
  130. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  131. }
  132. if(!param.getPassword().matches(ConstantRegex.PASSWORD_REGEX)){
  133. throw new BusinessException(LoginConstant.FAILURE_CODE_3011, LoginConstant.FAILURE_MSG_3011);
  134. }
  135. if (!param.getConfirmPwd().equals(param.getPassword())){
  136. throw new BusinessException(LoginConstant.FAILURE_CODE_3009, LoginConstant.FAILURE_MSG_3009);
  137. }
  138. checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
  139. User user = userService.getByUserName(param.getPhoneNum());
  140. if(user != null){
  141. throw new BusinessException(LoginConstant.FAILURE_CODE_3008, LoginConstant.FAILURE_MSG_3008);
  142. }
  143. userService.register(param);
  144. }
  145. public JSONObject createLoginQrCode() throws Exception {
  146. String uuid = NumberUtils.getUUID();
  147. String filePath = QrCodeFilePath.LOGIN_QR_CODE_PATH + uuid + ".png";
  148. String path =this.getClass().getResource("/static/img/logo.jpg").getPath();
  149. MatrixToImageWriterUtil.createQRCode(mainUrl + "app/index.html?m="+uuid, filePath,true,path);
  150. JSONObject json = new JSONObject();
  151. json.put("url", filePath.replace(ConstantFilePath.BASE_PATH, ""));
  152. json.put("uuid", uuid);
  153. redisUtil.set(RedisKeyUtil.QRCODE+uuid,uuid,5*60);
  154. return json;
  155. }
  156. public JSONObject sendUserInfo(String uuid) {
  157. if (StringUtils.isEmpty(uuid)){
  158. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  159. }
  160. //二维码失效,清除本地文件二维码
  161. if(!redisUtil.hasKey(RedisKeyUtil.QRCODE +uuid)){
  162. FileUtils.delFile(QrCodeFilePath.LOGIN_QR_CODE_PATH + uuid + ".png");
  163. throw new BusinessException(LoginConstant.FAILURE_CODE_3035, LoginConstant.FAILURE_MSG_3035);
  164. }
  165. if(!redisUtil.hasKey(uuid)){
  166. throw new BusinessException(LoginConstant.FAILURE_CODE_3035, LoginConstant.FAILURE_MSG_3035);
  167. }
  168. String childName = redisUtil.get(uuid);
  169. Camera camera = cameraService.getBySnCode(childName);
  170. if(camera == null){
  171. throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
  172. }
  173. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  174. if(cameraDetail == null){
  175. throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
  176. }
  177. UserVo userVo = new UserVo();
  178. userVo.setUserName(childName);
  179. userVo.setId(cameraDetail.getUserId());
  180. userVo.setCameraId(camera.getId());
  181. userVo.setCameraLogin(1);
  182. String token = this.redisLogin(childName,JSONObject.toJSONString(userVo));
  183. JSONObject obj = new JSONObject();
  184. obj.put("token",token);
  185. obj.put("childName",childName);
  186. obj.put("to",1);
  187. redisUtil.del(uuid);
  188. FileUtils.deleteFile(QrCodeFilePath.LOGIN_QR_CODE_PATH +uuid +".png");
  189. return obj;
  190. }
  191. public void getEmailAuthCode(String email, String country) throws Exception {
  192. if(StringUtils.isEmpty(email) || StringUtils.isEmpty(country)){
  193. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  194. }
  195. String code = String.valueOf((int)((Math.random()*9+1)*100000));
  196. if("1".equals(country)){
  197. SendMailAcceUtils.sendMail(email, SendMailAcceUtils.EN_CODE_SUBJECT, SendMailAcceUtils.EN_CODE_MSG.replace("${code}", code), null);
  198. }if("2".equals(country)){
  199. SendMailAcceUtils.sendMailUsa(email, SendMailAcceUtils.EN_CODE_SUBJECT_USA, SendMailAcceUtils.EN_CODE_MSG_USA.replace("${code}", code), null);
  200. }else {
  201. SendMailAcceUtils.sendMail(email, SendMailAcceUtils.CN_CODE_SUBJECT, SendMailAcceUtils.CN_CODE_MSG.replace("${code}", code), null);
  202. }
  203. //删除缓存
  204. if (redisUtil.hasKey(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email)){
  205. redisUtil.del(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email);
  206. }
  207. //短信验证码,5分钟有效
  208. redisUtil.set(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email, code, 300);
  209. }
  210. public void changePassword(RegisterParam param) {
  211. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getConfirmPwd()) ||
  212. StringUtils.isEmpty(param.getMsgAuthCode())
  213. || StringUtils.isEmpty(param.getPhoneNum())){
  214. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  215. }
  216. if (!param.getPassword().equals(param.getConfirmPwd())){
  217. throw new BusinessException(LoginConstant.FAILURE_CODE_3009, LoginConstant.FAILURE_MSG_3009);
  218. }
  219. //对前端传的密码解密
  220. String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
  221. //正则判断密码是否符合规则(8位以上并且数字英文组合)
  222. if(!password.matches(ConstantRegex.PASSWORD_REGEX)){
  223. throw new BusinessException(LoginConstant.FAILURE_CODE_3011, LoginConstant.FAILURE_MSG_3011);
  224. }
  225. User user = userService.getByUserName(param.getPhoneNum());
  226. if(user == null){
  227. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  228. }
  229. checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
  230. String pwdMd5 = SecurityUtil.MD5(password);
  231. userService.updatePassword(param.getPhoneNum(), pwdMd5);
  232. }
  233. public String redisLogin(String userName,String value){
  234. String token = JwtUtil.createJWT(-1,userName,"user");
  235. String redisKey = String.format(RedisKey.TOKEN_V3,token);
  236. redisUtil.set(redisKey, value,2 * 60 * 60);
  237. return token;
  238. }
  239. public void loginCheck(String token) {
  240. String redisKey = String.format(RedisKey.TOKEN_V3,token);
  241. if(!redisUtil.hasKey(redisKey)){
  242. throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
  243. }
  244. }
  245. public void checkSms(String msgAuthCode, String userName,boolean del) {
  246. //验证码校验
  247. String codeValue = redisUtil.get(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + userName);
  248. if (StringUtils.isEmpty(codeValue)){
  249. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  250. }
  251. if (!codeValue.equals(msgAuthCode)){
  252. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  253. }
  254. if(del){
  255. redisUtil.del(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + userName);
  256. }
  257. }
  258. public Object loginClear(LoginParam param) {
  259. param.setClear("YES"); //明文登录
  260. return login(param);
  261. }
  262. }