feat(消息转发): 更新转发逻辑并优化状态管理
在消息记录和转发模态框中添加了选中聊天记录的更新逻辑,确保转发功能的正确性。同时,简化了转发模态框的参数传递,移除了不必要的回调,提升了用户体验。
This commit is contained in:
@@ -32,7 +32,9 @@ const MessageEnter: React.FC<MessageEnterProps> = ({ contract }) => {
|
|||||||
const EnterModule = useWeChatStore(state => state.EnterModule);
|
const EnterModule = useWeChatStore(state => state.EnterModule);
|
||||||
const updateShowCheckbox = useWeChatStore(state => state.updateShowCheckbox);
|
const updateShowCheckbox = useWeChatStore(state => state.updateShowCheckbox);
|
||||||
const updateEnterModule = useWeChatStore(state => state.updateEnterModule);
|
const updateEnterModule = useWeChatStore(state => state.updateEnterModule);
|
||||||
|
const updateTransmitModal = useWeChatStore(
|
||||||
|
state => state.updateTransmitModal,
|
||||||
|
);
|
||||||
const handleSend = async () => {
|
const handleSend = async () => {
|
||||||
if (!inputValue.trim()) return;
|
if (!inputValue.trim()) return;
|
||||||
console.log("发送消息", contract);
|
console.log("发送消息", contract);
|
||||||
@@ -140,7 +142,7 @@ const MessageEnter: React.FC<MessageEnterProps> = ({ contract }) => {
|
|||||||
updateEnterModule("common");
|
updateEnterModule("common");
|
||||||
};
|
};
|
||||||
const handTurnRignt = () => {
|
const handTurnRignt = () => {
|
||||||
console.log("转发");
|
updateTransmitModal(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -20,12 +20,8 @@ import styles from "./TransmitModal.module.scss";
|
|||||||
import { weChatGroupService, contractService } from "@/utils/db";
|
import { weChatGroupService, contractService } from "@/utils/db";
|
||||||
import { useWeChatStore } from "@/store/module/weChat/weChat";
|
import { useWeChatStore } from "@/store/module/weChat/weChat";
|
||||||
import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
|
import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
|
||||||
|
import { useWebSocketStore } from "@/store/module/websocket/websocket";
|
||||||
export interface TransmitModalProps {
|
const TransmitModal: React.FC = () => {
|
||||||
onConfirm?: (params: (ContractData | weChatGroup)[]) => void; // 可选,因为会自动更新到store
|
|
||||||
}
|
|
||||||
|
|
||||||
const TransmitModal: React.FC<TransmitModalProps> = ({ onConfirm }) => {
|
|
||||||
const [searchValue, setSearchValue] = useState("");
|
const [searchValue, setSearchValue] = useState("");
|
||||||
const [allContacts, setAllContacts] = useState<
|
const [allContacts, setAllContacts] = useState<
|
||||||
(ContractData | weChatGroup)[]
|
(ContractData | weChatGroup)[]
|
||||||
@@ -36,12 +32,19 @@ const TransmitModal: React.FC<TransmitModalProps> = ({ onConfirm }) => {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
const pageSize = 20;
|
const pageSize = 20;
|
||||||
|
const { sendCommand } = useWebSocketStore.getState();
|
||||||
// 从 Zustand store 获取更新方法
|
// 从 Zustand store 获取更新方法
|
||||||
const openTransmitModal = useWeChatStore(state => state.openTransmitModal);
|
const openTransmitModal = useWeChatStore(state => state.openTransmitModal);
|
||||||
const updateTransmitModal = useWeChatStore(
|
const updateTransmitModal = useWeChatStore(
|
||||||
state => state.updateTransmitModal,
|
state => state.updateTransmitModal,
|
||||||
);
|
);
|
||||||
|
const updateSelectedChatRecords = useWeChatStore(
|
||||||
|
state => state.updateSelectedChatRecords,
|
||||||
|
);
|
||||||
|
|
||||||
|
const selectedChatRecords = useWeChatStore(
|
||||||
|
state => state.selectedChatRecords,
|
||||||
|
);
|
||||||
|
|
||||||
// 加载联系人数据
|
// 加载联系人数据
|
||||||
const loadContacts = async () => {
|
const loadContacts = async () => {
|
||||||
@@ -115,8 +118,20 @@ const TransmitModal: React.FC<TransmitModalProps> = ({ onConfirm }) => {
|
|||||||
|
|
||||||
// 确认转发
|
// 确认转发
|
||||||
const handleConfirm = () => {
|
const handleConfirm = () => {
|
||||||
console.log("handleConfirm", selectedWechatFriend);
|
for (const user of selectedWechatFriend) {
|
||||||
onConfirm?.(selectedWechatFriend);
|
for (const record of selectedChatRecords) {
|
||||||
|
const params = {
|
||||||
|
wechatAccountId: user.wechatAccountId,
|
||||||
|
wechatChatroomId: user?.chatroomId ? user.id : 0,
|
||||||
|
wechatFriendId: user?.chatroomId ? 0 : user.id,
|
||||||
|
msgSubType: record.msgSubType,
|
||||||
|
msgType: record.msgType,
|
||||||
|
content: record.content,
|
||||||
|
};
|
||||||
|
sendCommand("CmdSendMessage", params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateSelectedChatRecords([]);
|
||||||
updateTransmitModal(false);
|
updateTransmitModal(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -130,7 +145,7 @@ const TransmitModal: React.FC<TransmitModalProps> = ({ onConfirm }) => {
|
|||||||
title="转发消息"
|
title="转发消息"
|
||||||
open={openTransmitModal}
|
open={openTransmitModal}
|
||||||
onCancel={() => updateTransmitModal(false)}
|
onCancel={() => updateTransmitModal(false)}
|
||||||
width={800}
|
width={"60%"}
|
||||||
className={styles.transmitModal}
|
className={styles.transmitModal}
|
||||||
footer={[
|
footer={[
|
||||||
<Button key="cancel" onClick={() => updateTransmitModal(false)}>
|
<Button key="cancel" onClick={() => updateTransmitModal(false)}>
|
||||||
|
|||||||
@@ -46,7 +46,10 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
|
|||||||
state.kfUserList.find(kf => kf.id === contract.wechatAccountId),
|
state.kfUserList.find(kf => kf.id === contract.wechatAccountId),
|
||||||
);
|
);
|
||||||
|
|
||||||
const openTransmitModal = useWeChatStore(state => state.openTransmitModal);
|
const updateSelectedChatRecords = useWeChatStore(
|
||||||
|
state => state.updateSelectedChatRecords,
|
||||||
|
);
|
||||||
|
|
||||||
const updateTransmitModal = useWeChatStore(
|
const updateTransmitModal = useWeChatStore(
|
||||||
state => state.updateTransmitModal,
|
state => state.updateTransmitModal,
|
||||||
);
|
);
|
||||||
@@ -514,6 +517,7 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
|
|||||||
// 从选中记录中移除
|
// 从选中记录中移除
|
||||||
setSelectedRecords(prev => prev.filter(record => record.id !== msg.id));
|
setSelectedRecords(prev => prev.filter(record => record.id !== msg.id));
|
||||||
}
|
}
|
||||||
|
updateSelectedChatRecords(selectedRecords);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 检查消息是否被选中
|
// 检查消息是否被选中
|
||||||
@@ -663,21 +667,11 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
|
|||||||
loadChatMessages(false, timestamp);
|
loadChatMessages(false, timestamp);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDeleteMessage = (messageId: string) => {
|
|
||||||
// 删除消息的处理逻辑
|
|
||||||
console.log("删除消息:", messageId);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleForwardMessage = (messageData: ChatRecord) => {
|
const handleForwardMessage = (messageData: ChatRecord) => {
|
||||||
|
updateSelectedChatRecords([messageData]);
|
||||||
updateTransmitModal(true);
|
updateTransmitModal(true);
|
||||||
// 转发消息的处理逻辑
|
|
||||||
console.log("转发消息:", messageData);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRetryMessage = (messageData: ChatRecord) => {
|
|
||||||
// 重试发送消息的处理逻辑
|
|
||||||
console.log("重试发送:", messageData);
|
|
||||||
};
|
|
||||||
const handRecall = messageData => {
|
const handRecall = messageData => {
|
||||||
// 撤回消息的处理逻辑
|
// 撤回消息的处理逻辑
|
||||||
fetchReCallApi({
|
fetchReCallApi({
|
||||||
@@ -696,7 +690,6 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
|
|||||||
// 多条转发逻辑
|
// 多条转发逻辑
|
||||||
updateEnterModule(!showCheckbox ? "multipleForwarding" : "common");
|
updateEnterModule(!showCheckbox ? "multipleForwarding" : "common");
|
||||||
updateShowCheckbox(!showCheckbox);
|
updateShowCheckbox(!showCheckbox);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "quote":
|
case "quote":
|
||||||
// 引用逻辑
|
// 引用逻辑
|
||||||
@@ -710,13 +703,6 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleConfirmTransmit = (
|
|
||||||
selectedContacts: ContractData[] | weChatGroup[],
|
|
||||||
) => {
|
|
||||||
// 确认转发逻辑
|
|
||||||
console.log("确认转发:", selectedContacts);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.messagesContainer}>
|
<div className={styles.messagesContainer}>
|
||||||
<div className={styles.loadMore} onClick={() => loadMoreMessages()}>
|
<div className={styles.loadMore} onClick={() => loadMoreMessages()}>
|
||||||
@@ -753,11 +739,7 @@ const MessageRecord: React.FC<MessageRecordProps> = ({ contract }) => {
|
|||||||
onCommad={handCommad}
|
onCommad={handCommad}
|
||||||
/>
|
/>
|
||||||
{/* 转发模态框 */}
|
{/* 转发模态框 */}
|
||||||
<TransmitModal
|
<TransmitModal />
|
||||||
onConfirm={(selectedContacts: ContractData[] | weChatGroup[]) =>
|
|
||||||
handleConfirmTransmit(selectedContacts)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -61,9 +61,19 @@ export const useWeChatStore = create<WeChatState>()(
|
|||||||
// ==================== 聊天消息管理方法 ====================
|
// ==================== 聊天消息管理方法 ====================
|
||||||
/** 添加新消息到当前聊天 */
|
/** 添加新消息到当前聊天 */
|
||||||
addMessage: message => {
|
addMessage: message => {
|
||||||
set(state => ({
|
const { wechatAccountId, wechatChatroomId, wechatFriendId } = message;
|
||||||
currentMessages: [...state.currentMessages, message],
|
const currentContract = get().currentContract;
|
||||||
}));
|
if (currentContract) {
|
||||||
|
if (
|
||||||
|
currentContract.wechatAccountId === wechatAccountId &&
|
||||||
|
(currentContract.id === wechatFriendId ||
|
||||||
|
currentContract.id === wechatChatroomId)
|
||||||
|
) {
|
||||||
|
set(state => ({
|
||||||
|
currentMessages: [...state.currentMessages, message],
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
/** 更新指定消息内容 */
|
/** 更新指定消息内容 */
|
||||||
updateMessage: (messageId, updates) => {
|
updateMessage: (messageId, updates) => {
|
||||||
@@ -126,7 +136,7 @@ export const useWeChatStore = create<WeChatState>()(
|
|||||||
) => {
|
) => {
|
||||||
const state = useWeChatStore.getState();
|
const state = useWeChatStore.getState();
|
||||||
// 切换联系人时清空当前消息,等待重新加载
|
// 切换联系人时清空当前消息,等待重新加载
|
||||||
set({ currentMessages: [] });
|
set({ currentMessages: [], openTransmitModal: false });
|
||||||
clearUnreadCount([contract.id]).then(() => {
|
clearUnreadCount([contract.id]).then(() => {
|
||||||
if (isExist) {
|
if (isExist) {
|
||||||
updateChatSession({ ...contract, unreadCount: 0 });
|
updateChatSession({ ...contract, unreadCount: 0 });
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ const messageHandlers: Record<string, MessageHandler> = {
|
|||||||
// 发送消息响应
|
// 发送消息响应
|
||||||
CmdSendMessageResp: message => {
|
CmdSendMessageResp: message => {
|
||||||
console.log("发送消息响应", message);
|
console.log("发送消息响应", message);
|
||||||
addMessage(message.friendMessage || message.chatroomMessage);
|
// addMessage(message.friendMessage || message.chatroomMessage);
|
||||||
// 在这里添加具体的处理逻辑
|
// 在这里添加具体的处理逻辑
|
||||||
},
|
},
|
||||||
CmdSendMessageResult: message => {
|
CmdSendMessageResult: message => {
|
||||||
|
|||||||
Reference in New Issue
Block a user