فهرست منبع

企微聊天文本消息发送

Long 2 هفته پیش
والد
کامیت
911188f50e

+ 2 - 2
fs-qw-api-msg/src/main/java/com/fs/app/controller/QwMsgController.java

@@ -163,13 +163,13 @@ public class QwMsgController {
                         aiHookService.qwHookNotifyAiReply(id,sender,content,wxWorkMsgResp.getUuid(),wxWorkMessageDTO.getMsgtype());
 
                         // 保存聊天消息
-                        aiHookService.saveQwMsg(id, sender, content, wxWorkMsgResp.getUuid(), 1);
+                        aiHookService.saveQwMsg(id, sender, content, wxWorkMsgResp.getUuid(), 1, wxWorkMsgResp.getJson());
                     }else {
                         System.out.println("销售发送");
                         aiHookService.qwHookNotifyAddMsg(id,receiver,content,wxWorkMsgResp.getUuid());
 
                         // 保存聊天消息
-                        aiHookService.saveQwMsg(id, receiver, content, wxWorkMsgResp.getUuid(), 2);
+                        aiHookService.saveQwMsg(id, receiver, content, wxWorkMsgResp.getUuid(), 2, wxWorkMsgResp.getJson());
                     }
 
                 }

+ 8 - 6
fs-service-system/src/main/java/com/fs/fastGpt/service/AiHookService.java

@@ -19,11 +19,13 @@ public interface AiHookService {
 
     /**
      * 保存企微聊天信息
-     * @param qwUserId  企微用户ID
-     * @param userId    用户ID
-     * @param content   聊天内容
-     * @param uuid      UUID
-     * @param sendType  发送者类型 1用户 2客服
+     *
+     * @param qwUserId 企微用户ID
+     * @param userId   用户ID
+     * @param content  聊天内容
+     * @param uuid     UUID
+     * @param sendType 发送者类型 1用户 2客服
+     * @param json     消息json
      */
-    void saveQwMsg(Long qwUserId, Long userId, String content, String uuid, int sendType);
+    void saveQwMsg(Long qwUserId, Long userId, String content, String uuid, int sendType, String json);
 }

+ 9 - 6
fs-service-system/src/main/java/com/fs/fastGpt/service/impl/AiHookServiceImpl.java

@@ -1261,15 +1261,17 @@ public class AiHookServiceImpl implements AiHookService {
 
     /**
      * 保存企微聊天信息
-     * @param qwUserId  企微用户ID
-     * @param userId    用户ID
-     * @param content   聊天内容
-     * @param uuid      UUID
-     * @param sendType  发送者类型 1用户 2客服
+     *
+     * @param qwUserId 企微用户ID
+     * @param userId   用户ID
+     * @param content  聊天内容
+     * @param uuid     UUID
+     * @param sendType 发送者类型 1用户 2客服
+     * @param json     消息json
      */
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public void saveQwMsg(Long qwUserId, Long userId, String content, String uuid, int sendType) {
+    public void saveQwMsg(Long qwUserId, Long userId, String content, String uuid, int sendType, String json) {
         // 查询企微用户
         QwUser qwUser = qwUserService.selectQwUserById(qwUserId);
         if (Objects.isNull(qwUser)){
@@ -1316,6 +1318,7 @@ public class AiHookServiceImpl implements AiHookService {
         qwMsg.setCompanyId(qwUser.getCompanyId());
         qwMsg.setCompanyUserId(qwUser.getCompanyUserId());
         qwMsg.setMsgType(1);
+        qwMsg.setMsgJson(json);
         qwMsg.setStatus(0);
         qwMsg.setQwUserId(qwSession.getQwUserId());
         qwMsg.setQwExtId(qwSession.getQwExtId());

+ 86 - 7
fs-service-system/src/main/java/com/fs/qw/service/impl/QwMsgServiceImpl.java

@@ -2,12 +2,12 @@ package com.fs.qw.service.impl;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.http.HttpRequest;
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fs.common.core.domain.R;
 import com.fs.common.utils.DateUtils;
+import com.fs.common.utils.StringUtils;
 import com.fs.his.config.FsSysConfig;
 import com.fs.his.utils.ConfigUtil;
 import com.fs.qw.Bean.MsgBean;
@@ -23,6 +23,8 @@ import com.fs.qw.service.IQwUserService;
 import com.fs.qw.vo.QwContactListVO;
 import com.fs.qw.vo.QwMessageListVO;
 import com.fs.qwHookApi.vo.QwHookMsgVO;
+import com.fs.wxwork.dto.*;
+import com.fs.wxwork.service.WxWorkService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -50,6 +52,8 @@ public class QwMsgServiceImpl extends ServiceImpl<QwMsgMapper, QwMsg> implements
 
     @Autowired
     private ConfigUtil configUtil;
+    @Autowired
+    private WxWorkService wxWorkService;
 
     /**
      * 查询企微聊天记录
@@ -244,12 +248,87 @@ public class QwMsgServiceImpl extends ServiceImpl<QwMsgMapper, QwMsg> implements
 
     @Override
     public R sendMsg(QwMsgSendParam param) {
-        FsSysConfig config = configUtil.getSysConfig();
-        String domainName = config.getHookUrl();
-        HttpRequest.post(domainName+"/app/qwmsg/sendMsg")
-                .body(JSON.toJSONString(param),"application/json;charset=UTF-8")
-                .execute().body();
-        return R.ok();
+        if (StringUtils.isBlank(param.getContent())) {
+            return R.error("消息内容不能为空");
+        }
+
+        if (Objects.isNull(param.getSessionId())) {
+            return R.error("会话ID不能为空");
+        }
+
+        // 查询会话
+        QwSession qwSession = qwSessionMapper.selectQwSessionBySessionId(param.getSessionId());
+        if (Objects.isNull(qwSession)) {
+            return R.error("会话不存在");
+        }
+
+        // 外部联系人
+        QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactById(Long.valueOf(qwSession.getQwExtId()));
+        if (Objects.isNull(qwExternalContact)) {
+            return R.error("联系人不存在");
+        }
+
+        // 企微用户
+        QwUser qwUser = qwUserMapper.selectQwUserById(Long.parseLong(qwSession.getQwUserId()));
+        if (Objects.isNull(qwUser)) {
+            return R.error("用户不存在");
+        }
+
+        Long serverId = qwUser.getServerId();
+        String uuid = qwUser.getUid();
+        String openId = qwExternalContact.getExternalUserId();
+        String sCorpId = qwUser.getCorpId();
+
+        WxWorkUserId2VidDTO params = new WxWorkUserId2VidDTO();
+        params.setOpenid(Collections.singletonList(openId));
+        params.setUuid(uuid);
+        params.setScorpid(sCorpId);
+        WxWorkResponseDTO<List<WxWorkVid2UserIdRespDTO>> listWxWorkResponseDTO = wxWorkService.UserId2Vid(params, serverId);
+
+        // 发送消息
+        WxWorkSendTextMsgDTO textMsgDTO = new WxWorkSendTextMsgDTO();
+        textMsgDTO.setUuid(uuid);
+        textMsgDTO.setSend_userid(listWxWorkResponseDTO.getData().get(0).getUser_id());
+        textMsgDTO.setIsRoom(false);
+        textMsgDTO.setContent(param.getContent());
+        WxWorkResponseDTO<WxWorkSendTextMsgRespDTO> msgRespDTOWxWorkResponseDTO = wxWorkService.SendTextMsg(textMsgDTO, serverId);
+
+        String msg = msgRespDTOWxWorkResponseDTO.getErrmsg();
+        if ("ok".equals(msg)) {
+            // 消息保存本地数据库
+            QwMsg qwMsg = new QwMsg();
+            qwMsg.setContent(param.getContent());
+            qwMsg.setSessionId(qwSession.getSessionId());
+            qwMsg.setSendType(2);
+            qwMsg.setCompanyId(qwUser.getCompanyId());
+            qwMsg.setCompanyUserId(qwUser.getCompanyUserId());
+            qwMsg.setMsgType(1);
+            qwMsg.setStatus(0);
+            qwMsg.setQwUserId(qwSession.getQwUserId());
+            qwMsg.setQwExtId(qwSession.getQwExtId());
+            qwMsg.setAvatar(qwExternalContact.getAvatar());
+            qwMsg.setNickName(qwExternalContact.getRemark());
+            qwMsg.setCreateTime(new Date());
+            qwMsgMapper.insertQwMsg(qwMsg);
+
+            // 组装返回消息结构
+            QwMessageListVO listVO = new QwMessageListVO();
+            QWFromUser qwFromUser = new QWFromUser();
+            qwFromUser.setId(Long.parseLong(qwMsg.getQwUserId()));
+            qwFromUser.setDisplayName(qwUser.getQwUserName());
+            qwFromUser.setAvatar("https://cos.his.cdwjyyh.com/fs/20241231/22a765a96da247d1b83ea94fef438a41.png");
+
+            listVO.setType("text");
+            listVO.setStatus("succeed");
+            listVO.setFromUser(qwFromUser);
+            listVO.setSendTime(qwMsg.getCreateTime().getTime());
+            listVO.setId(qwMsg.getMsgId().toString());
+            listVO.setContent(qwMsg.getContent());
+            listVO.setToContactId(String.valueOf(param.getSessionId()));
+            return R.ok().put("data", listVO);
+        } else {
+            return R.error(msg);
+        }
     }
 
     @Override

+ 1 - 1
fs-service-system/src/main/java/com/fs/qw/service/impl/QwUserServiceImpl.java

@@ -1008,7 +1008,7 @@ public class QwUserServiceImpl implements IQwUserService
             return R.ok("登录成功");
         }
         WxWorkSetCallbackUrlDTO wxWorkSetCallbackUrlDTO = new WxWorkSetCallbackUrlDTO();
-        wxWorkSetCallbackUrlDTO.setUrl("http://c38da856.natappfree.cc/msg/callback/"+serverId);
+        wxWorkSetCallbackUrlDTO.setUrl("http://1.14.207.209:8008/msg/callback/"+serverId);
         wxWorkSetCallbackUrlDTO.setUuid(data.getUuid());
         wxWorkService.SetCallbackUrl(wxWorkSetCallbackUrlDTO,serverId);
 

+ 4 - 0
fs-service-system/src/main/java/com/fs/wxwork/dto/WxWorkUserId2VidDTO.java

@@ -6,8 +6,12 @@ import java.util.List;
 
 @Data
 public class WxWorkUserId2VidDTO {
+    // qw_user uid
     private String uuid;
+    // qw_user corp_id
     private String scorpid;
+    //
     private String corpid;
+    // qw_external_contact external_user_id
     private List<String> openid;
 }