LoginService.java 14 KB

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