feat(未讀消息數據結構優化): 更新多個接口和數據結構,將unreadCount移入config對象中,並調整相關邏輯以支持新結構,提升代碼可讀性和維護性。

This commit is contained in:
超级老白兔
2025-09-23 11:51:14 +08:00
parent 8742560e6c
commit 92242bb8ff
9 changed files with 61 additions and 60 deletions

View File

@@ -14,10 +14,10 @@ export interface MessageListData {
avatar?: string; // 头像
groupId: number; // 分组ID
config?: {
chat: boolean;
chat?: boolean;
unreadCount: number; // 未读消息数
}; // 配置信息
labels?: string[]; // 标签列表
unreadCount: number; // 未读消息数
// 联系人特有字段当dataType为'contracts'时使用)
wechatId?: string; // 微信ID
@@ -113,10 +113,10 @@ export interface weChatGroup {
chatroomAvatar: string;
groupId: number;
config?: {
chat: boolean;
chat?: boolean;
unreadCount: number;
};
labels?: string[];
unreadCount: number;
notice: string;
selfDisplyName: string;
wechatChatroomId: number;
@@ -152,10 +152,11 @@ export interface ContractData {
additionalPicture: string;
desc: string;
config?: {
chat: boolean;
chat?: boolean;
unreadCount: number;
};
lastMessageTime: number;
unreadCount: number;
duplicate: boolean;
[key: string]: any;
}
@@ -246,7 +247,9 @@ export interface ChatSession {
avatar?: string;
lastMessage: string;
lastTime: string;
unreadCount: number;
config: {
unreadCount: number;
};
online: boolean;
members?: string[];
pinned?: boolean;

View File

@@ -30,14 +30,28 @@ export function getTrafficPoolList() {
);
}
// 好友列表
export function getWechatFriendList(params) {
export function getContactList(params) {
return request("/v1/kefu/wechatFriend/list", params, "GET");
}
// 群列表
export function getWechatChatroomList(params) {
export function getGroupList(params) {
return request("/v1/kefu/wechatChatroom/list", params, "GET");
}
//==============-原接口=================
// 获取联系人列表
// export const getContactList = (params: { prevId: number; count: number }) => {
// return request2("/api/wechatFriend/list", params, "GET");
// };
// //获取群列表
// export function getGroupList(params: { prevId: number; count: number }) {
// return request2(
// "/api/wechatChatroom/listExcludeMembersByPage?",
// params,
// "GET",
// );
// }
//群、好友聊天记录列表
export function getMessageList() {
@@ -130,15 +144,6 @@ export function getChatroomMessages(params: {
return request2("/api/ChatroomMessage/SearchMessage", params, "GET");
}
//获取群列表
export function getGroupList(params: { prevId: number; count: number }) {
return request2(
"/api/wechatChatroom/listExcludeMembersByPage?",
params,
"GET",
);
}
//获取群成员
export function getGroupMembers(params: { id: number }) {
return request2(
@@ -168,11 +173,6 @@ export function getChuKeBaoUserInfo() {
return request2("/api/account/self", {}, "GET");
}
// 获取联系人列表
export const getContactList = (params: { prevId: number; count: number }) => {
return request2("/api/wechatFriend/list", params, "GET");
};
//获取控制终端列表
export const getControlTerminalList = params => {
return request2("/api/wechataccount", params, "GET");

View File

@@ -24,9 +24,10 @@ export interface ContractData {
thirdParty: null;
additionalPicture: string;
desc: string;
config: null;
lastMessageTime: number;
unreadCount: number;
config: {
unreadCount: number;
};
duplicate: boolean;
[key: string]: any;
}
@@ -40,7 +41,9 @@ export interface ChatSession {
avatar?: string;
lastMessage: string;
lastTime: string;
unreadCount: number;
config: {
unreadCount: number;
};
online: boolean;
members?: string[];
pinned?: boolean;

View File

@@ -79,7 +79,9 @@ const MessageList: React.FC<MessageListProps> = () => {
<div
className={styles.lastMessage}
data-count={
session.unreadCount > 0 ? session.unreadCount : ""
session.config.unreadCount > 0
? session.config.unreadCount
: ""
}
>
{session?.lastMessage}

View File

@@ -15,9 +15,9 @@ const VerticalUserList: React.FC = () => {
const session = chatSessions.filter(
v => v.wechatAccountId === wechatAccountId,
);
return session.reduce((pre, cur) => pre + cur.unreadCount, 0);
return session.reduce((pre, cur) => pre + cur.config.unreadCount, 0);
} else {
return chatSessions.reduce((pre, cur) => pre + cur.unreadCount, 0);
return chatSessions.reduce((pre, cur) => pre + cur.config.unreadCount, 0);
}
};

View File

@@ -14,10 +14,10 @@ export interface MessageListData {
avatar?: string; // 头像
groupId: number; // 分组ID
config?: {
chat: boolean;
chat?: boolean;
unreadCount: number; // 未读消息数
}; // 配置信息
labels?: string[]; // 标签列表
unreadCount: number; // 未读消息数
// 联系人特有字段当dataType为'contracts'时使用)
wechatId?: string; // 微信ID
@@ -113,10 +113,11 @@ export interface weChatGroup {
chatroomAvatar: string;
groupId: number;
config?: {
chat: boolean;
chat?: boolean;
unreadCount: number;
};
labels?: string[];
unreadCount: number;
notice: string;
selfDisplyName: string;
wechatChatroomId: number;
@@ -152,10 +153,10 @@ export interface ContractData {
additionalPicture: string;
desc: string;
config?: {
chat: boolean;
chat?: boolean;
unreadCount: number;
};
lastMessageTime: number;
unreadCount: number;
duplicate: boolean;
[key: string]: any;
}
@@ -246,7 +247,9 @@ export interface ChatSession {
avatar?: string;
lastMessage: string;
lastTime: string;
unreadCount: number;
config: {
unreadCount: number;
};
online: boolean;
members?: string[];
pinned?: boolean;

View File

@@ -79,8 +79,8 @@ export const chatInitAPIdata = async () => {
// 如果时间相同,则按未读消息数量降序排列
if (timeCompare === 0) {
// 如果unreadCount不存在则将其排在后面
const aUnread = a.unreadCount || 0;
const bUnread = b.unreadCount || 0;
const aUnread = a.config.unreadCount || 0;
const bUnread = b.config.unreadCount || 0;
return bUnread - aUnread; // 未读消息多的排在前面
}
@@ -102,15 +102,6 @@ export const chatInitAPIdata = async () => {
};
//发起soket连接
export const initSocket = () => {
// 检查WebSocket是否已经连接
// const { status } = useWebSocketStore.getState();
// 如果已经连接或正在连接,则不重复连接
// if (["connected", "connecting"].includes(status)) {
// console.log("WebSocket已连接或正在连接跳过重复连接", { status });
// return;
// }
// 从store获取token和accountId
const { token2 } = useUserStore.getState();
const { getAccountId } = useCkChatStore.getState();
@@ -178,16 +169,16 @@ export const getControlTerminalListByWechatAccountIds = (
export const getAllContactList = async () => {
try {
let allContacts = [];
let prevId = 0;
const count = 1000;
let page = 1;
const limit = 1000;
let hasMore = true;
while (hasMore) {
const contractList = await getContactList({
prevId,
count,
const Result = await getContactList({
page,
limit,
});
const contractList = Result.list;
if (
!contractList ||
!Array.isArray(contractList) ||
@@ -199,15 +190,11 @@ export const getAllContactList = async () => {
allContacts = [...allContacts, ...contractList];
// console.log(contractList.length == 0);
// 如果返回的数据少于请求的数量,说明已经没有更多数据了
if (contractList.length == 0) {
hasMore = false;
} else {
// 获取最后一条数据的id作为下一次请求的prevId
const lastContact = contractList[contractList.length - 1];
prevId = lastContact.id;
page = page + 1;
}
}
return allContacts;

View File

@@ -143,7 +143,10 @@ export const useWeChatStore = create<WeChatState>()(
set({ currentMessages: [], openTransmitModal: false });
clearUnreadCount([contract.id]).then(() => {
if (isExist) {
updateChatSession({ ...contract, unreadCount: 0 });
updateChatSession({
...contract,
config: { unreadCount: 0 },
});
} else {
addChatSession(contract);
}

View File

@@ -392,7 +392,7 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
set({
messages: [...currentState.messages, newMessage],
unreadCount: currentState.unreadCount + 1,
unreadCount: currentState.config.unreadCount + 1,
});
//消息处理器
msgManageCore(data);