集成websocket

This commit is contained in:
wangxulei
2025-01-09 17:33:57 +08:00
parent ed34df44ee
commit 8516efd1c8
21 changed files with 515 additions and 766 deletions

View File

@@ -55,7 +55,7 @@ public class AuthChatApi {
@OperLog(operModule = "获取消息列表",operType = OperType.QUERY,operDesc = "获取消息列表")
public ResultBean<List<ChatVo>> getMessageList(ChatDto chatDto) {
String followId = String.valueOf(request.getAttribute("authorId"));
List<ChatVo> chatVos = chatService.selectChatList(followId);
List<ChatVo> chatVos = chatService.getMessageList(followId);
return ResultBean.success(chatVos);
}

View File

@@ -1,6 +1,8 @@
package com.dd.admin.business.chat.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.dd.admin.business.chat.domain.AuthorChat;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
@@ -35,6 +37,20 @@ public class ChatController {
@Autowired
ChatService chatService;
@ApiOperation(value = "作者列表")
@ApiOperationSupport(order = 2)
@GetMapping("/admin/chat/authorList")
public ResultBean<List<AuthorChat>> authorList() {
List<AuthorChat> authorChats = chatService.selectAuthorChatList();
authorChats.stream().forEach(authorChat -> {
authorChat.setIndex(String.valueOf(PinyinUtil.getFirstLetter(authorChat.getIndex().charAt(0))));
});
return ResultBean.success(authorChats);
}
@ApiOperation(value = "-分页列表")
@ApiOperationSupport(order = 1)
@GetMapping("/admin/chat/page")

View File

@@ -0,0 +1,39 @@
package com.dd.admin.business.chat.domain;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AuthorChat {
// 消息的唯一标识id
@ApiModelProperty(value = "用户id")
private String id;
// 显示名称,例如聊天对象的昵称等
@ApiModelProperty(value = "显示名称")
private String displayName;
// 头像的网络地址,用于展示聊天对象的头像图片
@ApiModelProperty(value = "头像")
private String avatar;
// 索引字段,可能用于排序、分组等功能,具体含义依业务而定
@ApiModelProperty(value = "索引")
private String index;
// 未读消息的数量
@ApiModelProperty(value = "未读消息数量")
private Integer unread;
// 最近一条消息的内容,经过相应的渲染处理(如表情替换等)
@ApiModelProperty(value = "最近一条消息内容")
private String lastContent;
// 最近一条消息的发送时间,通常是时间戳形式(单位可能是毫秒)
@ApiModelProperty(value = "最近一条消息发送时间")
private Long lastSendTime;
}

View File

@@ -0,0 +1,40 @@
package com.dd.admin.business.chat.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MessageBean {
// 发送方用户信息
private FromUser fromUser;
// 消息处理类型这里对应数字6具体含义需根据业务确定
private int handlerType;
// 接收方联系人ID这里是一个字符串形式的ID具体格式由业务定义
private String toContactId;
// 消息的唯一标识IDUUID格式具体使用方式依业务而定
private String id;
// 消息类型这里为text表示文本消息可能还有其他类型如image、audio等
private String type;
// 消息内容此处为文本内容“111”根据不同消息类型会有不同格式
private String content;
// 消息状态这里是going具体状态值及含义需结合业务场景明确
private String status;
// 消息发送时间,这里是一个时间戳形式(可能是毫秒级时间戳,需根据业务确认)
private long sendTime;
// 内部类,用于表示发送方用户信息
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class FromUser {
// 显示名称,例如用户的昵称等
private String displayName;
// 用户的唯一标识ID
private String id;
// 用户头像的URL或者其他相关标识这里为空字符串具体使用方式由业务决定
private String avatar;
}
}

View File

@@ -1,6 +1,7 @@
package com.dd.admin.business.chat.mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.admin.business.chat.domain.AuthorChat;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -26,5 +27,10 @@ public interface ChatMapper extends BaseMapper<Chat> {
ChatVo selectChat(@Param("chatId") String chatId);
List<ChatVo> selectChatDetail(@Param("chatDto") ChatDto chatDto);
//查询我的聊天记录列表 当我作为收发方都需要考虑
List<ChatVo> selectChatList(@Param("authorId")String authorId);
List<ChatVo> getMessageList(@Param("authorId")String authorId);
//admin下面是后台使用的接口
//查询客服聊天列表 当我作为收发方都需要查询最后一条
List<AuthorChat> selectAuthorChatList(@Param("authorId")String authorId);
}

View File

@@ -58,7 +58,7 @@
</if>
limit 1
</select>
<select id="selectChatList" resultType="com.dd.admin.business.chat.domain.ChatVo"
<select id="getMessageList" resultType="com.dd.admin.business.chat.domain.ChatVo"
parameterType="java.lang.String">
select * from (
SELECT
@@ -83,7 +83,11 @@
business_author b ON a.FROM_ID = b.AUTHOR_ID
WHERE
a.TO_ID = #{authorId}
UNION ALL
SELECT
a.TO_ID AS authorId,
a.TO_NAME AS authorName,
@@ -99,9 +103,66 @@
a.FROM_ID = #{authorId}
ORDER BY
create_time DESC
) a1
GROUP BY a1.authorId
ORDER BY
create_time DESC
</select>
<select id="selectAuthorChatList" resultType="com.dd.admin.business.chat.domain.AuthorChat">
SELECT
wa.AUTHOR_ID id,
wa.AUTHOR_NAME displayName,
wa.AVATAR_URL avatar,
wa.AUTHOR_NAME AS 'index',
wb.unReadCount unRead,
UNIX_TIMESTAMP(CONVERT_TZ(wb.CREATE_TIME, '+08:00', '+00:00')) lastSendTime
FROM
business_author wa
LEFT JOIN (
SELECT
a.FROM_ID AS authorId,
a.FROM_NAME AS authorName,
b.AVATAR_URL AS authorAvatar,
a.content,
a.create_time,
(
SELECT
count(1)
FROM
business_chat ca
WHERE
ca.FROM_ID = a.FROM_ID
AND ca.to_id = #{authorId}
AND ca.MESSAGE_STATUS = 0
) AS unReadCount
FROM
business_chat a
LEFT JOIN business_author b ON a.FROM_ID = b.AUTHOR_ID
WHERE
a.TO_ID = #{authorId}
UNION ALL
SELECT
a.TO_ID AS authorId,
a.TO_NAME AS authorName,
b.AVATAR_URL AS authorAvatar,
a.content,
a.create_time,
0 AS unReadCount
FROM
business_chat a
LEFT JOIN business_author b ON a.TO_ID = b.AUTHOR_ID
WHERE
a.FROM_ID = #{authorId}
ORDER BY
create_time DESC
) wb ON wa.author_id = wb.authorId
GROUP BY
wa.author_id
ORDER BY
wb.create_time DESC
</select>
</mapper>

View File

@@ -1,6 +1,7 @@
package com.dd.admin.business.chat.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.admin.business.chat.domain.AuthorChat;
import com.dd.admin.business.chat.entity.Chat;
import com.baomidou.mybatisplus.extension.service.IService;
import com.dd.admin.business.chat.domain.ChatVo;
@@ -24,9 +25,13 @@ public interface ChatService extends IService<Chat> {
ChatVo selectChat(String chatId);
//-列表
List<ChatVo> selectChatDetail(ChatDto chatDto);
List<ChatVo> selectChatList(String authorId);
List<ChatVo> getMessageList(String authorId);
void readMessage(String authorId,String loginId);
//未读聊天消息的数量
Integer selectUnReadCount(String authorId);
//admin
List<AuthorChat> selectAuthorChatList(String authorId);
}

View File

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.admin.business.chat.domain.AuthorChat;
import com.dd.admin.common.model.PageFactory;
import com.dd.admin.business.chat.entity.Chat;
import com.dd.admin.business.chat.mapper.ChatMapper;
@@ -43,8 +44,8 @@ public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements Ch
}
@Override
public List<ChatVo> selectChatList(String authorId) {
return baseMapper.selectChatList(authorId);
public List<ChatVo> getMessageList(String authorId) {
return baseMapper.getMessageList(authorId);
}
@Override
@@ -64,4 +65,9 @@ public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements Ch
queryWrapper.eq(Chat::getMessageStatus,0);
return baseMapper.selectCount(queryWrapper);
}
@Override
public List<AuthorChat> selectAuthorChatList(String authorId) {
return baseMapper.selectAuthorChatList(authorId);
}
}

View File

@@ -1 +1 @@
package com.dd.admin.business.webSocket;
package com.dd.admin.business.webSocket;

View File

@@ -0,0 +1 @@
package com.dd.admin.business.webSocket.handler;

View File

@@ -63,7 +63,7 @@ tio:
websocket:
server:
port: 9326
heartbeat-timeout: 10000
heartbeat-timeout: 20000
# 集群配置 默认关闭
cluster:
enabled: false