wks пре 1 година
родитељ
комит
d105a0bc1c

+ 4 - 3
src/main/java/com/ydtech/config/WechatWorkConfig.java

@@ -62,9 +62,10 @@ public class WechatWorkConfig {
     @Bean
     public WxCpService wxCpService() {
         WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
-        config.setCorpId(corpId);
-        config.setCorpSecret(secret);
-        config.setAgentId(agentId);
+        config.setCorpId(wechatCustomerProperties.getCorpId());
+        config.setCorpSecret(wechatCustomerProperties.getSecret());
+        config.setToken(wechatCustomerProperties.getToken());
+        config.setAesKey(wechatCustomerProperties.getEncodingAesKey());
         WxCpServiceImpl wxCpService = new WxCpServiceImpl();
         wxCpService.setWxCpConfigStorage(config);
         return wxCpService;

+ 2 - 0
src/main/java/com/ydtech/modules/admin/controller/wechat/SysWechatCustomerConfigController.java

@@ -1,6 +1,7 @@
 package com.ydtech.modules.admin.controller.wechat;
 
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.ydtech.core.page.HttpResult;
 import com.ydtech.modules.admin.model.po.SysWechatCustomer;
 import com.ydtech.modules.admin.model.vo.wechat.*;
@@ -13,6 +14,7 @@ import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
+import org.springframework.http.MediaType;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 

+ 2 - 1
src/main/java/com/ydtech/modules/admin/controller/wechat/WechatCustomerController.java

@@ -9,6 +9,7 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.*;
 
@@ -35,7 +36,7 @@ public class WechatCustomerController {
 
     @PostMapping(path = "/callback", consumes = MediaType.TEXT_XML_VALUE, produces = MediaType.TEXT_XML_VALUE)
     @ApiOperation(value = "客服回调(处理事件发送欢迎语)")
-    public HttpResult<String> customerCallback(@RequestBody String wechatEncrypt) throws JsonProcessingException {
+    public HttpResult<String> customerCallback(@RequestBody String wechatEncrypt) throws JsonProcessingException, WxErrorException {
         customerService.callbackSendMsgOnEvent(wechatEncrypt);
         return HttpResult.ok();
     }

+ 3 - 0
src/main/java/com/ydtech/modules/admin/model/vo/wechat/WechatCustomerCallbackVo.java

@@ -21,6 +21,9 @@ public class WechatCustomerCallbackVo {
     @JacksonXmlProperty(localName = "ToUserName")
     private String toUserName;
 
+    @JacksonXmlProperty(localName = "FromUserName")
+    private String fromUserName;
+
     @JacksonXmlProperty(localName = "CreateTime")
     private String createTime;
 

+ 1 - 0
src/main/java/com/ydtech/modules/admin/service/SysWechatCustomerBusinessService.java

@@ -68,4 +68,5 @@ public interface SysWechatCustomerBusinessService {
      * @return 跳转链接
      */
     String getWeChatCustomer(SysUser user) throws WxErrorException;
+
 }

+ 2 - 1
src/main/java/com/ydtech/modules/admin/service/WechatCustomerService.java

@@ -3,6 +3,7 @@ package com.ydtech.modules.admin.service;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.ydtech.core.page.BasePageVO;
 import com.ydtech.modules.admin.components.wechat.customer.response.CustomerListResponse;
+import me.chanjar.weixin.common.error.WxErrorException;
 
 import java.util.List;
 
@@ -24,7 +25,7 @@ public interface WechatCustomerService {
      *
      * @param wechatEncrypt
      */
-    void callbackSendMsgOnEvent(String wechatEncrypt) throws JsonProcessingException;
+    void callbackSendMsgOnEvent(String wechatEncrypt) throws JsonProcessingException, WxErrorException;
 
 
     /**

+ 45 - 22
src/main/java/com/ydtech/modules/admin/service/impl/SysWechatCustomerBusinessServiceImpl.java

@@ -93,7 +93,7 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
         if (!customer.isEmpty()) {
             // 获取值班时间列表
             Map<Long, List<SysWechatCustomerDuty>> dutyMap = sysWechatCustomerDutyService.lambdaQuery()
-                    .in(SysWechatCustomerDuty::getId, customer)
+                    .in(SysWechatCustomerDuty::getCustomerId, customer)
                     .list()
                     .stream()
                     .collect(Collectors.groupingBy(SysWechatCustomerDuty::getCustomerId));
@@ -203,6 +203,7 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
             list.add(sysWechatCustomerAddVo.getReceptionistId());
             wxCpKfService.addServicer(sysWechatCustomer.getOpenKfId(), list);
             sysWechatCustomer.setReceptionistId(sysWechatCustomerAddVo.getReceptionistId());
+            sysWechatCustomer.setReceptionist(sysWechatCustomerAddVo.getReceptionist());
         }
 
         // 昵称和头像是否跟换
@@ -212,50 +213,71 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
 
         sysWechatCustomer.setNickname(sysWechatCustomerAddVo.getNickname());
         sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar());
+        sysWechatCustomer.setUserId(sysWechatCustomerAddVo.getUserId());
         sysWechatCustomerService.updateById(sysWechatCustomer);
     }
 
     @Override
     public String getWeChatCustomer(SysUser user) throws WxErrorException {
         SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByUserId(user.getLeaderId());
-        if (!ObjectUtils.isEmpty(sysWechatCustomer)) {
-            List<SysWechatCustomerDuty> list = sysWechatCustomerDutyService.lambdaQuery()
-                    .ge(SysWechatCustomerDuty::getStartTime, LocalDateTime.now())
-                    .le(SysWechatCustomerDuty::getEndTime, LocalDateTime.now())
-                    .list();
-            if (sysWechatCustomer.getDutyCustomerId() > 0 && list != null && !list.isEmpty()) {
-                sysWechatCustomer = sysWechatCustomerService.getById(sysWechatCustomer.getDutyCustomerId());
-            }
-        }
+        List<SysWechatCustomer> sysWechatCustomerList = sysWechatCustomerService.list();
+        List<Long> sysWechatCustomerIdList = sysWechatCustomerList.stream().map(SysWechatCustomer::getId).collect(Collectors.toList());
+
         if (ObjectUtils.isEmpty(sysWechatCustomer)) {
+            // 获取最近一次的客服记录
             SysWechatCustomerRecording sysWechatCustomerRecording = sysWechatCustomerRecordingService.lambdaQuery()
+                    .in(!sysWechatCustomerIdList.isEmpty(), SysWechatCustomerRecording::getCustomerId, sysWechatCustomerIdList)
                     .eq(SysWechatCustomerRecording::getUserId, user.getId())
                     .orderByDesc(SysWechatCustomerRecording::getCreateTime)
+                    .last("limit 1")
                     .one();
-            if (!ObjectUtils.isEmpty(sysWechatCustomerRecording)) {
+            if (ObjectUtils.isEmpty(sysWechatCustomerRecording)) {
+                // 没有客服记录使用最近的一条
                 sysWechatCustomerRecording = sysWechatCustomerRecordingService.lambdaQuery()
+                        .in(!sysWechatCustomerIdList.isEmpty(), SysWechatCustomerRecording::getCustomerId, sysWechatCustomerIdList)
                         .orderByDesc(SysWechatCustomerRecording::getCreateTime)
                         .last("limit 1")
                         .one();
+                Long i = Optional.ofNullable(sysWechatCustomerRecording)
+                        .map(SysWechatCustomerRecording::getCustomerId)
+                        .orElse(0L);
+                sysWechatCustomer = sysWechatCustomerService.lambdaQuery()
+                        .gt(i > 0, SysWechatCustomer::getId, i)
+                        .orderByAsc(SysWechatCustomer::getId)
+                        .last("limit 1")
+                        .one();
+                if (ObjectUtils.isEmpty(sysWechatCustomer)) {
+                    sysWechatCustomer = sysWechatCustomerService.lambdaQuery()
+                            .orderByAsc(SysWechatCustomer::getId)
+                            .last("limit 1")
+                            .one();
+                }
+            } else {
+                // 使用上一次的客服
+                sysWechatCustomer = sysWechatCustomerService.lambdaQuery()
+                        .eq(SysWechatCustomer::getId, sysWechatCustomerRecording.getCustomerId())
+                        .one();
             }
 
-            Long i = Optional.ofNullable(sysWechatCustomerRecording)
-                    .map(SysWechatCustomerRecording::getCustomerId)
-                    .orElse(0L);
-            sysWechatCustomer = sysWechatCustomerService.lambdaQuery()
-                    .ge(i > 0, SysWechatCustomer::getId, sysWechatCustomerRecording.getCustomerId())
-                    .orderByDesc(SysWechatCustomer::getId)
-                    .last("limit 1")
-                    .one();
-            if (ObjectUtils.isEmpty(sysWechatCustomer)) {
-                throw new SystemException("请联系管理员穿件客服");
-            }
             SysWechatCustomerRecording customerRecording = new SysWechatCustomerRecording();
             customerRecording.setCustomerId(sysWechatCustomer.getId());
             customerRecording.setUserId(user.getId());
             sysWechatCustomerRecordingService.save(customerRecording);
         }
 
+        List<SysWechatCustomerDuty> list = sysWechatCustomerDutyService.lambdaQuery()
+                .eq(SysWechatCustomerDuty::getCustomerId, sysWechatCustomer.getId())
+                .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now())
+                .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now())
+                .list();
+        if (sysWechatCustomer.getDutyCustomerId() > 0 && list != null && !list.isEmpty()) {
+            sysWechatCustomer = sysWechatCustomerService.getById(sysWechatCustomer.getDutyCustomerId());
+        }
+
+        if (ObjectUtils.isEmpty(sysWechatCustomer)) {
+            throw new SystemException("请联系管理员,配置客服");
+        }
+
         String openKfId = sysWechatCustomer.getOpenKfId();
         WxCpKfAccountLink wxCpKfAccountLink = new WxCpKfAccountLink();
         wxCpKfAccountLink.setOpenKfid(openKfId);
@@ -264,4 +286,5 @@ public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBu
         return accountLink.getUrl();
     }
 
+
 }

+ 2 - 2
src/main/java/com/ydtech/modules/admin/service/impl/SysWechatCustomerServiceImpl.java

@@ -58,8 +58,8 @@ public class SysWechatCustomerServiceImpl extends ServiceImpl<SysWechatCustomerM
         List<SysWechatCustomerDutyRulesVo.DutyDateTime> collect = sysWechatCustomerDuties.stream()
                 .map(sysWechatCustomerDuty -> {
                     SysWechatCustomerDutyRulesVo.DutyDateTime dutyDateTime = new SysWechatCustomerDutyRulesVo.DutyDateTime();
-                    dutyDateTime.setEndTime(DateTimeUtil.getStartTimeOfDayStr(sysWechatCustomerDuty.getEndTime()));
-                    dutyDateTime.setStartTime(DateTimeUtil.getStartTimeOfDayStr(sysWechatCustomerDuty.getStartTime()));
+                    dutyDateTime.setEndTime(DateTimeUtil.localDateTimetoString(sysWechatCustomerDuty.getEndTime()));
+                    dutyDateTime.setStartTime(DateTimeUtil.localDateTimetoString(sysWechatCustomerDuty.getStartTime()));
                     return dutyDateTime;
                 })
                 .collect(Collectors.toList());

+ 47 - 7
src/main/java/com/ydtech/modules/admin/service/impl/WechatCustomerServiceImpl.java

@@ -11,18 +11,24 @@ import com.ydtech.modules.admin.components.wechat.customer.response.CustomerAddC
 import com.ydtech.modules.admin.components.wechat.customer.response.CustomerListResponse;
 import com.ydtech.modules.admin.components.wechat.customer.response.CustomerSyncMsgResponse;
 import com.ydtech.modules.admin.model.SysUser;
+import com.ydtech.modules.admin.model.po.SysWechatCustomer;
 import com.ydtech.modules.admin.model.vo.wechat.WechatCustomerCallbackVo;
 import com.ydtech.modules.admin.model.vo.wechat.WechatEncryptVo;
 import com.ydtech.modules.admin.service.SysUserService;
+import com.ydtech.modules.admin.service.SysWechatCustomerService;
 import com.ydtech.modules.admin.service.WechatCustomerService;
 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.WxCpKfServiceStateResp;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Service
@@ -35,6 +41,11 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
 
     private final XmlMapper xmlMapper = new XmlMapper();
 
+    private final WxCpKfService wxCpKfService;
+
+    private final SysWechatCustomerService sysWechatCustomerService;
+
+
     @Override
     public String customerCallback(String msgSignature, String timestamp, String nonce, String echoStr) {
         WxCryptUtil wxCryptUtil = wechatCustomerComponents.getWxCryptUtil();
@@ -42,7 +53,7 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
     }
 
     @Override
-    public void callbackSendMsgOnEvent(String wechatEncrypt) throws JsonProcessingException {
+    public void callbackSendMsgOnEvent(String wechatEncrypt) throws JsonProcessingException, WxErrorException {
         log.info("回调参数为{}", wechatEncrypt);
         WechatEncryptVo wechatEncryptVo = xmlMapper.readValue(wechatEncrypt, WechatEncryptVo.class);
         WxCryptUtil wxCryptUtil = wechatCustomerComponents.getWxCryptUtil();
@@ -56,21 +67,50 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
         customerSyncMsgRequest.setToken(wechatCustomerCallbackVo.getToken());
         customerSyncMsgRequest.setOpenKfid(wechatCustomerCallbackVo.getOpenKfId());
         CustomerSyncMsgResponse customerSyncMsgResponse = wechatCustomerComponents.syncMsg(customerSyncMsgRequest);
+        while (customerSyncMsgResponse.getHasMore() == 1) {
+            String nextCursor = customerSyncMsgResponse.getNextCursor();
+            customerSyncMsgRequest.setCursor(nextCursor);
+            customerSyncMsgResponse = wechatCustomerComponents.syncMsg(customerSyncMsgRequest);
+        }
         List<CustomerSyncMsgResponse.MsgListDTO> msgList = customerSyncMsgResponse.getMsgList();
         CustomerSyncMsgResponse.MsgListDTO msgListDTO = customerSyncMsgResponse.getMsgList().get(msgList.size() - 1);
 
+        if (msgListDTO.getOrigin() == 3) {
+            List<CustomerSyncMsgResponse.MsgListDTO> msgListDTOList = msgList.stream()
+                    .filter(x -> "event".equals(x.getMsgtype()))
+                    .collect(Collectors.toList());
+            msgListDTOList.forEach(x -> {
+                try {
+                    CustomerSyncMsgResponse.MsgListDTO.EventDTO xEvent = x.getEvent();
+                    WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserid());
+                    if (serviceState.getServiceState() == 0) {
+                        List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery()
+                                .eq(SysWechatCustomer::getOpenKfId, xEvent.getOpenKfid())
+                                .list();
+                        wxCpKfService.transServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserid(), 3, list.get(0)
+                                .getReceptionistId());
+                    }
+                } catch (WxErrorException e) {
+                    throw new RuntimeException(e);
+                }
+            });
+        }
+
         CustomerSyncMsgResponse.MsgListDTO.EventDTO event = msgListDTO.getEvent();
-        if(ObjectUtils.isEmpty(event)) {
+        if (ObjectUtils.isEmpty(event)) {
             return;
         }
-        String sceneParam = event.getSceneParam();
-        if (StringUtils.isEmpty(sceneParam)) {
+
+        String scene = event.getScene();
+        if (StringUtils.isEmpty(scene)) {
             return;
         }
+        if (msgListDTO.getOrigin() == 4) {
+            SysUser sysUser = sysUserService.getById(scene);
+            CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, msgListDTO.getMsgid(), event.getWelcomeCode());
+            wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
+        }
 
-        SysUser sysUser = sysUserService.getById(sceneParam);
-        CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, msgListDTO.getMsgid(), event.getWelcomeCode());
-        wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
     }
 
     @Override

+ 10 - 0
src/main/java/com/ydtech/utils/DateTimeUtil.java

@@ -230,6 +230,16 @@ public class DateTimeUtil {
         return getFirstDayOfYearStr(localDateTime, DATETIME_PATTERN);
     }
 
+    /**
+     * 获取指定日期
+     *
+     * @param localDateTime 指定日期时间
+     * @return String 格式:yyyy-MM-dd HH:mm:ss
+     */
+    public static String localDateTimetoString(LocalDateTime localDateTime){
+        return localDateTime.format(DateTimeFormatter.ofPattern(DATETIME_PATTERN));
+    }
+
     /**
      * 获取指定日期当年最后一天的日期字符串
      *