LoginService.java 14 KB

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