package com.doumee.service.business.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.doumee.config.jwt.JwtTokenUtil;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.core.model.LoginUserInfo;
|
import com.doumee.core.utils.Constants;
|
import com.doumee.dao.business.YwCustomerMapper;
|
import com.doumee.dao.business.model.YwCustomer;
|
import com.doumee.dao.system.dto.LoginPhoneDTO;
|
import com.doumee.service.business.YwCustomerH5AuthService;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.Collections;
|
import java.util.Date;
|
|
@Service
|
public class YwCustomerH5AuthServiceImpl implements YwCustomerH5AuthService {
|
|
@Autowired
|
private YwCustomerMapper ywCustomerMapper;
|
@Autowired
|
private JwtTokenUtil jwtTokenUtil;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public String loginByPhone(LoginPhoneDTO dto) {
|
YwCustomer customer = findActiveByPhone(dto.getPhone());
|
bindOpenId(customer, dto.getOpenid());
|
touchLogin(customer);
|
return jwtTokenUtil.generateToken(toLoginUserInfo(customer));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public String loginByCustomerId(Integer customerId) {
|
YwCustomer customer = requireActiveCustomer(customerId);
|
touchLogin(customer);
|
return jwtTokenUtil.generateToken(toLoginUserInfo(customer));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public String loginByOpenId(String openId) {
|
if (StringUtils.isBlank(openId)) {
|
return null;
|
}
|
YwCustomer customer = ywCustomerMapper.selectOne(new QueryWrapper<YwCustomer>().lambda()
|
.eq(YwCustomer::getIsdeleted, Constants.ZERO)
|
.eq(YwCustomer::getOpenid, openId.trim())
|
.last(" limit 1 "));
|
if (customer == null) {
|
return null;
|
}
|
assertCustomerEnabled(customer);
|
touchLogin(customer);
|
return jwtTokenUtil.generateToken(toLoginUserInfo(customer));
|
}
|
|
@Override
|
public LoginUserInfo buildLoginUserInfo(Integer customerId) {
|
return toLoginUserInfo(requireActiveCustomer(customerId));
|
}
|
|
private YwCustomer findActiveByPhone(String phone) {
|
if (StringUtils.isBlank(phone)) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "手机号不能为空");
|
}
|
YwCustomer customer = ywCustomerMapper.selectOne(new QueryWrapper<YwCustomer>().lambda()
|
.eq(YwCustomer::getIsdeleted, Constants.ZERO)
|
.eq(YwCustomer::getPhone, phone.trim())
|
.last(" limit 1 "));
|
if (customer == null) {
|
throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), "商户不存在或未注册");
|
}
|
assertCustomerEnabled(customer);
|
return customer;
|
}
|
|
private YwCustomer requireActiveCustomer(Integer customerId) {
|
if (customerId == null) {
|
throw new BusinessException(ResponseStatus.DATA_EMPTY);
|
}
|
YwCustomer customer = ywCustomerMapper.selectById(customerId);
|
if (customer == null || Constants.equalsInteger(customer.getIsdeleted(), Constants.ONE)) {
|
throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), "商户不存在或未注册");
|
}
|
assertCustomerEnabled(customer);
|
return customer;
|
}
|
|
private void assertCustomerEnabled(YwCustomer customer) {
|
if (customer.getStatus() != null && Constants.equalsInteger(customer.getStatus(), Constants.ONE)) {
|
throw new BusinessException(ResponseStatus.NO_ALLOW_LOGIN.getCode(), "商户账号已禁用");
|
}
|
}
|
|
private void bindOpenId(YwCustomer customer, String openid) {
|
if (StringUtils.isBlank(openid)) {
|
return;
|
}
|
ywCustomerMapper.update(null, new UpdateWrapper<YwCustomer>().lambda()
|
.set(YwCustomer::getOpenid, null)
|
.eq(YwCustomer::getOpenid, openid.trim())
|
.ne(YwCustomer::getId, customer.getId()));
|
customer.setOpenid(openid.trim());
|
}
|
|
private void touchLogin(YwCustomer customer) {
|
Date now = new Date();
|
customer.setLastLoginDate(now);
|
customer.setEditDate(now);
|
customer.setLoginNum((customer.getLoginNum() == null ? 0 : customer.getLoginNum()) + 1);
|
ywCustomerMapper.updateById(customer);
|
}
|
|
private LoginUserInfo toLoginUserInfo(YwCustomer customer) {
|
LoginUserInfo loginUserInfo = new LoginUserInfo();
|
loginUserInfo.setCustomerId(customer.getId());
|
loginUserInfo.setId(customer.getId());
|
loginUserInfo.setH5UserType(LoginUserInfo.H5_USER_CUSTOMER);
|
loginUserInfo.setRealname(customer.getName());
|
loginUserInfo.setMobile(customer.getPhone());
|
loginUserInfo.setUsername("customer_" + customer.getId());
|
loginUserInfo.setSource(LoginUserInfo.SOURCE_H5_CUSTOMER);
|
loginUserInfo.setRoles(Collections.singletonList("h5_customer"));
|
loginUserInfo.setPermissions(Collections.emptyList());
|
return loginUserInfo;
|
}
|
}
|