Преглед изворни кода

值班客服修改回之前的代码

wks пре 9 месеци
родитељ
комит
a1621ed2cb

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

@@ -37,7 +37,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 Exception {
-        customerService.callbackSendMsgOnEvent(wechatEncrypt);
+        customerService.callbackSendMsgOnEventOld(wechatEncrypt);
         return HttpResult.ok();
     }
 

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

@@ -25,7 +25,14 @@ public interface WechatCustomerService {
      *
      * @param wechatEncrypt
      */
-    void callbackSendMsgOnEvent(String wechatEncrypt) throws Exception;
+    void callbackSendMsgOnEventNew(String wechatEncrypt) throws Exception;
+
+    /**
+     * 回调事件
+     *
+     * @param wechatEncrypt
+     */
+    void callbackSendMsgOnEventOld(String wechatEncrypt) throws Exception;
 
 
     /**

+ 80 - 4
src/main/java/com/ydtech/modules/admin/service/impl/WechatCustomerServiceImpl.java

@@ -63,7 +63,83 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
     }
 
     @Override
-    public void callbackSendMsgOnEvent(String wechatEncrypt) throws Exception {
+    public void callbackSendMsgOnEventOld(String wechatEncrypt) throws Exception {
+        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();
+
+        distributedLockComponent.executeWithLock(wxCpKfMsgItem.getMsgId(), () -> {
+            // 改变会话状态
+            if (wxCpKfMsgItem.getOrigin() == 3) {
+                WxCpKfServiceStateResp serviceState;
+                try {
+                    serviceState = wxCpKfService.getServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId());
+                } catch (WxErrorException e) {
+                    throw new RuntimeException(e);
+                }
+                Integer state1 = serviceState.getServiceState();
+                if (state1 == 0 || state1 == 1) {
+                    state1 = 3;
+                    try {
+                        wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
+                    } catch (WxErrorException e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+            } else if (wxCpKfMsgItem.getOrigin() == 4) {
+                // 发送欢迎语
+                WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();
+                WxCpKfServiceStateResp serviceState = null;
+                try {
+                    serviceState = wxCpKfService.getServiceState(event.getOpenKfid(), event.getExternalUserId());
+                } catch (WxErrorException e) {
+                    throw new RuntimeException(e);
+                }
+                Integer state = serviceState.getServiceState();
+                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(), true);
+                    wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
+                }
+
+                if (state == 0 || state == 1) {
+                    state = 3;
+                    List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, wxCpKfMsgItem.getEvent().getOpenKfid()).list();
+                    String receptionistId = list.get(0).getReceptionistId();
+                    try {
+                        wxCpKfService.transServiceState(wxCpKfMsgItem.getEvent().getOpenKfid(), wxCpKfMsgItem.getEvent().getExternalUserId(), state, receptionistId);
+                    } catch (WxErrorException e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+            }
+        }, 2, 2);
+    }
+
+    @Override
+    public void callbackSendMsgOnEventNew(String wechatEncrypt) throws Exception {
         log.info("回调参数为{}", wechatEncrypt);
         WechatEncryptVo wechatEncryptVo = xmlMapper.readValue(wechatEncrypt, WechatEncryptVo.class);
         WxCryptUtil wxCryptUtil = wechatCustomerComponents.getWxCryptUtil();
@@ -171,9 +247,9 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
 
     private void restTimeSendMsg(boolean workingHours, String externalUserId, String openKfId) {
         String message;
-        if(!workingHours){
-             message = "您好~当前客服暂时不在服务时间,服务时间为:周一至周五:8:30-12:00,14:00-18:30,周六至周日:9:00-12:00,14:00-18:30,当前未到服务时间,请您在服务时段内咨询~";
-        }else {
+        if (!workingHours) {
+            message = "您好~当前客服暂时不在服务时间,服务时间为:周一至周五:8:30-12:00,14:00-18:30,周六至周日:9:00-12:00,14:00-18:30,当前未到服务时间,请您在服务时段内咨询~";
+        } else {
             message = "您咨询的客服目前暂时不在服务时间,请您前往“晋掌柜App”点击“微信客服”,将为您安排新的客服为您提供服务~(客服服务时间:周一至周五  8:30-18:30,周六至周日  9:00-18:30,午间 12:00-14:00 休息)";
         }
         WxCpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCpKfMsgSendRequest();