优化推送逻辑

This commit is contained in:
wangxulei 2025-01-15 11:25:49 +08:00
parent 3ba2d600c2
commit 2bdf00f7f7
7 changed files with 59 additions and 9 deletions

View File

@ -49,6 +49,7 @@ import javax.servlet.http.HttpServletRequest;
import java.util.List;
import static com.dd.admin.business.webSocket.WsConst.*;
import static com.dd.admin.common.consts.XhsConst.TRUE;
@ApiModel("用户操作类Api")
@RestController
@ -95,7 +96,7 @@ public class AuthActionApi {
oneByFollow.setFollowName(follow.getAuthorName());
followService.save(oneByFollow);
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),author.getAuthorId(),"4",oneByFollow);
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),author.getAuthorId(),HANDLER_FOLLOW,oneByFollow);
}else{
throw new ApiException("已经关注过了~");
}
@ -150,7 +151,7 @@ public class AuthActionApi {
upNotesService.save(upNotes);
//发送点赞信息
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),noteDto.getAuthorId(),"1",upNotes);
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),noteDto.getAuthorId(),HANDLER_UP,upNotes);
}else{
//在则表示取消赞删除数据
@ -182,7 +183,7 @@ public class AuthActionApi {
starNotesService.save(starNotes);
//发送点赞信息
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),noteDto.getAuthorId(),"2",starNotes);
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),noteDto.getAuthorId(),HANDLER_STAR,starNotes);
}else{
//在则表示取消赞删除数据
starNotesService.removeById(starNotes);
@ -261,7 +262,7 @@ public class AuthActionApi {
Reply reply = BeanUtil.copyProperties(replyDto, Reply.class);
replyService.save(reply);
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),reply.getParentAuthorId(),"3",reply);
TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),reply.getParentAuthorId(),HANDLER_REPLY,reply);
return ResultBean.success();
}

View File

@ -7,6 +7,7 @@ import com.dd.admin.business.note.service.NoteService;
import com.dd.admin.business.notice.domain.SendNoticeDto;
import com.dd.admin.business.receive.entity.Receive;
import com.dd.admin.business.receive.service.ReceiveService;
import com.dd.admin.business.webSocket.util.TioUtil;
import com.dd.admin.common.exception.ApiException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
@ -27,6 +28,9 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;
import org.tio.websocket.starter.TioWebSocketServerBootstrap;
import static com.dd.admin.business.webSocket.WsConst.HANDLER_NOTICE_COUNT;
/**
* <p>
@ -46,6 +50,9 @@ public class NoticeController {
@Autowired
ReceiveService receiveService;
@Autowired
private TioWebSocketServerBootstrap bootstrap;
@ApiOperation(value = "通知公告表-添加")
@ApiOperationSupport(order = 3)
@PostMapping("/admin/notice/sendNotice")
@ -61,7 +68,11 @@ public class NoticeController {
receive.setNoticeId(notice.getNoticeId());
receive.setAuthorId(author.getAuthorId());
receive.setAuthorName(author.getAuthorName());
receiveList.add(receive);
boolean add = receiveList.add(receive);
//给指定用户发送
if(add){
TioUtil.sendChatMessageToUser(bootstrap.getServerGroupContext(),author.getAuthorId(),HANDLER_NOTICE_COUNT,receive);
}
});
receiveService.saveBatch(receiveList);

View File

@ -0,0 +1,34 @@
package com.dd.admin.business.webSocket;
public class WsConst {
//点赞
public final static String HANDLER_UP = "1";
//收藏
public final static String HANDLER_STAR = "2";
//回复
public final static String HANDLER_REPLY = "3";
//关注
public final static String HANDLER_FOLLOW = "4";
//聊天
public final static String HANDLER_CHAT = "5";
//客服
public final static String HANDLER_SERVE = "6";
//客服
public final static String HANDLER_READ_CHAT = "7";
//客服
public final static String HANDLER_CHAT_COUNT = "8";
//客服
public final static String HANDLER_TOTAL_COUNT = "9";
//客服
public final static String HANDLER_NOTICE_COUNT = "10";
}

View File

@ -18,6 +18,8 @@ import org.tio.core.ChannelContext;
import java.util.Map;
import static com.dd.admin.business.webSocket.WsConst.HANDLER_TOTAL_COUNT;
@Slf4j
@Service("9")
@ -49,7 +51,7 @@ public class GetApiReadCountMessageHandler implements MsgHandlerInterface {
UnReadCountBean unReadCountBean = new UnReadCountBean(chatUnReadCount,upNotesUnReadCount,starNotesUnReadCount,replyUnReadCount,followUnReadCount,receiveUnReadCount,totalUnReadCount);
unReadCountBean.calculateTotalUnReadCount();
TioUtil.sendChatMessageToUser(context.getGroupContext(),authorId,"9",unReadCountBean);
TioUtil.sendChatMessageToUser(context.getGroupContext(),authorId,HANDLER_TOTAL_COUNT,unReadCountBean);
return null;
}
}

View File

@ -11,6 +11,8 @@ import org.tio.core.ChannelContext;
import java.util.Map;
import static com.dd.admin.business.webSocket.WsConst.HANDLER_CHAT_COUNT;
@Slf4j
@Service("8")
@ -22,7 +24,7 @@ public class GetReadCountMessageHandler implements MsgHandlerInterface {
public Object handler(Map map, ChannelContext context) {
String authorId = String.valueOf(map.get("authorId"));
Integer unReadCount = chatService.selectUnReadCount(authorId);
TioUtil.sendChatMessageToUser(context.getGroupContext(),authorId,"8",unReadCount);
TioUtil.sendChatMessageToUser(context.getGroupContext(),authorId,HANDLER_CHAT_COUNT,unReadCount);
return null;
}
}

View File

@ -1 +1 @@
package com.dd.admin.business.webSocket.handler; import cn.hutool.core.bean.BeanUtil; import com.dd.admin.business.chat.domain.ChatVo; import com.dd.admin.business.chat.domain.MessageBean; import com.dd.admin.business.chat.entity.Chat; import com.dd.admin.business.chat.service.ChatService; import com.dd.admin.business.webSocket.MsgHandlerInterface; import com.dd.admin.business.webSocket.util.TioUtil; import com.dd.admin.common.utils.AddressUtils; import com.dd.admin.common.utils.HttpContext; import com.dd.admin.common.utils.IPUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.http.common.HttpRequest; import org.tio.utils.lock.SetWithLock; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.*; @Component @Slf4j @Service("5") public class P2PMessageHandler implements MsgHandlerInterface { public static P2PMessageHandler handler; @Autowired ChatService chatService; public static long convertToShanghaiTimeZoneTimestamp(Date createTime) { Calendar calendar = Calendar.getInstance(); calendar.setTime(createTime); calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); return calendar.getTimeInMillis(); } @Override public Object handler(Map map, ChannelContext context ){ Chat chat = BeanUtil.toBean(map, Chat.class); chat.setIpAddress(context.getClientNode().getIp()); chat.setIpRealAddress(AddressUtils.getRealAddress(chat.getIpAddress())); //ip真实地址 chatService.save(chat); ChatVo chatVo = chatService.selectChat(chat.getChatId()); if(chat.getToId().equals("8")){ MessageBean messageBean = new MessageBean(); messageBean.setId(chatVo.getChatId()); messageBean.setContent(chatVo.getContent()); messageBean.setSendTime(convertToShanghaiTimeZoneTimestamp(chatVo.getCreateTime())); messageBean.setStatus("succeed"); messageBean.setType("text"); messageBean.setToContactId(chatVo.getFromId()); MessageBean.FromUser fromUser = new MessageBean.FromUser(); fromUser.setAvatar(chatVo.getFromAvatar()); fromUser.setDisplayName(chatVo.getFromName()); fromUser.setId(chatVo.getFromId()); messageBean.setFromUser(fromUser); System.out.println(messageBean); TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getToId(),"6",messageBean); }else{ TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getToId(),"5",chatVo); } TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getFromId(),"5",chatVo); return null; }}
package com.dd.admin.business.webSocket.handler; import cn.hutool.core.bean.BeanUtil; import com.dd.admin.business.chat.domain.ChatVo; import com.dd.admin.business.chat.domain.MessageBean; import com.dd.admin.business.chat.entity.Chat; import com.dd.admin.business.chat.service.ChatService; import com.dd.admin.business.webSocket.MsgHandlerInterface; import com.dd.admin.business.webSocket.util.TioUtil; import com.dd.admin.common.utils.AddressUtils; import com.dd.admin.common.utils.HttpContext; import com.dd.admin.common.utils.IPUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.http.common.HttpRequest; import org.tio.utils.lock.SetWithLock; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.*; import static com.dd.admin.business.webSocket.WsConst.HANDLER_CHAT; import static com.dd.admin.business.webSocket.WsConst.HANDLER_SERVE; @Component @Slf4j @Service("5") public class P2PMessageHandler implements MsgHandlerInterface { public static P2PMessageHandler handler; @Autowired ChatService chatService; public static long convertToShanghaiTimeZoneTimestamp(Date createTime) { Calendar calendar = Calendar.getInstance(); calendar.setTime(createTime); calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); return calendar.getTimeInMillis(); } @Override public Object handler(Map map, ChannelContext context ){ Chat chat = BeanUtil.toBean(map, Chat.class); chat.setIpAddress(context.getClientNode().getIp()); chat.setIpRealAddress(AddressUtils.getRealAddress(chat.getIpAddress())); //ip真实地址 chatService.save(chat); ChatVo chatVo = chatService.selectChat(chat.getChatId()); if(chat.getToId().equals("8")){ MessageBean messageBean = new MessageBean(); messageBean.setId(chatVo.getChatId()); messageBean.setContent(chatVo.getContent()); messageBean.setSendTime(convertToShanghaiTimeZoneTimestamp(chatVo.getCreateTime())); messageBean.setStatus("succeed"); messageBean.setType("text"); messageBean.setToContactId(chatVo.getFromId()); MessageBean.FromUser fromUser = new MessageBean.FromUser(); fromUser.setAvatar(chatVo.getFromAvatar()); fromUser.setDisplayName(chatVo.getFromName()); fromUser.setId(chatVo.getFromId()); messageBean.setFromUser(fromUser); System.out.println(messageBean); TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getToId(),HANDLER_SERVE,messageBean); }else{ TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getToId(),HANDLER_CHAT,chatVo); } TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getFromId(),HANDLER_CHAT,chatVo); return null; }}

View File

@ -1 +1 @@
package com.dd.admin.business.webSocket.handler; import cn.hutool.core.bean.BeanUtil; import com.dd.admin.business.chat.domain.ChatVo; import com.dd.admin.business.chat.domain.MessageBean; import com.dd.admin.business.chat.entity.Chat; import com.dd.admin.business.chat.service.ChatService; import com.dd.admin.business.webSocket.MsgHandlerInterface; import com.dd.admin.business.webSocket.util.TioUtil; import com.dd.admin.common.utils.AddressUtils; import lombok.extern.slf4j.Slf4j; import org.checkerframework.checker.units.qual.C; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.tio.core.ChannelContext; import java.util.Map; @Component @Slf4j @Service("6") public class ServiceMessageHandler implements MsgHandlerInterface { public static ServiceMessageHandler handler; @Autowired ChatService chatService; @Override public Object handler(Map map, ChannelContext context ){ System.out.println(map); MessageBean messageBean = BeanUtil.toBean(map, MessageBean.class); //xx发送人 MessageBean.FromUser fromUser = messageBean.getFromUser(); Chat chat = new Chat(); chat.setFromId(fromUser.getId()); chat.setFromName(fromUser.getDisplayName()); chat.setToId(messageBean.getToContactId()); chat.setContent(messageBean.getContent()); chat.setIpAddress(context.getClientNode().getIp()); chat.setIpRealAddress(AddressUtils.getRealAddress(chat.getIpAddress())); //ip真实地址 chatService.save(chat); ChatVo chatVo = chatService.selectChat(chat.getChatId()); //如果对方是移动端则按照移动端消息格式推送 TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getToId(),"5",chatVo); //还需要推送给客服自己 // TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getFromId(),"6",messageBean); return null; }}
package com.dd.admin.business.webSocket.handler; import cn.hutool.core.bean.BeanUtil; import com.dd.admin.business.chat.domain.ChatVo; import com.dd.admin.business.chat.domain.MessageBean; import com.dd.admin.business.chat.entity.Chat; import com.dd.admin.business.chat.service.ChatService; import com.dd.admin.business.webSocket.MsgHandlerInterface; import com.dd.admin.business.webSocket.util.TioUtil; import com.dd.admin.common.utils.AddressUtils; import lombok.extern.slf4j.Slf4j; import org.checkerframework.checker.units.qual.C; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.tio.core.ChannelContext; import java.util.Map; import static com.dd.admin.business.webSocket.WsConst.HANDLER_CHAT; @Component @Slf4j @Service("6") public class ServiceMessageHandler implements MsgHandlerInterface { public static ServiceMessageHandler handler; @Autowired ChatService chatService; @Override public Object handler(Map map, ChannelContext context ){ System.out.println(map); MessageBean messageBean = BeanUtil.toBean(map, MessageBean.class); //xx发送人 MessageBean.FromUser fromUser = messageBean.getFromUser(); Chat chat = new Chat(); chat.setFromId(fromUser.getId()); chat.setFromName(fromUser.getDisplayName()); chat.setToId(messageBean.getToContactId()); chat.setContent(messageBean.getContent()); chat.setIpAddress(context.getClientNode().getIp()); chat.setIpRealAddress(AddressUtils.getRealAddress(chat.getIpAddress())); //ip真实地址 chatService.save(chat); ChatVo chatVo = chatService.selectChat(chat.getChatId()); //如果对方是移动端则按照移动端消息格式推送 TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getToId(),HANDLER_CHAT,chatVo); //还需要推送给客服自己 // TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getFromId(),"6",messageBean); return null; }}