LoginService.java 14 KB

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