|
|
@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.bx.imclient.IMClient; |
|
|
import com.bx.imclient.IMClient; |
|
|
import com.bx.imcommon.enums.IMTerminalType; |
|
|
import com.bx.imcommon.enums.IMTerminalType; |
|
|
|
|
|
import com.bx.imcommon.model.IMSystemMessage; |
|
|
import com.bx.imcommon.util.JwtUtil; |
|
|
import com.bx.imcommon.util.JwtUtil; |
|
|
import com.bx.implatform.config.props.JwtProperties; |
|
|
import com.bx.implatform.config.props.JwtProperties; |
|
|
import com.bx.implatform.dto.LoginDTO; |
|
|
import com.bx.implatform.dto.LoginDTO; |
|
|
@ -20,17 +21,14 @@ import com.bx.implatform.entity.*; |
|
|
import com.bx.implatform.enums.ResultCode; |
|
|
import com.bx.implatform.enums.ResultCode; |
|
|
import com.bx.implatform.exception.GlobalException; |
|
|
import com.bx.implatform.exception.GlobalException; |
|
|
import com.bx.implatform.mapper.UserMapper; |
|
|
import com.bx.implatform.mapper.UserMapper; |
|
|
import com.bx.implatform.result.ResultUtils; |
|
|
|
|
|
import com.bx.implatform.service.*; |
|
|
import com.bx.implatform.service.*; |
|
|
import com.bx.implatform.session.SessionContext; |
|
|
import com.bx.implatform.session.SessionContext; |
|
|
import com.bx.implatform.session.UserSession; |
|
|
import com.bx.implatform.session.UserSession; |
|
|
import com.bx.implatform.util.BeanUtils; |
|
|
import com.bx.implatform.util.BeanUtils; |
|
|
import com.bx.implatform.util.IpUtils; |
|
|
|
|
|
import com.bx.implatform.util.SensitiveFilterUtil; |
|
|
import com.bx.implatform.util.SensitiveFilterUtil; |
|
|
import com.bx.implatform.vo.*; |
|
|
import com.bx.implatform.vo.*; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder; |
|
|
import org.springframework.security.crypto.password.PasswordEncoder; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
@ -39,8 +37,6 @@ import org.springframework.util.StringUtils; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
import static com.bx.implatform.enums.ResultCode.XSS_PARAM_ERROR; |
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@Service |
|
|
@Service |
|
|
@RequiredArgsConstructor |
|
|
@RequiredArgsConstructor |
|
|
@ -193,38 +189,39 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 通过uniqueToken查询客服ID,如果有多条匹配则随机返回一条 |
|
|
* 通过uniqueToken查询客服ID,【优先分配在线客服】,在线客服为空则随机分配离线客服 |
|
|
*/ |
|
|
*/ |
|
|
public Long getCustomerServiceIdByUniqueToken(String uniqueToken) { |
|
|
public Long getCustomerServiceIdByUniqueToken(String uniqueToken) { |
|
|
// 1. 构建查询条件
|
|
|
|
|
|
LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery(); |
|
|
LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery(); |
|
|
queryWrapper.eq(User::getIsCustomer, 2); // 只查客服
|
|
|
queryWrapper.eq(User::getIsCustomer, 2); |
|
|
|
|
|
|
|
|
if (StrUtil.isNotBlank(uniqueToken)) { |
|
|
|
|
|
queryWrapper.eq(User::getUniqueToken, uniqueToken); |
|
|
queryWrapper.eq(User::getUniqueToken, uniqueToken); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
queryWrapper.select(User::getId); |
|
|
queryWrapper.select(User::getId); |
|
|
|
|
|
|
|
|
// 2. 查询所有匹配的客服
|
|
|
|
|
|
List<User> customerList = this.list(queryWrapper); |
|
|
List<User> customerList = this.list(queryWrapper); |
|
|
|
|
|
|
|
|
// 3. 如果没有匹配的客服,抛出异常
|
|
|
|
|
|
if (customerList == null || customerList.isEmpty()) { |
|
|
if (customerList == null || customerList.isEmpty()) { |
|
|
throw new GlobalException("未找到对应的客服,请检查 uniqueToken 是否正确"); |
|
|
throw new GlobalException("未找到对应的客服,请检查 uniqueToken 是否正确"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 4. 如果只有一条,直接返回
|
|
|
List<Long> allCustomerIds = customerList.stream() |
|
|
if (customerList.size() == 1) { |
|
|
.map(User::getId) |
|
|
Long customerId = customerList.get(0).getId(); |
|
|
.collect(Collectors.toList()); |
|
|
return customerId; |
|
|
List<Long> onlineCustomerIds = imClient.getOnlineUser(allCustomerIds); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 5. 如果有多条,随机选择一条返回
|
|
|
List<User> onlineCustomerList = customerList.stream() |
|
|
int randomIndex = new Random().nextInt(customerList.size()); |
|
|
.filter(user -> onlineCustomerIds.contains(user.getId())) |
|
|
Long randomCustomerId = customerList.get(randomIndex).getId(); |
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
List<User> targetList; |
|
|
|
|
|
if (!onlineCustomerList.isEmpty()) { |
|
|
|
|
|
targetList = onlineCustomerList; |
|
|
|
|
|
} else { |
|
|
|
|
|
targetList = customerList; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return randomCustomerId; |
|
|
// 6. 随机选择一个
|
|
|
|
|
|
int randomIndex = new Random().nextInt(targetList.size()); |
|
|
|
|
|
return targetList.get(randomIndex).getId(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@ -320,11 +317,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
|
|
throw new GlobalException(ResultCode.PASSWOR_ERROR); |
|
|
throw new GlobalException(ResultCode.PASSWOR_ERROR); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//未设置套餐或套餐过期
|
|
|
|
|
|
if(imAgentService.isPackageExpire(user.getUniqueToken())) { |
|
|
if(imAgentService.isPackageExpire(user.getUniqueToken())) { |
|
|
throw new GlobalException("套餐已过期"); |
|
|
throw new GlobalException("套餐已过期"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SystemMessageVO msgVo = new SystemMessageVO(); |
|
|
|
|
|
msgVo.setType(2); |
|
|
|
|
|
|
|
|
|
|
|
IMSystemMessage<SystemMessageVO> systemMessage = new IMSystemMessage<>(); |
|
|
|
|
|
systemMessage.getRecvIds().add(user.getId()); |
|
|
|
|
|
systemMessage.setData(msgVo); |
|
|
|
|
|
imClient.sendSystemMessage(systemMessage); |
|
|
|
|
|
|
|
|
// 生成token
|
|
|
// 生成token
|
|
|
UserSession session = BeanUtils.copyProperties(user, UserSession.class); |
|
|
UserSession session = BeanUtils.copyProperties(user, UserSession.class); |
|
|
session.setUserId(user.getId()); |
|
|
session.setUserId(user.getId()); |
|
|
@ -339,9 +343,50 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
|
|
vo.setAccessTokenExpiresIn(jwtProperties.getAccessTokenExpireIn()); |
|
|
vo.setAccessTokenExpiresIn(jwtProperties.getAccessTokenExpireIn()); |
|
|
vo.setRefreshToken(refreshToken); |
|
|
vo.setRefreshToken(refreshToken); |
|
|
vo.setRefreshTokenExpiresIn(jwtProperties.getRefreshTokenExpireIn()); |
|
|
vo.setRefreshTokenExpiresIn(jwtProperties.getRefreshTokenExpireIn()); |
|
|
|
|
|
vo.setUser(user); |
|
|
return vo; |
|
|
return vo; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
// public LoginVO loginCustom(LoginDTO dto) {
|
|
|
|
|
|
// User user = this.findUserByUserName(dto.getUserName());
|
|
|
|
|
|
// if (Objects.isNull(user)) {
|
|
|
|
|
|
// throw new GlobalException("用户不存在");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if(user.getIsCustomer() == 1){
|
|
|
|
|
|
// throw new GlobalException("用户不存在");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if (user.getIsBanned()) {
|
|
|
|
|
|
// String tip = String.format("您的账号因'%s'已被管理员封禁,请联系客服!",user.getReason());
|
|
|
|
|
|
// throw new GlobalException(tip);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if (!passwordEncoder.matches(dto.getPassword(), user.getPassword())) {
|
|
|
|
|
|
// throw new GlobalException(ResultCode.PASSWOR_ERROR);
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// //未设置套餐或套餐过期
|
|
|
|
|
|
// if(imAgentService.isPackageExpire(user.getUniqueToken())) {
|
|
|
|
|
|
// throw new GlobalException("套餐已过期");
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// // 生成token
|
|
|
|
|
|
// UserSession session = BeanUtils.copyProperties(user, UserSession.class);
|
|
|
|
|
|
// session.setUserId(user.getId());
|
|
|
|
|
|
// session.setTerminal(dto.getTerminal());
|
|
|
|
|
|
// String strJson = JSON.toJSONString(session);
|
|
|
|
|
|
// String accessToken = JwtUtil.sign(user.getId(), strJson, jwtProperties.getAccessTokenExpireIn(),
|
|
|
|
|
|
// jwtProperties.getAccessTokenSecret());
|
|
|
|
|
|
// String refreshToken = JwtUtil.sign(user.getId(), strJson, jwtProperties.getRefreshTokenExpireIn(),
|
|
|
|
|
|
// jwtProperties.getRefreshTokenSecret());
|
|
|
|
|
|
// LoginVO vo = new LoginVO();
|
|
|
|
|
|
// vo.setAccessToken(accessToken);
|
|
|
|
|
|
// vo.setAccessTokenExpiresIn(jwtProperties.getAccessTokenExpireIn());
|
|
|
|
|
|
// vo.setRefreshToken(refreshToken);
|
|
|
|
|
|
// vo.setRefreshTokenExpiresIn(jwtProperties.getRefreshTokenExpireIn());
|
|
|
|
|
|
// return vo;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public LoginVO refreshToken(String refreshToken) { |
|
|
public LoginVO refreshToken(String refreshToken) { |
|
|
//验证 token
|
|
|
//验证 token
|
|
|
@ -614,16 +659,6 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us |
|
|
return vos; |
|
|
return vos; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void updateIpAndAddress(User user) { |
|
|
|
|
|
String ip = user.getLastLoginIp(); |
|
|
|
|
|
String address = IpUtils.getIpAddress(ip); |
|
|
|
|
|
this.update(new UpdateWrapper<User>().lambda() |
|
|
|
|
|
.eq(User::getId, user.getId()) |
|
|
|
|
|
.set(User::getLastLoginIp, ip) |
|
|
|
|
|
.set(User::getIpAddress, address)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void changeCustomer(Long customerId, Long targetId, Long userId) { |
|
|
public void changeCustomer(Long customerId, Long targetId, Long userId) { |
|
|
// 更新好友关系
|
|
|
// 更新好友关系
|
|
|
|