LoginService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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("${sms.template.verification-code}")
  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(User user){
  61. String token = this.redisLogin(user.getUserName(),JSONObject.toJSONString(user),"user");
  62. loginLogService.addLog("",token);
  63. UserVo userVo = new UserVo();
  64. BeanUtils.copyProperties(user,userVo);
  65. LoginVo vo = new LoginVo();
  66. vo.setToken(token);
  67. vo.setUser(userVo);
  68. return vo;
  69. }
  70. public LoginVo login(LoginParam param) {
  71. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
  72. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  73. }
  74. String password ;
  75. if(StringUtils.isNotBlank(param.getClear()) && param.getClear().equals("YES")){
  76. password = param.getPassword();
  77. }else {
  78. password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
  79. }
  80. String passwordCode = SecurityUtil.MD5(password);
  81. User user = userService.getByUserName(param.getPhoneNum());
  82. if(user == null){
  83. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  84. }
  85. if(!user.getPassword().equals(passwordCode)){
  86. throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
  87. }
  88. return login(user);
  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(StringUtils.isBlank(areaNum)){
  117. areaNum = "86";
  118. }
  119. if ("86".equals(areaNum)){
  120. String sendCode = null;
  121. try {
  122. sendCode = smsService.sendSms(phoneNum, "{\"code\":\"" + code + "\"}", cnCode);
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. if("isv.BUSINESS_LIMIT_CONTROL".equals(sendCode)){
  127. throw new BusinessException(LoginConstant.FAILURE_CODE_3023, LoginConstant.FAILURE_MSG_3023);
  128. }
  129. }else{
  130. try{
  131. smsService.sendSMSMessage(areaNum + phoneNum, code);
  132. }catch (Exception e){
  133. e.printStackTrace();
  134. throw new BusinessException(LoginConstant.FAILURE_CODE_3013, LoginConstant.FAILURE_MSG_3013);
  135. }
  136. }
  137. if(redisUtil.hasKey(redisKeyMsg)){
  138. redisUtil.del(redisKeyMsg);
  139. }
  140. redisUtil.set(redisKeyMsg,code,300);
  141. redisUtil.set(redisKeyTime,String.valueOf(new Date().getTime()),60);
  142. }
  143. public void register(RegisterParam param) {
  144. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum()) ||
  145. StringUtils.isEmpty(param.getMsgAuthCode())
  146. || StringUtils.isEmpty(param.getConfirmPwd())){
  147. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  148. }
  149. if(StringUtils.isBlank(param.getCountry())){
  150. param.setCountry("86");
  151. }
  152. String password ;
  153. if(StringUtils.isNotBlank(param.getClear()) && param.getClear().equals("YES")){
  154. password = param.getPassword();
  155. }else {
  156. password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
  157. }
  158. if(!password.matches(ConstantFilePath.PASSWORD_REGEX)){
  159. throw new BusinessException(LoginConstant.FAILURE_CODE_3011, LoginConstant.FAILURE_MSG_3011);
  160. }
  161. if (!param.getConfirmPwd().equals(param.getPassword())){
  162. throw new BusinessException(LoginConstant.FAILURE_CODE_3009, LoginConstant.FAILURE_MSG_3009);
  163. }
  164. if(!param.getMsgAuthCode().equals(registerValidCode)){
  165. checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
  166. }
  167. User user = userService.getByUserName(param.getPhoneNum());
  168. if(user != null){
  169. throw new BusinessException(LoginConstant.FAILURE_CODE_3008, LoginConstant.FAILURE_MSG_3008);
  170. }
  171. param.setPassword(password);
  172. userService.register(param);
  173. }
  174. public JSONObject createLoginQrCode() throws Exception {
  175. String uuid = NumberUtils.getUUID();
  176. String filePath = QrCodeFilePath.LOGIN_QR_CODE_PATH + uuid + ".png";
  177. File file = new File(QrCodeFilePath.LOGO_IMAGE_LOCAL);
  178. if(!file.exists()){
  179. fYunFileServiceInterface.downloadFile(QrCodeFilePath.LOGO_IMAGE_OSS,QrCodeFilePath.LOGO_IMAGE_LOCAL);
  180. }
  181. MatrixToImageWriterUtil.createQRCode(NacosProperty.getMainUrl() + "app/index.html?m="+uuid, filePath,true,QrCodeFilePath.LOGO_IMAGE_LOCAL);
  182. JSONObject json = new JSONObject();
  183. json.put("url", filePath.replace(ConstantFilePath.BASE_PATH, ""));
  184. json.put("uuid", uuid);
  185. redisUtil.set(RedisKeyUtil.QRCODE+uuid,uuid,5*60);
  186. return json;
  187. }
  188. public JSONObject sendUserInfo(String uuid) {
  189. if (StringUtils.isEmpty(uuid)){
  190. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  191. }
  192. //二维码失效,清除本地文件二维码
  193. if(!redisUtil.hasKey(RedisKeyUtil.QRCODE +uuid)){
  194. FileUtils.delFile(QrCodeFilePath.LOGIN_QR_CODE_PATH + uuid + ".png");
  195. throw new BusinessException(LoginConstant.FAILURE_CODE_3035, LoginConstant.FAILURE_MSG_3035);
  196. }
  197. if(!redisUtil.hasKey(uuid)){
  198. throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
  199. }
  200. String childName = redisUtil.get(uuid);
  201. Camera camera = cameraService.getBySnCode(childName);
  202. if(camera == null){
  203. throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
  204. }
  205. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  206. if(cameraDetail == null){
  207. throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
  208. }
  209. UserVo userVo = new UserVo();
  210. userVo.setUserName(childName);
  211. userVo.setId(cameraDetail.getUserId());
  212. userVo.setCameraId(camera.getId());
  213. userVo.setCameraLogin(1);
  214. String token = this.redisLogin(childName,JSONObject.toJSONString(userVo),"camera");
  215. JSONObject obj = new JSONObject();
  216. obj.put("token",token);
  217. obj.put("childName",childName);
  218. obj.put("to",1);
  219. redisUtil.del(uuid);
  220. FileUtils.deleteFile(QrCodeFilePath.LOGIN_QR_CODE_PATH +uuid +".png");
  221. return obj;
  222. }
  223. public void getEmailAuthCode(String email, String country) throws Exception {
  224. if(StringUtils.isEmpty(email) || StringUtils.isEmpty(country)){
  225. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  226. }
  227. String code = String.valueOf((int)((Math.random()*9+1)*100000));
  228. switch (country) {
  229. case "1":
  230. case "33":
  231. mailTemplateService.sendCodeMail(email,code,"en");
  232. break;
  233. case "2":
  234. mailTemplateService.sendCodeMail(email,code,"en");
  235. break;
  236. default:
  237. mailTemplateService.sendCodeMail(email,code,"zh");
  238. break;
  239. }
  240. //删除缓存
  241. if (redisUtil.hasKey(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email)){
  242. redisUtil.del(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email);
  243. }
  244. //短信验证码,5分钟有效
  245. redisUtil.set(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email, code, 300);
  246. }
  247. public void changePassword(RegisterParam param) {
  248. if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getConfirmPwd()) ||
  249. StringUtils.isEmpty(param.getMsgAuthCode())
  250. || StringUtils.isEmpty(param.getPhoneNum())){
  251. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  252. }
  253. if (!param.getPassword().equals(param.getConfirmPwd())){
  254. throw new BusinessException(LoginConstant.FAILURE_CODE_3009, LoginConstant.FAILURE_MSG_3009);
  255. }
  256. //对前端传的密码解密
  257. String password;
  258. if(StringUtils.isNotBlank(param.getClear()) && param.getClear().equals("YES")){
  259. password = param.getPassword();
  260. }else {
  261. password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
  262. }
  263. //正则判断密码是否符合规则(8位以上并且数字英文组合)
  264. if(!password.matches(ConstantRegex.PASSWORD_REGEX)){
  265. throw new BusinessException(LoginConstant.FAILURE_CODE_3011, LoginConstant.FAILURE_MSG_3011);
  266. }
  267. User user = userService.getByUserName(param.getPhoneNum());
  268. if(user == null){
  269. throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
  270. }
  271. if(!param.getMsgAuthCode().equals(registerValidCode)){
  272. checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
  273. }
  274. String pwdMd5 = SecurityUtil.MD5(password);
  275. userService.updatePassword(param.getPhoneNum(), pwdMd5);
  276. }
  277. public String redisLogin(String userName,String value,String loginType){
  278. String token = JwtUtil.createJWT(-1,userName,loginType);
  279. String redisKey = String.format(RedisKey.TOKEN_V3,token);
  280. redisUtil.set(redisKey, value,2 * 60 * 60);
  281. return token;
  282. }
  283. public void loginCheck(String token) {
  284. String redisKey = String.format(RedisKey.TOKEN_V3,token);
  285. if(!redisUtil.hasKey(redisKey)){
  286. throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
  287. }
  288. }
  289. public void checkSms(String msgAuthCode, String userName,boolean del) {
  290. //验证码校验
  291. String codeValue = redisUtil.get(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + userName);
  292. if (StringUtils.isEmpty(codeValue)){
  293. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  294. }
  295. if (!codeValue.equals(msgAuthCode)){
  296. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  297. }
  298. if(del){
  299. redisUtil.del(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + userName);
  300. }
  301. }
  302. public LoginVo loginClear(LoginParam param) {
  303. param.setClear("YES"); //明文登录
  304. return login(param);
  305. }
  306. }