LoginService.java 15 KB

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