From f060439c675cb9185252cfc8f034853290863c62 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期一, 31 三月 2025 09:45:39 +0800 Subject: [PATCH] fix 登录 --- src/main/java/com/mzl/flower/web/login/CustomerLoginController.java | 71 +++++++++++++++++++++++++++++++++++ 1 files changed, 71 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/mzl/flower/web/login/CustomerLoginController.java b/src/main/java/com/mzl/flower/web/login/CustomerLoginController.java index f0b1510..ee32007 100644 --- a/src/main/java/com/mzl/flower/web/login/CustomerLoginController.java +++ b/src/main/java/com/mzl/flower/web/login/CustomerLoginController.java @@ -14,6 +14,7 @@ import com.mzl.flower.constant.Constants; import com.mzl.flower.dto.request.CreateWechatUserDTO; import com.mzl.flower.dto.request.UserLoginDTO; +import com.mzl.flower.dto.request.UserPhoneLoginDTO; import com.mzl.flower.dto.response.customer.CustomerDTO; import com.mzl.flower.dto.response.wx.WxUserVO; import com.mzl.flower.entity.system.User; @@ -259,4 +260,74 @@ } } } + + @PostMapping("/login/customer/phone/v2") + @ApiOperation(value = "手机验证码登录系统", notes = "手机验证码登录系统") + public ResponseEntity<ReturnDataDTO<OAuth2AccessToken>> loginPhoneV2(HttpServletRequest request, + @RequestBody UserPhoneLoginDTO loginDTO) { + String tel = loginDTO.getUsername(); + String smsCode = loginDTO.getSmsCode(); + if (StringUtils.isBlank(tel)) { + throw new ValidationException("手机号码不能为空"); + } + if (StringUtils.isBlank(smsCode)) { + throw new ValidationException("手机验证码不能为空"); + } + //从缓存中获取验证码 + String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.customer.name() + SEPARATOR + tel); + if (!StringUtils.equals(smsCode, smsCacheCode)) { + throw new ValidationException("手机验证码不正确"); + } + + User user = userService.findByTel(tel, Constants.USER_TYPE.customer.name()); + String tokenCache =""; + if(user == null){ + loginDTO.setUserType(Constants.USER_TYPE.customer.name()); + User user1 = userService.registPhoneUser(loginDTO); + if(org.springframework.util.StringUtils.isEmpty(user1)){ + throw new ValidationException("注册用户信息报错"); + } + tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user1.getId()); + CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(user1.getId()); + if (ObjectUtils.isEmpty(currentCustomer)) { + throw new ValidationException("用户不存在"); + } + if (currentCustomer.getIsEnabled() == false) { + throw new ValidationException("用户已禁用,请联系管理员"); + } + tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user1.getId()); + if (StringUtils.isNotBlank(tokenCache)) { + //强制删除token,下线 + removeToken(tokenCache,user.getId()); + } + }else{ + CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(user.getId()); + if (ObjectUtils.isEmpty(currentCustomer)) { + throw new ValidationException("用户不存在"); + } + if (currentCustomer.getIsEnabled() == false) { + throw new ValidationException("用户已禁用,请联系管理员"); + } + tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user.getId()); + if (StringUtils.isNotBlank(tokenCache)) { + //强制删除token,下线 + removeToken(tokenCache,user.getId()); + } + } + try { + PhoneAuthenticationToken authRequest = new PhoneAuthenticationToken(tel, smsCode, Constants.USER_TYPE.customer.name()); + authRequest.setDetails(authenticationDetailsSource.buildDetails(request)); + Authentication authentication = authenticationManager.authenticate(authRequest); + OAuth2AccessToken token = loginService.getAccessToken(authentication, Constants.USER_TYPE.customer.name()); + //删除缓存中的验证码 + stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.customer.name() + SEPARATOR + tel); + stringCacheClient.set(TOKEN_KEY + SEPARATOR + user.getId(),token.getValue()); + return returnData(R.SUCCESS.getCode(),token); + }catch (UsernameNotFoundException e){ + throw new ValidationException("用户不存在"); + }catch (Exception e) { + log.error(e.getMessage(), e); + throw new BaseException(R.RUNTIME_EXCEPTION.getCode(),"登录错误"); + } + } } -- Gitblit v1.9.3