|
@@ -1,39 +1,27 @@
|
|
|
package com.fs.app.websocket.service;
|
|
package com.fs.app.websocket.service;
|
|
|
|
|
|
|
|
|
|
|
|
|
-import cn.hutool.core.util.IdUtil;
|
|
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.fs.ai.service.IBaiduAIService;
|
|
|
|
|
-import com.fs.ai.vo.BaiduAIMsgResultVO;
|
|
|
|
|
-import com.fs.app.param.SopLogsEditParam;
|
|
|
|
|
-import com.fs.app.websocket.bean.MsgBean;
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.fs.app.websocket.bean.ResultMsgVo;
|
|
|
import com.fs.app.websocket.bean.SendMsgVo;
|
|
import com.fs.app.websocket.bean.SendMsgVo;
|
|
|
-import com.fs.chat.config.WxConfig;
|
|
|
|
|
-import com.fs.chat.domain.ChatKeyword;
|
|
|
|
|
-import com.fs.chat.domain.ChatMsg;
|
|
|
|
|
-import com.fs.chat.domain.ChatRole;
|
|
|
|
|
-import com.fs.chat.domain.ChatSession;
|
|
|
|
|
-import com.fs.chat.service.IChatMsgService;
|
|
|
|
|
-import com.fs.chat.service.IChatRoleService;
|
|
|
|
|
-import com.fs.chat.service.IChatSessionService;
|
|
|
|
|
|
|
+import com.fs.company.domain.CompanyWxClient;
|
|
|
|
|
+import com.fs.company.mapper.CompanyWxClientMapper;
|
|
|
|
|
+import com.fs.company.service.impl.CompanyWxServiceImpl;
|
|
|
|
|
+import com.fs.wxcid.vo.wxvo.ContactInfoVo;
|
|
|
|
|
+import com.fs.wxcid.vo.wxvo.SyncInfoVo;
|
|
|
|
|
+import com.fs.wxcid.vo.wxvo.WxSendMsgVo;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.common.utils.spring.SpringUtils;
|
|
import com.fs.common.utils.spring.SpringUtils;
|
|
|
-
|
|
|
|
|
-import com.fs.company.domain.CompanyUser;
|
|
|
|
|
-import com.fs.company.domain.CompanyWxChat;
|
|
|
|
|
-import com.fs.company.service.ICompanyUserService;
|
|
|
|
|
-import com.fs.company.service.ICompanyWxChatService;
|
|
|
|
|
-import com.fs.course.domain.FsCourseSopLogs;
|
|
|
|
|
-import com.fs.course.service.IFsCourseSopLogsService;
|
|
|
|
|
-import com.fs.his.domain.FsUser;
|
|
|
|
|
-import com.fs.his.service.IFsUserService;
|
|
|
|
|
-import com.fs.wx.kf.dto.WeixinKfImageMsgDTO;
|
|
|
|
|
-import com.fs.wx.kf.dto.WeixinKfMsgSendDTO;
|
|
|
|
|
-import com.fs.wx.kf.dto.WeixinKfTextMsgDTO;
|
|
|
|
|
-import com.fs.wxUser.domain.CompanyWxUser;
|
|
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
+import com.fs.company.domain.CompanyWxAccount;
|
|
|
|
|
+import com.fs.company.mapper.CompanyWxAccountMapper;
|
|
|
|
|
+import com.fs.wxcid.domain.WxContact;
|
|
|
|
|
+import com.fs.wxcid.mapper.WxContactMapper;
|
|
|
|
|
+import com.fs.wxcid.service.IWxMsgLogService;
|
|
|
|
|
+import com.fs.wxcid.vo.wxvo.WxSendResultMsgVo;
|
|
|
|
|
+import com.hc.openapi.tool.fastjson.JSON;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.websocket.*;
|
|
import javax.websocket.*;
|
|
@@ -41,116 +29,160 @@ import javax.websocket.server.PathParam;
|
|
|
import javax.websocket.server.ServerEndpoint;
|
|
import javax.websocket.server.ServerEndpoint;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.ZoneId;
|
|
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
-import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
-
|
|
|
|
|
-import static com.fs.common.constant.FsConstants.REDIS_CHAT_SESSION;
|
|
|
|
|
|
|
|
|
|
-@ServerEndpoint("/app/webSocket/{uid}")
|
|
|
|
|
|
|
+@Slf4j
|
|
|
@Component
|
|
@Component
|
|
|
|
|
+@ServerEndpoint("/app/webSocket/{wxId}")
|
|
|
public class WebSocketServer {
|
|
public class WebSocketServer {
|
|
|
|
|
|
|
|
- private final ICompanyWxChatService companyWxChatService = SpringUtils.getBean(ICompanyWxChatService.class);
|
|
|
|
|
- private final ICompanyUserService companyUserService = SpringUtils.getBean(ICompanyUserService.class);
|
|
|
|
|
-
|
|
|
|
|
//concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。
|
|
//concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。
|
|
|
- protected static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();
|
|
|
|
|
- RedisCache redisCache=SpringUtils.getBean(RedisCache.class);
|
|
|
|
|
- IFsCourseSopLogsService iFsCourseSopLogsService=SpringUtils.getBean(IFsCourseSopLogsService.class);
|
|
|
|
|
|
|
+ public static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();
|
|
|
|
|
+ RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
|
|
|
|
|
+ CompanyWxAccountMapper accountMapper = SpringUtils.getBean(CompanyWxAccountMapper.class);
|
|
|
|
|
+ WxContactMapper wxContactMapper = SpringUtils.getBean(WxContactMapper.class);
|
|
|
|
|
+ IWxMsgLogService wxMsgLogService = SpringUtils.getBean(IWxMsgLogService.class);
|
|
|
|
|
+ CompanyWxClientMapper companyWxClientMapper = SpringUtils.getBean(CompanyWxClientMapper.class);
|
|
|
|
|
+ CompanyWxServiceImpl companyWxService = SpringUtils.getBean(CompanyWxServiceImpl.class);
|
|
|
|
|
|
|
|
//发送消息
|
|
//发送消息
|
|
|
- public void sendMessage(Session session, String message) throws IOException {
|
|
|
|
|
- if(session != null){
|
|
|
|
|
|
|
+ public <T> void sendMessage(Session session, ResultMsgVo<T> data) {
|
|
|
|
|
+ if (session != null) {
|
|
|
synchronized (session) {
|
|
synchronized (session) {
|
|
|
- System.out.println("发送数据:" + message);
|
|
|
|
|
- session.getBasicRemote().sendText(message);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- //给指定用户发送信息
|
|
|
|
|
- public void sendInfo(String id, String message){
|
|
|
|
|
- Session session = sessionPools.get(id);
|
|
|
|
|
- try {
|
|
|
|
|
- if(session != null){
|
|
|
|
|
- sendMessage(session, message);
|
|
|
|
|
|
|
+ log.info("发送数据:{}", data);
|
|
|
|
|
+ try {
|
|
|
|
|
+ session.getBasicRemote().sendText(JSON.toJSONString(data));
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("发送消息失败!:{}", data, e);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
//建立连接成功调用
|
|
//建立连接成功调用
|
|
|
@OnOpen
|
|
@OnOpen
|
|
|
- public void onOpen(Session session, @PathParam(value = "uid") String uid){
|
|
|
|
|
-
|
|
|
|
|
- sessionPools.put(uid, session);
|
|
|
|
|
-
|
|
|
|
|
- System.out.println(uid + "加入webSocket!当前人数为" + sessionPools.size());
|
|
|
|
|
-
|
|
|
|
|
|
|
+ public void onOpen(Session session, @PathParam(value = "wxId") String wxId) {
|
|
|
|
|
+ sessionPools.put(wxId, session);
|
|
|
|
|
+ CompanyWxAccount companyWxAccount = accountMapper.selectOne(new QueryWrapper<CompanyWxAccount>().eq("wx_no", wxId));
|
|
|
|
|
+ if(companyWxAccount == null){
|
|
|
|
|
+ sendMessage(session, ResultMsgVo.error("未找到对应微信数据"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ companyWxAccount.setLoginStatus(1);
|
|
|
|
|
+ companyWxAccount.setLoginTime(LocalDateTime.now());
|
|
|
|
|
+ accountMapper.updateById(companyWxAccount);
|
|
|
|
|
+ log.info("{}加入webSocket!当前人数为{}", wxId, sessionPools.size());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//关闭连接时调用
|
|
//关闭连接时调用
|
|
|
@OnClose
|
|
@OnClose
|
|
|
- public void onClose(@PathParam(value = "uid") String uid){
|
|
|
|
|
-
|
|
|
|
|
- sessionPools.remove(uid);
|
|
|
|
|
- System.out.println(uid + "断开webSocket连接!当前人数为" + sessionPools.size());
|
|
|
|
|
|
|
+ public void onClose(@PathParam(value = "wxId") String wxId) {
|
|
|
|
|
+ sessionPools.remove(wxId);
|
|
|
|
|
+ CompanyWxAccount companyWxAccount = accountMapper.selectOne(new QueryWrapper<CompanyWxAccount>().eq("wx_no", wxId));
|
|
|
|
|
+ if(companyWxAccount != null){
|
|
|
|
|
+ companyWxAccount.setLoginStatus(0);
|
|
|
|
|
+ companyWxAccount.setOutTime(LocalDateTime.now());
|
|
|
|
|
+ companyWxAccount.setOutRemark("连接断开");
|
|
|
|
|
+ accountMapper.updateById(companyWxAccount);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("{}断开webSocket连接!当前人数为{}", wxId, sessionPools.size());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//收到客户端信息
|
|
//收到客户端信息
|
|
|
@OnMessage
|
|
@OnMessage
|
|
|
- public void onMessage(String message) throws IOException{
|
|
|
|
|
|
|
+ public void onMessage(String message, @PathParam(value = "wxId") String wxId) {
|
|
|
SendMsgVo msg = JSONObject.parseObject(message, SendMsgVo.class);
|
|
SendMsgVo msg = JSONObject.parseObject(message, SendMsgVo.class);
|
|
|
- Session session;
|
|
|
|
|
- System.out.println( "收到数据" + msg.getCmd());
|
|
|
|
|
|
|
+ if(msg.getType() == 0){
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Session session = sessionPools.get(wxId);
|
|
|
|
|
+ if(session == null){
|
|
|
|
|
+ log.error("参数异常:{}", wxId);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("收到数据:{}", msg.getCmd());
|
|
|
|
|
+ CompanyWxAccount companyWxAccount = accountMapper.selectOne(new QueryWrapper<CompanyWxAccount>().eq("wx_no", wxId));
|
|
|
|
|
+ if(companyWxAccount == null){
|
|
|
|
|
+ log.error("未找到对应账号:{}", wxId);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
try {
|
|
try {
|
|
|
- switch (msg.getCmd()){
|
|
|
|
|
- case "heartbeat":
|
|
|
|
|
- session=sessionPools.get(msg.getUserId());
|
|
|
|
|
- sendMessage(session, JSONObject.toJSONString(msg));
|
|
|
|
|
|
|
+ switch (msg.getCmd()) {
|
|
|
|
|
+ case HEARTBEAT:
|
|
|
|
|
+ log.info("接收心跳:{}", wxId);
|
|
|
break;
|
|
break;
|
|
|
- case "sendSop":
|
|
|
|
|
-
|
|
|
|
|
|
|
+ case SYNC_CONTACT_PERSON:
|
|
|
|
|
+ ContactInfoVo contactInfoVo = JSON.parseObject(msg.getDataJson(), ContactInfoVo.class);
|
|
|
|
|
+ if(contactInfoVo == null || StringUtils.isEmpty(contactInfoVo.getRemark())){
|
|
|
|
|
+ log.error("{}同步数据失败,数据缺失:{}", wxId, contactInfoVo);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ WxContact contact = wxContactMapper.selectOne(new QueryWrapper<WxContact>().eq("remark", contactInfoVo.getRemark()));
|
|
|
|
|
+ if(contact != null){
|
|
|
|
|
+ contact.setNickName(contactInfoVo.getNickName());
|
|
|
|
|
+ contact.setCity(contactInfoVo.getAddress());
|
|
|
|
|
+ contact.setUserName(contactInfoVo.getWxNo());
|
|
|
|
|
+ contact.setUpdateTime(new Date());
|
|
|
|
|
+ wxContactMapper.updateById(contact);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ WxContact contact1 = new WxContact();
|
|
|
|
|
+ contact1.setUserName(contactInfoVo.getWxNo());
|
|
|
|
|
+ contact1.setNickName(contactInfoVo.getNickName());
|
|
|
|
|
+ contact1.setCity(contactInfoVo.getAddress());
|
|
|
|
|
+ contact1.setAccountId(companyWxAccount.getId());
|
|
|
|
|
+ contact1.setCompanyId(companyWxAccount.getCompanyId());
|
|
|
|
|
+ contact1.setCompanyUserId(companyWxAccount.getCompanyUserId());
|
|
|
|
|
+ contact1.setRemark(contactInfoVo.getRemark());
|
|
|
|
|
+ contact1.setCreateTime(new Date());
|
|
|
|
|
+ contact1.setUpdateTime(new Date());
|
|
|
|
|
+ wxContactMapper.insert(contact1);
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case SEND_MSG:
|
|
|
|
|
+ log.info("发送返回:{}", msg);
|
|
|
|
|
+ wxMsgLogService.insertLog(JSON.parseObject(msg.getDataJson(), WxSendResultMsgVo.class), companyWxAccount, 0);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case SEND_RESULT:
|
|
|
|
|
+ log.info("接收消息:{}", msg);
|
|
|
|
|
+ wxMsgLogService.insertLog(JSON.parseObject(msg.getDataJson(), WxSendResultMsgVo.class), companyWxAccount, 0);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case SYNC_INFO:
|
|
|
|
|
+ SyncInfoVo syncInfoVo = JSON.parseObject(msg.getDataJson(), SyncInfoVo.class);
|
|
|
|
|
+ companyWxAccount.setHeadImgUrl(syncInfoVo.getImg());
|
|
|
|
|
+ companyWxAccount.setPhone(syncInfoVo.getPhone());
|
|
|
|
|
+ accountMapper.updateById(companyWxAccount);
|
|
|
break;
|
|
break;
|
|
|
- case "sendMsg":
|
|
|
|
|
- CompanyWxUser companyWxUser = companyUserService.selectCompanyUserByNickName(msg.getUserId(), msg.getNickName());
|
|
|
|
|
- if(companyWxUser != null){
|
|
|
|
|
- saveWxChat(msg.getMsg(), companyWxUser, 1);
|
|
|
|
|
|
|
+ case ADD_WX_RESULT:
|
|
|
|
|
+ com.fs.wxcid.vo.wxvo.AddResultWxVo addResultWxVo = JSON.parseObject(msg.getDataJson(), com.fs.wxcid.vo.wxvo.AddResultWxVo.class);
|
|
|
|
|
+ log.info("接收到加好友回调:{}", addResultWxVo);
|
|
|
|
|
+ WxContact wxContact = wxContactMapper.selectOne(new QueryWrapper<WxContact>().eq("remark", addResultWxVo.getRemark()).eq("friends", 0));
|
|
|
|
|
+ wxContact.setFriends(1);
|
|
|
|
|
+ wxContactMapper.updateById(wxContact);
|
|
|
|
|
+ List<CompanyWxClient> clients = companyWxClientMapper.selectWxV2(companyWxAccount.getId(), wxContact.getPhone());
|
|
|
|
|
+ if(clients != null){
|
|
|
|
|
+ clients.parallelStream().forEach(e -> {
|
|
|
|
|
+ e.setIsAdd(1);
|
|
|
|
|
+ e.setRemark(addResultWxVo.getRemark());
|
|
|
|
|
+ e.setWxName(addResultWxVo.getUserName());
|
|
|
|
|
+ e.setSuccessAddTime(LocalDateTime.now());
|
|
|
|
|
+ companyWxClientMapper.updateById(e);
|
|
|
|
|
+ companyWxService.triggerWorkflowOnAddWxSuccess(e.getId());
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- catch (Exception e){
|
|
|
|
|
- System.out.println( "收到数据" + e.getMessage());
|
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发生错误;{}", e.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
//错误时调用
|
|
//错误时调用
|
|
|
@OnError
|
|
@OnError
|
|
|
- public void onError(Session session, Throwable throwable){
|
|
|
|
|
- System.out.println("发生错误"+throwable.getMessage());
|
|
|
|
|
|
|
+ public void onError(Session session, Throwable throwable) {
|
|
|
|
|
+ log.error("发生错误;{}", throwable.getMessage());
|
|
|
throwable.printStackTrace();
|
|
throwable.printStackTrace();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public SendMsgVo saveWxChat(String msg, CompanyWxUser user, Integer sendType) {
|
|
|
|
|
- SendMsgVo build = SendMsgVo.builder().cmd("sendMsg").msg(msg).nickName(user.getNickName()).build();
|
|
|
|
|
- CompanyWxChat companyWxChat = new CompanyWxChat();
|
|
|
|
|
- companyWxChat.setWxUserId(user.getUserId());
|
|
|
|
|
- companyWxChat.setCompanyId(user.getCompanyId());
|
|
|
|
|
- companyWxChat.setCompanyUserId(user.getCompanyUserId());
|
|
|
|
|
- companyWxChat.setContent(msg);
|
|
|
|
|
- companyWxChat.setSendType(sendType);
|
|
|
|
|
- companyWxChat.setCreateTime(new Date());
|
|
|
|
|
- companyWxChatService.save(companyWxChat);
|
|
|
|
|
- return build;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|