| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package com.ydtech.modules.admin.service.impl;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.dataformat.xml.XmlMapper;
- import com.ydtech.modules.admin.components.WechatCustomerComponents;
- import com.ydtech.modules.admin.components.wechat.customer.request.CustomerAddContactWayRequest;
- import com.ydtech.modules.admin.components.wechat.customer.request.CustomerListRequest;
- import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSendMsgOnEventRequest;
- import com.ydtech.modules.admin.components.wechat.customer.response.CustomerAddContactWayResponse;
- import com.ydtech.modules.admin.components.wechat.customer.response.CustomerListResponse;
- import com.ydtech.modules.admin.model.SysUser;
- import com.ydtech.modules.admin.model.po.SysWechatCustomer;
- import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
- import com.ydtech.modules.admin.model.vo.wechat.WechatCustomerCallbackVo;
- import com.ydtech.modules.admin.model.vo.wechat.WechatEncryptVo;
- import com.ydtech.modules.admin.service.*;
- import com.ydtech.utils.StringUtils;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import me.chanjar.weixin.common.error.WxErrorException;
- import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
- import me.chanjar.weixin.cp.api.WxCpKfService;
- import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgListResp;
- import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgSendRequest;
- import me.chanjar.weixin.cp.bean.kf.WxCpKfServiceStateResp;
- import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfEventMsg;
- import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfTextMsg;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.stereotype.Service;
- import org.springframework.util.ObjectUtils;
- import java.time.LocalDateTime;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import java.util.stream.Collectors;
- @Slf4j
- @Service
- @RequiredArgsConstructor
- public class WechatCustomerServiceImpl implements WechatCustomerService {
- private final WechatCustomerComponents wechatCustomerComponents;
- private final SysUserService sysUserService;
- private final XmlMapper xmlMapper = new XmlMapper();
- private final WxCpKfService wxCpKfService;
- private final SysWechatCustomerService sysWechatCustomerService;
- private final IWorkScheduleConfigService workScheduleConfigService;
- private final SysWechatCustomerDutyService sysWechatCustomerDutyService;
- private final StringRedisTemplate redisTemplate;
- @Override
- public String customerCallback(String msgSignature, String timestamp, String nonce, String echoStr) {
- WxCryptUtil wxCryptUtil = wechatCustomerComponents.getWxCryptUtil();
- return wxCryptUtil.decrypt(echoStr);
- }
- @Override
- public void callbackSendMsgOnEvent(String wechatEncrypt) throws JsonProcessingException, WxErrorException {
- log.info("回调参数为{}", wechatEncrypt);
- WechatEncryptVo wechatEncryptVo = xmlMapper.readValue(wechatEncrypt, WechatEncryptVo.class);
- WxCryptUtil wxCryptUtil = wechatCustomerComponents.getWxCryptUtil();
- log.info("解密参数为{}", wechatEncryptVo.getEncrypt());
- String encrypt = wxCryptUtil.decrypt(wechatEncryptVo.getEncrypt());
- log.info("解密后的参数为{}", encrypt);
- WechatCustomerCallbackVo wechatCustomerCallbackVo = xmlMapper.readValue(encrypt, WechatCustomerCallbackVo.class);
- // 获取消息
- WxCpKfMsgListResp wxCpKfMsgListResp = wxCpKfService.syncMsg(null, wechatCustomerCallbackVo.getToken(), null, null, wechatCustomerCallbackVo.getOpenKfId());
- while (wxCpKfMsgListResp.getHasMore() == 1) {
- String nextCursor = wxCpKfMsgListResp.getNextCursor();
- wxCpKfMsgListResp = wxCpKfService.syncMsg(nextCursor, wechatCustomerCallbackVo.getToken(), null, null, wechatCustomerCallbackVo.getOpenKfId());
- }
- List<WxCpKfMsgListResp.WxCpKfMsgItem> msgList = wxCpKfMsgListResp.getMsgList();
- WxCpKfMsgListResp.WxCpKfMsgItem wxCpKfMsgItem = wxCpKfMsgListResp.getMsgList().get(msgList.size() - 1);
- SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, wechatCustomerCallbackVo.getOpenKfId()).one();
- boolean workingHours = workScheduleConfigService.isWorkingHours();
- List<SysWechatCustomerDuty> sysWechatCustomerDuties = sysWechatCustomerDutyService.lambdaQuery()
- .eq(SysWechatCustomerDuty::getCustomerId, sysWechatCustomer.getId())
- .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now())
- .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now())
- .list();
- // 改变会话状态
- if (wxCpKfMsgItem.getOrigin() == 3) {
- // 1 管理人是否一致
- // 2 休息日
- // 3 工作时间内 安排了值班客服
- if (!sysWechatCustomerService.whetherTheManagersAreConsistent(wxCpKfMsgItem.getExternalUserId(), sysWechatCustomer) || !workingHours || !sysWechatCustomerDuties.isEmpty()) {
- WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId());
- Integer state1 = serviceState.getServiceState();
- if (state1 == 0) {
- state1 = 1;
- wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
- restTimeSendMsg(wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid());
- } else if (state1 == 1) {
- String s = redisTemplate.opsForValue().get(wxCpKfMsgItem.getMsgId());
- if (!ObjectUtils.isEmpty(s)) {
- return;
- }
- restTimeSendMsg(wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid());
- redisTemplate.opsForValue().set(wxCpKfMsgItem.getMsgId(), wxCpKfMsgItem.getMsgId(), 1, TimeUnit.MINUTES);
- } else if (state1 == 3) {
- state1 = 4;
- wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
- }
- } else {
- WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId());
- Integer state1 = serviceState.getServiceState();
- if (state1 == 0 || state1 == 1) {
- state1 = 3;
- wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
- }
- }
- } else if (wxCpKfMsgItem.getOrigin() == 4) {
- // 发送欢迎语
- WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();
- if (ObjectUtils.isEmpty(event)) {
- return;
- }
- String scene = event.getScene();
- if (!StringUtils.isEmpty(scene)) {
- SysUser sysUser = sysUserService.getById(scene);
- sysUser.setExternalUserId(event.getExternalUserId());
- sysUserService.updateById(sysUser);
- CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, event.getWelcomeCode(), workingHours);
- wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
- }
- WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(event.getOpenKfid(), event.getExternalUserId());
- Integer state = serviceState.getServiceState();
- if (workingHours && (state == 0 || state == 1)) {
- state = 3;
- List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, wxCpKfMsgItem.getEvent().getOpenKfid()).list();
- String receptionistId = list.get(0).getReceptionistId();
- wxCpKfService.transServiceState(wxCpKfMsgItem.getEvent().getOpenKfid(), wxCpKfMsgItem.getEvent().getExternalUserId(), state, receptionistId);
- }
- }
- List<WxCpKfMsgListResp.WxCpKfMsgItem> msgListDTOList = msgList.stream().filter(x -> "event".equals(x.getMsgType())).collect(Collectors.toList());
- msgListDTOList.forEach(x -> {
- try {
- WxCpKfEventMsg xEvent = x.getEvent();
- WxCpKfServiceStateResp wxCpKfServiceStateResp = wxCpKfService.getServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId());
- Integer state = wxCpKfServiceStateResp.getServiceState();
- if (workingHours && (state == 0 || state == 1)) {
- state = 3;
- // 休息时间
- List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, xEvent.getOpenKfid()).list();
- String receptionistId = list.get(0).getReceptionistId();
- wxCpKfService.transServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId(), state, receptionistId);
- }
- } catch (WxErrorException e) {
- throw new RuntimeException(e);
- }
- });
- }
- private void restTimeSendMsg(String externalUserId, String openKfId) throws WxErrorException {
- String message = "您咨询的客服目前暂时不在服务时间,请您前往“晋掌柜App”点击“微信客服”,将为您安排新的客服为您提供服务~(客服服务时间:周一至周五 8:30-18:30,周六至周日 9:00-18:00,午间 12:00-14:00 休息)";
- WxCpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCpKfMsgSendRequest();
- wxCpKfMsgSendRequest.setToUser(externalUserId);
- wxCpKfMsgSendRequest.setOpenKfid(openKfId);
- wxCpKfMsgSendRequest.setMsgType("text");
- WxCpKfTextMsg wxCpKfTextMsg = new WxCpKfTextMsg();
- wxCpKfTextMsg.setContent(message);
- wxCpKfMsgSendRequest.setText(wxCpKfTextMsg);
- wxCpKfService.sendMsg(wxCpKfMsgSendRequest);
- }
- @Override
- public List<CustomerListResponse.AccountListDTO> list() {
- CustomerListRequest customerListResponse = new CustomerListRequest(0, 100);
- return wechatCustomerComponents.list(customerListResponse).getAccountList();
- }
- @Override
- public String addContactWay(String openKfId) {
- CustomerAddContactWayRequest customerAddContactWayRequest = new CustomerAddContactWayRequest(openKfId, "WELCOME_MESSAGE");
- CustomerAddContactWayResponse customerAddContactWayResponse = wechatCustomerComponents.addContactWay(customerAddContactWayRequest);
- return customerAddContactWayResponse.getUrl();
- }
- }
|