LoginService.java 14 KB

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