LoginService.java 13 KB

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