完善交互

This commit is contained in:
huangdeliang
2021-06-03 10:36:21 +08:00
parent 953d188f50
commit ea8276e6a0
17 changed files with 348 additions and 113 deletions

View File

@ -99,6 +99,11 @@
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -89,4 +89,6 @@ public class SysServicesTopic {
List<SysServicesTopic> replys;
Integer count;
}

View File

@ -18,4 +18,6 @@ public interface SysServicesTopicMapper {
int inserSysServicesTopicReply(SysServicesTopic topic);
List<SysServicesTopic> selectSysServicesTopicSessionByTopicId(String topicId);
List<SysServicesTopic> selectUnreadTopicCount(List<SysServicesTopic> topics);
}

View File

@ -0,0 +1,143 @@
package com.stdiet.custom.server;
import com.alibaba.fastjson.JSONObject;
import com.stdiet.common.core.domain.model.LoginUser;
import com.stdiet.common.utils.spring.SpringUtils;
import com.stdiet.custom.domain.SysServicesTopic;
import com.stdiet.custom.service.ISysServicesTopicService;
import com.stdiet.custom.utils.WsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
@ServerEndpoint(value = "/ws")
@Component
@Slf4j
public class WebSocketServer {
// concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
//private static ConcurrentHashMap<String,WebSocketServer> websocketList = new ConcurrentHashMap<>();
// 与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
// 接收sid
private String sid = "";
public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() {
return webSocketSet;
}
/**
* 群发自定义消息
*/
public static void sendInfo(String message, String sid) throws IOException {
log.info("推送消息到窗口" + sid + ",推送内容:" + message + "目标:" + webSocketSet.size());
for (WebSocketServer item : webSocketSet) {
try {
// 这里可以设定只推送给这个sid的为null则全部推送
if (sid == null) {
item.sendMessage(message);
} else if (item.sid.equals(sid)) {
item.sendMessage(message);
}
} catch (IOException e) {
// 清理断开的连接
webSocketSet.remove(item);
continue;
}
}
}
// * 连接建立成功调用的方法*
@OnOpen
public void onOpen(Session session) {
this.session = session;
this.sid = String.valueOf(getUserId(session));
webSocketSet.add(this); // 加入set中
try {
JSONObject object = new JSONObject();
object.put("type", WsUtils.WS_TYPE_SYSTEM_MESSAGE);
object.put("msg", "连接成功");
sendMessage(object.toJSONString());
} catch (IOException e) {
log.error("websocket IO异常");
}
}
@OnClose
public void onClose() {
webSocketSet.remove(this); // 从set中删除
log.info("有一连接关闭!");
}
/**
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("发生错误");
error.printStackTrace();
}
/**
* 实现服务器主动推送
*/
public void sendMessage(String message) throws IOException {
log.info("服务器消息推送:" + message);
this.session.getBasicRemote().sendText(message);
}
public Long getUserId(Session session) {
try {
return ((LoginUser) ((UsernamePasswordAuthenticationToken) session.getUserPrincipal()).getPrincipal()).getUser().getUserId();
} catch (Exception e) {
return 0L;
}
}
@OnMessage
public void onMessage(String message, Session session) {
log.info("收到来自窗口" + sid + "的信息:" + message);
try {
String sid = String.valueOf(getUserId(session));
if (sid.equals("0")) {
return;
}
JSONObject resultObj = new JSONObject();
if (message.equals(WsUtils.WS_GET_UNREAD_COUNT)) {
SysServicesTopic topic = new SysServicesTopic();
topic.setUid(sid);
List<SysServicesTopic> statusList = new ArrayList<>();
statusList.add(topic);
ISysServicesTopicService servicesTopicService = SpringUtils.getBean(ISysServicesTopicService.class);
List<SysServicesTopic> result = servicesTopicService.selectUnreadTopicCount(statusList);
JSONObject dataObj = new JSONObject();
dataObj.put("count", result.get(0).getCount());
resultObj.put("type", WsUtils.WS_TYPE_MESSAGE_COUNT);
resultObj.put("msg", "未读消息数");
resultObj.put("data", dataObj);
} else if (message.equals(WsUtils.WS_PING)) {
}
WebSocketServer.sendInfo(resultObj.toJSONString(), sid);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -17,4 +17,6 @@ public interface ISysServicesTopicService {
SysServicesTopic inserSysServicesTopicComment(SysServicesTopic topic);
List<SysServicesTopic> selectSysServicesTopicSessionByTopicId(String topicId);
List<SysServicesTopic> selectUnreadTopicCount(List<SysServicesTopic> topic);
}

View File

@ -1,15 +1,19 @@
package com.stdiet.custom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.common.utils.uuid.UUID;
import com.stdiet.custom.domain.SysCustomer;
import com.stdiet.custom.domain.SysServicesTopic;
import com.stdiet.custom.mapper.SysCustomerMapper;
import com.stdiet.custom.mapper.SysServicesTopicMapper;
import com.stdiet.custom.server.WebSocketServer;
import com.stdiet.custom.service.ISysServicesTopicService;
import com.stdiet.custom.utils.WsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -40,35 +44,58 @@ public class SysServicesTopicServiceImp implements ISysServicesTopicService {
List<SysServicesTopic> statusList = new ArrayList<>();
String customerId = String.valueOf(customer.getId());
SysServicesTopic customerStatus = new SysServicesTopic();
customerStatus.setUid(String.valueOf(customer.getId()));
customerStatus.setUid(customerId);
customerStatus.setRole("customer");
customerStatus.setRead(1);
customerStatus.setTopicId(topic.getTopicId());
statusList.add(customerStatus);
String dieticianId = String.valueOf(customer.getMainDietitian());
SysServicesTopic dieticianStatus = new SysServicesTopic();
dieticianStatus.setUid(String.valueOf(customer.getMainDietitian()));
dieticianStatus.setUid(dieticianId);
dieticianStatus.setRole("dietician");
dieticianStatus.setRead(0);
dieticianStatus.setTopicId(topic.getTopicId());
statusList.add(dieticianStatus);
String afterSaleId = String.valueOf(customer.getAfterDietitian());
SysServicesTopic afterSaleStatus = new SysServicesTopic();
afterSaleStatus.setUid(String.valueOf(customer.getAfterDietitian()));
afterSaleStatus.setUid(afterSaleId);
afterSaleStatus.setRole("after_sale");
afterSaleStatus.setRead(0);
afterSaleStatus.setTopicId(topic.getTopicId());
statusList.add(afterSaleStatus);
String dieticianAssistantId = String.valueOf(customer.getAssistantDietitian());
SysServicesTopic dieticianAssistantStatus = new SysServicesTopic();
dieticianAssistantStatus.setUid(String.valueOf(customer.getAssistantDietitian()));
dieticianAssistantStatus.setUid(dieticianAssistantId);
dieticianAssistantStatus.setRole("dietician_assistant");
dieticianAssistantStatus.setRead(0);
dieticianAssistantStatus.setTopicId(topic.getTopicId());
statusList.add(dieticianAssistantStatus);
servicesTopicMapper.insertSysServicesTopicStatus(statusList);
int rows = servicesTopicMapper.insertSysServicesTopicStatus(statusList);
if (rows > 0) {
try {
List<SysServicesTopic> counts = servicesTopicMapper.selectUnreadTopicCount(statusList);
for (int i = 0; i < counts.size(); i++) {
topic.setId(statusList.get(i).getId());
JSONObject dataObj = new JSONObject();
dataObj.put("count", counts.get(i).getCount());
dataObj.put("data", topic);
JSONObject msgObj = new JSONObject();
msgObj.put("type", WsUtils.WS_TYPE_MESSAGE_COUNT);
msgObj.put("msg", "未读消息数");
msgObj.put("data", dataObj);
WebSocketServer.sendInfo(msgObj.toJSONString(), counts.get(i).getUid());
}
} catch (IOException e) {
e.printStackTrace();
}
}
topic.setId(customerStatus.getId());
topic.setUid(null);
@ -94,10 +121,13 @@ public class SysServicesTopicServiceImp implements ISysServicesTopicService {
status.setTopicId(topic.getTopicId());
status.setRole(topic.getRole());
servicesTopicMapper.updateSysServicesTopicStatus(status);
afterReply(topic);
}
return topic;
}
@Override
public SysServicesTopic inserSysServicesTopicComment(SysServicesTopic topic) {
String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
@ -110,12 +140,51 @@ public class SysServicesTopicServiceImp implements ISysServicesTopicService {
status.setTopicId(topic.getTopicId());
status.setRole(topic.getRole());
servicesTopicMapper.updateSysServicesTopicStatus(status);
afterReply(topic);
}
return topic;
}
public void afterReply(SysServicesTopic topic) {
SysCustomer customer = sysCustomerMapper.selectSysCustomerById(Long.parseLong(topic.getFromUid()));
List<SysServicesTopic> statusList = new ArrayList<>();
SysServicesTopic dieticianStatus = new SysServicesTopic();
dieticianStatus.setUid(String.valueOf(customer.getMainDietitian()));
statusList.add(dieticianStatus);
SysServicesTopic afterSaleStatus = new SysServicesTopic();
afterSaleStatus.setUid(String.valueOf(customer.getAfterDietitian()));
statusList.add(afterSaleStatus);
SysServicesTopic dieticianAssistantStatus = new SysServicesTopic();
dieticianAssistantStatus.setUid(String.valueOf(customer.getAssistantDietitian()));
statusList.add(dieticianAssistantStatus);
try {
List<SysServicesTopic> counts = servicesTopicMapper.selectUnreadTopicCount(statusList);
for (int i = 0; i < counts.size(); i++) {
JSONObject dataObj = new JSONObject();
dataObj.put("count", counts.get(i).getCount());
dataObj.put("topicId", topic.getTopicId());
JSONObject msgObj = new JSONObject();
msgObj.put("type", WsUtils.WS_TYPE_NEW_CUSTOMER_REPLY);
msgObj.put("msg", "新客户回复");
msgObj.put("data", dataObj);
WebSocketServer.sendInfo(msgObj.toJSONString(), counts.get(i).getUid());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List<SysServicesTopic> selectSysServicesTopicSessionByTopicId(String topicId) {
return servicesTopicMapper.selectSysServicesTopicSessionByTopicId(topicId);
}
@Override
public List<SysServicesTopic> selectUnreadTopicCount(List<SysServicesTopic> statusList) {
return servicesTopicMapper.selectUnreadTopicCount(statusList);
}
}

View File

@ -5,4 +5,12 @@ public class WsUtils {
public static final String WS_TYPE_HEART_BEAT = "WS_TYPE_HEART_BEAT";
public static final String WS_TYPE_SYSTEM_MESSAGE = "WS_TYPE_SYSTEM_MESSAGE";
public static final String WS_TYPE_MESSAGE_COUNT = "WS_TYPE_MESSAGE_COUNT";
public static final String WS_TYPE_NEW_CUSTOMER_REPLY = "WS_TYPE_NEW_CUSTOMER_REPLY";
public static final String WS_GET_UNREAD_COUNT = "GET_UNREAD_COUNT";
public static final String WS_PING = "ping";
}

View File

@ -225,5 +225,23 @@
</trim>
</insert>
<resultMap type="SysServicesTopic" id="SysServicesCountResult">
<result column="uid" property="uid"/>
<result column="count" property="count"/>
</resultMap>
<select id="selectUnreadTopicCount" parameterType="java.util.List" resultMap="SysServicesCountResult">
<foreach collection="list" item="status" index="index">
<choose>
<when test="index == 0">
SELECT COUNT(*) AS count, uid FROM sys_services_topic_status WHERE `read` = 0 AND uid =
#{status.uid}
</when>
<otherwise>
UNION ALL SELECT COUNT(*) AS count, uid FROM sys_services_topic_status WHERE `read` = 0 AND uid =
#{status.uid}
</otherwise>
</choose>
</foreach>
</select>
</mapper>