package cn.stylefeng.rest.modular.user.service;
|
|
import cn.hutool.core.util.StrUtil;
|
import cn.stylefeng.guns.utils.GeneralUtil;
|
import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginRequest;
|
import cn.stylefeng.roses.kernel.auth.api.pojo.auth.LoginResponse;
|
import cn.stylefeng.roses.kernel.customer.api.exception.CustomerException;
|
import cn.stylefeng.roses.kernel.customer.api.exception.enums.CustomerExceptionEnum;
|
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
|
import cn.stylefeng.roses.kernel.customer.modular.request.CustomerRequest;
|
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
|
import cn.stylefeng.roses.kernel.rule.enums.CustomerUserTypeEnum;
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
|
/**
|
* C端登录业务接口
|
*/
|
@Slf4j
|
@Service
|
public class CustomerLoginBizService {
|
|
@Resource
|
private CustomerService customerService;
|
|
public LoginResponse phoneLogin(String phone, CustomerUserTypeEnum userType) {
|
// 查询用户信息
|
Wrapper wrapper = Wrappers.<Customer>lambdaQuery()
|
.eq(Customer::getUserType, userType.getCode())
|
.and(w -> w
|
.eq(Customer::getAccount, phone)
|
.or().eq(Customer::getTelephone, phone)
|
);
|
Customer customer = customerService.getOne(wrapper, false);
|
|
// 账号不存在时,判断是否去注册
|
assertAccountNoExistToReg(customer, userType, phone, null);
|
|
if (customer == null) {
|
// 可能已经注册的用户,再获取一次用户
|
customer = customerService.getOne(wrapper, false);
|
}
|
// 用户登录信息
|
LoginResponse loginResponse = customerService.wrapperLoginResponse(customer, new LoginRequest());
|
return loginResponse;
|
}
|
|
public LoginResponse wxOpenIdLogin(String wxOpenId, String phone, String workerNo, CustomerUserTypeEnum userType) {
|
// 查询用户信息
|
Wrapper wrapper = Wrappers.<Customer>lambdaQuery()
|
.eq(Customer::getUserType, userType.getCode())
|
.eq(Customer::getWxOpenId, wxOpenId);
|
Customer customer = customerService.getOne(wrapper, false);
|
|
if (customer == null && StrUtil.isNotBlank(phone)) {
|
// 判断手机号是否注册
|
Wrapper phonewrapper = Wrappers.<Customer>lambdaQuery()
|
.eq(Customer::getUserType, userType.getCode())
|
.eq(Customer::getAccount, phone)
|
.last("LIMIT 1");
|
customer = customerService.getOne(phonewrapper, false);
|
if (customer != null) {
|
// 更新微信openId
|
customerService.update(
|
Wrappers.<Customer>lambdaUpdate()
|
.set(Customer::getWxOpenId, wxOpenId)
|
.eq(Customer::getCustomerId, customer.getCustomerId())
|
);
|
}
|
}
|
|
// 账号不存在时,判断是否去注册
|
assertAccountNoExistToReg(customer, userType, phone, wxOpenId);
|
|
if (customer == null) {
|
// 可能已经注册的用户,再获取一次用户
|
customer = customerService.getOne(wrapper, false);
|
}
|
|
if (StrUtil.isNotBlank(workerNo)) {
|
// 绑定顾问岗位ID数据
|
customerService.userBindClassConsultWorkerId(customer.getCustomerId(), workerNo);
|
}
|
|
// 用户登录信息
|
LoginResponse loginResponse = customerService.wrapperLoginResponse(customer, new LoginRequest());
|
return loginResponse;
|
}
|
|
/**
|
* 判断(用户、工作人员)账号是否存在,(用户)不存在则去注册
|
*
|
* @param customer
|
* @param userType
|
* @param phone
|
* @param wxOpenId
|
*/
|
public void assertAccountNoExistToReg(Customer customer, CustomerUserTypeEnum userType, String phone, String wxOpenId) {
|
if (customer == null && StrUtil.isNotBlank(phone)) {
|
if (CustomerUserTypeEnum.USER.getCode().equals(userType.getCode())) {
|
// 用户手机号登录,提示账号不存在时,直接注册
|
CustomerRequest regRequest = new CustomerRequest();
|
regRequest.setAccount(phone);
|
regRequest.setEmail(GeneralUtil.generateEmail(phone));
|
regRequest.setPassword("111111");
|
regRequest.setNickName(GeneralUtil.generateNickName());
|
|
// 扩展注册信息
|
regRequest.setUserType(userType.getCode());
|
regRequest.setTelephone(phone);
|
regRequest.setWxOpenId(wxOpenId);
|
regRequest.setShowId(GeneralUtil.generateShowId());
|
|
// 用户注册
|
customerService.reg(regRequest);
|
} else {
|
// 异常提示账号不存在
|
throw new CustomerException(CustomerExceptionEnum.CANT_FIND_CUSTOMER, phone);
|
}
|
}
|
}
|
}
|