cloudroam
2025-03-29 5997dc8acfa81a6c867c28810d1c3c9714efc46c
src/main/java/com/mzl/flower/web/login/CustomerLoginController.java
@@ -14,10 +14,13 @@
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;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.service.customer.CustomerService;
import com.mzl.flower.service.impl.WechatUserDetailsService;
import com.mzl.flower.service.login.LoginService;
import com.mzl.flower.service.system.UserService;
import com.mzl.flower.service.system.WeChatService;
@@ -41,6 +44,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.Map;
@Slf4j
@RestController
@@ -76,6 +80,7 @@
    public static final String SEPARATOR = ":";
    @PostMapping("/login/wechat")
    @ApiOperation(value = "微信小程序登录", notes = "微信小程序登录")
    public ResponseEntity<ReturnDataDTO<OAuth2AccessToken>> loginWeChat(
@@ -84,6 +89,17 @@
        String openId = session.getOpenid();
        String sessionKey = session.getSessionKey();
        String unionId = session.getUnionid();
        // 判断当前用户账号是否存在
        dto.setOpenId(openId);
        dto.setSessionKey(sessionKey);
        dto.setUnionId(unionId);
        dto.setUserType(Constants.USER_TYPE.customer.name());
        User existUserByOpenId = userService.getExistUserByOpenId(openId, sessionKey, unionId, Constants.USER_TYPE.customer.name());
        if(ObjectUtils.isEmpty(existUserByOpenId)){
            userService.registWxUser(dto);
        }
        User user = userService.findUserByOpenId(openId, sessionKey, unionId
                , dto.getImgUrl(), dto.getNickname(),Constants.USER_TYPE.customer.name());
        String tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user.getId());
@@ -106,6 +122,23 @@
            log.error(e.getMessage(), e);
            throw new BaseException(R.RUNTIME_EXCEPTION.getCode(), "登录错误");
        }
    }
    @PostMapping("/wx/getuserphonenumber")
    @ApiOperation(value = "获取当前授权的手机号")
    public ResponseEntity<ReturnDataDTO> getuserphonenumber(@RequestBody CreateWechatUserDTO dto) throws Exception{
        return returnData(R.SUCCESS.getCode(), weChatService.getuserphonenumber(dto));
    }
    @PostMapping("/wx/getExistUserByOpenId")
    @ApiOperation(value = "查看当前微信号是否已经绑定了用户")
    public ResponseEntity<ReturnDataDTO> getExistUserByOpenId(@RequestBody CreateWechatUserDTO dto) throws Exception{
        WxMaJscode2SessionResult session = weChatService.getWxMaSessionInfo(dto.getCode());
        String openId = session.getOpenid();
        String sessionKey = session.getSessionKey();
        String unionId = session.getUnionid();
        User user=userService.getExistUserByOpenId(openId, sessionKey, unionId,dto.getUserType());
        return returnData(R.SUCCESS.getCode(), new WxUserVO(openId,sessionKey,unionId,user));
    }
    @GetMapping("/wx/jscode2session")
@@ -185,6 +218,13 @@
        if(user == null){
            throw new ValidationException("用户不存在");
        }
        CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(user.getId());
        if (ObjectUtils.isEmpty(currentCustomer)) {
            throw new ValidationException("用户不存在");
        }
        if (currentCustomer.getIsEnabled() == false) {
            throw new ValidationException("用户已禁用,请联系管理员");
        }
        String tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + user.getId());
        if (StringUtils.isNotBlank(tokenCache))  {
            //强制删除token,下线
@@ -220,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(),"登录错误");
        }
    }
}