| | |
| | | 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; |
| | |
| | | |
| | | @Autowired |
| | | private CustomerMapper customerMapper; |
| | | |
| | | @Autowired |
| | | private CustomerService customerService; |
| | | |
| | | |
| | | @Autowired |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | @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(),"登录错误"); |
| | | } |
| | | } |
| | | } |