refactor(wechat): 优化消息接收处理和数据库结构
- 移除未使用的kfUserService导入 - 为weChatGroup和ContractData接口添加serverId字段 - 重构receivedMsg方法,根据消息类型从数据库获取会话信息 - 简化数据库表结构,移除冗余的WithServerId接口
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React from "react";
|
import React, { useEffect } from "react";
|
||||||
import { List, Avatar, Badge } from "antd";
|
import { List, Avatar, Badge } from "antd";
|
||||||
import { UserOutlined, TeamOutlined } from "@ant-design/icons";
|
import { UserOutlined, TeamOutlined } from "@ant-design/icons";
|
||||||
import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
|
import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
|
||||||
@@ -15,6 +15,9 @@ const MessageList: React.FC<MessageListProps> = () => {
|
|||||||
const onContactClick = (session: ContractData | weChatGroup) => {
|
const onContactClick = (session: ContractData | weChatGroup) => {
|
||||||
setCurrentContact(session, true);
|
setCurrentContact(session, true);
|
||||||
};
|
};
|
||||||
|
// useEffect(() => {
|
||||||
|
// console.log(chatSessions);
|
||||||
|
// }, [chatSessions]);
|
||||||
return (
|
return (
|
||||||
<div className={styles.messageList}>
|
<div className={styles.messageList}>
|
||||||
<List
|
<List
|
||||||
|
|||||||
@@ -115,17 +115,19 @@ export interface weChatGroup {
|
|||||||
config?: {
|
config?: {
|
||||||
chat: boolean;
|
chat: boolean;
|
||||||
};
|
};
|
||||||
labels: string[];
|
labels?: string[];
|
||||||
unreadCount: number;
|
unreadCount: number;
|
||||||
notice: string;
|
notice: string;
|
||||||
selfDisplyName: string;
|
selfDisplyName: string;
|
||||||
wechatChatroomId: number;
|
wechatChatroomId: number;
|
||||||
|
serverId?: number;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 联系人数据接口
|
// 联系人数据接口
|
||||||
export interface ContractData {
|
export interface ContractData {
|
||||||
id?: number;
|
id?: number;
|
||||||
|
serverId?: number;
|
||||||
wechatAccountId: number;
|
wechatAccountId: number;
|
||||||
wechatId: string;
|
wechatId: string;
|
||||||
alias: string;
|
alias: string;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
KfUserListData,
|
KfUserListData,
|
||||||
ContactGroupByLabel,
|
ContactGroupByLabel,
|
||||||
} from "@/pages/pc/ckbox/data";
|
} from "@/pages/pc/ckbox/data";
|
||||||
import { kfUserService, weChatGroupService, contractService } from "@/utils/db";
|
import { weChatGroupService, contractService } from "@/utils/db";
|
||||||
import { createContractList } from "@/store/module/ckchat/api";
|
import { createContractList } from "@/store/module/ckchat/api";
|
||||||
|
|
||||||
export const useCkChatStore = createPersistStore<CkChatState>(
|
export const useCkChatStore = createPersistStore<CkChatState>(
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
import { WeChatState } from "./weChat.data";
|
import { WeChatState } from "./weChat.data";
|
||||||
import { clearUnreadCount, updateConfig } from "@/pages/pc/ckbox/api";
|
import { clearUnreadCount, updateConfig } from "@/pages/pc/ckbox/api";
|
||||||
import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
|
import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
|
||||||
|
import { weChatGroupService, contractService } from "@/utils/db";
|
||||||
import {
|
import {
|
||||||
addChatSession,
|
addChatSession,
|
||||||
updateChatSession,
|
updateChatSession,
|
||||||
@@ -87,12 +88,12 @@ export const useWeChatStore = create<WeChatState>()(
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
receivedMsg: message => {
|
receivedMsg: async message => {
|
||||||
const currentContract = useWeChatStore.getState().currentContract;
|
const currentContract = useWeChatStore.getState().currentContract;
|
||||||
//判断群还是好友
|
//判断群还是好友
|
||||||
const getMessageId =
|
const getMessageId =
|
||||||
message?.wechatChatroomId || message.wechatFriendId;
|
message?.wechatChatroomId || message.wechatFriendId;
|
||||||
|
const isWechatGroup = message?.wechatChatroomId;
|
||||||
//当前选中聊天的群或好友
|
//当前选中聊天的群或好友
|
||||||
if (currentContract && currentContract.id == getMessageId) {
|
if (currentContract && currentContract.id == getMessageId) {
|
||||||
set(state => ({
|
set(state => ({
|
||||||
@@ -106,11 +107,15 @@ export const useWeChatStore = create<WeChatState>()(
|
|||||||
session.unreadCount = Number(session.unreadCount) + 1;
|
session.unreadCount = Number(session.unreadCount) + 1;
|
||||||
updateChatSession(session);
|
updateChatSession(session);
|
||||||
} else {
|
} else {
|
||||||
// 新增会话
|
if (isWechatGroup) {
|
||||||
addChatSession({
|
const [group] = await weChatGroupService.findByIds(getMessageId);
|
||||||
...session,
|
if (group) {
|
||||||
unreadCount: 1,
|
addChatSession(group);
|
||||||
});
|
}
|
||||||
|
} else {
|
||||||
|
const [user] = await contractService.findByIds(getMessageId);
|
||||||
|
addChatSession(user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -39,16 +39,6 @@ export interface KfUserWithServerId extends Omit<KfUserListData, "id"> {
|
|||||||
id?: number; // 接口数据的原始ID字段
|
id?: number; // 接口数据的原始ID字段
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface weChatGroupServerId extends Omit<weChatGroup, "id"> {
|
|
||||||
serverId: number | string; // 服务器ID作为主键
|
|
||||||
id?: number; // 接口数据的原始ID字段
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ContractWithServerId extends Omit<ContractData, "id"> {
|
|
||||||
serverId: number | string; // 服务器ID作为主键
|
|
||||||
id?: number; // 接口数据的原始ID字段
|
|
||||||
}
|
|
||||||
|
|
||||||
// 新联系人列表数据接口
|
// 新联系人列表数据接口
|
||||||
export interface NewContactListData {
|
export interface NewContactListData {
|
||||||
serverId: number | string; // 服务器ID作为主键
|
serverId: number | string; // 服务器ID作为主键
|
||||||
@@ -61,8 +51,8 @@ export interface NewContactListData {
|
|||||||
// 数据库类
|
// 数据库类
|
||||||
class CunkebaoDatabase extends Dexie {
|
class CunkebaoDatabase extends Dexie {
|
||||||
kfUsers!: Table<KfUserWithServerId>;
|
kfUsers!: Table<KfUserWithServerId>;
|
||||||
weChatGroup!: Table<weChatGroupServerId>;
|
weChatGroup!: Table<weChatGroup>;
|
||||||
contracts!: Table<ContractWithServerId>;
|
contracts!: Table<ContractData>;
|
||||||
newContractList!: Table<NewContactListData>;
|
newContractList!: Table<NewContactListData>;
|
||||||
messageList!: Table<MessageListData>;
|
messageList!: Table<MessageListData>;
|
||||||
|
|
||||||
@@ -74,7 +64,7 @@ class CunkebaoDatabase extends Dexie {
|
|||||||
kfUsers:
|
kfUsers:
|
||||||
"serverId, id, tenantId, wechatId, nickname, alias, avatar, gender, region, signature, bindQQ, bindEmail, bindMobile, createTime, currentDeviceId, isDeleted, deleteTime, groupId, memo, wechatVersion, lastUpdateTime, isOnline",
|
"serverId, id, tenantId, wechatId, nickname, alias, avatar, gender, region, signature, bindQQ, bindEmail, bindMobile, createTime, currentDeviceId, isDeleted, deleteTime, groupId, memo, wechatVersion, lastUpdateTime, isOnline",
|
||||||
weChatGroup:
|
weChatGroup:
|
||||||
"serverId, id, wechatAccountId, tenantId, accountId, chatroomId, chatroomOwner, conRemark, nickname, chatroomAvatar, groupId, config, unreadCount, notice, selfDisplyName",
|
"serverId, id, wechatAccountId, tenantId, accountId, chatroomId, chatroomOwner, conRemark, nickname, chatroomAvatar,wechatChatroomId, groupId, config, unreadCount, notice, selfDisplyName",
|
||||||
contracts:
|
contracts:
|
||||||
"serverId, id, wechatAccountId, wechatId, alias, conRemark, nickname, quanPin, avatar, gender, region, addFrom, phone, signature, accountId, extendFields, city, lastUpdateTime, isPassed, tenantId, groupId, thirdParty, additionalPicture, desc, config, lastMessageTime, unreadCount, duplicate",
|
"serverId, id, wechatAccountId, wechatId, alias, conRemark, nickname, quanPin, avatar, gender, region, addFrom, phone, signature, accountId, extendFields, city, lastUpdateTime, isPassed, tenantId, groupId, thirdParty, additionalPicture, desc, config, lastMessageTime, unreadCount, duplicate",
|
||||||
newContractList: "serverId, id, groupName, contacts",
|
newContractList: "serverId, id, groupName, contacts",
|
||||||
|
|||||||
Reference in New Issue
Block a user