From ec15861e14c66c38b1a8f5fffc6975d7da6c315c Mon Sep 17 00:00:00 2001 From: zhujie <leon.zhu@cloudroam.com.cn> Date: 星期三, 23 四月 2025 02:31:00 +0800 Subject: [PATCH] 1 --- src/main/java/com/mzl/flower/web/login/CustomerLoginController.java | 137 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 136 insertions(+), 1 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..024b956 100644 --- a/src/main/java/com/mzl/flower/web/login/CustomerLoginController.java +++ b/src/main/java/com/mzl/flower/web/login/CustomerLoginController.java @@ -8,14 +8,18 @@ import com.mzl.flower.base.cache.StringCacheClient; import com.mzl.flower.config.exception.BaseException; import com.mzl.flower.config.exception.ValidationException; +import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.config.security.token.PhoneAuthenticationToken; import com.mzl.flower.config.security.token.UserIdAuthenticationToken; import com.mzl.flower.config.security.token.WebAuthenticationToken; 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.dto.security.UserDTO; +import com.mzl.flower.entity.customer.Customer; import com.mzl.flower.entity.system.User; import com.mzl.flower.mapper.customer.CustomerMapper; import com.mzl.flower.service.customer.CustomerService; @@ -27,6 +31,7 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationDetailsSource; @@ -70,6 +75,9 @@ @Autowired private CustomerMapper customerMapper; + + @Autowired + private CustomerService customerService; @Autowired @@ -259,4 +267,131 @@ } } } -} + + @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()); + User user1 = null; + String tokenCache =""; + if(user == null){ + loginDTO.setUserType(Constants.USER_TYPE.customer.name()); + 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("用户已禁用,请联系管理员"); + } + + //验证邀请码 + if (StringUtils.isNotEmpty(loginDTO.getIntevailCode())) { + 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); + if(user == null){ + stringCacheClient.set(TOKEN_KEY + SEPARATOR + user1.getId(),token.getValue()); + }else { + 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(),"登录错误"); + } + } + + + @PostMapping("/account/close") + @ApiOperation(value = "账户注销", notes = "账户注销") + public ResponseEntity<ReturnDataDTO<Boolean>> accountClose() { + + String userId = SecurityUtils.getUserId(); + + if(StringUtils.isNotBlank(userId)){ + // + CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(userId); + if(null==currentCustomer){ + throw new BaseException(R.RUNTIME_EXCEPTION.getCode(),"用户不存在"); + } + + currentCustomer.setIsClosed(true); + + User user=userService.getUserById(userId); + + + if (StringUtils.isNotBlank(user.getLoginName())) { + currentCustomer.setName("智信-" + user.getLoginName().substring(user.getLoginName().length() - 4)); + + } + currentCustomer.setCover("https://hmy-flower.oss-cn-shanghai.aliyuncs.com/8f/8f205ea4618b4ce48d5bd204ae73f075tmp_f0c47a66148245dc17d74563c5939e764273ba583619664d.jpg"); + + Customer customer=new Customer(); + BeanUtils.copyProperties(currentCustomer,customer); + + // 会员注销 + customer.setIsMember(false); + // 会员过期时间设置为空 + customer.setMemberOvertime(null); + + customerMapper.updateById(customer); + + return returnData(R.SUCCESS.getCode(),true); + + }else{ + throw new BaseException(R.RUNTIME_EXCEPTION.getCode(),"用户ID不能为空"); + } + + + } + + + } -- Gitblit v1.9.3