|
@@ -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,96 @@ 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);
|
|
|
+
|
|
|
+ if (listWxWorkResponseDTO.getErrcode() != 0) {
|
|
|
+ return R.error(listWxWorkResponseDTO.getErrmsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送消息
|
|
|
+ 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);
|
|
|
+
|
|
|
+ if (msgRespDTOWxWorkResponseDTO.getErrcode() != 0) {
|
|
|
+ return R.error(msgRespDTOWxWorkResponseDTO.getErrmsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ 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.setMsgJson(JSONObject.toJSONString(textMsgDTO));
|
|
|
+ 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
|